Jump to content

Running a Lisp from a C++ Code


Alex11

Recommended Posts

Hi everyone,

 

I was wondering if I can run a lisp in AutoCAD from building (running) a C++ code in Visual Studio. What I mean is can I have AutoCAD open and run a C++ code in parallel and have the lisp loaded automatically in AutoCAD?

The lisp I want to use is 'ascpoint', which gets a text file with coordinates as input and inserts points at those coordinates.

 

Any help will be really appreciated.

 

Thanks,

Alex

Link to comment
Share on other sites

Welcome to CADTutor, Alex; and congrats on your first post! :beer:

 

I was wondering if I can run a lisp in AutoCAD from building (running) a C++ code in Visual Studio. What I mean is can I have AutoCAD open and run a C++ code in parallel and have the lisp loaded automatically in AutoCAD?

 

Firstly, is there a reason you're not instead simply debugging a new AutoCAD process? :unsure:

 

 

 

Given that ObjectARX (C++), like .NET (C#, F#, VB, etc.) compiles to an assembly which in turn needs to be [ARX/NET]-loaded into your active session (process) of AutoCAD... Doing what you're after is impossible for a .NET assembly (as there is no such NETUNLOAD CommandMethod/LispFunction Method), however, I suspect that your ARX *might* be able to as it can be subsequently ARXUNLOAD-ed.

 

Methinks that your VS Project may need to obtain the active AutoCAD Process, and use the COM API to invoke the LISP (not sure what APIs ARX exposes for this sort of thing?).

 

I typically code .NET plug-ins which are loaded at session start, or on demand, so I'm afraid that I cannot be of more help to you here, as ObjectARX is not (yet) a language which I am adept. The only other advice I can offer is that ARX can call LISP directly via acedInvoke() Method.

Link to comment
Share on other sites

Short answer is yes. You can build a C# project which will either open AutoCAD or use an existing instance of it already open, then send a command to it which will load and run a LISP program of your choice. I do it all the time. I use the COM method, preferring to stay outside of the AutoCAD environment as much as possible. The LISP code I use also reads text files for input to create drawings. Oh, I should mention that I use VB.NET. If you search the Internet, you'll find numerous examples of how to do this. It will look something like this. You will need to add a reference to AutoCAD for this.

 

Imports AutoCAD

Module LaunchAutoCAD

   '**************************************************************************************************************
   ' LAUNCH AUTOCAD & LISP
   '**************************************************************************************************************
   Sub Launch_AutoCAD(ByVal DwgName As String)

       Dim vAcadApp As AcadApplication
       Dim vAcadDoc As AcadDocument
       
       Dim DwgPath As String = "\\My_Path_To\Engineering\Automated Drawings\"
       Dim LispPath As String = "//My_Path_To/LISPFiles/"

       Try
           If Process.GetProcessesByName("acad").Length > 0 Then
               vAcadApp = GetObject(, "AutoCAD.Application.19")
           Else
               vAcadApp = New AcadApplication
           End If

           vAcadApp.Visible = True
           vAcadApp.WindowState = AcWindowState.acMax
           vAcadDoc = vAcadApp.Documents.Open(DwgPath & DwgName, True)
           vAcadDoc.SendCommand("(load """ & LispPath & My_Lisp & ".lsp"" ""The load failed"") " & Cmd_To_Start_LISP_Program & Chr(13))
       Catch ex As Exception

       Finally
           vAcadApp = Nothing
           vAcadDoc = Nothing

       End Try
   End Sub

 

This code was shared with me about a year or so ago and it's configured for AutoCAD 13. It also leaves the instance of AutoCAD open as my project receives request from over the Internet 24/7. There may well be better methods but this works for my needs.

Edited by Bill Tillman
  • Like 1
Link to comment
Share on other sites

Thanks for the response!

I'm not sure what you mean by "simply debugging a new AutoCAD process". Unfortunately I'm kind of new to this area (using Lisp and running codes to make something happen in AutoCAD).

I don't insist on using Lisp to make what I want happen. I even rather not to use it! What I'm trying to achieve is writing a piece of C++ code, which after running will make AutoCAD get a text file containing coordinates as input and insert points at those coordinates (without the need to do anything in AutoCAD).

Could you help me in this direction please?

 

Thanks a lot,

 

Alex

Link to comment
Share on other sites

Hi Bill,

 

Seems like what I am looking for! But could you please elaborate a bit on how I can get this running? I did a search on internet but no success.

 

Thanks,

 

Alex

Link to comment
Share on other sites

Thanks for the response!

I'm not sure what you mean by "simply debugging a new AutoCAD process". Unfortunately I'm kind of new to this area (using Lisp and running codes to make something happen in AutoCAD).

I don't insist on using Lisp to make what I want happen. I even rather not to use it! What I'm trying to achieve is writing a piece of C++ code, which after running will make AutoCAD get a text file containing coordinates as input and insert points at those coordinates (without the need to do anything in AutoCAD).

Could you help me in this direction please?

 

I'm swamped at the moment, but for obtaining an instance of AutoCAD, given this thread a read.

 

You should be able to do what you're after without calling LISP from the ObjectARX (C++) API natively... I'm a .NET API guy (C#, VB.NET), unfortunately.

 

FWIW - Especially if you're new to development with AutoCAD, if you're not already adept at C++, you could easily hop into C# instead, as it too can do everything you're after here.

 

Cheers

Link to comment
Share on other sites

I'm swamped at the moment, but for obtaining an instance of AutoCAD, given this thread a read.

 

I just looked at this thread and the first post there by Bill is exactly what I'm looking for. I just have issues running it. I need to know what's the rest of the code and what references I need to add and etc.

It would be great if I could get that code running.

And it's fine for me to use VB.

 

Thanks a lot,

 

Alex

Link to comment
Share on other sites

First, I see you're running AutoCAD 2012. I think the number 19 changes to 18 for that version. As for the rest of the code, that could be answered in many ways. There's always more than one way to do something with AutoCAD and .NET projects. The above code though should get you started by either opening up AutoCAD or using an existing instance of it. Once your project executes the "SendCommand" portion it will open any LISP file you want and then execute it. That LISP file could be one which reads a text file of points and places them in the model space for you.

 

I think you could also do this inside the AutoCAD environment. My projects are totally automated, no user input allowed. Your setup sounds a little different so you really could use either just C#.NET, VB.NET or plain old LISP itself. LISP can open and read text files quite easily. Outside of that we'd need to know more about your file format, etc... to offer advice.

 

Excuse me, but I now see you're wanting to run ascpoint.lsp, which is an already existing LISP program. The code above can be pointed to just such a file, but again, I'm not clear on why you need to use another development environment to run it. If you can explain a little more on why you need a 2nd development platform to run this LISP that might be helpful.

Link to comment
Share on other sites

Bill,

 

What I am trying to achieve is making a VB code that when I run it, will read a text file containing coordinates and insert a point at that coordinates in AutoCAD. I also want it automated and I don't want to do anything (such as typing a command) in AutoCAD. I would appreciate it if you could help me in that direction.

Also, could you let me know what references to add for the code above?

 

Thanks a lot,

 

Alex

Link to comment
Share on other sites

Sorry for the delayed reply. You will need to add the reference for your version of AutoCAD, which in your case would be "AutoCAD 2012 Type Library". You will find this under the COM tab when you add the reference.

 

Now the trouble with this method is that if you want the VB.NET program to run in the AutoCAD environment

will read a text file containing coordinates and insert a point at that coordinates in AutoCAD
you will not use the COM reference. You can do this, I'm not up to speed on it that well as I use the COM method. By that I mean I use VB.NET to load/open AutoCAD, start a drawing template, then load and execute a LISP file which does all the work. In your case, the LISP file would read your text file and insert the points.

 

There are several LISP files already out there which do this. Lee-Mac has one on his site, and Jeffery Sanders has XYZImport.lsp which does a good job of this. Customizing it to your needs will required some more doing though. We'll all try to help as much as we can. Post back with what you've got done so far.

Link to comment
Share on other sites

  • 2 weeks later...

I am trying to run this code and getting "Error Binding to the method." I am running AutoCAD Architecture 2014.

 

       <CommandMethod("SMB", CommandFlags.Session)> _
       Public Sub SMB(ByVal DwgName As String)
           ' LAUNCH AUTOCAD & LISP
           'Sub Launch_AutoCAD(ByVal DwgName As String)

           Dim vAcadApp As AcadApplication
           Dim vAcadDoc As AcadDocument


           Dim DwgPath As String = "\\\.....

           Dim LispPath As String = ".....

           Dim My_Lisp As String = "SMBTEST"

           Dim Cmd_To_Start_LISP_Program As String = "SMBRUN"


           Try
               If Process.GetProcessesByName("acad").Length > 0 Then
                   vAcadApp = GetObject(, "AutoCAD.Application")
               Else
                   'vAcadApp = CreateObject("AutoCAD.Application.20")
                   vAcadApp = New AcadApplication
               End If

               vAcadApp.Visible = True
               vAcadApp.WindowState = AcWindowState.acMax
               vAcadDoc = vAcadApp.Documents.Open(DwgPath & DwgName, True)
               vAcadDoc.SendCommand("(load """ & LispPath & My_Lisp & ".lsp"" ""The load failed"") " & Cmd_To_Start_LISP_Program & Chr(13))
           Catch ex As System.Exception

           Finally
               vAcadApp = Nothing
               vAcadDoc = Nothing

           End Try
       End Sub














Link to comment
Share on other sites

I am trying to run this code and getting "Error Binding to the method." I am running AutoCAD Architecture 2014.

 

You've coded a CommandMethod Method (a plug-in) which is trying to access the Application Object from the Process (typically done from a stand-alone application via COM), when you could instead just get the Application Object from Autodesk.AutoCAD.ApplicationServices.Application

 

HTH

Link to comment
Share on other sites

Yes, what BlackBox said....but it's often hard for us novice and beginners to understand that jargon and buzzwords. Basically what he's saying is the code I provided for you was intended to be run as a stand-alone program, or what is known as the COM method.

 

The first lines in your code are approaching it from the other end of the spectrum, that is you're trying to code something meant to run inside of the AutoCAD environment itself. Perhaps other more experienced people here can elaborate on this for us. I typically program using the COM method, but others prefer the Application method. If you're trying to run this while you have AutoCAD already running an opened drawing, then you need to look at something different than what I provided. The method I posted is basically for a machine sitting there idle or only has AutoCAD opened with no drawing file loaded. And a compiled exe program is run without AutoCAD ever knowing about it until it gets called upon from that compiled exe. The automation coding I do is not suited well for the Application method, thus I run everything from outside of AutoCAD.

 

I hope I didn't muddy the water too much there. I cannot by any means claim to be an expert on this subject.

Link to comment
Share on other sites

I figure out this with lots of trials, though. I am have created a stand alone application via COM now. I am able to launch AutoCAD, but when I send the command I get "the load failed."

Can you point to me what wrong with my code?

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 

       Const progID As String = "AutoCAD.Application.19.1"
       Dim acApp As AcadApplication = Nothing

       Dim LispPath As String = "..."

       Dim My_Lisp As String = "SMBTEST"

       Dim Cmd_To_Start_LISP_Program As String = "SMBRUN"

       Try
           acApp = DirectCast(Marshal.GetActiveObject(progID), AcadApplication)
       Catch
           Try
               Dim acType As Type = Type.GetTypeFromProgID(progID)
               acApp = DirectCast(Activator.CreateInstance(acType, True), AcadApplication)
           Catch
               MessageBox.Show("Cannot create object of type """ + progID + """")
           End Try
       End Try
       If acApp IsNot Nothing Then
           acApp.Visible = True
           acApp.ActiveDocument.SendCommand("(load """ & LispPath & My_Lisp & ".lsp"" ""The load failed"") " & Cmd_To_Start_LISP_Program & Chr(13))
       End If
   End Sub

Link to comment
Share on other sites

I'm not sure what jargon, or buzzwords I could have used... What I said was direct. If another member doesn't know the difference between .NET and COM interfaces; that's fine, but having never coded a COM app myself (unless you count Visual LISP, which I do not), I rather consider myself a beginner with regard to .NET API.

 

The little I know, I may know well, but that is hardly indicative of being an expert (which I am not)... I was actually trying to be helpful, believe it or not.

 

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...