[英]Where Did AutoCAD API's Application.AcadApplication object go?
[英]Location and usage of AcadApplication for AutoCAD 2022
我試圖找出如何讓 AutoCAD 識別正在運行的實例。 但是,我遇到了一個問題,即應用程序上不存在AcadApplication
未被識別,如下面的代碼中所述。
我這樣做是為了避免必須制作一個直接插件,而是一個可以單獨與 AutoCAD 通信的 WPF 應用程序(創建一個工具包,將來還可以提供與 AutoCAD 無關的 function)。 如果這種方法不是一個好主意,請隨時告訴我,因為我正在尋找解決這個問題的最佳方法。
任何人都可以幫助我使此代碼適用於 AutoCAD 2022 嗎? 目前這是在運行 .NET Framework 4.7.2 的 WPF 應用程序中運行(參考是從我安裝的 AutoCAD 中手動包含的)
using System.Windows;
using System.Runtime.InteropServices;
using System;
using aD = Autodesk.AutoCAD.ApplicationServices;
namespace
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
aD.Application.AcadApplication acAppComObj = null;
const string strProgId = "AutoCAD.Application.22";
public MainWindow()
{
InitializeComponent();
acAppComObj = Marshal.GetActiveObject(strProgId) as aD.Application.AcadApplication;
// Get a running instance of AutoCAD
try
{
acAppComObj = (aD.Application.AcadApplication)Marshal.GetActiveObject(strProgId);
}
catch // An error occurs if no instance is running
{
try
{
// Create a new instance of AutoCAD
acAppComObj = (aD.Application.AcadApplication)Activator.CreateInstance(Type.GetTypeFromProgID(strProgId), true);
}
catch (Exception)
{
// If an instance of AutoCAD is not created then message and exit
MessageBox.Show("Instance of 'AutoCAD.Application' could not be created.");
return;
}
}
}
}
}
簡而言之,我的問題是如何使用AcadApplication
類型,以及從哪里訪問它?
這是一個示例 class,您可以使用它開始。
我的猜測是,為什么您無法啟動 AutoCAD 實例有兩個根本原因:
1.) 您沒有使用 Autodesk.AutoCAD.Interop.AcadApplication 參考
2.)您為 AutoCAD 2022 使用了錯誤的 progId
試試這個代碼,然后看看你是否可以向后工作以找到問題。
當您想要啟動 AutoCAD/使用沒有直接插件的 AutoCAD 時,您需要使用 COM 互操作對象,而不是 Autodesk.AutoCAD.ApplicationServices。
如果這有幫助,請標記為解決方案
此外,這里是您需要的新參考的路徑:
C:\Program Files\Autodesk\AutoCAD 2022\Autodesk.AutoCAD.Interop.dll
C:\Program Files\Autodesk\AutoCAD 2022\Autodesk.AutoCAD.Interop.Common.dll
C:\Program Files (x86)\Reference\Assemblies\Microsoft\Framework.NETFramework\v4.7.2\Microsoft.VisualBasic.dll
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
namespace StackOverflow
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
bool isAutoCADRunning = Utilities.IsAutoCADRunning();
if (isAutoCADRunning == false)
{
MessageBox.Show("Starting new AutoCAD 2022 instance...this will take a some time.");
Utilities.StartAutoCADApp();
}
else
{
MessageBox.Show("AutoCAD already running.");
}
Utilities.SendMessage("AutoCAD started from WPF");
MyDriver.CreateMyProfile();
Utilities.SendMessage("Profile created:" + Utilities.yourProfileName);
//MyDriver.NetloadMyApp(@"C:\YourPathFolderPath\custom.Dll");
}
}
public class MyDriver
{
public static void CreateMyProfile()
{
bool isAutoCADRunning = Utilities.IsAutoCADRunning();
if (isAutoCADRunning == false)
Utilities.StartAutoCADApp();
Utilities.CreateProfile();
}
public static void NetloadMyApp(String dllPath)
{
bool isAutoCADRunning = Utilities.IsAutoCADRunning();
if (isAutoCADRunning == false)
Utilities.StartAutoCADApp();
Utilities.NetloadDll(dllPath);
}
}
public class Utilities
{
[System.Runtime.InteropServices.DllImport("user32")]
public static extern IntPtr GetWindowThreadProcessId(IntPtr hwnd, ref IntPtr lpdwProcessId);
private static readonly string AutoCADProgId = "AutoCAD.Application.24.1";
private static AcadApplication App;
public static void SendMessage(String message)
{
App.ActiveDocument.SendCommand("(princ \"" + message + "\")(princ)" + Environment.NewLine);
}
public static bool IsAutoCADRunning()
{
bool isRunning = GetRunningAutoCADInstance();
return isRunning;
}
public static bool ConfigureRunningAutoCADForUsage()
{
if (App == null)
return false;
MessageFilter.Register();
SetAutoCADWindowToNormal();
return true;
}
public static bool StartAutoCADApp()
{
Type autocadType = System.Type.GetTypeFromCLSID(new Guid("AA46BA8A-9825-40FD-8493-0BA3C4D5CEB5"), true);
object obj = System.Activator.CreateInstance(autocadType, true);
AcadApplication appAcad = (AcadApplication)obj;
App = appAcad;
MessageFilter.Register();
SetAutoCADWindowToNormal();
return true;
}
public static bool NetloadDll(string dllPath)
{
if (!System.IO.File.Exists(dllPath))
throw new Exception("Dll does not exist: " + dllPath);
App.ActiveDocument.SendCommand("(setvar \"secureload\" 0)" + Environment.NewLine);
dllPath = dllPath.Replace(@"\", @"\\");
App.ActiveDocument.SendCommand("(command \"_netload\" \"" + dllPath + "\")" + Environment.NewLine);
return true;
}
public static bool CreateProfile()
{
if (App == null)
return false;
bool profileExists = DoesProfileExist(App, yourProfileName);
if (profileExists)
{
SetYourProfileActive(App, yourProfileName);
AddTempFolderToTrustedPaths(App);
}
else
{
CreateYourCustomProfile(App, yourProfileName);
AddTempFolderToTrustedPaths(App);
}
SetYourProfileActive(App, yourProfileName);
return true;
}
public static bool SetAutoCADWindowToNormal()
{
if (App == null)
return false;
App.WindowState = AcWindowState.acNorm;
return true;
}
private static bool GetRunningAutoCADInstance()
{
Type autocadType = System.Type.GetTypeFromProgID(AutoCADProgId, true);
AcadApplication appAcad;
try
{
object obj = Microsoft.VisualBasic.Interaction.GetObject(null, AutoCADProgId);
appAcad = (AcadApplication)obj;
App = appAcad;
return true;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
return false;
}
public static readonly string yourProfileName = "myCustomProfile";
private static void SetYourProfileActive(AcadApplication appAcad, string profileName)
{
AcadPreferencesProfiles profiles = appAcad.Preferences.Profiles;
profiles.ActiveProfile = profileName;
}
private static void CreateYourCustomProfile(AcadApplication appAcad, string profileName)
{
AcadPreferencesProfiles profiles = appAcad.Preferences.Profiles;
profiles.CopyProfile(profiles.ActiveProfile, profileName);
profiles.ActiveProfile = profileName;
}
private static bool DoesProfileExist(AcadApplication appAcad, string profileName)
{
AcadPreferencesProfiles profiles = appAcad.Preferences.Profiles;
object pNames = null;
profiles.GetAllProfileNames(out pNames);
string[] profileNames = (string[])pNames;
foreach (string name in profileNames)
{
if (name.Equals(profileName))
return true;
}
return false;
}
private static void AddTempFolderToTrustedPaths(AcadApplication appAcad)
{
string trustedPathsString = System.Convert.ToString(appAcad.ActiveDocument.GetVariable("TRUSTEDPATHS"));
string tempDirectory = System.IO.Path.GetTempPath();
List<string> newPaths = new List<string>() { tempDirectory };
if (!trustedPathsString.Contains(tempDirectory))
AddTrustedPaths(appAcad, newPaths);
}
private static void AddTrustedPaths(AcadApplication appAcad, List<string> newPaths)
{
string trustedPathsString = System.Convert.ToString(appAcad.ActiveDocument.GetVariable("TRUSTEDPATHS"));
List<string> oldPaths = new List<string>();
oldPaths = trustedPathsString.Split(System.Convert.ToChar(";")).ToList();
string newTrustedPathsString = trustedPathsString;
foreach (string newPath in newPaths)
{
bool pathAlreadyExists = trustedPathsString.Contains(newPath);
if (!pathAlreadyExists)
newTrustedPathsString = newPath + ";" + newTrustedPathsString;
}
appAcad.ActiveDocument.SetVariable("TRUSTEDPATHS", newTrustedPathsString);
}
}
public class MessageFilter : IOleMessageFilter
{
[DllImport("Ole32.dll")]
private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, ref IOleMessageFilter oldFilter);
public static void Register()
{
IOleMessageFilter newFilter = new MessageFilter();
IOleMessageFilter oldFilter = null;
CoRegisterMessageFilter(newFilter, ref oldFilter);
}
public static void Revoke()
{
IOleMessageFilter oldFilter = null;
CoRegisterMessageFilter(null, ref oldFilter);
}
public int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
{
return 0;
}
public int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
{
if (dwRejectType == 2)
// flag = SERVERCALL_RETRYLATER.
// Retry the thread call immediately if return >=0 &
// <100.
return 99;
// Too busy; cancel call.
return -1;
}
public int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
{
return 2;
}
}
[ComImport()]
[Guid("00000016-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IOleMessageFilter
{
[PreserveSig]
int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);
[PreserveSig]
int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);
[PreserveSig]
int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.