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

Open dwg-file with lisp

15 REPLIES 15
SOLVED
Reply
Message 1 of 16
Anonymous
9788 Views, 15 Replies

Open dwg-file with lisp

Hi

 

I have a lisp script that should save the open file to a new folder, and then open the "old" file again.

 

For this I thought I would get the file path and file name and store it in a variable, then open the old file using that variable.

But I'm not sure how I would go by doing that. I don't really know how to get the variable "filepath" to work with my fileopen command.

 

(defun c:savefile()
	(setq filepath (strcat (getvar "dwgprefix") (getvar "dwgname")))
	(command "qsave")
	(command "_saveas" "2013" (strcat "c:\\newfolder\\" (getvar "dwgname")))
	(command "fileopen" (filepath))
    )

 

15 REPLIES 15
Message 2 of 16
Ajilal.Vijayan
in reply to: Anonymous

If you want to save a copy of the opened by without closing the current file

try this

 

(command "_SAVE" (strcat "c:\\newfolder\\" (getvar "dwgname")))

 This will save a copy of the current drawing to the specified folder without closing the active drawing

Message 3 of 16
p_mcknight
in reply to: Anonymous

Rather than save the drawing and try to reopen it, it would be easier to copy the file to a new location.  You can copy it even though it is open without any issue.

 

(defun c:test37 ( / )
  (vl-file-copy (strcat (getvar "dwgprefix") (getvar "dwgname")) "c://Autodesk//test.dwg")
  (princ)
  )

 

Note that if you want, you should add a line to save the drawing before copying it as this will make sure you get the most recent edits in the new file.

Message 4 of 16
Anonymous
in reply to: p_mcknight

Ajilal.Vijayan, I think your suggestion works best for now. Thank you very much.

p_mcknight, allthough I couldn't get yours to quite work, I just wonder if it's possible to avoid the overwrite promt if file exists with your suggestion?

Thanks, both of you!

Message 5 of 16
Anonymous
in reply to: Anonymous

Ok so I need to open the new file for some adjustments and then save it and open the old file again.

 

(defun c:savefile ( / )

  (setq file (getvar "DWGNAME"))
  (setq path (getvar "DWGPREFIX"))
  (setq pathfile (strcat path file))

  (setq newpathfile (strcat "c://newsavefolder//" file))
  (setq opennewpathfile (strcat "c:/newsavefolder/" file)); I would like to skip this line if possible...

  (command "qsave")

  (vl-file-delete newpathfile)
  (vl-file-copy pathfile newpathfile)
  (princ)

  (command "fileopen" opennewpathfile)

  (command "PURGE" "ALL" "*" "N")
  (command "PURGE" "REGAPPS" "*" "N")

  (command "qsave")

  (command "fileopen" pathfile); I think this line messes things up for me by containing double / in the path.
)

 

But it doesn't seem to be able to use the string "pathfile" with the "FILEOPEN"-command. I think it's because the "pathfile"-string contains //

 

Are there any other method for opening files where I can use the varible containing double /, or can I change // to / somehow?

Message 6 of 16
dmfrazier
in reply to: Anonymous

"(setq opennewpathfile (strcat "c:/newsavefolder/" file)); I would like to skip this line if possible..."

 

This line in your code has single foreslashes in the path.  Others have double foreslashes.

 

I thought the rule in AutoLISP was either a single foreslash ("/") or double backslash ("\\").

Message 7 of 16
Gary_J_Orr
in reply to: Anonymous

Watch your string formatting: Path delimiters need to be passed as either a double backslash or a single forward slash... NOT as double forward slashes.

This is a VALID path string:
(strcat "c:/newsavefolder/" file)
This is a VALID path string:
(strcat "c:\\newsavefolder\\" file)
Even this is a VALID path string (although strongly discouraged to mix delimiters):
(strcat "c:\\newsavefolder/" file)

this is an INVALID path string:
(strcat "c://newsavefolder//" file)

-Gary
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 8 of 16
Anonymous
in reply to: Anonymous

Thank you Gary for clarifying that. I've removed the invalid path variable. Allthough it did work anyway (for some reason) up until the point of opening the old file again, I got rid of unnecessary code.

 

But I've still got a problem with opening the file with the line:

 

(command "fileopen" pathfile)

Is there a problem with the command "fileopen" that can't use a variable to open a file or is it something else?

 

The variable "pathfile" contains a valid path, "C:\newsavefolder\myfile.dwg"

 

Thanks!

Message 9 of 16
Gary_J_Orr
in reply to: Anonymous


@Anonymous wrote:

Thank you Gary for clarifying that. I've removed the invalid path variable. Allthough it did work anyway (for some reason) up until the point of opening the old file again, I got rid of unnecessary code.

 

But I've still got a problem with opening the file with the line:

 

(command "fileopen" pathfile)

Is there a problem with the command "fileopen" that can't use a variable to open a file or is it something else?

 

The variable "pathfile" contains a valid path, "C:\newsavefolder\myfile.dwg"

 

Thanks!


Question for you: What version of Acad are you using? not just 2014/2013 etc, but are you using any vertical flavor of acad (Architecture, MEP, Civil 3d, Map, etc...).

Not that it really matters for what I am about to point out...

Lisp "memory" is document resident and only for the duration of the current editing session.

That is the first and foremost thing to remember when writing lisp routines.

There are advanced techniques for overcoming that limitation, but for you, and for now...

You need to realize that when you open a new drawing you do not have access to any variables that were/are stored in the original drawing (as well as the routine definition itself) so you need to give serious consideration to what you are trying to accomplish and the steps to get there.

Basically, once you hit that first fileopen you lose the variable that you created that contains the filename that you want to reopen after working on the secondary file.

 

-Gary

Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 10 of 16
dmfrazier
in reply to: Anonymous

"I think it's because the "pathfile"-string contains //"

 

You are correct.

As suggested earlier, change the "//" to either "/" (single foreslash) or "\\" (double backslash) and it should work. 

Message 11 of 16
dmfrazier
in reply to: Gary_J_Orr

Good catch.

Message 12 of 16
Anonymous
in reply to: Gary_J_Orr

Okey! I did not know that =P

 

I use ACAD 2013-14-15 Architectural but the script will be used on flavourless ACAD13-14-15 aswell.

 

First thing that comes to mind is storing the path in a file on the computer, like c:\mypath.txt. But maybe you have a better suggestion?

 

Thanks /M

Message 13 of 16
Ajilal.Vijayan
in reply to: Anonymous
Message 14 of 16
Gary_J_Orr
in reply to: Anonymous

your next problem is the fact that the routine itself is a variable in the current document... once that gets closed (or simply goes out of focus depending on how you handle the opening of the new file) it is not available in the second document...

You end up needing a "layered" structure of routines that call other routines on startup, or you need to use a script file to process the documents and loadfunctions/perform tasks in each document, or a more advanced ActiveX method to open drawings in a seperate session while your original session sends commands and/or control functions to that seperate session.

Some of the easiest sounding things become monsters once you get into them and multiple documents from lisp is one of those things.

-Gary
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 15 of 16
Anonymous
in reply to: Anonymous

Ok so instead of opening the copied file, I used "save as". Solved everything. The variable is still accessable and I can open the old file!

 

Thanks everyone for guiding me. /M

 

 

(defun c:savefile ( / )

  (setq file (getvar "DWGNAME"))
  (setq path (getvar "DWGPREFIX"))
  (setq pathfile (strcat path file))

  (setq newpathfile (strcat "c:/newsavepath/" file))

  (command "qsave")

  (vl-file-delete newpathfile)
  (princ)
  (command "_saveas" "2013" newpathfile)

  (command "UCS" "W")
  (command "PURGE" "ALL" "*" "N")
  (command "PURGE" "REGAPPS" "*" "N")

  (command "qsave")

  (command "fileopen" pathfile)
)

 

Message 16 of 16
kefer_kb
in reply to: Gary_J_Orr


@MadfwafaGary_J_Orr wrote:
your next problem is the fact that the routine itself is a variable in the current document... once that gets closed (or simply goes out of focus depending on how you handle the opening of the new file) it is not available in the second document...

You end up needing a "layered" structure of routines that call other routines on startup, or you need to use a script file to process the documents and loadfunctions/perform tasks in each document, or a more advanced ActiveX method to open drawings in a seperate session while your original session sends commands and/or control functions to that seperate session.

Some of the easiest sounding things become monsters once you get into them and multiple documents from lisp is one of those things.


Maybe, but not really.

A easy way to manipulate a lot of drawings is about to use it in single document inferface.

e.g.

Settings at beginning in your lisp routine:

(setvar "lispinit" 0)         ;LISP-Prg. mem. resistent
(setvar "sdi" 1)              ;Single Document Interface

 

Your drawings (best incl. Path) in a list

e.g.

(setq list_dwglist '("drawing1.dwg" "drawing2.dwg" ...))

 

Modify all drawings:

 

(foreach dwg  list_dwglist

 

(if (> (getvar "DBMOD") 0)
       (command "_.open" "_y"dwg  )
       (command "_.open" dwg  )
     );endif

 

;Your LISP Code here for working with each drawing (plot, title block update, ...)

(....)

...

 

(command "_.saveas" "" <newname>)

 

);end foreach

 

Note: Above It is not a fully LISP Programm for working. But you can built your own application for modify many drawings with one running lisp routine without using of script's, acaddoc.lsp and so on.

 

kind regards,

Franz

www.gfm.at

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

Post to forums  

Autodesk Design & Make Report

”Boost