Need help to add "save as" command in my lisp

Need help to add "save as" command in my lisp

francine.zimmermannSRSWJ
Advocate Advocate
2,200 Views
13 Replies
Message 1 of 14

Need help to add "save as" command in my lisp

francine.zimmermannSRSWJ
Advocate
Advocate

I have written a small lisp allowing me to draw a tube, at the end of this lisp I would like to use the "save as" function with the variables seqt DIA and OFF preceded by the text CHS
The name of the drawing should be, for example:
CHS 21.3 1.5.dwg
21.3 being DIA
1.5 being OFF

 

can anyone help me please

0 Likes
2,201 Views
13 Replies
Replies (13)
Message 2 of 14

john.uhden
Mentor
Mentor

This might work:

(defun c:dt( / DIA OFF NEWDIA DWG)
(setq DIA (getdist  "Enter outside diameter: "))
(setq OFF (getdist "Enter a Thickness value: "))
(setq NEWDIA (- DIA OFF OFF))
(command "_.circle" "0,0" "D" DIA)
(command "_.circle" "0,0" "D" NEWDIA)
(setq DWG (strcat "CHS " (rtos DIA 2 1) " " (rtos OFF 2 1) ".dwg"))
(command "_.saveas" DWG)
(princ)
)

John F. Uhden

Message 3 of 14

francine.zimmermannSRSWJ
Advocate
Advocate

Many thank 

After testing I had to change a line by adding "": 

(command "_.saveas" DWG)

 to (command "_.saveas" "" DWG)

0 Likes
Message 4 of 14

Kent1Cooper
Consultant
Consultant

I always react against making a lot of variables that are used only once -- just put the information right where it is to be used.

And there's no need to include the ".dwg" in the name in the SAVEAS command -- it will make that file type without your specifying it.

Setting of multiple variables can be done within one (setq) function, and multiple commands within one (command) function.

Since AutoLisp cannot start in one drawing and continue in another, and SAVEAS will take you into the new drawing, the (princ) at the end will never be reached.

 

(defun c:dt (/ DIA OFF)
  (setq
    DIA (getdist "Outside diameter: ")
    OFF (getdist "Wall Thickness: ")
  )
  (command
    "_.circle" "0,0" "D" DIA
    "_.circle" "0,0" "D" (- DIA (* OFF 2))
    "_.saveas" "" (strcat "CHS " (rtos DIA 2 1) " " (rtos OFF 2 1))
  )
)

 

Consider whether a file path should be included, since presumably you want these in some meaningful folder location.

Consider whether you need to be taken into the new drawing.  If it makes as much sense to remain in the current one, use SAVE rather than SAVEAS, or even WBLOCK, and then the (princ) makes sense.

The sample drawing has a lot of baggage in it -- consider PURGE beforehand [whether or not built into the command].  Or use WBLOCK instead of SAVE or SAVEAS, and only the Circles will be written off to the new drawing.  [That would require an additional variable, to store the first Circle for object selection, and would split up the commands.]

Kent Cooper, AIA
0 Likes
Message 5 of 14

francine.zimmermannSRSWJ
Advocate
Advocate

Is it also possible to add the folder where I want to save the files : "C:\Folder\CHS\" 

0 Likes
Message 6 of 14

Kent1Cooper
Consultant
Consultant

@francine.zimmermannSRSWJ wrote:

Is it also possible to add the folder where I want to save the files : "C:\Folder\CHS\" 


 

....
    "_.saveas" "" (strcat "C:/Folder/CHS/CHS " (rtos DIA 2 1) " " (rtos OFF 2 1))
....

 

Note the forward slashes, needed instead of [single] backslashes because those are used to trigger special character codes.  You could use double backslashes instead:  "C:\\Folder\\CHS\\CHS "

Kent Cooper, AIA
Message 7 of 14

francine.zimmermannSRSWJ
Advocate
Advocate

I have add zoom extents and now it works perfect

Many thank Kent 

0 Likes
Message 8 of 14

Kent1Cooper
Consultant
Consultant

(setq DWG (strcat "C:\\SteelProfiles\\CHS\\" "CHS " (rtos DIA 2 1) " " (rtos OFF 2 1) ".dwg"))

 

That is an unnecessary division of the string into two pieces.  [And again, you don't need the ".dwg".]  It can be just:

 

(setq DWG (strcat "C:\\SteelProfiles\\CHS\\CHS " (rtos DIA 2 1) " " (rtos OFF 2 1)))

 

[My other streamlining and which-command suggestions remain....]

Kent Cooper, AIA
Message 9 of 14

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

....  Or use WBLOCK instead of SAVE or SAVEAS, and only the Circles will be written off to the new drawing. ....


Another advantage of that approach is that when used in a (command) function, WBLOCK removes the selected objects from the drawing.  That means you could make a whole batch of these from inside a single [Purged] drawing, one after the other, without needing to Erase the Circles made for the previous one every time, as you would need to do if you use SAVE/SAVEAS.  It also means that if you don't have the command loaded in all drawings by an acaddoc.lsp file, you need to load the command definition only once, in the drawing you make multiple drawings from, instead of needing to load it within every SAVEAS'd drawing as you go before you can make another.

Kent Cooper, AIA
Message 10 of 14

francine.zimmermannSRSWJ
Advocate
Advocate

but what does the last command line look like? : (command "-wblock" DWG "Y" "*")
how do I take the objects without having to select them?

How WBLOCK removes the selected objects from the drawing?

0 Likes
Message 11 of 14

Kent1Cooper
Consultant
Consultant

You can always find the sequence of prompts to respond to by starting the command in an AutoLisp (command) function at the command line, i.e. (command "_.wblock").  [That's better than using -WBLOCK at the command line, because in some cases they work a little differently.]

You should be able to do it like this [untested -- I didn't set up the folder structure]:

 

(defun c:dt (/ DIA OFF c1)
  (setq
    DIA (getdist "Outside diameter: ")
    OFF (getdist "Wall Thickness: ")
  )
  (command "_.circle" "0,0" "D" DIA)
  (setq c1 (entlast)); for later object selection
  (command
    "_.circle" "0,0" "D" (- DIA (* OFF 2))
    "_.wblock"
      (strcat "C:\\SteelProfiles\\CHS\\CHS " (rtos DIA 2 1) " " (rtos OFF 2 1))
      "" "0,0" ; new-drawing option, insertion point
      c1 (entlast) "" ; object selection
  )
  (princ)
)

 

Come to think of it, if you start in a clean empty Purged drawing, with 0,0 as its insertion base point, you could use the whole-drawing option instead of object selection.  That would eliminate the need for the c1 variable and the splitting up of the commands, but it retains the Circles, so if you want them gone to make another, they need to be Erased:

 

(defun c:dt (/ DIA OFF)
  (setq
    DIA (getdist "Outside diameter: ")
    OFF (getdist "Wall Thickness: ")
  )
  (command
    "_.circle" "0,0" "D" DIA
    "_.circle" "0,0" "D" (- DIA (* OFF 2))
    "_.wblock"
      (strcat "C:\\SteelProfiles\\CHS\\CHS " (rtos DIA 2 1) " " (rtos OFF 2 1))
      "*" ; entire-drawing option
    "_.erase" "_all" ""
  )
  (princ)
)

 

EDIT:  [I've removed an erroneous right parenthesis from the second one, so if you caught it before that, it would explain the fact that it didn't work right.]

Then there's the question, in all of these, of whether you should build in a check on whether the drawing name is already in that folder....

 

Further EDIT:  Come to think of it some more, if you start in a clean drawing like that, you could use SAVE instead of WBLOCK, and eliminate the need to answer a prompt beyond giving it the file location/name:

(defun c:dt (/ DIA OFF)
  (setq
    DIA (getdist "Outside diameter: ")
    OFF (getdist "Wall Thickness: ")
  )
  (command
    "_.circle" "0,0" "D" DIA
    "_.circle" "0,0" "D" (- DIA (* OFF 2))
    "_.save" (strcat "C:\\SteelProfiles\\CHS\\CHS " (rtos DIA 2 1) " " (rtos OFF 2 1))
    "_.erase" "_all" ""
  )
  (princ)
)

 

Kent Cooper, AIA
Message 12 of 14

francine.zimmermannSRSWJ
Advocate
Advocate

Many thank for all your help, with the first lisp it's easier to control which tube are drawn, with the second lisp with "wblock" the size of the file is smaller.

0 Likes
Message 13 of 14

Kent1Cooper
Consultant
Consultant

@francine.zimmermannSRSWJ wrote:

.... with the second lisp with "wblock" the size of the file is smaller.


[It might not be if the source drawing were thoroughly Purged first.  SAVEAS takes the whole drawing; WBLOCK with the object-selection approach only the selected objects.  So if the whole drawing consists of nothing but those objects, there shouldn't be any meaningful difference in file size.]

Kent Cooper, AIA
Message 14 of 14

Sea-Haven
Mentor
Mentor

Posted over here as well made a suggestion a different direction.

 

Need help to add "save as" command in my lisp - AutoLISP, Visual LISP & DCL - AutoCAD Forums (cadtut...