Explorar o código

mxg:添加Revit相关的两个扩展方法

mengxiangge %!s(int64=5) %!d(string=hai) anos
pai
achega
557c0592c6

+ 151 - 0
MBI/SAGA.DotNetUtils/Revit/RevitProductUtility.cs

@@ -0,0 +1,151 @@
+/* ==============================================================================
+ * 功能描述:stringUtility  
+ * 创 建 者:Garrett
+ * 创建日期:2019/8/1 11:14:34
+ * ==============================================================================*/
+
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Text.RegularExpressions;
+using Microsoft.Win32;
+
+namespace SAGA.DotNetUtils.Revit
+{
+    /// <summary>
+    /// Revit版本信息
+    /// </summary>
+    public class RevitProductUtility
+    {
+        [DllImport("msi.dll", CharSet = CharSet.Unicode)]
+        public static extern uint MsiEnumClients(string szComponent, uint iProductIndex, string lpProductBuf);
+
+        [DllImport("msi.dll", CharSet = CharSet.Unicode)]
+        public static extern int MsiGetProductInfo(string product, string property, string valueBuf, ref int len);
+
+        [DllImport("advapi32.dll", SetLastError = true)]
+        public static extern int RegCloseKey(UIntPtr hKey);
+
+        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
+        public static extern int RegOpenKeyEx(UIntPtr hKey, string subKey, int ulOptions, int samDesired, ref UIntPtr hkResult);
+
+        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
+        public static extern int RegQueryValueEx(UIntPtr hKey, string lpValueName, int lpReserved, ref uint lpType, ref uint lpData, ref uint lpcbData);
+
+        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
+        public static extern int RegQueryValueEx(UIntPtr hKey, string lpValueName, int lpReserved, ref uint lpType, StringBuilder lpData, ref uint lpcbData);
+        /// <summary>
+        /// 获取所有安装的Revit版本
+        /// </summary>
+        /// <returns>
+        /// 2018,E:\Program Files\Autodesk\Revit 2018\
+        /// </returns>
+        public static List<Tuple<string, string>> GetAllInstalledRevitProducts()
+        {
+            List<Tuple<string, string>> list = new List<Tuple<string, string>>();
+            initializeProductsDictionary();
+            try
+            {
+                List<string>.Enumerator enumerator = GetInstalledCodes().GetEnumerator();
+                while (enumerator.MoveNext())
+                {
+                    Tuple<string, string> item = GetProductVersionInfo(enumerator.Current);
+                    if (null != item)
+                    {
+                        list.Add(item);
+                    }
+                }
+            }
+            catch (Exception)
+            {
+            }
+            return list;
+        }
+
+        private static Tuple<string, string> getInstalledProductInfo(string productRegGUID)
+        {
+            Tuple<string, string> tuple = null;
+            string installPath, version;
+
+            uint num4 = 0;
+            if (!m_productsHashtable.ContainsKey(productRegGUID))
+            {
+                return null;
+            }
+            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall").OpenSubKey(productRegGUID);
+            if (null != key)
+            {
+                installPath = (string)key.GetValue("InstallLocation");
+                version = (string)key.GetValue("DisplayName");
+                tuple = new Tuple<string, string>(version, installPath);
+                goto Label_0365;
+            }
+            UIntPtr hkResult = new UIntPtr();
+            uint lpcbData = 0x400;
+            uint lpData = 0;
+            StringBuilder builder = new StringBuilder(0x400);
+            RegQueryValueEx(hkResult, "InstallLocation", 0, ref num4, builder, ref lpcbData);
+            installPath = builder.ToString();
+            RegQueryValueEx(hkResult, "DisplayName", 0, ref num4, builder, ref lpcbData);
+            version = builder.ToString();
+            tuple = new Tuple<string, string>(version, installPath);
+
+            return tuple;
+            Label_0365:
+            key.Close();
+            return tuple;
+        }
+
+        private static List<string> GetInstalledCodes()
+        {
+            List<string> list = new List<string>();
+            string lpProductBuf = new string('0', 0x26);
+            string szComponent = "{DF7D485F-B8BA-448E-A444-E6FB1C258912}";
+            uint num = 1;
+            if (MsiEnumClients(szComponent, 0, lpProductBuf) == 0)
+            {
+                uint num2;
+                do
+                {
+                    string item = string.Copy(lpProductBuf);
+                    list.Add(item);
+                    num2 = num;
+                    num++;
+                }
+                while (MsiEnumClients(szComponent, num2, lpProductBuf) == 0);
+            }
+            return list;
+        }
+
+        private static Tuple<string, string> GetProductVersionInfo(string productCode)
+        {
+            Match match = new Regex(@"^(\{{0,1}(7346B4A[0-9a-fA-F])-(?<Majorversion>([0-9a-fA-F]){2})(?<Subversion>([0-9a-fA-F]){2})-(?<Discipline>([0-9a-fA-F]){2})(?<Platform>([0-9a-fA-F]){1})[0-9a-fA-F]-(?<Language>([0-9a-fA-F]){4})-705C0D862004\}{0,1})$").Match(productCode);
+            if (!match.Success)
+            {
+                return getInstalledProductInfo(productCode);
+            }
+
+            string version = "20" + match.Result("${Majorversion}");
+            string installPath = new string(' ', 260);
+            int num4 = 260;
+            MsiGetProductInfo(productCode, "InstallLocation", installPath, ref num4);
+            return new Tuple<string, string>(version, installPath.Substring(0,num4));
+        }
+        private static Hashtable m_productsHashtable;
+        private static void initializeProductsDictionary()
+        {
+            if (null == m_productsHashtable)
+            {
+                m_productsHashtable = new Hashtable();
+            }
+            else
+            {
+                m_productsHashtable.Clear();
+            }
+        }
+
+    }
+}

+ 57 - 0
MBI/SAGA.DotNetUtils/Revit/RevitVisionUtil.cs

@@ -0,0 +1,57 @@
+/* ==============================================================================
+ * 功能描述:RevitVisionUtil  
+ * 创 建 者:Garrett
+ * 创建日期:2019/6/28 9:25:34
+ * ==============================================================================*/
+
+using System.IO;
+using System.Text;
+using System.Text.RegularExpressions;
+
+namespace SAGA.DotNetUtils.Revit
+{
+    /// <summary>
+    /// RevitVisionUtil
+    /// </summary>
+    public class RevitVisionUtil
+    {
+        /// <summary>
+        /// 获取Revit文件的版本
+        /// </summary>
+        /// <param name="path">文件路径</param>
+        /// <returns></returns>
+        public static string GetRevitVision(string path)
+        {
+            string revitVision = null;
+            FileStream stream = new FileStream(path, FileMode.Open);
+
+            int size = 1024 * 1024;
+            byte[] bytes = new byte[size];
+
+            
+            while (stream.Read(bytes, 0, size) > 0)
+            {
+                string str = Encoding.Unicode.GetString(bytes);
+                //if (str.Contains("2014"))
+                //{
+                //    revitVision = "2014";
+
+                //    File.WriteAllText(@"D:\abc.txt", str);
+                //    System.Diagnostics.Process.Start("notepad.exe", path);
+                //    break;
+                //}
+
+                string pattern = @"Autodesk Revit \d{4}";
+                var match = Regex.Match(str, pattern);
+                if (match.Success)
+                {
+                    revitVision = match.Value.Substring(match.Length - 4, 4);
+
+                    //File.WriteAllText(@"D:\abc.txt", str);
+                    break;
+                }
+            }
+            return revitVision;
+        }
+    }
+}

+ 2 - 0
MBI/SAGA.DotNetUtils/SAGA.DotNetUtils.csproj

@@ -381,6 +381,8 @@
     <Compile Include="Others\PubMethod.cs" />
     <Compile Include="Others\RECT.cs" />
     <Compile Include="Extend\RefletcExtend.cs" />
+    <Compile Include="Revit\RevitProductUtility.cs" />
+    <Compile Include="Revit\RevitVisionUtil.cs" />
     <Compile Include="Serializer\SerializerByDataContract.cs" />
     <Compile Include="Serializer\SerializerByBinary.cs" />
     <Compile Include="Others\SetWindowPosFlags.cs" />