Browse Source

添加打开CAD工具类

ouruisong 6 months ago
parent
commit
7267ba8cec

+ 181 - 0
CollaborativePlatformMain/CADStartUtil/SendToCAD.cs

@@ -0,0 +1,181 @@
+using Microsoft.Win32;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace CollaborativePlatformMain.CADStartUtil
+{
+    /// <summary>
+    /// 
+    /// <para>文件名(File Name):     SendToCAD.cs</para>
+    /// 
+    /// <para>描述(Description):     发送命令到CAD</para>
+    /// 
+    /// <para>数据表(Tables):        nothing</para>
+    /// 
+    /// <para>作者(Author):          Ou Rui Song</para>
+    /// 
+    /// <para>日期(Create Date):     2024年5月7日11:28:52</para>
+    /// 
+    /// 修改记录(Revision History):
+    ///     R1:
+    ///         修改作者:
+    ///         修改日期:
+    ///         修改理由:
+    /// 
+    /// </summary>
+    public class SendToCAD
+    {
+        [DllImport("user32.dll")]
+        public static extern bool SetForegroundWindow(IntPtr hWnd);
+
+        [DllImport("user32.dll", EntryPoint = "FindWindow")]
+        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
+
+        [DllImport("user32.dll")]
+        private static extern IntPtr SendMessageW(IntPtr hWnd, int Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
+
+        public static void SendCommandToAutoCAD(string toSend, IntPtr hwnd)
+        {
+            const int WM_COPYDATA = 0x4A;
+            COPYDATASTRUCT cds = new COPYDATASTRUCT();
+            cds.dwData = new IntPtr(1);
+            string data = toSend + "\0";
+            cds.cbData = data.Length * Marshal.SystemDefaultCharSize;
+            cds.lpData = Marshal.StringToCoTaskMemAuto(data);
+            SendMessageW(hwnd, WM_COPYDATA, IntPtr.Zero, ref cds);
+            Marshal.FreeCoTaskMem(cds.lpData);
+        }
+
+        private struct COPYDATASTRUCT
+        {
+            public IntPtr dwData;
+
+            public int cbData;
+
+            public IntPtr lpData;
+
+        }
+
+    }
+
+
+
+    /// <summary>
+    /// 
+    /// <para>文件名(File Name):     AcadDirs.cs</para>
+    /// 
+    /// <para>描述(Description):     CAD注册表环境读取</para>
+    /// 
+    /// <para>数据表(Tables):        nothing</para>
+    /// 
+    /// <para>作者(Author):          Li Wen Jin</para>
+    /// 
+    /// <para>日期(Create Date):     2023年10月20日13:43:58</para>
+    /// 
+    /// 修改记录(Revision History):
+    ///     R1:
+    ///         修改作者:
+    ///         修改日期:
+    ///         修改理由:
+    /// 
+    /// </summary>
+    public class AcadDirs
+    {
+        public List<AcadProductKey> Dirs { get; }
+        /// <summary>
+        /// 通过注册表获取本机所有cad的安装路径
+        /// </summary>
+        public AcadDirs()
+        {
+            Dirs = new List<AcadProductKey>();
+
+            RegistryKey Adsk = Registry.CurrentUser.OpenSubKey(@"Software\Autodesk\AutoCAD");
+            if (Adsk == null)
+            {
+                //Console.WriteLine("Adsk == null");
+                return;
+            }
+            foreach (string ver in Adsk.GetSubKeyNames())
+            {
+                try
+                {
+                    RegistryKey emnuAcad = Adsk.OpenSubKey(ver);
+                    var curver = emnuAcad.GetValue("CurVer");
+                    if (curver == null)
+                    {
+                        //Console.WriteLine("curver == null");
+                        continue;
+                    }
+                    string app = curver.ToString();
+                    string fmt = @"Software\Autodesk\AutoCAD\{0}\{1}";
+                    emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app));
+                    if (emnuAcad == null)
+                    {
+                        fmt = @"Software\Wow6432Node\Autodesk\AutoCAD\{0}\{1}";
+                        emnuAcad = Registry.LocalMachine.OpenSubKey(string.Format(fmt, ver, app));
+                    }
+
+                    var acadLocation = emnuAcad.GetValue("AcadLocation");
+                    if (acadLocation == null)
+                    {
+                        //Console.WriteLine("acadLocation == null");
+                        continue;
+                    }
+                    string location = acadLocation.ToString();
+                    if (File.Exists(location + "\\acad.exe"))
+                    {
+                        var produ = emnuAcad.GetValue("ProductName");
+                        if (produ == null)
+                        {
+                            //Console.WriteLine("produ == null");
+                            continue;
+                        }
+                        string productname = produ.ToString();
+                        var release = emnuAcad.GetValue("Release");
+                        if (release == null)
+                        {
+                            //Console.WriteLine("release == null");
+                            continue;
+                        }
+                        string[] strVer = release.ToString().Split('.');
+                        var pro = new AcadProductKey()
+                        {
+                            ProductKey = emnuAcad,
+                            Location = location,
+                            ProductName = productname,
+                            Version = double.Parse(strVer[0] + "." + strVer[1]),
+                        };
+                        Dirs.Add(pro);
+                    }
+                }
+                catch { }
+            }
+        }
+
+        public struct AcadProductKey
+        {
+            /// <summary>
+            /// 注册表位置
+            /// </summary>
+            public RegistryKey ProductKey;
+            /// <summary>
+            /// cad安装路径
+            /// </summary>
+            public string Location;
+            /// <summary>
+            /// cad名称
+            /// </summary>
+            public string ProductName;
+            /// <summary>
+            /// cad版本号 17.1之类的
+            /// </summary>
+            public double Version;
+        }
+    }
+
+}

+ 107 - 0
CollaborativePlatformMain/CADStartUtil/StartCADUtil.cs

@@ -0,0 +1,107 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Drawing;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using static CollaborativePlatformMain.CADStartUtil.AcadDirs;
+
+namespace CollaborativePlatformMain.CADStartUtil
+{
+    /// <summary>
+    /// 
+    /// <para>文件名(File Name):     StartCADUtil.cs</para>
+    /// 
+    /// <para>描述(Description):     启动CAD并打开图纸</para>
+    /// 
+    /// <para>数据表(Tables):        nothing</para>
+    /// 
+    /// <para>作者(Author):          Ou Rui Song</para>
+    /// 
+    /// <para>日期(Create Date):     2024年5月7日11:31:34</para>
+    /// 
+    /// 修改记录(Revision History):
+    ///     R1:
+    ///         修改作者:
+    ///         修改日期:
+    ///         修改理由:
+    /// 
+    /// </summary>
+    public class StartCADUtil
+    {
+
+        static string location;
+
+        public static void StartCADMath()
+        {
+            try
+            {
+                //获取本机cad路径
+                AcadDirs regedits = new AcadDirs();
+
+                //未安装CAD
+                if (regedits.Dirs.Count == 0)
+                {
+                    MessageBox.Show(DateTime.Now + "当前电脑未安装 AutoCAD ");
+                    return;
+                }
+
+                //未安装2019版CAD
+                List<AcadProductKey> cadPathpks = regedits.Dirs.Where(x => x.ProductName.Equals("AutoCAD 2019 - 简体中文 (Simplified Chinese)")).ToList();
+                if (cadPathpks.Count == 0)
+                {
+                    Console.WriteLine(DateTime.Now + "当前电脑未安装 AutoCAD 2019 - 简体中文 (Simplified Chinese)");
+                    Console.ReadLine();
+                    return;
+                }
+
+                var pros = Process.GetProcessesByName("acad");
+                if (pros.Length != 0)
+                {
+                    //已打开cad
+
+                }
+                else
+                {
+                    location = cadPathpks[0].Location;
+
+                    // 创建一个脚本文件并写入要执行的命令
+                    //string scriptFile = location + "\\autocad_script.scr";
+                    //File.WriteAllText(scriptFile, "DFBIMDwgComparisonCooperate\n");
+
+                    // 启动AutoCAD进程并运行脚本文件
+                    ProcessStartInfo startInfo = new ProcessStartInfo
+                    {
+                        FileName = location + "\\acad.exe",
+                        //Arguments = $"/b \"{scriptFile}\"",
+                        UseShellExecute = false,
+                        RedirectStandardError = true,
+                        RedirectStandardInput = true,
+                        RedirectStandardOutput = true,
+                        CreateNoWindow = true
+                    };
+
+                    using (Process autocadProcess = new Process { StartInfo = startInfo })
+                    {
+                        autocadProcess.Start();
+
+                        // 等待AutoCAD进程完成
+                        autocadProcess.WaitForInputIdle();
+
+                        // 删除脚本文件
+                        //File.Delete(scriptFile);
+                    }
+                }
+            }
+            catch (Exception)
+            {
+                return;
+            }
+        }
+
+
+    }
+}

+ 96 - 1
CollaborativePlatformMain/CollaborativePlatformMain.csproj

@@ -14,9 +14,24 @@
     <WarningLevel>4</WarningLevel>
     <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
     <Deterministic>true</Deterministic>
+    <PublishUrl>publish\</PublishUrl>
+    <Install>true</Install>
+    <InstallFrom>Disk</InstallFrom>
+    <UpdateEnabled>false</UpdateEnabled>
+    <UpdateMode>Foreground</UpdateMode>
+    <UpdateInterval>7</UpdateInterval>
+    <UpdateIntervalUnits>Days</UpdateIntervalUnits>
+    <UpdatePeriodically>false</UpdatePeriodically>
+    <UpdateRequired>false</UpdateRequired>
+    <MapFileExtensions>true</MapFileExtensions>
+    <ApplicationRevision>0</ApplicationRevision>
+    <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
+    <IsWebBootstrapper>false</IsWebBootstrapper>
+    <UseApplicationTrust>false</UseApplicationTrust>
+    <BootstrapperEnabled>true</BootstrapperEnabled>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
-    <PlatformTarget>AnyCPU</PlatformTarget>
+    <PlatformTarget>x64</PlatformTarget>
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
@@ -34,7 +49,47 @@
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
   </PropertyGroup>
+  <PropertyGroup>
+    <TargetZone>LocalIntranet</TargetZone>
+  </PropertyGroup>
+  <PropertyGroup>
+    <GenerateManifests>false</GenerateManifests>
+  </PropertyGroup>
+  <PropertyGroup>
+    <ApplicationManifest>Properties\app.manifest</ApplicationManifest>
+  </PropertyGroup>
+  <PropertyGroup>
+    <SignManifests>false</SignManifests>
+  </PropertyGroup>
   <ItemGroup>
+    <Reference Include="accoremgd, Version=23.0.0.0, Culture=neutral, processorArchitecture=AMD64">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>SystemLibrary\CAD\2019\accoremgd.dll</HintPath>
+    </Reference>
+    <Reference Include="AcCui, Version=23.0.0.0, Culture=neutral, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>SystemLibrary\CAD\2019\AcCui.dll</HintPath>
+    </Reference>
+    <Reference Include="acdbmgd, Version=23.0.0.0, Culture=neutral, processorArchitecture=AMD64">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>SystemLibrary\CAD\2019\acdbmgd.dll</HintPath>
+    </Reference>
+    <Reference Include="acmgd, Version=23.0.0.0, Culture=neutral, processorArchitecture=AMD64">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>SystemLibrary\CAD\2019\acmgd.dll</HintPath>
+    </Reference>
+    <Reference Include="AcWindows, Version=23.0.0.0, Culture=neutral, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>SystemLibrary\CAD\2019\AcWindows.dll</HintPath>
+    </Reference>
+    <Reference Include="AdWindows, Version=2017.11.2.0, Culture=neutral, processorArchitecture=MSIL">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>SystemLibrary\CAD\2019\AdWindows.dll</HintPath>
+    </Reference>
+    <Reference Include="CADNETCommon2019, Version=1.0.0.0, Culture=neutral, processorArchitecture=AMD64">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>SystemLibrary\CAD\CADNETCommon2019.dll</HintPath>
+    </Reference>
     <Reference Include="HandyControl, Version=3.5.1.0, Culture=neutral, PublicKeyToken=45be8712787a1e5b, processorArchitecture=MSIL">
       <HintPath>..\packages\HandyControl.3.5.1\lib\net48\HandyControl.dll</HintPath>
     </Reference>
@@ -47,6 +102,7 @@
     <Reference Include="System.Data">
       <Private>True</Private>
     </Reference>
+    <Reference Include="System.Drawing" />
     <Reference Include="System.Drawing.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
       <HintPath>..\packages\System.Drawing.Common.8.0.3\lib\net462\System.Drawing.Common.dll</HintPath>
     </Reference>
@@ -87,6 +143,8 @@
       <Generator>MSBuild:Compile</Generator>
       <SubType>Designer</SubType>
     </ApplicationDefinition>
+    <Compile Include="CADStartUtil\SendToCAD.cs" />
+    <Compile Include="CADStartUtil\StartCADUtil.cs" />
     <Compile Include="DFEntity\FloorLayerData.cs" />
     <Compile Include="DFEntity\MessageSubUtil\ContactsUtil.cs" />
     <Compile Include="DFEntity\MessageSubUtil\LegendEntity.cs" />
@@ -308,6 +366,7 @@
       <LastGenOutput>Resources.Designer.cs</LastGenOutput>
     </EmbeddedResource>
     <None Include="packages.config" />
+    <None Include="Properties\app.manifest" />
     <None Include="Properties\Settings.settings">
       <Generator>SettingsSingleFileGenerator</Generator>
       <LastGenOutput>Settings.Designer.cs</LastGenOutput>
@@ -324,5 +383,41 @@
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
     </Resource>
   </ItemGroup>
+  <ItemGroup>
+    <BootstrapperPackage Include=".NETFramework,Version=v4.8">
+      <Visible>False</Visible>
+      <ProductName>Microsoft .NET Framework 4.8 %28x86 和 x64%29</ProductName>
+      <Install>true</Install>
+    </BootstrapperPackage>
+    <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
+      <Visible>False</Visible>
+      <ProductName>.NET Framework 3.5 SP1</ProductName>
+      <Install>false</Install>
+    </BootstrapperPackage>
+  </ItemGroup>
+  <ItemGroup />
+  <ItemGroup>
+    <Content Include="SystemLibrary\CAD\2019\accoremgd.dll">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
+    <Content Include="SystemLibrary\CAD\2019\AcCui.dll">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
+    <Content Include="SystemLibrary\CAD\2019\acdbmgd.dll">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
+    <Content Include="SystemLibrary\CAD\2019\acmgd.dll">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
+    <Content Include="SystemLibrary\CAD\2019\AcWindows.dll">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
+    <Content Include="SystemLibrary\CAD\2019\AdWindows.dll">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
+    <Content Include="SystemLibrary\CAD\CADNETCommon2019.dll">
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
+    </Content>
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

+ 8 - 0
CollaborativePlatformMain/CollaborativePlatformMain.csproj.user

@@ -2,5 +2,13 @@
 <Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <ProjectView>ProjectFiles</ProjectView>
+    <PublishUrlHistory>publish\</PublishUrlHistory>
+    <InstallUrlHistory />
+    <SupportUrlHistory />
+    <UpdateUrlHistory />
+    <BootstrapperUrlHistory />
+    <ErrorReportUrlHistory />
+    <FallbackCulture>zh-CN</FallbackCulture>
+    <VerifyUploadedFiles>false</VerifyUploadedFiles>
   </PropertyGroup>
 </Project>

+ 5 - 0
CollaborativePlatformMain/MainWindow.xaml

@@ -90,6 +90,11 @@
 
 
                 </StackPanel>
+
+                <StackPanel>
+                    <Button Width="80" Content="eargjiag" Click="Button_Click"/>
+                </StackPanel>
+                
             </StackPanel>
         </Border>
     </LayUI:LayTitleBar>

+ 12 - 2
CollaborativePlatformMain/MainWindow.xaml.cs

@@ -1,4 +1,5 @@
-using CollaborativePlatformMain.Form;
+using CollaborativePlatformMain.CADStartUtil;
+using CollaborativePlatformMain.Form;
 using CollaborativePlatformMain.Util;
 using System;
 using System.Collections.Generic;
@@ -132,6 +133,15 @@ namespace CollaborativePlatformMain
             this.Close();
         }
 
-        
+
+        /// <summary>
+        /// 启动CAD测试
+        /// </summary>
+        /// <param name="sender"></param>
+        /// <param name="e"></param>
+        private void Button_Click(object sender, RoutedEventArgs e)
+        {
+            StartCADUtil.StartCADMath();
+        }
     }
 }

+ 73 - 0
CollaborativePlatformMain/Properties/app.manifest

@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="utf-8"?>
+<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
+  <assemblyIdentity version="1.0.0.0" name="MyApplication.app" />
+  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
+    <security>
+      <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
+        <!-- UAC 清单选项
+             如果想要更改 Windows 用户帐户控制级别,请使用
+             以下节点之一替换 requestedExecutionLevel 节点。
+
+        <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
+        <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
+        <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />
+
+            指定 requestedExecutionLevel 元素将禁用文件和注册表虚拟化。
+            如果你的应用程序需要此虚拟化来实现向后兼容性,则移除此
+            元素。
+        -->
+        <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
+      </requestedPrivileges>
+      <applicationRequestMinimum>
+        <defaultAssemblyRequest permissionSetReference="Custom" />
+        <PermissionSet class="System.Security.PermissionSet" version="1" Unrestricted="true" ID="Custom" SameSite="site" />
+      </applicationRequestMinimum>
+    </security>
+  </trustInfo>
+  <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
+    <application>
+      <!-- 设计此应用程序与其一起工作且已针对此应用程序进行测试的
+           Windows 版本的列表。取消评论适当的元素,
+           Windows 将自动选择最兼容的环境。 -->
+      <!-- Windows Vista -->
+      <!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
+      <!-- Windows 7 -->
+      <!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
+      <!-- Windows 8 -->
+      <!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
+      <!-- Windows 8.1 -->
+      <!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
+      <!-- Windows 10 -->
+      <!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
+    </application>
+  </compatibility>
+  <!-- 指示该应用程序可感知 DPI 且 Windows 在 DPI 较高时将不会对其进行
+       自动缩放。Windows Presentation Foundation (WPF)应用程序自动感知 DPI,无需
+       选择加入。选择加入此设置的 Windows 窗体应用程序(面向 .NET Framework 4.6)还应
+       在其 app.config 中将 "EnableWindowsFormsHighDpiAutoResizing" 设置设置为 "true"。
+       
+       将应用程序设为感知长路径。请参阅 https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
+  <!--
+  <application xmlns="urn:schemas-microsoft-com:asm.v3">
+    <windowsSettings>
+      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
+      <longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
+    </windowsSettings>
+  </application>
+  -->
+  <!-- 启用 Windows 公共控件和对话框的主题(Windows XP 和更高版本) -->
+  <!--
+  <dependency>
+    <dependentAssembly>
+      <assemblyIdentity
+          type="win32"
+          name="Microsoft.Windows.Common-Controls"
+          version="6.0.0.0"
+          processorArchitecture="*"
+          publicKeyToken="6595b64144ccf1df"
+          language="*"
+        />
+    </dependentAssembly>
+  </dependency>
+  -->
+</assembly>

BIN
CollaborativePlatformMain/SystemLibrary/CAD/2019/AcCui.dll


BIN
CollaborativePlatformMain/SystemLibrary/CAD/2019/AcWindows.dll


BIN
CollaborativePlatformMain/SystemLibrary/CAD/2019/AdWindows.dll


BIN
CollaborativePlatformMain/SystemLibrary/CAD/2019/accoremgd.dll


BIN
CollaborativePlatformMain/SystemLibrary/CAD/2019/acdbmgd.dll


BIN
CollaborativePlatformMain/SystemLibrary/CAD/2019/acmgd.dll


BIN
CollaborativePlatformMain/SystemLibrary/CAD/CADNETCommon2019.dll