leonucadomi Posted June 7, 2021 Posted June 7, 2021 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? Quote
Ajmal Posted June 7, 2021 Posted June 7, 2021 (defun c:cir(/) (setq dia(getreal (strcat "\nIngresa Dia <" (vl-princ-to-string dia) "> ? : "))) (setq pt1 (getpoint "\nSeleciona centro: ")) (command "_circle" pt1 "d" dia)) 1 Quote
mhupp Posted June 7, 2021 Posted June 7, 2021 (edited) 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 June 8, 2021 by mhupp fixed code errors 1 1 Quote
tombu Posted June 7, 2021 Posted June 7, 2021 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. 1 Quote
leonucadomi Posted June 7, 2021 Author Posted June 7, 2021 WHY ARE VARIABLES PUTTED BETWEEN PARENTESIS? WHAT IS YOUR FUNCTION? Quote
Steven P Posted June 7, 2021 Posted June 7, 2021 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 1 Quote
ronjonp Posted June 7, 2021 Posted June 7, 2021 Another: http://www.lee-mac.com/localising.html 2 Quote
leonucadomi Posted June 7, 2021 Author Posted June 7, 2021 EXCELLENT AND THANK YOU FOR THE INFORMATION Quote
leonucadomi Posted June 7, 2021 Author Posted June 7, 2021 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 Quote
mhupp Posted June 7, 2021 Posted June 7, 2021 (edited) 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 June 8, 2021 by mhupp fix error in code 1 Quote
leonucadomi Posted June 7, 2021 Author Posted June 7, 2021 UFFF I CAN'T BELIEVE IT KEEP MARKING ERROR Quote
mhupp Posted June 7, 2021 Posted June 7, 2021 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. 1 Quote
mhupp Posted June 7, 2021 Posted June 7, 2021 (edited) 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.lsp Edited June 8, 2021 by mhupp 1 Quote
leonucadomi Posted June 8, 2021 Author Posted June 8, 2021 good moorning , I tried the routine and it was excellent, it works perfect thanks. I will study it regards 1 Quote
laijumalias Posted June 10, 2021 Posted June 10, 2021 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 Quote
David Bethel Posted June 10, 2021 Posted June 10, 2021 (edited) 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 June 10, 2021 by David Bethel Been a while since I posted code with this software :) 1 Quote
Recommended Posts
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.