For the last few days, I've been working on labelling macro that I need for my workplace. The program is now complete, but I just wanted to clean the program so that I can have a better understanding next time I come back to add more.
Here are part of my code that is related to this topic. The section commented out shows my previous approach compared to new approach with "parse" function that I want to finish.
When I executed, as expected, the change in "var" wasn't transferred to "*pnlLim" since invocating "parse" function copies the value of "*pnlLim" instead of the variable itself (or pointer).
Are there any way to pass the variable "by reference"? (in the sense of OOP)
I'm not really familiar with the memory of AutoCAD 😕
(defun parse (var msg / tmp) (setq tmp (getint (strcat "\n" msg " <" (cond (var (itoa var)) ("1")) ; offer previous value if present; otherwise 1 ">: " ); strcat ); getint ); setq (setq var (cond (tmp) (var) (1)) ); setq ) (defun C:Label(/ term txt tmpsl tmppl tmpsc tmppc entName entType entInfo) ; initialize (parse *pnlLim "Enter number of panels per string") ; (setq tmppl ; (getint ; (strcat "\nEnter number of panels per string <" ; (cond (*strLim (itoa *strLim)) ("1")) ; ; offer previous value if present; otherwise 1 ; ">: " ; ); strcat ; ); getint ; ); setq ; (setq *pnlLim ; (cond (tmppl) (*pnlLim) (1)) ; ); setq (setq tmpsl (getint (strcat "\nEnter number of strings existing <" (cond (*strLim (itoa *strLim)) ("1")) ; offer previous value if present; otherwise 1 ">: " ); strcat ); getint ); setq (setq *strLim (cond (tmpsl) (*strLim) (1)) ); setq (while (> (setq tmppc (getint (strcat "\nEnter current panel number <" (cond (*pnlCur (itoa *pnlCur)) ("1")) ; offer previous value if present; otherwise 1 ">: " ); strcat ); getint ); setq *pnlLim ); if current panel number is bigger than the limit (princ (strcat " must be less than " (itoa *pnlLim))) ); while (setq *pnlCur (cond (tmppc) (*pnlCur) (1)) ); setq (while (> (setq tmpsc (getint (strcat "\nEnter current string number <" (cond (*strCur (itoa *strCur)) ("1")) ; offer previous value if present; otherwise 1 ">: " ); strcat ); getint ); setq *strLim ); if current string number is bigger than the limit (princ (strcat " must be less than " (itoa *strLim))) ); while (setq *strCur (cond (tmpsc) (*strCur) (1)) ); setq
Solved! Go to Solution.
Solved by Lee_Mac. Go to Solution.
I think [but haven't tested that] this will do what you're after:
....
(parse (eval *pnlLim) "Enter number of panels per string")
....
When I executed, as expected, the change in "var" wasn't transferred to "*pnlLim" since invocating "parse" function copies the value of "*pnlLim" instead of the variable itself (or pointer).
Are there any way to pass the variable "by reference"? (in the sense of OOP)
I'm not really familiar with the memory of AutoCAD 😕
(defun parse (var msg / tmp) (setq tmp (getint (strcat "\n" msg " <" (cond (var (itoa var)) ("1")) ; offer previous value if present; otherwise 1 ">: " ); strcat ); getint ); setq (setq var (cond (tmp) (var) (1)) ); setq )
Your choice of name is somewhat confusing, as usually parsing means something rather more involved:
http://en.wikipedia.org/wiki/Parsing
- Actually in Lisp pretty much all variables are passed by reference: everything is done by pointers behind the scene.
What you are attempting would probably be something like this:
(defun ask-int (varname msg / tmp varvalue) ;; Asks for an integer value to set into the variable, defaulting to the previous value (setq varvalue (vl-symbol-value varname)) (setq tmp (getint (strcat "\n" msg " <" (cond (varvalue (itoa varvalue)) ("1")) ; offer previous value if present; otherwise 1 ">: "))) (set varname (cond (tmp) (varvalue) (1))))
Usage: (ask-int '*pnlLim "Enter number of panels per string")
- note the variable name is quoted in the call
--
If I've understood correctly, the following should suffice:
(defun parse ( var msg ) (set var (cond ((getint (strcat "\n" msg " <" (if (eval var) (itoa (eval var)) "1") ">: "))) ((eval var)) (1))) )
Call with quoted symbol, e.g.:
(parse '*pnlLim "Enter number of panels per string")