Saveas lisp, subfolder

Saveas lisp, subfolder

andreas7ZYXQ
Advocate Advocate
2,159 Views
4 Replies
Message 1 of 5

Saveas lisp, subfolder

andreas7ZYXQ
Advocate
Advocate

Im looking for a Saveas lisp to run. 
i have searched throught various posts but i cant find anything to work. 

I would like to start my routine with save as in a subfolder where the curent drawings is saved. 

after this routine i want to add various things so it has to be "opened" as i normally does when pressing save as in the file tab...

Anyone out there that knows a good trick for this? 
If it not too much to ask it would be great to have some comments through the code so i can begin to understand what happends. I would like to understand more of this and maybe even build some own code from scratch

0 Likes
Accepted solutions (1)
2,160 Views
4 Replies
Replies (4)
Message 2 of 5

Shneuph
Collaborator
Collaborator
Accepted solution

Here is a basic example of how you can do this (with notes).  If I understood what you wanted clearly.

 

(Defun C:Saveas_Sub_Folder ( / subfolder cpath newpath); Define "COMMAND" & localize variables (delete when lisp is done running)
  (setq subfolder (getstring "Enter Subfolder Name: ")); get subfolder name from user (no checking for allowed windows characters)
  (setq cpath (getvar "dwgprefix")) ; get current drawing path
  (Setq newpath (strcat cpath subfolder)) ; add the new sub-folder to the end of the current path
  (if (not (findfile newpath)) ; check if the subfolder already exists
    (vl-mkdir newpath) ; if not make it
    );if
  (command "saveas" "" (strcat newpath "\\" (getvar "dwgname"))); saveas with new path and existing dwg name
  );defun

 

 

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
Message 3 of 5

Kent1Cooper
Consultant
Consultant

@andreas7ZYXQ wrote:

....
I would like to start my routine with save as in a subfolder where the curent drawings is saved. 

after this routine i want to add various things so it has to be "opened" as i[t] normally does when pressing save as in the file tab...
....


I assume you mean a subfolder under the one in which the current drawing is located, not the same subfolder in which it is, as @Shneuph appears to have assumed.  There's no need to have to spell out a subfolder name, nor check whether it exists [whether because it hasn't been made yet or you mis-typed it].  You can do this:

 

(defun C:DOSTUFF (/ some local variables)
  (... do some initial stuff ...)
  (initdia); INITiate the DIAlog box for the following command
  (command "_.saveas"); do all you need to do in the dialog box
  (... do more stuff ...)
  (... do more other stuff ...)
  (princ)
); defun

 

The (initdia) is needed because without it, commands called in a (command) function use the command-line version.  The all you need to do part includes the options of picking on an existing subfolder if there is one [no risk of typing its name incorrectly], just entering a new drawing name if you want it in the same folder the starting drawing is in, right-clicking in the list of drawings and picking New > Folder if you need to make one that doesn't exist yet, navigating to somewhere entirely unrelated if that's what you need, etc., etc.  Since it's operating through the dialog box, there's no need to do the wait-for-input-until-the-command-is-done thing that you typically do with a command in which you don't know the amount of inputs the User will need to provide, in command-line mode.

 

I wasn't sure this approach would work, because I had always heard that an AutoLisp routine can't start in one drawing and continue in another, but I tried it, and in this case it does, presumably because SAVEAS moves you into the new drawing, and the starting one doesn't remain active.  [I think an example of what wouldn't work would be with an OPEN command, in which case the drawing you start the routine in is still active, but I haven't tested the theory].  I did the following as a completely arbitrary test:

 

(defun C:TEST ()
  (initdia)
  (command "_.saveas")
  (command "_.circle" (getvar 'viewctr) 10)
  (command "_.move" "_last" "" "0,5" "")
)

and sure enough, when I ran the TEST command, I got to save a new drawing, was taken into it, and it did draw a 10-radius Circle in the middle of the screen, and Moved it up 5 units.  So it will take you into the new drawing and continue with your "add various things" part.

 

Kent Cooper, AIA
0 Likes
Message 4 of 5

Shneuph
Collaborator
Collaborator

I read it as the drawing was already saved in a folder e.g., "C:\FolderA\This Drawing.dwg" and the user wanted to saveas to a subfolder of the "Folder A" (new or existing) like, "C:\Folder A\New or E Subfolder\This Drawing.dwg"

 

I could have misinterpreted what the user wanted.

---sig---------------------------------------
'(83 104 110 101 117 112 104 64 71 109 97 105 108 46 99 111 109)
0 Likes
Message 5 of 5

andreas7ZYXQ
Advocate
Advocate

Sheneuph you just made my day alot better 😛

Actually i find both of your codes useful so i will use them both but in this case Sheneuphs code seems to fulfil my wishes 😃

saves my file and continue to draw a circle there. 😛

Thanks alot guys, and for the great comments so i can understand the code.

 

(Defun C:ss ( / subfolder cpath newpath); Define "COMMAND" & localize variables (delete when lisp is done running)
  (setq subfolder "3D"); get subfolder name from user (no checking for allowed windows characters)
  (setq cpath (getvar "dwgprefix")); get current drawing path
  (Setq newpath (strcat cpath subfolder)); add the new sub-folder to the end of the current path
  (if (not (findfile newpath)); check if the subfolder already exists
    (vl-mkdir newpath); if not make it
    );if
  (command "saveas" "" (strcat newpath "\\" (getvar "dwgname"))); saveas with new path and existing dwg name
  (command "_.circle" (getvar 'viewctr) 10)
  (command "_.move" "_last" "" "0,5" "")
  (command "_-view" "_swiso" )
  (command "zoom" "e" )
)
0 Likes