Jump to content

Simple saveas LISP


Hickoz_bro

Recommended Posts

Hi guys,

 

Need help with a really simple problem here... It's been a while since i've dealth with LISP, so i'm pretty rusty... I'm trying to create a simple save to file path kind of routine... The user simply provides the file name, and the lisp saves it to a pre-defined path.... NOW... I remember there was some trick to dealing with file path names, but I can't recall what it was... I've tried back slashes, forward slashes, and double forward slashes, but nothing seems to work...

 

Where am I going wrong?

 

(defun c:ssave ()
 (setvar "cmdecho" 0)
 (setvar "filedia" 0)
 (command "saveas" "2010" ("\\hdnt10\grp_eng\Group\2D AutoCAD\HDAL Library\")
  
 )
 (setvar "filedia" 1)
 (setvar "cmdecho" 1)
 (princ
)

 

Cheers

Link to comment
Share on other sites

Maybe something like this

 

(defun c:ssave (/ FileName)
 (setvar "cmdecho" 0)
 (setvar "filedia" 0)
 (setq FileName (getstring 1 "\nType Filename: "))
 (command "SAVEAS" "2010" (strcat  "C:\\hdnt10\\grp_eng\\Group\\2D AutoCAD\HDAL Library\\" FileName))	  
 (setvar "filedia" 1)
 (setvar "cmdecho" 1)
 (princ)
)

Link to comment
Share on other sites

I see what you've done there, I recall using the strcat function before...

 

I've modified your code a bit because the save location isn't on C:\ drive, it's acctually on a network location "\\hdnt10", I'm assuming I'm still suppose to double slash this bit? So it becomes "\\\\hdnt10"? When I use the code below the check code give me the error "error: malformed list on input"

 

Everything else looks good, thanks for the help

 

(defun c:ssave (/ FileName)
 (setvar "cmdecho" 0)
 (setvar "filedia" 0)
 (setq FileName (getstring 1 "\nType Filename: "))
 (command "SAVEAS" "2010" (strcat  "\\\\hdnt10\\grp_eng\\Group\\2D AutoCAD\\HDAL Library\\" FileName))	  
 (setvar "filedia" 1)
 (setvar "cmdecho" 1)
 (princ)

 

Regards

Link to comment
Share on other sites

I figured you would replace the C: with the drive you want to be saving to . The only way I can think of for you to get the right path is:

Open up a drawing already saved to the location you want to be saving to and type DWGPREFIX at the command line. You should get the path

 

the way lisp reads it. Use that to replace in the routine, not forgetting doubling up the slashes. If that does not work, there are a lot of people here with higher skills than me who hopefully will come along and help.

 

PS: I don't know if you can use four slashes. I work on a network too and there is a drive associated with each location. Use that in place of C:

Link to comment
Share on other sites

Okay,

 

I did as you suggested, I removed reference to the network location, and used the mapped drive location instead, so the code looks like this:

 

(defun c:ssave (/ FileName)
 (setvar "cmdecho" 0)
 (setvar "filedia" 0)
 (setq FileName (getstring 1 "\nEnter Filename for Save: "))
 (command "SAVEAS" "2010" (strcat  "G:\\Group\\2D AutoCAD\\HDAL Library" FileName))	  
 (setvar "filedia" 1)
 (setvar "cmdecho" 1)
 (princ)
 )

 

Now, I've tried to adapt the same thing to the OPEN command, thinking it would be nice and easy... but i can't get it to work...

 

(defun c:oopen (/ FileName)
 (setvar "cmdecho" 0)
 (setvar "filedia" 0)
 (setq FileName (getstring 1 "\nEnter Filename to Open: "))
 (command "OPEN" (strcat  "G:\\Group\\2D AutoCAD\\HDAL Library\\" FileName))	  
 (setvar "filedia" 1)
 (setvar "cmdecho" 1)
 (princ)
 )

 

For some reason, when it gets to the open command, it exits it in the same step, so when it parses the directory and filename, it treats it as a seperate command, so I get the message:

 

Command: G:\Group\2D AutoCAD\HDAL Library\a100218-01.dwg Unknown command 
"G:\GROUP\2D AUTOCAD\HDAL LIBRARY\A100218-01.DWG".  Press F1 for help.

 

Any suggestions?

Link to comment
Share on other sites

Here is something I found with web search by T. Willey posted at the AUGI forums:

 

You can't use the open command within lisp to open a draiwng. You have to add the file to the document object of autocad, and go about it that way.

(defun MyOpen (FileName ReadOnly / )
; If readonly is not nil, then it will open the file as read-only.

(vl-load-com)
(vla-Open
(vla-get-Documents
 (vlax-get-Acad-Object)
)
FileName
(if ReadOnly
 :vlax-true
 :vlax-false
)
)
)

 

It might get you on the right path. I have to figure it out myself. If I do I will share it with you.

Link to comment
Share on other sites

Perhaps something like this:

 

(defun c:ssave ( / *error* vars old s )

 (defun *error* ( msg )
   (and old (mapcar 'setvar vars old))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )

 (setq vars '("CMDECHO" "FILEDIA") old (mapcar 'getvar vars))
 (mapcar 'setvar vars '(0 0))

 (while
   (progn
     (setq s (getstring (strcat "\nEnter Filename for Save <" (getvar 'DWGNAME) "> : ")))

     (cond ( (eq "" s)
             (setq s (getvar 'DWGNAME)) nil)

           ( (not (snvalid s))
             (princ "\n** Invalid **")))
   )
 )

 (command "_.saveas" "_2010" (strcat "G:\\Group\\2D AutoCAD\\HDAL Library\\" s))
 (mapcar 'setvar vars old)

 (princ)
)

 

Or

 

(defun c:ssave2 ( / s )
 (vl-load-coM)

 (while
   (progn
     (setq s (getstring (strcat "\nEnter Filename for Save <" (getvar 'DWGNAME) "> : ")))

     (cond ( (eq "" s)
             (setq s (getvar 'DWGNAME)) nil)
       
           ( (not (snvalid s))
             (princ "\n** Invalid **")))
   )
 )

 (vla-saveas
   (vla-get-ActiveDocument
     (vlax-get-acad-object)
   )
   (strcat "G:\\Group\\2D AutoCAD\\HDAL Library\\" s)
 )

 (princ)
)

 

And for open:

 

(defun c:oopen ( / s )
 (vl-load-com)

 (while
   (or
     (eq ""
       (setq s
         (getstring "\nEnter Filename to Open: ")
       )
     )
     (not (snvalid s))
   )
   (princ "\n** Invalid **")
 )

 (vla-open
   (vla-get-Documents
     (vlax-get-acad-object)
   )
   (strcat "G:\\Group\\2D AutoCAD\\HDAL Library\\" s) :vlax-false
 )

 (princ)
)
 

Link to comment
Share on other sites

Bloody hell! I'm sure LISP wasn't that difficult last time I played with it... I'll try and get my head around that one, and give it a shot at work tomorrow. Thanks heaps for the advice.

Link to comment
Share on other sites

this works for me...

 

(defun c:oopen (/ FileName)
 (setvar "cmdecho" 0)
 (setvar "filedia" 0)
 (setq FileName (getstring 1 "\nEnter Filename to Open: "))
[b] (setq myFile (strcat "P:\\Design_Office\\E106\\E106-11\\" FileName))[/b]
[b] (command "OPEN" myFile)[/b]
 (setvar "filedia" 1)
 (setvar "cmdecho" 1)
 (princ)
 )

 

of course it goes wrong if the file doesn't exist but you can sort that out.

Link to comment
Share on other sites

Bloody hell! I'm sure LISP wasn't that difficult last time I played with it... I'll try and get my head around that one, and give it a shot at work tomorrow. Thanks heaps for the advice.

 

X2...same here. I understand functions in isolation. Its when it comes to the nesting, and which function to use in what situation, and how to get them to work together that gets me. But at least you got your request written with bells and whistles. I was hoping someone would come along and say "let their be light"...thanks Lee.

Link to comment
Share on other sites

this works for me...

 

(defun c:oopen (/ FileName)
 (setvar "cmdecho" 0)
 (setvar "filedia" 0)
 (setq FileName (getstring 1 "\nEnter Filename to Open: "))
[b] (setq myFile (strcat "P:\\Design_Office\\E106\\E106-11\\" FileName))[/b]
[b] (command "OPEN" myFile)[/b]
 (setvar "filedia" 1)
 (setvar "cmdecho" 1)
 (princ)
 )

 

of course it goes wrong if the file doesn't exist but you can sort that out.

 

I tried...still does not work for me.

Link to comment
Share on other sites

Is there any way to check if files exist? I want to save versions this way. I added some rules to your code but I have no idea if I can check for drawing existence.

(defun c:ssave ( / *error* vars old s )

 (defun *error* ( msg )
   (and old (mapcar 'setvar vars old))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )

 (setq vars '("CMDECHO" "FILEDIA") old (mapcar 'getvar vars))
 (mapcar 'setvar vars '(0 0))

 (while
   (progn
     (setq s (getstring (strcat "\nEnter Filename for Save <" (getvar 'DWGNAME) "> : ")))

     (cond ( (eq "" s)
             (setq s (getvar 'DWGNAME)) nil)

           ( (not (snvalid s))
             (princ "\n** Invalid **")))
   )
 )if not;check if drawing excist if it does (setq n (1+ n)) and check again***********************************************
 (acet-file-mkdir (strcat (getvar "dwgprefix") "\backup\\"))
 (setq DWGNM (strcat (getvar "dwgprefix") "\backup\\"(strcat (GETVAR "DWGNAME" ))))
 (command "_.saveas" "_2007" (strcat (getvar "dwgprefix") "\backup\\" s (itoa n)))
   
 );and if

 (mapcar 'setvar vars old)

 (princ)
)

Link to comment
Share on other sites

Perhaps something like:

 

(defun c:ssave ( / *error* vars old s path dwg )
 (vl-load-com)

 (defun *error* ( msg )
   (and old (mapcar 'setvar vars old))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )

 (setq vars '("CMDECHO" "FILEDIA") old (mapcar 'getvar vars))
 (mapcar 'setvar vars '(0 0))

 (while
   (progn
     (setq s (getstring (strcat "\nEnter Filename for Save <" (vl-filename-base (getvar 'DWGNAME)) "> : ")))

     (cond ( (eq "" s)
             (setq s (vl-filename-base (getvar 'DWGNAME))) nil)

           ( (not (snvalid s))
             (princ "\n** Invalid **")))
   )
 )

 (if (not (findfile (setq path (strcat (getvar 'DWGPREFIX) "backup"))))
   (vl-mkdir path)
 )

 (if (findfile path)
   (progn
     (if (findfile (setq dwg (strcat path "\\" s ".dwg")))
       (
         (lambda ( i )
           (while (findfile (setq dwg (strcat path "\\" s (itoa (setq i (1+ i))) ".dwg"))))
         )
         0
       )
     )

     (command "_.saveas" "_2007" dwg)
   )    
 )

 (mapcar 'setvar vars old)

 (princ)
)

 

Not sure what file structure you are looking for however?

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