Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Starting a New Instance of AutoCAD with a Specified Drawing

20 REPLIES 20
Reply
Message 1 of 21
JohnnyBGoode1
757 Views, 20 Replies

Starting a New Instance of AutoCAD with a Specified Drawing

Hi all.

 

I'm having a bit of trouble with some code I'm writing, and I'm hoping someone can help.

 

The truncated version of the story is that I'm using the ExportToAutoCAD command to purge any AEC objects which may be in the drawing.  After the purge and renaming of the drawing -- which is working fine in the routine -- I want to be able to open the drawing that was just created in a new instance of AutoCAD.  The new drawing has to be stored as a variable in the routine.

 

I've done quite a bit of research on the subject and have come to the conclusion that this cannot be done through a simple AutoLISP routine and that Visual LISP needs to be used.  The problem I'm facing is that I know nothing about Visual LISP.  If there's anyone out there who's willing to help me out with the code, I'd be very appreciative.

 

Incidentally, all of the Visual LISP code that follows was found during my research so I'm pretty sure it's wrong.

 

Here's a snippet of the code:  (The vl-load-com function is called right after the defun.)

 

      (COMMAND "-EXPORTTOAUTOCAD" "M" "Y" "P" "NEW-" "" "")
      (setq newfile (strcat "NEW-" (getvar "dwgname")))
      (setq quotenewfile (strcat "\"" newfile "\""))
      (setq newfilepath (strcat (getvar "dwgprefix") newfile))
      (setq quotenewfilepath (strcat "\"" newfilepath "\""))
             (vla-activate
               (vla-open
                 (vla-get-documents
                   (vla-get-application
                     (vlax-get-acad-object))) newfilepath))

 

Thanks very much in advance.

20 REPLIES 20
Message 2 of 21
hmsilva
in reply to: JohnnyBGoode1

Something like this perhaps.

 

(COMMAND "-EXPORTTOAUTOCAD" "M" "Y" "P" "NEW-" "" "")
(setq newfile	       (strcat "NEW-" (getvar "dwgname"))
      quotenewfile     (strcat "\"" newfile "\"")
      newfilepath      (strcat (getvar "dwgprefix") newfile)
      quotenewfilepath (strcat "\"" newfilepath "\"")
)
(setq new (vla-open (vla-get-documents (vlax-get-acad-object)) quotenewfilepath)) ; for open a drawing
(vla-put-activedocument (vlax-get-acad-object) new) ; to to put the drawing active

HTH

Henrique

EESignature

Message 3 of 21
BlackBox_
in reply to: JohnnyBGoode1

If memory serves, you can utilize the SHELL Comand for this... Just can't seem to recall the syntax (similar to what is shown in your application icon's Target path).

 

HTH



"How we think determines what we do, and what we do determines what we get."

Message 4 of 21
BlackBox_
in reply to: hmsilva

I *believe* the OP is wanting to open the exported drawing in a new instance of AutoCAD, my friend.

 

Cheers



"How we think determines what we do, and what we do determines what we get."

Message 5 of 21
hmsilva
in reply to: BlackBox_


@BlackBox_ wrote:

I *believe* the OP is wanting to open the exported drawing in a new instance of AutoCAD, my friend.

 

Cheers


I missed that part from the OP... Smiley Embarassed

I must start reading more slowly, I'm getting too old... Smiley Happy

 

Cheers

Henrique

EESignature

Message 6 of 21
JohnnyBGoode1
in reply to: hmsilva

Thank you for your responses.

 

That's correct.  I need to start a new instance of AutoCAD and open the file that the routine calls in the new instance.

 

Again, I really appreciate your help.

 

 

Message 7 of 21
hmsilva
in reply to: JohnnyBGoode1

Try it, is minimally tested...

(DEFUN C:TEST ( / APP NEWFILE NEWFILEPATH QUOTENEWFILE)
(COMMAND "-EXPORTTOAUTOCAD" "M" "Y" "P" "NEW-" "" "")
(setq newfile	       (strcat "NEW-" (getvar "dwgname"))
      quotenewfile     (strcat "\"" newfile "\"")
      newfilepath      (strcat (getvar "dwgprefix") newfile)
)
(setq app (findfile "acad.exe"))
(startapp (strcat app " " newfilepath))

 Henrique

 

 

 

EESignature

Message 8 of 21
JohnnyBGoode1
in reply to: hmsilva

Sorry I'm just now responding.  It was a busy weekend.

 

I can't use the (findfile) function as the company I work for has the path on a network drive and we use an add-on to AutoCAD.  Adding the network path to the file support path doesn't work either.  I'm not sure why.  Beside, I don't want to have the routine modify everybody's support path.

 

Another thing is that ACAD.exe isn't used to open a new instance of the program.  The file I need is in the following format:

 

w:\\folder\\program.exe /add-on

 

I've tried several things and can't seem to get anything to work including the (startapp) function as below.  I'm not sure if it's because my code is faulty or if it's because of the way you have to call the add-on.

 

(startapp "W:\\Folder\\Program.exe /add-on" quotenewfilepath)

 That's why I was hoping that Visual LISP might be able to solve my problem of opening a new instance of our special version AutoCAD by simply calling the variable where the new file is located. 

 

I hope this makes sense...

 

 

Message 9 of 21
bhull1985
in reply to: JohnnyBGoode1

Here is a quick .bat routine that will execute a script onto all of the drawings within the specified path, each opening a new instance of autocad (which slows it from native scripting but is still useful for a variety of reasons)

 

FOR %%f in ("Z:/YOURFOLDER/SUBFOLDER/*.dwg") do start /wait C:\"Program Files"\Autodesk\"AutoCAD Mechanical 2012"\acad.exe "%%f" /b "Z:YOURSCRIPT/LOCATION/script.scr"

 

obviously you'd need to change this as per your needs, and even call it from within your lisp routine somehow, but it's perhaps a way to do what you're trying to do under the conditions in which you must do it

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 10 of 21
hmsilva
in reply to: JohnnyBGoode1

A shot in the dark, I have no way to test it...

 

(startapp "\"W:\\Folder\\Program.exe /add-on\"" quotenewfilepath)

 Henrique

EESignature

Message 11 of 21
JohnnyBGoode1
in reply to: hmsilva


@hmsilva wrote:

A shot in the dark, I have no way to test it...

 

(startapp "\"W:\\Folder\\Program.exe /add-on\"" quotenewfilepath)

 Henrique


I tried you code and I just received nil in return.  It's gotta be the "/add-on" that's the issue.  I know this is going to be difficult for me to iron out because everything I'm working with is proprietary in nature and no other company has this AutoCAD add-on.  It's gotten pretty difficult because I've had to put code in this routine to kill a process running in the background in order to get a new instance of our AutoCAD based program to start.

 

The startapp function will only start a new instance of AutoCAD at this point but will not open the new file.  I can only get the new file to open in the old instance.  And because I've had to kill that process, I get a fatal error when the drawing opens in the old instance.

Maybe I can just PM the code to you if you're willing to continue helping me?  I'll go back and comment the hell out of it so you know what my intentions are.  I know you won't be able to test it but maybe we can come up with a work around.

 

Who knows... maybe it's impossible to get Auto/Visual LISP to do what I need it to do.

 


@bhull1985 wrote:

 

FOR %%f in ("Z:/YOURFOLDER/SUBFOLDER/*.dwg") do start /wait C:\"Program Files"\Autodesk\"AutoCAD Mechanical 2012"\acad.exe "%%f" /b "Z:YOURSCRIPT/LOCATION/script.scr"

 


That's actually kinda cool. How would you include that in a LISP routine?  Could variables be used in place of the paths?

Message 12 of 21
bhull1985
in reply to: JohnnyBGoode1

Well first thing I would do is check out some of the links provided by our best friend google:

 

https://www.google.com/#q=using+a+.bat+within+autolisp

 

within those, and within 5 minutes of looking I've located this post via kent cooper, a resident guru and general great help guy:

 

Shell goes out to the operating system level for using DOS commands, of which batch files are one kind.

If a variable called BatPathAndFile contains the full filepath and batch file name [with or without the .bat part], you should be able to do this:

(command "_.shell" BatPathAndFile)

to run it.  It doesn't look as though the code you posted goes quite as far as putting together the entire filepath and name -- you would need to have it do that first.

--
Kent Cooper


nils wrote...
....where is the shell command compare to dwgprefix? How to make the lisp run shell and start the *.bat file?

 

-------------------------------------------------------------------------------------------------------------------------------------------------

 

 

So this should allow you to make some steps towards what you're doing.

Keep in mind that you can attach a script to the new instance of autocad, and from within that script you can make calls to the open function from within lisp/visual lisp. Take a look at the following, stolen from Alan @ augi

 

 

(vl-load-com)

(defun OpenDwgFile (arg1 / )
  (vla-activate (vla-open (vla-get-documents (vlax-get-acad-object)) arg1))
  ); defun

 

and the call to it would look like this within your routine after setting the appropriate variables

 (setq stDetailName (strcat stDetailFolder stCSIDivision stDetailName ".dwg"))

 

and then

 

(OpenDwgFile stDetailName)

 

..............................................................................

All that can be done within a script that you can attach to the new instance of autocad via the .bat file described earlier. The only thing to keep in mind when dealing with Vlisp over regular lisp (called vanilla lisp alot, not to be confused with visual lisp) is that in order to use the vlisp functions you have to load them all. That's what the (vl-load-com) function does and is the only thing you have to do to be able to use the vlisp functions. So if you're requiring usage of them, include the vl-load-com call before or after your routine, preferably before, at the very top, just to ensure everything works well.

 

 

HTH

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 13 of 21
BlackBox_
in reply to: bhull1985

If only someone had suggested SHELL Command earlier. 😉



Sent from my iPhone


"How we think determines what we do, and what we do determines what we get."

Message 14 of 21
bhull1985
in reply to: BlackBox_

lol yes I'm aware you had as well, BB

but it's more of a 'connecting the dots' thing at this point for him i'm sure 😛

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 15 of 21
hmsilva
in reply to: BlackBox_


@BlackBox_ wrote:
If only someone had suggested SHELL Command earlier. 😉

I have a feeling that someone has already suggested it... Smiley LOL

 

Henrique

EESignature

Message 16 of 21
bhull1985
in reply to: bhull1985

syntaxially , it's a command-called function not just a function, fyi though

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 17 of 21
BlackBox_
in reply to: bhull1985

No worries; I've been in the field for some time (construction inspection), and just thought to skim some emails while waiting for this directional drill crew to clean up a large frak-out.

You sited several great references and I'm sure the OP will get the help they need in short order due to your, henrique's and others' capable hands. *Tips hat*

Cheers



Sent from my iPhone


"How we think determines what we do, and what we do determines what we get."

Message 18 of 21
JohnnyBGoode1
in reply to: bhull1985


@bhull1985 wrote:

lol yes I'm aware you had as well, BB

but it's more of a 'connecting the dots' thing at this point for him i'm sure 😛


Exactly! lol Smiley Happy

I'm probably about average when it comes to writing small routines in AutoLISP, but I've never had to use the shell command prior to this save for what I'd mentioned earlier about killing a process.  Even that I had to google.  And, believe it or not, everything else in the routine works like a charm. 

 

But beyond that I have no clue what I'm doing when it comes to Visual LISP or Dos Shell. Smiley Embarassed

 

And about your awesome suggestion, BB...  A light bulb came on so I spent about an hour trying to fugure out how to do it...   The the light bulb blew... major fail on my part.

 

As soon as I get some free time, I'm gonna try out what you suggested, bhull.  I'll get back to you as soon as I screw it up. Smiley Tongue

 

I can't tell you guys how much I appreciate your help, and above all your patience.

Message 19 of 21
bhull1985
in reply to: JohnnyBGoode1

We'll get this working for you....well, *you* will get this working for you, with our help.

Just let us know how it goes, if you need to debug (get rid of an error) or how to proceede with syntax or whatnot.....just post any relevant lisp or dwg when you reply so we can all be on the same page with your progress.

 

In review;

 

 (COMMAND "-EXPORTTOAUTOCAD" "M" "Y" "P" "NEW-" "" "")
      (setq newfile (strcat "NEW-" (getvar "dwgname")))
      (setq quotenewfile (strcat "\"" newfile "\""))
      (setq newfilepath (strcat (getvar "dwgprefix") newfile))
      (setq quotenewfilepath (strcat "\"" newfilepath "\""))
             (vla-activate
               (vla-open
                 (vla-get-documents
                   (vla-get-application
                     (vlax-get-acad-object))) newfilepath))

 

So you've got your main routine that you need to create code in order to have it open and execute a .bat file, that is created with notepad and saved as batfilename.bat. To edit it after creating, you have to right click and select edit. Doubleclicking will cause it to run 😄

 

So, via

 

(command "_.shell" BatPathAndFile)

 

once your .bat is created containing the following code:

FOR %%f in ("Z:/YOURFOLDER/SUBFOLDER/*.dwg") do start /wait C:\"Program Files"\Autodesk\"AutoCAD Mechanical 2012"\acad.exe "%%f" /b "Z:YOURSCRIPT/LOCATION/script.scr"

 

where of course you'd have to path correctly to your version/type of autocad as well as the paths for your drawing file location, and in addition the path to your (not yet) created script.....on that note,

 

your script will contain:

(command "._open" dwgvar)
zoom extents

[blank space here that enters the command on the line above, script ends with this blank line placeholder thingy)

 

and whatever else you need it to do, basically you're just forcing the .bat file to open with a script attached to it. Be careful here because currently that .bat is setup to open cad, then the first drawing in a folder, execute the script, when it's finished to close autocad and then re-open autocad with the 2nd file in the folder, and so on and so forth.

 

If you're not requiring the start/wait portions then google how to write a .bat that simply opens a new instance of acad w/script attached

 

heading to lunch, that should get you thinkin'..

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 20 of 21
jsowinski
in reply to: JohnnyBGoode1

Here is a method that works for me using "Shell.Application". It first launches AutoCAD without any additional prompts. Then It will open my drawing in the new instance of AutoCAD. If you can get to the point of launching AutoCAD with your addon then try the last part of the code with your drawing path and name. I hope that helps.

 

(defun open_dwg (/ filename getshell)
 (startapp (findfile "acad.exe"))	; <<< (LAUNCHES AUTOCAD), *** PLACE YOUR METHOD OF OPENING THE PROGRAM HERE ***

 (setq filename "C:\\YourPath\\DwgName.dwg")

 (setq getshell (vlax-create-object "Shell.Application" ))
 (vlax-invoke-method getshell 'Open filename)
 (vlax-release-object getshell)
)
(open_dwg)

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost