save lisp parameters for next use

save lisp parameters for next use

Anonymous
Not applicable
3,098 Views
21 Replies
Message 1 of 22

save lisp parameters for next use

Anonymous
Not applicable

hi. is there any way to save and read last used lisp parameters every time it's used? for example: the offset command "remembers" the last offset distance and layer options parameters used. i'd like to have 2 saved parameters in my file if possible.

0 Likes
3,099 Views
21 Replies
Replies (21)
Message 2 of 22

regisrohde
Advocate
Advocate
You can create a function that saves the parameters in variables.
Please mark this as the solution if it resolves your issue.Kudos gladly accepted.
Regis Rohde
Message 3 of 22

Ajilal.Vijayan
Advisor
Advisor

Define the variables if they are not present.

So next time when you run the code, you can retrieve that value.

Example:

 

(defun setmyvariables ()
	(if (and myvar1 myvar2)
	(progn
		(print "variables found")
		(print myvar1)
		(print myvar2)
	);progn
	(progn
		(print "variables not found, setting now")
		(setq 
		myvar1 10
		myvar2 20)
	);progn
	);if
);defun

Also if you need to save these variables for another drawings or even in a new AutoCAD session try setenv and getenv

 

Message 4 of 22

Kent1Cooper
Consultant
Consultant

@Ajilal.Vijayan wrote:

.... 

....
	(if (and myvar1 myvar2)
....

Also if you need to save these variables for another drawings or even in a new AutoCAD session try setenv and getenv

 


[A small quibble, perhaps, and I know it's just a generic example, but....  In that situation, if one of those two variables has a value in it but not the other, both will be set to the built-in values, possibly changing the value already in the one variable.  If that might be an issue, it's easily avoided with slightly more code, by doing the same for each variable separately instead of together.]

 

There are countless examples around of the use of global variables for this kind of thing within the same session in the same drawing. For just a couple, see my routine for labeling entities and breaking them around the labels, here, and another similar one that masks instead of breaks the labeled entities.  Routines can be written with built-in standard initial-default values similarly to Ajilal.Vijayan's example, or to have no initial defaults, but to always ask the User for a value on first use, to become the default that will be offered on subsequent use.

 

For some examples of Ajilal.Vijayan's suggested use of environment variables, so that the values are available not only the next time you use the command in the current drawing but in any drawing, and are remembered even after closing and getting back into AutoCAD, I wrote some routines that use that approach to save multiple Fillet radii and multiple Chamfer distances.

 

 

[Most of the cited examples are around these Forums somewhere -- it's just easier for me to find them on the Cadalyst CAD Tips site.]

Kent Cooper, AIA
Message 5 of 22

hmsilva
Mentor
Mentor

@Anonymous wrote:

hi. is there any way to save and read last used lisp parameters every time it's used? for example: the offset command "remembers" the last offset distance and layer options parameters used. i'd like to have 2 saved parameters in my file if possible.


Hi ilan7YZJH,

if by 'my file', you mean the current dwg file, this System Variables, are intended for user use, and saved in the Drawing...

USERI1 to USERI5 to store integer values

USERR1 to USERR5 to store real numbers

 

ex.

 

(if (setq myvarname (getdist (strcat "\nSpecify Distance <" (rtos (getvar 'USERR4) 2 2) ">: ")))
    (setvar 'USERR4 myvarname)
    (setq myvarname (getvar 'USERR4))
)

 

Hope this helps,
Henrique

EESignature

Message 6 of 22

Kent1Cooper
Consultant
Consultant

@hmsilva wrote:

....

if by 'my file', you mean the current dwg file, this System Variables, are intended for user use, and saved in the Drawing...

USERI1 to USERI5 to store integer values

USERR1 to USERR5 to store real numbers

 

....

In addition to those, there are also similar variables available for User-specified text Strings: USERS1 through USERS5.  But they are usable only within the same editing session in a drawing -- for some unknown reason, they are not saved in the drawing file, as the Integer and Real number variables are.

Kent Cooper, AIA
Message 7 of 22

Anonymous
Not applicable

thanks everyone for the suggestions. i'll clear one thing up: by "my file" i meant the lisp and not the drawing. the ideal for me will be that settings are kept even for different sessions, so i'll have a look into setenv and getenv.

can i also make that the status bar will display available parameters with highlighted trigger letters, similar to the built-in commands? again an example from the offset command: the through, erase and layer parameters are available with T, E and L highlighted in blue, respectively.

0 Likes
Message 8 of 22

ВeekeeCZ
Consultant
Consultant

@Anonymous wrote:

thanks everyone for the suggestions. i'll clear one thing up: by "my file" i meant the lisp and not the drawing. the ideal for me will be that settings are kept even for different sessions, so i'll have a look into setenv and getenv.

...


If you can modify your lisp... so why not to use some exteral file to store all old values of all variables you need?

 

You can have some oldvariables.txt stored next to your routines where you gonna have pair of name and values

VARIABLE1 VALUE1

VARIABLE2 VALUE2

 

Message 9 of 22

Anonymous
Not applicable
Sound like a great idea but I'll tell you the truth I have no idea how to do that. Alsom, if I send the lisp to someone else it'd be neat to send just one file, but that's the least of my problems...
0 Likes
Message 10 of 22

hmsilva
Mentor
Mentor

@ВeekeeCZ wrote:

@Anonymous wrote:

thanks everyone for the suggestions. i'll clear one thing up: by "my file" i meant the lisp and not the drawing. the ideal for me will be that settings are kept even for different sessions, so i'll have a look into setenv and getenv.

...


If you can modify your lisp... so why not to use some exteral file to store all old values of all variables you need?

 

You can have some oldvariables.txt stored next to your routines where you gonna have pair of name and values

VARIABLE1 VALUE1

VARIABLE2 VALUE2

 


Why not Ajilal's advice, setenv and getenv...should be easier to write and read.

 

ex.

(if (not (setq VARIABLE1 (getenv "VARIABLE1")))
    (progn
        (setenv "VARIABLE1" "1.00") ; change to the correct default value
        (setq VARIABLE1 (getenv "VARIABLE1"))
    )
)

(if (setq myvarname (getdist (strcat "\nSpecify Distance <" (getenv "VARIABLE1") ">: ")))
    (setenv "VARIABLE1" (rtos myvarname 2 2));change the precision if necessary
    (setq myvarname (read (getenv "VARIABLE1")))
)

 

 

Henrique

EESignature

Message 11 of 22

Ajilal.Vijayan
Advisor
Advisor

@Anonymous wrote:

can i also make that the status bar will display available parameters with highlighted trigger letters, similar to the built-in commands? again an example from the offset command: the through, erase and layer parameters are available with T, E and L highlighted in blue, respectively.


Normally the command options are set by using getkword & initget. ( and its not in the status bar but in the command prompt)

I just tried with one example and like in the built-in commands the options are not highlited neither the keywords are in blue.

So I am not sure how we can do this.

Message 12 of 22

ВeekeeCZ
Consultant
Consultant

@Ajilal.Vijayan wrote:

@Anonymous wrote:

can i also make that the status bar will display available parameters with highlighted trigger letters, similar to the built-in commands? again an example from the offset command: the through, erase and layer parameters are available with T, E and L highlighted in blue, respectively.


Normally the command options are set by using getkword & initget. ( and its not in the status bar but in the command prompt)

I just tried with one example and like in the built-in commands the options are not highlited neither the keywords are in blue.

So I am not sure how we can do this.


Did you use the standard format? I can't try it right now, but this should work...

(initget "Yes No")

(getkword "\nSelect [Yes/No] <Yes>: ")

Message 13 of 22

Ajilal.Vijayan
Advisor
Advisor

Thanks @ВeekeeCZ

Yes,I was trying with the standard format(as in the examples) and that's why the options are not highlighted.

 

@Anonymous,

Please try with the example that @ВeekeeCZ provided.

Message 14 of 22

Ajilal.Vijayan
Advisor
Advisor

Kent1Cooper wrote

Spoiler

[A small quibble, perhaps, and I know it's just a generic example, but....  In that situation, if one of those two variables has a value in it but not the other, both will be set to the built-in values, possibly changing the value already in the one variable.  If that might be an issue, it's easily avoided with slightly more code, by doing the same for each variable separately instead of together.]

 Thanks for pointing out that @Kent1Cooper, like you mentioned my intention was to show an example to @Anonymous.

Message 15 of 22

scot-65
Advisor
Advisor
Local variables - session only = Gremlins.

Local variables - session only = USERS1-5 (string).
Saved variables in a drawing = USERI1-5 (integer) and USERR1-5 (real).
Do not depend on these USERs as they are designed for DIESEL displaying of user's values.

Saved variables in a drawing = VLAX-LDATA-PUT and VLAX-LDATA-GET.

Saved variables user profile = SETENV and GETENV.
Saved variables user profile = VL-REGISTRY-READ and VL-REGISTRY-WRITE.

Save variables for everyone on a network = OPEN / READ-LINE and WRITE-LINE / CLOSE.
Suggest setting this one up as an .INI structure.

There could be other methods.

???

...and Welcome to these Forums!

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

Message 16 of 22

mid-awe
Collaborator
Collaborator

@scot-65 wrote:

There could be other methods.

I know that I'm speaking up a bit late, but I have at one point in time or another tried all of those options through the years, and my experience is that the one option not mentioned seems to be the most convinient. Custom Drawing Properties are simple to work with and can be added and altered without any code if necessary.

 

I'm just sayin', it works best for me. Smiley Wink

 

 

Message 17 of 22

Anonymous
Not applicable

thanks everyone for the suggestions, I really appreciate all your help.

reagarding setenv and getenv: they work great and provided what i wanted.

regarding keywords: also work, but that's not exactly what i wanted. i'll try to clarify again. my lisp requires two polylines to work, what I'd like to have is that while I'm requested to select them then I'll have the option to change any of the parameters if wanted. like the offset command - when triggered I'm prompted to enter distance OR change parameters. maybe in my lisp I can have something similar like "Select two polylines OR [PARAMETER1 PARAMETER2 ...] <...>"?

0 Likes
Message 18 of 22

ВeekeeCZ
Consultant
Consultant

Maybe something like this (just to show a principle, not tested)

 

(while (not en2)
  (initget 1 "Par1 Par2 Par3")
  (setq f (entsel (strcat "\nSelect " (if en1 "second" "first") " polyline or [Par1/Par2/Par3]: ")))
  (cond ((= f "Par1")
	 (setq par1 (getdist "Set par1: ")))
	((= f "Par2")
	 (setq par2 (getdist "Set par2: ")))
	((= f "Par3")
	 (setq par3 (getdist "Set par3: ")))
	((and f
	      (= "LWPOLYLINE" (cdr (assoc 0 (entget (car f))))))
	 (if en1
	   (setq en2 (car f))
	   (setq en1 (car f))))
	("\nWrong selection. Need polyline. Try again!"))
)
Message 19 of 22

Anonymous
Not applicable

that seems to be genrealy working well. the polylines selection is through a differene function, but I'll find a way to debug everything so that it'll work.

0 Likes
Message 20 of 22

Ajilal.Vijayan
Advisor
Advisor
ilan7YZJH wrote:
maybe in my lisp I can have something similar like "Select two polylines OR [PARAMETER1 PARAMETER2 ...] <...>"?

I think the suggestion which @ВeekeeCZ posted will allow to select only one polyline.

As initget does not allow to use ssget, you may need to enter the parameter first and then go for the selection.