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

why does this work?

6 REPLIES 6
Reply
Message 1 of 7
Anonymous
344 Views, 6 Replies

why does this work?

I'm going through The ABC's of AutoLISP (George Omura) and I came across a program I don't quite understand. The original started off as:

(defun getinfo ()
(setq pt1 (getpoint "Pick first corner: "))
(setq pt3 (getcorner pt1 "Pick opposite corner: "))
)

(defun procinfo ()
(setq pt2 (list (car pt3) (cadr pt1)))
(setq pt4 (list (car pt1) (cadr pt3)))
)

(defun output ()
(command "pline" pt1 pt2 pt3 pt4 "c" )
)

(defun C:BOX1 (/ pt1 pt2 pt3 pt4)
(getinfo)
(procinfo)
(output)
)

Which draws a 2D box and I get it but as you go along and add things, this is what it becomes:

(defun BOX1 (/ pt1 pt2 pt3 pt4)
(getinfo)
(procinfo)
(output)
)

(defun 3DBOX (/ pt1 pt2 pt3 pt4 h)
(getinfo)
(setq h (getreal "Enter height of box: "))
(procinfo)
(output)
(command "change" "L" "" "P" "th" h ""
"3dface" pt1 pt2 pt3 pt4 ""
"3dface" ".xy" pt1 h ".xy" pt2 h
".xy" pt3 h ".xy" pt4 h ""
)
)

(defun C:MAINBOX (/ pt1 pt2 pt3 pt4 h choose)
(setq choose (getstring "\nDo you want a 3D box ? "))
(if (or (equal choose "y") (equal choose "Y"))
(progn
(getinfo)
(setq h (getreal "Enter height of box: "))
(procinfo)
(output)
(command "change" "Last" "" "Properties" "thickness" h ""
"3dface" pt1 pt2 pt3 pt4 ""
"3dface" ".xy" pt1 h ".xy" pt2 h
".xy" pt3 h ".xy" pt4 h ""
)
)
(progn
(getinfo)
(procinfo)
(output)
)
)
)

You now have the choice of a 2D or 3D box. My questions lies with BOX1. Why does the first example have to spell it out in order to function, yet the second has only the following and works perfectly?

(defun BOX1 (/ pt1 pt2 pt3 pt4)
(getinfo)
(procinfo)
(output)
)

Maybe an explanation of what getinfo, procinfo, and output do and what they are would help. Are they functions or variables or what?
6 REPLIES 6
Message 2 of 7
Anonymous
in reply to: Anonymous

If youre trying it out, you have to load this one too

(defun RXY (/ pt lt x last pick lpt1)
(if (not pt1)(setq lpt1 (getvar "lastpoint"))(setq lpt1 pt1))
(while (/= pick t)
(setq pt (cadr (setq lt (grread t))))
(if (= (car lt) 5)(progn
(setq x (strcat
(rtos (- (car pt) (car lpt1))) " x "
(rtos (- (cadr pt) (cadr lpt1))) " SI= "
(rtos (*(- (car pt) (car lpt1))
(- (cadr pt) (cadr lpt1))
)
2 2)
)
)
(grtext -2 x)
)
)
(setq pick (= 3 (car lt)))
)
(cadr lt)
)
Message 3 of 7
wkiernan
in reply to: Anonymous

There's a difference between C:BOX1 and BOX1. The first defines a command BOX1 so if you type

BOX1

at the command line it runs the routine. To execute BOX1 you have to enclose it in parentheses:

(BOX1)

If you load your example, you can see BOX1 and C:BOX1 as separate LISP objects, by typing at the command line

Command: !C:BOX1
#<SUBR @0fc4d708 C:BOX1>

Command: !BOX1
#<SUBR @0fc4d618 BOX1>

Now if you'd used defun-q instead of defun when defining BOX1 and C:BOX1 in your code, you'd have got a more informative answer:

Command: !box1
((/ PT1 PT2 PT3 PT4) (GETINFO) (PROCINFO) (OUTPUT))

Command: !C:BOX1
((/ PT1 PT2 PT3 PT4) (GETINFO) (PROCINFO) (OUTPUT))

Note also that you can call a LISP routine inside another even if it is defun-ed (or defun-q-ed) with the C: prefix, so where in your code it says

)
(progn
(getinfo)
(procinfo)
(output)
)

you could have replaced it with

(progn
(C:box1)
)

or even just

(C:box1)
Message 4 of 7
Anonymous
in reply to: Anonymous

I had you up until

)
(progn
(getinfo)
(procinfo)
(output)
)

Can be replaced with (C:box1).
I don't understand why it will work since I have not defined it in the program. I understand that BOX1 and C:BOX1 are different but C:BOX1 defines each pt. BOX1 doesn’t seem to be defined, yet 3DBOX is. Maybe you already answered this and I just missed it. I think you may have to spell it out for me.
Message 5 of 7
wkiernan
in reply to: Anonymous

Sorry about being unclear. OK, if you start a new drawing and load this first bunch of code, which I'll call version 1:

(defun getinfo ()
(setq pt1 (getpoint "Pick first corner: "))
(setq pt3 (getcorner pt1 "Pick opposite corner: "))
)

(defun procinfo ()
(setq pt2 (list (car pt3) (cadr pt1)))
(setq pt4 (list (car pt1) (cadr pt3)))
)

(defun output ()
(command "pline" pt1 pt2 pt3 pt4 "c" )
)

(defun C:BOX1 (/ pt1 pt2 pt3 pt4)
(getinfo)
(procinfo)
(output)
)

at this point you have defined the functions GETINFO, PROCINFO, OUTPUT and C:BOX1. If you then load this second bunch of code, which I'll call version 2:

(defun BOX1 (/ pt1 pt2 pt3 pt4)
(getinfo)
(procinfo)
(output)
)

(defun 3DBOX (/ pt1 pt2 pt3 pt4 h)
(getinfo)
(setq h (getreal "Enter height of box: "))
(procinfo)
(output)
(command "change" "L" "" "P" "th" h ""
"3dface" pt1 pt2 pt3 pt4 ""
"3dface" ".xy" pt1 h ".xy" pt2 h
".xy" pt3 h ".xy" pt4 h ""
)
)

(defun C:MAINBOX (/ pt1 pt2 pt3 pt4 h choose)
(setq choose (getstring "\nDo you want a 3D box ? "))
(if (or (equal choose "y") (equal choose "Y"))
(progn
(getinfo)
(setq h (getreal "Enter height of box: "))
(procinfo)
(output)
(command "change" "Last" "" "Properties" "thickness" h ""
"3dface" pt1 pt2 pt3 pt4 ""
"3dface" ".xy" pt1 h ".xy" pt2 h
".xy" pt3 h ".xy" pt4 h ""
)
)
(progn
(getinfo)
(procinfo)
(output)
)
)
)

at that point you have also defined the additional functions BOX1, 3DBOX and MAINBOX, but the original functions GETINFO, PROCINFO, OUTPUT and C:BOX1 are still defined.

Note that if you opened a new drawing and loaded only version 2 and not version 1, C:MAINBOX wouldn't work, because GETINFO, PROCINFO and OUTPUT are executed inside C:MAINBOX, but they are defined only in version 1.

Regarding

Maybe an explanation of what getinfo, procinfo, and output do and what they are would help. Are they functions or variables or what?

GETINFO, PROCINFO and OUTPUT are functions, defined by the DEFUN function. GETINFO prompts the user to pick two points and stores the two points as PT1 and PT3. PROCINFO computes PT2 and PT4 from PT1 and PT3. OUTPUT draws a polyline from PT1 to PT2 to PT3 to PT4 and close. BOX1 is another function, and what it does is execute the GETINFO, PROCINFO and OUTPUT in serial order. 3DBOX is yet another function. C:BOX1 is another function; it happens to be effectively identical to BOX1 but it is a different function.

In version 2, in the definition of C:MAINBOX you see the exact same steps as in 3DBOX (following the first PROGN), and likewise you see the exact steps in BOX1 repeated (following the second PROGN). My guess (I don't have that book) is that in the next version of C:MAINBOX, which I'll call C:MAINBOX2, they substitute BOX1 and 3DBOX for the analogous steps in C:MAINBOX, like so:

(defun C:MAINBOX2 (/ pt1 pt2 pt3 pt4 h choose)
(setq choose (getstring "\nDo you want a 3D box ? "))
 (if (or (equal choose "y") (equal choose "Y"))
   (3DBOX)
   (BOX1)
 )
)

So here you have a function C:MAINBOX2, which executes (if you press "Y") the function BOX1, which in turn executes the three functions GETINFO, PROCINFO and OUTPUT.
Message 6 of 7
Anonymous
in reply to: Anonymous

Got it.
THANKS!
Message 7 of 7
Anonymous
in reply to: wkiernan

Sorry for replying years after the original post appeared, but you wrote:

 

My guess (I don't have that book) is that in the next version of C:MAINBOX, which I'll call C:MAINBOX2, they substitute BOX1 and 3DBOX for the analogous steps in C:MAINBOX, like so:

(defun C:MAINBOX2 (/ pt1 pt2 pt3 pt4 h choose)
(setq choose (getstring "\nDo you want a 3D box ? "))
 (if (or (equal choose "y") (equal choose "Y"))
   (3DBOX)
   (BOX1)
 )
)

I am going through the material in that book right now, and you are goddam right! That's exactly what they do. Smiley Happy

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

Post to forums  

Autodesk Design & Make Report

”Boost