PDA

View Full Version : Rectangular Lisp Routine



robert.1.hall72202
2004-10-26, 05:40 PM
Im working on a lisp to do exactly what is shown in the attached cad
file. I want to create a rectangle, cut it by X amount (user input), and
then show the resulting rectangle in a step 1, step 2, step 3, type fashion.
I have started the rectangular cordinates with the following:


(defun dtr (a)
(* (/ a 180.0) pi)
)
(defun rtd (a)
(* (/ a pi) 180.0)
)

(defun c:prt ()
(setq ip (getpoint "\nPick starting point:")
)
(setq height (getdist "\nSpecify height:")
width (getdist "\nSpecify width:")
)
(setq cp1 (polar (dtr 90) height)
cp2 (polar cp1 0 width)
cp3 (polar (dtr 270) height)
cp4 (polar (dtr 180) width)
)

(command "line" ip cp2 "")
(command "line" cp2 cp3 "")
(command "line" cp3 cp4 "")
(command "line" cp4 cp1 "")
)
(princ)
What am I doing wrong??'
Any help would be appreciated....steer me in the right direction.

stig.madsen
2004-10-26, 08:51 PM
Not sure what you mean with cutting by X amount and the step-thing fashion but see if this might help (please refer to comments below):

(defun c:prt ()
(setq ip (getpoint "\nPick starting point:")
)
(setq height (getdist "\nSpecify height:")
width (getdist "\nSpecify width:")
)
;; some points were missing here:
(setq cp1 (polar ip (dtr 90) height)
cp2 (polar cp1 0 width)
cp3 (polar cp2 (dtr 270) height)
;; don't really need this point (= ip)
;; cp4 (polar cp3 (dtr 180) width)
)
;; some point sequences edited
;; also added osnap overrides
(command "line" "_none" ip "_none" cp1 "")
(command "line" "_none" cp1 "_none" cp2 "")
(command "line" "_none" cp2 "_none" cp3 "")
(command "line" "_none" cp3 "_none" ip "")
)

robert.1.hall72202
2004-10-26, 09:08 PM
I updated the file attachment.....I am basically trying to create 3 views from user input.
View 1 shows the incoming rectangle
View 2 shows how much material will get sheared off
View 3 shows the rectangle after shearing

Our company has to make several of these diagrams on a daily basis for use
as process sheets in production. Would be great if I could figure out how to
quickly make these drawings come to life.

CAB2k
2004-10-26, 10:37 PM
Something along these lines?

(defun c:prt (/ up dn fuzz llc urc height width cp1 cp2 nheight nwidth)
(setq up (/ pi 2) ; up direction
dn (* pi 1.5) ; down direction
fuzz 0.005
)
(command ".undo" "Begin")
;;=============================================
;; Step 1 Get full sheet location and size
;;=============================================
(while (not (setq llc (getpoint "\nPick lower left corner:")))
(princ "\nPlease try again...")
)
(setq height (getdist "\nEnter or pick height <96>: ")
width (getdist "\nEnter or pick width<48>: ")
)
(setq height (cond (height) (96))) ; default
(setq width (cond (width) (48))) ; default
(setq urc (polar (polar llc 0 width) up height)); upper right corner
(command ".rectangle" "_none" llc "_none" urc)
;;=============================================
;; Step 2 show sheet with cut line
;;=============================================
(setq nheight (getdist "\nEnter or pick new height <96>: ")
nwidth (getdist "\nEnter or pick new width<48>: ")
)
(setq nheight (min (cond (nheight) (96)) height)) ; default
(setq nwidth (min (cond (nwidth) (48)) width)) ; default
(setq llc (polar llc 0 (+ width 10)) ; offset new rec 10 units
urc (polar urc 0 (+ width 10))
)
(command ".rectangle" "_none" llc "_none" urc)
(if (not (equal width nwidth fuzz))
(progn; vertical cut line
(setq cp1 (polar llc 0 nwidth)
cp2 (polar urc pi (- width nwidth))
)
(command "line" "_none" cp1 "_none" cp2 "")
;; need to change line type here of (entlast)
)
)
(if (not (equal height nheight fuzz))
(progn; horriz cut line
(setq cp1 (polar llc up nheight)
cp2 (polar urc dn (- height nheight))
)
(command "line" "_none" cp1 "_none" cp2 "")
;; need to change line type here of (entlast)
)
)
;;=============================================
;; Step 3 show cut sheet
;;=============================================
(setq llc (polar llc 0 (+ width 10))
urc (polar (polar llc 0 nwidth) up nheight)
)
(command ".rectangle" "_none" llc "_none" urc)
(command ".undo" "End")
(princ)
)

robert.1.hall72202
2004-10-27, 01:08 PM
This works great, there are a couple of items I am trying to understand.

(progn; vertical cut line

progn is used for?????

and fuzz .005 is needed because???

Other than that I understand the rest of what is going on here.
I knew that the rectangle command would be way better than
what I was trying to start with :-p

kennet.sjoberg
2004-10-27, 01:32 PM
Hi rhall, here is an attempt to clarify progn when using if...

From the book : (if testexpr thenexpr [elseexpr])
compare to : (if TestIsOk (dothis) else (dothat) )
example :


(if T (princ "ok" ) (princ "Not ok") ) ; if testexpr is true message "ok"
(if nil (princ "ok" ) (princ "Not ok") ) ; if testexpr is false message "Not ok"
; when you need more than one command inside the ( ) you use progn
; like this :
(if T (progn ; if testexpr is true
(princ " ok " )
(princ " My name is " )
(princ " kennet " )
(princ " : ) Happy Computing ! " )
(princ)
) ; end progn
(princ "Not ok") ) ; if testexpr is false
) ; end if

CAB2k
2004-10-27, 03:22 PM
The (progn is used to group lines of code together
The (if will evaluate the first line of code if true or the second line of code if false
like this:

(if (= "ABC" "ABC")
(setq string "ABC"); if true
(setq string "DEF"); if false
)
or like this
(if (= "ABC" "ABC")
(setq string "ABC"
number 100 ); if true
(setq string "DEF"); if false
)
or like this
(if (= "ABC" "ABC")
(progn
(setq string "ABC"); if true
(setq number 100 ); if true
)
(setq string "DEF"); if false
)
or like this
(if (= "ABC" "ABC")
(progn
(setq string "ABC"); if true
(setq number 100 ); if true
)
;; do nothing if false
)


As for the fuzz factor all points are not equal due to rounding errors.
Read the VLIDE help file on 'equal' and 'eq' and '=' to see the differences
if you still have questions please ask.

robert.1.hall72202
2004-10-27, 03:41 PM
Thanks for the info.....that makes perfect sense now.

So we are grouping lines in the program that directs
a cut line to be drawn if the heights or widths are not equal.

So when progn is closed, the lisp routine will complete the
lines written, and then continue on with the rest of the program.

Makes sense to me now.