- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm developing a windows form application that needs to open AutoCAD and load my plug-in DLL to send custom commands and lisp functions to it.
I'm using the following C# code and it works fine in my device but when I tried to run it another machines, I got the attached error message "Cannot load file or assembly...".
But it worked fine after editing AutoCAD config file "acad.exe.config" by adding :
<runtime> <loadFromRemoteSources enabled="true" /> </runtime>
Is there any other way to fix this error programmatically to avoid editing the config file manually? or is there a better way to load my plug-in DLL and call the lisp functions from it?
//1-Opening AutoCAD//
AcadApplication _acadApp;
const string progID = "AutoCAD.Application";//for all AutoCAD versions
try
{
_acadApp = (AcadApplication)Marshal.GetActiveObject(progID);
}
catch
{
try
{
Type acType = Type.GetTypeFromProgID(progID);
if (acType == null)
acType = Type.GetTypeFromProgID("AutoCAD.Application");
_acadApp = (AcadApplication)Activator.CreateInstance(acType, true);
}
catch (System.Exception ex)
{
CommonOperations.Log("Exception", ex.ToString());
}
}
//2-Opening DWG//
_acadApp.Documents.Open("myDrawingPath", Type.Missing, Type.Missing);
//3-Load My Plug-In DLL//
string appDirectory = AppDomain.CurrentDomain.BaseDirectory; // get the application directory
string dllLocation = $"{appDirectory}SolutionMetric.Qsurv.AutoCAD_Plugin.dll"; // get the plugin directory
if (File.Exists(dllLocation))
{
dllLocation = dllLocation.Replace(@"\", @"\\"); // so that the lisp can understand the directory
_acadApp.ActiveDocument.SendCommand("(command \"NETLOAD\"" + $@"""{dllLocation}"") "); //TODO:Causes error in the user's devices
}
//4-Call lisp functions from the plug-in//
_acadApp.ActiveDocument.SendCommand($@"(LoadItems {taskID} {fileID}) ");
Solved! Go to Solution.