Though this blog mainly focuses on AutoCAD .NET Addin/API development stuffs, we are going to talk a bit about a small but very nice and cool Auto/Visual LISP widget as it is very helpful for.NET assembly loading, .NET command running, and AutoCAD .NET project debugging thus saves us tons of seconds every day.
The LISP tool can run automatically at AutoCAD startup and open a test drawing, load an assembly of concern and run a test command if necessary.
Here it is:
(setq aawDwgPath "C:/Temp") ; The drawing path. No tail slash, either forward or backward.
(setq aawDwgName "Test.dwg") ; The drawing name. No matter whether upper or lower.
(setq aawCmdToCall "Command1") ; The commond name to call right after the assembly is loaded.
(setq aawNetToLoad "C:/Temp/AcadNetAddinCS/bin/Debug/AcadNetAddinCS.dll") ; The assembly to load.
(Defun OpenDwg ( file / )
(vl-load-com)
(vla-activate (vla-open (vla-get-documents (vlax-get-acad-object)) file))
)
(Defun S::StartUp ( / dwgNameVar dwgFullName)
(setq dwgNameVar (strcase (getvar "DwgName") 0) )
(setq dwgFullName (strcat aawDwgPath '"/" aawDwgName) )
(if ( /= dwgNameVar (strcase aawDwgName 0) ) ; To avoid reentry!
(if (FindFile dwgFullName)
(OpenDwg dwgFullName)
(progn
(if (/= "" aawCmdToCall)
(Command "NetLoad" aawNetToLoad aawCmdToCall)
(Command "NetLoad" aawNetToLoad)
)
)
)
(progn
(if (/= "" aawCmdToCall)
(Command "NetLoad" aawNetToLoad aawCmdToCall)
(Command "NetLoad" aawNetToLoad)
)
)
)
)
The four variables can be set as any test drawing folder and name, the command to call, and the .NET assembly to load at AutoCAD startup.
The better part is that if the test drawing does not exist, the tool will skip the DWG opening step rather than interrupt the whole process; the drawing won’t be opened multiple times when the AutoCAD system variable AcadLspAsDoc is set as 1 or something like that; if no command is supposed to run, we can just assign an empty string to the aawCmdToCall. Of course, the full path the AutoCAD .NET assembly of concern needs to be provided and be right; otherwise, the cool LISP tool will do nothing meaningful.
The best part is that the AutoCAD .NET Addin Wizard (AcadNetAddinWizard) will create the LISP widget and fill out all these variables properly and automatically during project creation and command definition.
One more thing worth of mentioning is that the tool had better be named as Acad.LSP and put into the same folder as the .NET assembly resides rather than in the search/support paths of your AutoCAD and the AutoCAD is launched from inside the debugger window of the Visual Studio IDE. In that way, your AutoCAD product environment will not be affected at all. In the product environment, AutoCAD does not know anything about the tool and won’t load or run anything from the specific .NET assembly or open any drawing as specified in the LISP widget. In the debugging environment, everything is set properly.
So this tool is mainly for debugging and testing purposes and is not recommended to be included into some product/installer packages directly. Of course, nothing should prevent people from tailing it to suit some broader situations and incorporating it into some real/final product environment.
If you are using the leading edge AutoCAD .NET Addin Wizard (AcadNetAddinWizard), then no need to worry about all details about the tool since the wizard will take care of everything about it properly and automatically.
One I used since a while back:
http://forums.augi.com/showthread.php?107419-The-VSTA-language-and-other-NET-stuff&p=1032084&viewfull=1#post1032084
Posted by: Irné Barnard | 07/09/2013 at 03:33 AM
Nice. Great mind thinks alike.
Posted by: Spiderinnet1 | 07/09/2013 at 09:43 PM