Jump to content

Standalone .NET Project Calling LISP Programs


Bill Tillman

Recommended Posts

Okay, here's the scenario. I have a nice standalone exe project created with VS which is conveniently launched from a custom pull-down menu in each users' AutoCAD screen. I underline standalone because it's not a dll working inside of AutoCAD. I would like to take this one step further in that this exe will let the user run some of the commands in the custom menus within the existing instance of AutoCAD. The process will work on one (1) particular drawing file at a time so I have to make that one is the active document.

 

The first problem I see with this is that one user typically opens 10 drawing files at a time, another opens 3 or 4 at a time and another only opens one file at a time. Still others open multiple drawings but do it by launching 5 instances of AutoCAD with only a single file opened in each one. Call them crazy, and undisciplined if you want but they are after all only users, and they are my users and I love them and will fight for them....yeah, right!

 

I'm trying to do this without having to demand that all the users follow a rigorous procedure. Because we all know that rigorous procedures are meant to be broken. And if I plan this right, they users will never have to think, which will all know is a dangerous thing. :lol: I will report back with my progress and questions.

Link to comment
Share on other sites

The project is taking shape but like always there are glitches. I have a custom menu item which runs the following when selected:

^C^Cscript "//a_very_long_novell_path/MyScript.scr

This script runs perfect when called from the menu. So I think cool, let me use the SendCommand from inside of my VB.NET code to call it. But alas, it's not working:

Dim vAcadDoc As AcadDocument = vAcadApp.ActiveDocument
vAcadDoc.SendCommand("^C^Cscript ""//a_very_long_novell_path/MyScript.scr" & Chr(13))

Are the ^C's needed in doing this from outside of ACAD? I tried a couple of tests and apparently I don't know enough about scripts. When I copy and paste this macro on the command line, it whacks and reports "Unknown command "SCR". So before I go much further, how would one correctly run this macro from the command line. I did set FIELDIA to 0 for this testing.

 

BTW running this script is not mandatory, except that the client really wants the same code that used in the menu to be used in the app for compliance issues. If there's a way to achieve this without loading that script, and just using SendCommand, I could do it like that. But I'd prefer to inform the client that I did it exactly as he's asked and that will make his life simpler when he can report that it all runs the same code to the auditors.

Link to comment
Share on other sites

Almost there. I'm using a different code this time which instead of running a script shown above, it loads and runs a LISP program. This was actually easier than I first thought. Still there are hurdles due to the restrictions on the server that have to be accounted for and I think I can get clear of those shortly. I haven't fully tested this yet, except to see that it gets me into the drawing in an existing instance of AutoCAD then loads and launches the correct LISP file. I will complete the testing this evening once all the users are out of the network to avoid any conflicts, which I really don't anticipate, but as a developer, I've learned to err on the side of caution rather than bulldoze ahead.

 

I added this class to my project and call it from the Form1 code based on what the user checked on this form.

Imports AutoCAD
Public Class SaveACADFile
   Shared Sub SaveAcadFile(ByRef SaveType As String)
       Dim vAcadApp As AcadApplication = Nothing
       Try
           If Process.GetProcessesByName("acad").Length > 0 Then
               vAcadApp = GetObject(, "AutoCAD.Application")
               vAcadApp.ActiveDocument.SendCommand("(load ""//a_very_long_Novell_Path/VLISP/" & SaveType & ".lsp"" ""The load failed"") " & SaveType & vbCr)
           Else
               MsgBox("AutoCAD is not currently in session")
               Exit Sub
           End If
       Catch ex As Exception
           MsgBox("Error: Check with admin" & vbCrLf & ex.Message)
       End Try
   End Sub
End Class

I still need to figure out why I can't seem to get this code to load and run a script in lieu of a LISP program. That will require a little more research. And this code will have to have extensive error checking added to it because I know the users are going to figure out a way to break it with bad input.

Edited by Bill Tillman
Link to comment
Share on other sites

Try leaving out ^c^c this is only for menu's to cancel current command ie twice same with a ^p if included. pretty sure sendcommand will halt current command.

 

vAcadDoc.SendCommand("script" "//a_very_long_novell_path/MyScript.scr" & Chr(13)) maybe VBcr rather than chr(13)

Link to comment
Share on other sites

For running Commands, consider P/Invoking RunCommand(), to call any LISP expression P/Invoke acedEvaluateLisp() (undocumented), or to only call LISP that has been defined with c: prefix P/Invoke acedInvoke() for 2010 and older (2011 added Application.Invoke() Method to preclude the need to P/Invoke same).

 

Cheers

Link to comment
Share on other sites

Okay, I ran straight into the users today on this one. As a developer I always try to write code to work within the styles of the user audience. But this may not be possible.

 

One of the users typically opens up at least 5 instances of AutoCAD and in each one he may have anywhere from 1 to 5 different files opened up. Not the best technique I agree but it's his style and he likes it that way.

 

Based on this, and whether I'm using VB.NET or C# code for my project I do the standard practice of checking first for an instance of AutoCAD and then use it. In fact, in this project because it's meant to save an existing drawing using some strict parameters to comply with ISO-9001 standards if it doesn't find a running instance of AutoCAD it pops up a message for the user to go back and try again after opening AutoCAD and a drawing file. I'm not aware of any way to find which one of the five or so instances of AutoCAD the user has the particular drawing open in.

Link to comment
Share on other sites

Okay, I ran straight into the users today on this one. As a developer I always try to write code to work within the styles of the user audience. But this may not be possible.

 

One of the users typically opens up at least 5 instances of AutoCAD and in each one he may have anywhere from 1 to 5 different files opened up. Not the best technique I agree but it's his style and he likes it that way.

 

Based on this, and whether I'm using VB.NET or C# code for my project I do the standard practice of checking first for an instance of AutoCAD and then use it. In fact, in this project because it's meant to save an existing drawing using some strict parameters to comply with ISO-9001 standards if it doesn't find a running instance of AutoCAD it pops up a message for the user to go back and try again after opening AutoCAD and a drawing file. I'm not aware of any way to find which one of the five or so instances of AutoCAD the user has the particular drawing open in.

 

So port your code to be a plug-in using .NET API in lieu of COM, and have user run it via Ribbon button... That makes identifying the active process (Autodesk.AutoCAD.ApplicationServices.Application) simple, in addition to having direct access to the Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MidActiveDocument (when != null, for 2015+).

 

If that's too much work, then code yourself a new plug-in that does the above, and then feeds it to your stand alone application as string parameters via command line call.

 

Cheers

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...