Visual LISP, AutoLISP and General Customization
cancel
Showing results forย 
Showย ย onlyย  | Search instead forย 
Did you mean:ย 

๐Ÿ™ How to put a value in memory.

4 REPLIES 4
Reply
Message 1 of 5
arperezinf
542 Views, 4 Replies

๐Ÿ™ How to put a value in memory.

Hello everyone. ๐Ÿ‘‹

 

๐Ÿ‘‰How to put a value in memory.

 

I would like to know how to put a value in memory, only in the current drawing, to reuse it several times until you enter a new value or close the current drawing.

 

For example: Suppose we have a lisp routine and it asks us to enter a text or a number and that value is stored in memory, for when the command is called again, I can use the value that I previously entered, it is as if it were the last command you use in autocad.

 

Is it possible to do this?
How is it done?

 

Thank you very much for reading my message.
Greetings.


PS: I don't know anything about visual lisp programming, for this reason I ask you to write how it would be done.

Labels (3)
4 REPLIES 4
Message 2 of 5
Sea-Haven
in reply to: arperezinf

Look into global variables and local variables. Check out help. You want the 1st one.

Message 3 of 5
_gile
in reply to: arperezinf

Hi,

You can simply use a 'global variable'.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 4 of 5
hak_vz
in reply to: arperezinf

As stated by @Sea-Haven  and @_gile trick is in using global variable.

Here you have set of simple functions that take distance, point and value, assign variable name to it, and propagate to all open drawings. You can put lisp file into "startup suite" so that you can transfer value from drawing to drawing.

Function set value - sv  accepts autolisp expressions so you can make basic calculations.

For example:

 

Command: GD
Select first point > 
Select second point > 
Variable name for distance > a
"a = 4200"
Command: GD
Select first point > 
Select second point > 
Variable name for distance > b
"b = 2970"
Command: SV
Enter value or expression > (/ a b)
Variable name > c
1.41414

 

To recall variable name in command you have to add ! before variable name

 

SCALE
Select objects: 
1 found
Select objects: 
Specify the base point: 
Specify the Scale factor or [Copy/Reference] <1.0000>: !c
1.41414 
Specify the Scale factor or [Copy/Reference] <1.0000>: 1.414141414141414

 

Here is the code

 

(defun twodec (x)(atof(rtos x 2 3)))

(defun c:gd (/ pp _name)
;get distance and assign to variable name
	(setq pp (atof(rtos(distance (getpoint "\nSelect first point > ")(getpoint "\nSelect second point > ")) 2 5)))
	(setq _name (getstring "\n Variable name for distance > "))
    (set (read _name) pp)
	(vl-propagate (read _name))
	(strcat _name " = " (rtos pp 2 5))
)

(defun c:gp ( / pp _name)
;get point and assign to variable name
	(setq pp (mapcar 'twodec (getpoint "\nSelect point > "))
		_name (getstring "\n Variable name > "))
    
	(set (read _name) pp)
	(vl-propagate (read _name))
	pp
)

(defun c:sv( / pp _name)
	;assign value to variable name
	(setq pp (getstring "\n Enter value or expression >"))
	(setq _name (getstring "\n Variable name> "))	
    (set (read _name) (eval (read pp)))
	(vl-propagate (read _name))

)

 

Other combinations are possible.

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 5 of 5
Kent1Cooper
in reply to: arperezinf

The global variable suggestions are fine within the same editing session in the drawing, but they are lost when the drawing is closed.  If you want a numerical value to remain available the next time you open the same drawing, you can use the USER System Variables.  There are three kinds, only two of which work this way, and five of them in each category.

 

USERI1, USERI2, USERI3, USERI4, USERI5

store Integer values.

USERR1, USERR2, USERR3, USERR4, USERR5

store Real-number values.

 

Read about them in >Help<.  You store and retrieve them with the (setvar) and (getvar) AutoLisp functions.  Read about those functions in the >AutoLisp Reference<.

 

[There are also five USERS1-through-USERS5 System Variables to hold text-String values, but for some reason they are not saved when the drawing is closed.]

 

Then there are environment variables, which survive closing a drawing and are available to any drawing, and their values remain set until you set some other value in them.  For them, use the (setenv) and (getenv) functions.  Read about them.

 

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report