Jump to content

save in memory of routine lsp


leonucadomi

Recommended Posts

can someone help me i'm beginner.

 

 

I would like to learn how to request information in a routine lsp  and later that information is saved and is preset in the next use of the routine.

 

example:

 

this routine draws a circle asking the user for the diameter and a point to insert it.

 

(defun C:cir (/)

(setq dia (getreal "\nIngresa Dia"))
(setq pt1 (getpoint "\nSeleciona centro: "))
(command "_circle" pt1 "d" dia)
);defun

 

 

I would like that the next time, for default, it would offer me the last diameter previously entered..

 

 

how do I do that?

Link to comment
Share on other sites

 

 

(defun c:cir(/)
  (setq dia(getreal (strcat "\nIngresa Dia <" (vl-princ-to-string dia) "> ? :  ")))
  (setq pt1 (getpoint "\nSeleciona centro: "))
  (command "_circle" pt1 "d" dia))

 

  • Like 1
Link to comment
Share on other sites

the simplest is to use ldata it ill persists in the drawing if you save it.

(defun C:CIR (/ a dia pt1) 
  (setq dia (vlax-ldata-get "cir" "dia")) 	;checks for ldata and sets the variable
  (if (= dia nil) (setq dia "4")) 			;first time running it will default to this value
  (if (= "" (setq a (getstring (strcat "\nIngresa Dia [" dia "]: ")))) 
    (setq a dia) ;if you hit enter it will set a to dia value else override it to what you input
  )
  (setq pt1 (getpoint "\nSeleciona centro: "))
  (vl-cmdf "_.Circle" pt1 "D" a)
  (vlax-ldata-put "cir" "dia" a)           ;save variable to ldata persists in drawing when saved.
  (princ)
);defun 

 

You can also save the variable into the Registry so it will remember between drawings.  might need admin access to save variable IDK

(defun C:CIR (/ a dia pt1) 
  (or (setq dia (getenv "CirDia")) (setq dia "4")) ;looks for CirData in registry if not found sets it to 4
  (if (= "" (setq a (getstring (strcat "\nIngresa Dia [" dia "]: ")))) 
    (setq a dia) ;if you hit enter it will set a to dia value else override it to what you input
  )
  (setq pt1 (getpoint "\nSeleciona centro: "))
  (vl-cmdf "_.Circle" pt1 "D" a)
  (setenv "CirDia" a)                   ; saves update value dia value to registry
  (princ)
)

 

Edited by mhupp
fixed code errors
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

CIRCLERAD (System Variable) is not saved but will retrievable as long as that drawing stays open.

(setvar 'circlerad (getdist "Enter Radius or pick points "))

While there's no System Variable for diameter you could divide diameter in half to save as CIRCLERAD then multiply by 2 when you need diameter in the lisp.

  • Like 1
Link to comment
Share on other sites

variables within parenthesis:

( x / y )

 

x are for anything passed to the function when it is run typically from another function

y are local variables used only for this function. If you don't define y here they become global variables and their value can be used by any function - global variables can mess things up if you aren't careful. Unless you need to, define all variables as local and put them in these parenthesis

  • Like 1
Link to comment
Share on other sites

4 hours ago, mhupp said:

(defun C:CIR (/ a dia pt1) (setq dia (vlax-ldata-get "cir" "dia")) ;checks for ldata and sets the variable (if (= dia nil) (setq dia 4)) ;first time running it will default to this value (if (= "" (setq a (getstring (strcat "\nIngresa Dia [" dia "]: ")))) (setq a dia) ;if you hit enter it will set a to dia value else override it to what you input ) (setq pt1 (getpoint "\nSeleciona centro: ")) (vl-cmdf "_.Circle" pt1 "D" a) (vlax-ldata-put "cir" "dia" a) ;save variable to ldata persists in drawing when saved. (princ) );defun

 

SOMETHING IS WRONG THAT DOES NOT WORK

Link to comment
Share on other sites

5 hours ago, leonucadomi said:

 

SOMETHING IS WRONG THAT DOES NOT WORK

 

Sorry my code was wrong. when running for the first time it was setting dia variable to 4 but as an integer.

this caused an error in the if statement because it needs to be a string.

changed (setq dia 4) to (setq dia "4") so its stored as a string.

Also wasn't storing the correct variable in CirDia to have the updated input.

 

(defun C:CIR (/ a dia pt1) 
  (or (setq dia (getenv "CirDia")) (setq dia "4")) 
  (if (= "" (setq a (getstring (strcat "\nIngresa Dia [" dia "]: ")))) 
    (setq a dia) 
  )
  (setq pt1 (getpoint "\nSeleciona centro: "))
  (vl-cmdf "_.Circle" pt1 "D" a)
  (setenv "CirDia" a) 
  (princ)
)

 

also WELCOME TO THE FORUMS

Edited by mhupp
fix error in code
  • Like 1
Link to comment
Share on other sites

5 minutes ago, leonucadomi said:

 

UFFF   I CAN'T BELIEVE IT KEEP MARKING ERROR

 

no my dumb ass made a few mistakes should be fixed now.

  • Funny 1
Link to comment
Share on other sites

You might be using the old lisp still to reload the lsp file drag it from your explorer into autocad and drop it

the attached lisp is working for me. Also this is saving in the windows registry so if your using a mac or don't have permission to change it this might not work.

--update

setenv

Sets a system environment variable to a specified value

Supported Platforms: Windows and Mac OS

They are also stored in the environmental variables of your cad software so i don't think permissions are an issue either but don't know for sure.

 

Cir.gif.461229b6d34047c10fa6d028144adc72.gif

 

 

cir.lsp

Edited by mhupp
  • Like 1
Link to comment
Share on other sites

good moorning , I tried the routine and it was excellent, it works perfect thanks.

 

I will study it

 

 

regards

  • Like 1
Link to comment
Share on other sites

Please go throug the code I have created it will keep the command on until you give a null response

 

(defun C:cir (/)

(setq dia (getdist "\nIngresa Dia")); by changing the get real to get dist you can give the dia by clicking on the screen or enter a number from keyboard
(while (setq pt1 (getpoint "\nSeleciona centro or enter to exit: ")) ;This "while" loop the command repeats until you give a enter
(command "_circle" pt1 "d" dia)
)
);defun

 

Link to comment
Share on other sites

I've used a global variable for this scenario for a long time.

All of my global variables use the prefix gv_

 

(defun get_dia (/ d)
  (or gv_dia (setq gv_dia 2.))
  (initget 5)
  (setq d (getdist (strcat "\nDiameter <" (rtos gv_dia 2 2) ">:   ")))
  (or d (setq d gv_dia))
  (setq gv_dia d)
d)

 

The gv_ is global only in the current session.

 

It can be used with with numerous inputs and or rules.

 

HTH  -David

Edited by David Bethel
Been a while since I posted code with this software :)
  • Agree 1
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...