Hickoz_bro Posted July 18, 2010 Share Posted July 18, 2010 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 Quote Link to comment Share on other sites More sharing options...
ziko Posted July 19, 2010 Share Posted July 19, 2010 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) ) Quote Link to comment Share on other sites More sharing options...
Hickoz_bro Posted July 19, 2010 Author Share Posted July 19, 2010 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 Quote Link to comment Share on other sites More sharing options...
ziko Posted July 19, 2010 Share Posted July 19, 2010 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: Quote Link to comment Share on other sites More sharing options...
Hickoz_bro Posted July 20, 2010 Author Share Posted July 20, 2010 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? Quote Link to comment Share on other sites More sharing options...
ziko Posted July 20, 2010 Share Posted July 20, 2010 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. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 20, 2010 Share Posted July 20, 2010 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) ) Quote Link to comment Share on other sites More sharing options...
Hickoz_bro Posted July 21, 2010 Author Share Posted July 21, 2010 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. Quote Link to comment Share on other sites More sharing options...
dbroada Posted July 21, 2010 Share Posted July 21, 2010 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. Quote Link to comment Share on other sites More sharing options...
ziko Posted July 21, 2010 Share Posted July 21, 2010 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. Quote Link to comment Share on other sites More sharing options...
ziko Posted July 21, 2010 Share Posted July 21, 2010 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. Quote Link to comment Share on other sites More sharing options...
CADkitt Posted July 23, 2010 Share Posted July 23, 2010 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) ) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 23, 2010 Share Posted July 23, 2010 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.