Jump to content

Autolisp to Evaluate User Variables


mwade93

Recommended Posts

I have a plot setup to plot all the layout tabs in a drawing. I have provided the users the options to select the paper size, plot style, and orientation. These are stored as user variables (PDFPS, PST, and PLO). I have the setup working great, but I am trying to set it up where if the user hasn't selected one of the variables, the command will be cancelled. I have tried an if statement, but I wanted to evaluate all three variables at once. I'm thinking it should be doable, just have been able to figure it out. Any help is appreciated.

 

The code below is the code that plots all the layouts and places them in the PDF folder for each project.

 

^C^C^C(foreach LL (layoutlist)(setvar "ctab" LL)(command (setq DL (substr (getvar "dwgprefix") 1 (- (strlen (getvar "dwgprefix")) 13))) (setq DN (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 4))) (strcat DL DN);(setq FS (strcat DL "52 PDFs" (chr 92) DN "-" (getvar "CTAB") ".pdf")) "plot" "yes" "" "AutoCAD PDF (General Documentation)" PDFPS "Inches" PLO "No" "Extents" "1:1" "Center" "Yes" PST "Yes" "No" "No" "No" FS "Yes" "Yes" "Yes"));(alert "If all settings were selected, PDFs have been made of all layout sheets and saved in the PDFs folder for this project.  They were saved as Project Name-Layout Name");

 

For reference, I have a code that evaluates each variable and displays a message.

(if (= nil PDFPS) (alert "No paper size was selected.  Please cancel command and select paper size.") (strcat PDFPS))

Edited by mwade93
Link to comment
Share on other sites

This is your code, indented:

^C^C^C
(foreach LL (layoutlist)
(setvar "ctab" LL)
(command 
	(setq DL (substr (getvar "dwgprefix") 1 (- (strlen (getvar "dwgprefix")) 13)))
	(setq DN (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 4)))
	(strcat DL DN);
	(setq FS (strcat DL "52 PDFs" (chr 92) DN "-" (getvar "CTAB") ".pdf"))
	"plot" "yes" "" "AutoCAD PDF (General Documentation)" PDFPS 
	"Inches" PLO "No" "Extents" "1:1" "Center" "Yes" PST "Yes" "No" "No" "No" FS "Yes" "Yes" "Yes"
)
)
(alert
"If all settings were selected, PDFs have been made of all layout sheets and 
saved in the PDFs folder for this project.  They were saved as Project Name-Layout Name"
)

Example for a proper structure would be something like this (corrections in red color):

[color="red"](defun C:PlotMyLayouts ( / DL DN FS PDFPS PLO PST ) [color="darkgreen"]; Localise your variables (everything that is used with [i]setq [/i]function)[/color]
(if
	(and
		(setq DL ...)
		(setq DN ...)
		(setq FS ...)
		(setq PDFPS ...)
		(setq PLO ...)
		(setq PST ...)
	)
	(progn[/color]
		(foreach LL (layoutlist)
			(setvar "ctab" LL)
			(command 
				[color="red"]DL[/color]
				[color="red"]DN[/color]
				(strcat DL DN);
				[color="red"]FS[/color]
				"plot" "yes" "" "AutoCAD PDF (General Documentation)" PDFPS 
				"Inches" PLO "No" "Extents" "1:1" "Center" "Yes" PST "Yes" "No" "No" "No" FS "Yes" "Yes" "Yes"
			); command
		); foreach
		(alert
			"If all settings were selected, PDFs have been made of all layout sheets and 
			saved in the PDFs folder for this project.  They were saved as Project Name-Layout Name"
		)
[color="red"]		); progn
); if
(princ)
); defun[/color]

And btw note that you could use:

(setq DN (cadr (fnsplitl (getvar 'dwgname))))

Link to comment
Share on other sites

Thank you for the reply. I am trying to do everything on the command line without the use of an external LSP file. The variables that I am evaluating are entered using buttons that I have created to set the variables separately. Then the user can create the setup using the buttons.

 

What does the second code do?

Link to comment
Share on other sites

What does the second code do?

It does... nothing. Actually thats an example how to input / fill all the variables before the command call is evaluated,

in short: the code checks if everything you've entered is OK and then proceeds with the plotting, otherwise you simply crash the command ( and the code exits with error ).

Even simplier example:

(if
(and
	(setq pdfSaveDirectory ...)
	(setq pdfName ...)
	(setq PaperSize ...)
)
(command "_.PLOT" ... )
)	

Link to comment
Share on other sites

(if
   (and
       (setq pdfSaveDirectory ...)
       (setq pdfName ...)
       (setq PaperSize ...)
   )
   (command "_.PLOT" ... )
)    

 

I don't want to set the variables with this code though, I am just looking to verify that they do not have a value of nil. The setq here means that they will be set to what I enter in the code? The code crashing is exactly what I am looking for it to do though. Here is the image of the setup:

Nbg+mAAAAAElFTkSuQmCCAA==

Nbg+mAAAAAElFTkSuQmCCAA==

Edited by mwade93
Link to comment
Share on other sites

I don't want to set the variables with this code though, I am just looking to verify that they do not have a value of nil. The setq here means that they will be set to what I enter in the code? The code crashing is exactly what I am looking for it to do though.

Whats the reason not to set the variables as long as they are localised?

However try this (I don't know how to set it to a macro):

(if (and PDFPS PLO PST)
(progn
	(foreach LL (layoutlist)
		(setvar "ctab" LL)
		(command (setq DL (substr (getvar "dwgprefix") 1 (- (strlen (getvar "dwgprefix")) 13))) 
			(setq DN (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 4)))
			(strcat DL DN);
			(setq FS (strcat DL "52 PDFs" (chr 92) DN "-" (getvar "CTAB") ".pdf"))
			"plot" "yes" "" "AutoCAD PDF (General Documentation)" PDFPS "Inches" PLO "No" "Extents" "1:1"
			"Center" "Yes" PST "Yes" "No" "No" "No" FS "Yes" "Yes" "Yes"
		)
	)
	(alert 
		"If all settings were selected, PDFs have been made of all layout sheets and saved in the PDFs folder for this project.
		They were saved as Project Name-Layout Name"
	)
)
(cond 
	((not PDFPS) (alert "\n\"PDFPS\" was nil!"))
	((not PLO) (alert "\n\"PLO\" was nil!"))
	((not PST) (alert "\n\"PST\" was nil!"))
)
)

And BTW the images you've uploaded are not loading.

Link to comment
Share on other sites

A couple of suggestions

 

If you running this a a macro then make it ^c^c^c(load "plotroutine") so it loads a lisp.

 

Have a look at a prefilled dialouge you can put the current value into the boxes and check for any that have not been filled in and ask the question continue or exit completey, the continue will open the dialouge again the exit is just that use (exit) it will end the lisp.

 

Re dialouge you can have say 3 lists so the user can choose the correct option. Me I just have multiple options in a menu so users picks what they want.

Screen Shot 11-14-16 at 01.07 PM.jpg

Screen Shot 11-22-16 at 12.19 PM.PNG

Link to comment
Share on other sites

Whats the reason not to set the variables as long as they are localised?

And BTW the images you've uploaded are not loading.

 

The reason for that is that way I have everything set up. I have a panel on the ribbon to select paper size, plot styles, and orientations. They set the variables that I use in this code. It just makes it easier to use in my opinion. I will try the code that you posted out.

 

EDIT: This works perfectly, thank you very much for your help!

 

Here is the code I ended up using:

^C^C^C(if (and PDFPS PLO PST) (progn (foreach LL (layoutlist) (setvar "ctab" LL) (command (setq DL (substr (getvar "dwgprefix") 1 (- (strlen (getvar "dwgprefix")) 13))) (setq DN (substr (getvar "dwgname") 1 (- (strlen (getvar "dwgname")) 4))) (strcat DL DN); (setq FS (strcat DL "52 PDFs" (chr 92) DN "-" (getvar "CTAB") ".pdf")) "plot" "yes" "" "DWG to PDF" PDFPS "Inches" PLO "No" "Extents" "1:1" "Center" "Yes" PST "Yes" "No" "No" "No" FS "Yes" "Yes" "Yes")) (alert "PDFs have been made of all layout sheets and saved in the PDFs folder for this project.  They were saved as Project Name-Layout Name")) (cond ((not PDFPS) (alert "No paper size was selected.  Please select paper size and restart command.")) ((not PLO) (alert "No orientation was selected.  Please select orientation and restart command.")) ((not PST) (alert "No plot style was selected.  Please select plot style and restart command."))));

Edited by mwade93
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...