PDA

View Full Version : question on if statements


d_m_hopper
2008-03-10, 02:57 PM
I have two routines that use a cond statement...

The routines are written specifically for english units or metric units, I have tried with no success to add....if english do this if not do (metric)

Stand alone they work fine, my question is...does the cond statement mess up the if statement?

here is a snippet of code...

(if (= Inp "English")

(command "polygon" "3" bp"" "c" "5")
(setq tr (list (car bp) (+ (cadr bp) 10)))
(command "ortho" "off" "move" tr "" bp pause)

(setq tx (getvar "lastpoint"))
(command "color" "magenta" "text" "s" "dmt" "j" "m" tx"" "0" r#"" "color" "green")
(setq tr (list (car (getvar "lastpoint")) (+ (cadr (getvar "lastpoint")) 10)))

(cond
((= r# "1")
(setq coor '(1598.7021 -997.0960)
id '(1610.7021 -1002.0960)
)
)
((= r# "2")
(setq coor '(1598.7021 -1021.0960)
id '(1610.7021 -1026.0960)
)
)
((= r# "3")
(setq coor '(1598.7021 -1045.0960)
id '(1610.7021 -1050.0960)
)
)
)

I looked at the "Anatomy of an AUTOLISP file" thread and searched forum to find answer, might be search was poor any help will be helpful :mrgreen:

'gile'
2008-03-10, 04:13 PM
Hi,

With if, only one "then expression" is expected and only one (optional) "else expression" too. So you have to use the progn function to evaluate all expressions where only one is expected.

(if (test_expression)
(progn
(then_expression_1)
(then_expression_2)
(then_expression_3)
)
(progn
(else_expresson_1)
(else_expression_2)
)
)

d_m_hopper
2008-03-10, 07:37 PM
Hi,

With if, only one "then expression" is expected and only one (optional) "else expression" too. So you have to use the progn function to evaluate all expressions where only one is expected.

(if (test_expression)
(progn
(then_expression_1)
(then_expression_2)
(then_expression_3)
)
(progn
(else_expresson_1)
(else_expression_2)
)
)

ok that makes sense

CAB2k
2008-03-11, 05:07 AM
Scroll down to the (progn explination in this post.
http://forums.augi.com/showpost.php?p=602355&postcount=10

d_m_hopper
2008-03-11, 01:28 PM
Scroll down to the (progn explination in this post.
http://forums.augi.com/showpost.php?p=602355&postcount=10


thanks Cab

d_m_hopper
2008-03-11, 03:22 PM
I am still confused

(getvar "clayer")
(setq osm (getvar "osmode"))
(setvar "osmode" 767)

(defun C:Rev (/ strLayer strRevision )

(initget "English Metric") ;ASK USER FOR INPUT
(setq Inp (getkword "\nWhat are the units of measure? [English/Metric] <English>: "))
(or Inp (setq Inp "English") ;DEFAULT ENGLISH UNITS
)

(initget "Pline Rectangle Circle") ; ask for input
(setq Inp (getkword "\nWhat type of cloud would you like to draw? [Pline/Rectangle/Circle] <Pline>: "))
(or Inp (setq Inp "Pline"))

(cond
((= inp "Pline")
(command "_.PLINE")
(while (= (getvar "CMDNAMES") "PLINE")
(command pause)
)
(command "pedit" "l" "c" "")
(command "_.REVCLOUD" "a" "10" "" "O" (entlast) "N")
)
((= inp "Rectangle")
(setq p1(getpoint "\nPick first corner of window: "))
(setq p2(getcorner p1 "\nOpposite corner: "))
(setvar "plinewid" 0)
(command "rectang" p1 p2)
(command "_.REVCLOUD" "a" "10" "" "O" (entlast) "N")
)
((= inp "Circle")
(setq p1(getpoint "\nPick Center Of Circle : "))
(setq p2(getpoint p1 "\nOutside Edge: "))
(setvar "plinewid" 0)
(command "circle" p1 p2)
(command "_.REVCLOUD" "a" "10" "" "O" (entlast) "N")
)
)

(setq strRevision (getstring "\nWhich Revision is this 1 2 or 3: "))

(setvar "attdia" 0

(if (= Inp "Metric")
(command "-insert" "Anno_Revision_M" pause (getvar "dimscale") "" 0 strRevision
) ;DO THIS IF METRIC
(command "-insert" "Anno_Revision_E" pause (getvar "dimscale") "" 0 strRevision
) ;ELSE DO THIS IF ENGLISH
)

(if (= Inp "Metric")) <<<works until I get here
(progn
((= strRevision "1")
(setq coor '(40607.0 -25326.0)
id '(40894.0 -25453.0)
)
)
((= strRevision "2")
(setq coor '(40607.0 -25936.0)
id '(40894.0 -26063.0)
)
)
((= strRevision "3")
(setq coor '(40607.0 -26546.0)
id '(40894.0 -26673.0)
)
)
);if metric
(progn
((= strRevision "1")
(setq coor '(1598.7021 -997.0960)
id '(1610.7021 -1002.0960)
)
)
((= strRevision "2")
(setq coor '(1598.7021 -1021.0960)
id '(1610.7021 -1026.0960)
)
)
((= strRevision "3")
(setq coor '(1598.7021 -1045.0960)
id '(1610.7021 -1050.0960)
)
)
);if english
)

(setq lp (getvar "lastpoint"))
(command "_.copy" entlast"" lp"" coor"" )
(princ)
)

??

'gile'
2008-03-11, 04:06 PM
Hi,

In the last (if ...) expression, remove one closing parenthesis in the test expression, replace progn by cond .

(if (= Inp "Metric")
(cond
((= strRevision "1")
(setq coor '(40607.0 -25326.0)
id '(40894.0 -25453.0)
)
)
((= strRevision "2")
(setq coor '(40607.0 -25936.0)
id '(40894.0 -26063.0)
)
)
((= strRevision "3")
(setq coor '(40607.0 -26546.0)
id '(40894.0 -26673.0)
)
)
) ;if metric
(cond
((= strRevision "1")
(setq coor '(1598.7021 -997.0960)
id '(1610.7021 -1002.0960)
)
)
((= strRevision "2")
(setq coor '(1598.7021 -1021.0960)
id '(1610.7021 -1026.0960)
)
)
((= strRevision "3")
(setq coor '(1598.7021 -1045.0960)
id '(1610.7021 -1050.0960)
)
)
) ;if english

d_m_hopper
2008-03-11, 08:44 PM
Hi,

In the last (if ...) expression, remove one closing parenthesis in the test expression, replace progn by cond .

(if (= Inp "Metric")
(cond
((= strRevision "1")
(setq coor '(40607.0 -25326.0)
id '(40894.0 -25453.0)
)
)
((= strRevision "2")
(setq coor '(40607.0 -25936.0)
id '(40894.0 -26063.0)
)
)
((= strRevision "3")
(setq coor '(40607.0 -26546.0)
id '(40894.0 -26673.0)
)
)
) ;if metric
(cond
((= strRevision "1")
(setq coor '(1598.7021 -997.0960)
id '(1610.7021 -1002.0960)
)
)
((= strRevision "2")
(setq coor '(1598.7021 -1021.0960)
id '(1610.7021 -1026.0960)
)
)
((= strRevision "3")
(setq coor '(1598.7021 -1045.0960)
id '(1610.7021 -1050.0960)
)
)
) ;if english

gotcha, but I have to work on something since it still inserts as English even when Metric is selected. When English it is works great

d_m_hopper
2008-03-11, 10:38 PM
Since my drafters needed this yesterday I went with two routines until I can figure out how to make it insert what I need where I need it in Metric dwgs and at the correct scale....

Opie
2008-03-12, 05:36 AM
Since my drafters needed this yesterday I went with two routines until I can figure out how to make it insert what I need where I need it in Metric dwgs and at the correct scale....
What is the average value of the MEASUREMENT system variable for the two types of drawings? If this value is specified correctly, you can use that value to determine which routine to use.

I have not tested this entire routine, just bits and pieces. The majority of it is your originally posted code. I reworked some of the logic and steps.
(defun C:RVE (/ strLayer strRevision arc inp
COOR ID lp txt delta CLIST
INTREVISION P1 P2 REVBLOCK
)
(setq sysvarchanges
(mapcar '(lambda (x) (list x (getvar x)))
'("osmode" "cmdecho" "attdia" "attreq")
)
)
(setvar "osmode" 767)
(setvar "cmdecho" 0)

(if (zerop (getvar "measurement"))
(setq arc 10
RevBlock "E"
CList (list
(list '(1598.7021 -997.0960) ;_ Coordinates for Rev 1
'(1610.7021 -1002.0960) ;_ ID coordinates for Rev 1
)
(list '(1598.7021 -1021.0960) ;_ Coordinates for Rev 2
'(1610.7021 -1026.0960) ;_ ID coordinates for Rev 2
)
(list '(1598.7021 -1045.0960) ;_ Coordinates for Rev 3
'(1610.7021 -1050.0960) ;_ ID coordinates for Rev 3
)
)
)
(setq arc 254
RevBlock "M"
CList (list
(list '(40607.0 -25326.0) ;_ Coordinates for Rev 1
'(40894.0 -25453.0) ;_ ID coordinates for Rev 1
)
(list '(40607.0 -25936.0) ;_ Coordinates for Rev 2
'(40894.0 -26063.0) ;_ ID coordinates for Rev 2
)
(list '(40607.0 -26546.0) ;_ Coordinates for Rev 3
'(40894.0 -26673.0) ;_ ID coordinates for Rev 3
)
)
)
)
(initget 128 "Pline Rectangle Circle") ;_ ask for input
(setq Inp
(getkword
"\nSpecify cloud type [Pline/Rectangle/Circle] <Pline>: "
)
)
(cond
((= inp "Rectangle")
(setq p1 (getpoint "\nSpecify first corner point: "))
(setq p2 (getcorner p1 "\nSpecify other corner point: "))
(setvar "plinewid" 0)
(command "rectang" p1 p2)
(command "._REVCLOUD" "a" arc "" "O" (entlast) "N")
)
((= inp "Circle")
(setq p1 (getpoint "\nSpecify center of circle : "))
(setq p2 (getpoint p1 "\nSpecify outside edge: "))
(setvar "plinewid" 0)
(command "circle" p1 p2)
(command "._REVCLOUD" "a" arc "" "O" (entlast) "N")
)
(t ;_ The default setting is "Pline"
(command "_.PLINE")
(while (= (getvar "CMDNAMES") "PLINE")
(command pause)
)
(command "pedit" "l" "c" "")
(command "._REVCLOUD" "a" arc "" "O" (entlast) "N")
)
)

(initget 134 "1 2 3")
(setq strRevision (getint "\nSpecify revision number [1/2/3] <1>:"))
(if (numberp strRevision)
(setq intRevision (1- strRevision)
strRevision (itoa strRevision)
)
(setq intRevision 0
strRevision "1"
)
)
(setq coor (car (nth intRevision CList))
id (cadr (nth intRevision CList))
)


(setvar "attdia" 0)
(setvar "attreq" 1)
(command "-insert"
(strcat "Anno_Revision_" RevBlock)
pause
(getvar "dimscale")
""
0
strRevision
)

(setq lp (getvar "lastpoint"))

(setq txt (list (car id) (- (cadr id) 12.0)))

(setq delta (entlast))

(command "._copy" delta "" lp coor)

(command "text"
"s"
"dmt"
"j"
"bl"
id
0
(getstring T "\nInitials and Date, (ex. ABC - 4/16/93): ")
)
(setq id (entlast))
(command "._change" id "" "P" "la"
"border" "lt" "continuous" "lw" "0.0"
"c" "green" ""
)

(command "text"
"j"
"bl"
txt
0
(strcat "CHECKED BY: "
(getstring T "\nEnter text (ex. JGA): ")
)
)
(setq txt (entlast))
(command "._change" txt "" "P" "la"
"border" "lt" "continuous" "lw" "0.0"
"c" "green" ""
)
(foreach n syvarchanges
(setvar (car n) (cadr n))
)
(princ)
)

d_m_hopper
2008-03-12, 02:48 PM
Opie thanks for taking at look at this and rebuilding it, the measurement variable was set to 0 whether it was English or Metric.

I made a few changes, but nothing major.

I do not know what you did in portions of the code, so I have some questions for you....if you find time to answer them great.

Also I would like to have this routine change existing rev clouds to the color red

I have been trying to work this out, see code below

(initget 0 "Yes No") ; ask for input
(setq Inp (COND ((getkword "\nIs this the first revision? [Yes/No] <Yes>: "))))

(or Inp (setq Inp "Yes"))

(cond ((eq "NO" Inp)

(setq ss (ssget "x" '((0 . "LWPOLYLINE")(410 . "Model")(-4 . "/=")(42 . 0.0)(-4 . ">")(90 . 4))))
(command "._chprop" ss"" "c" "red" "")

(princ "done")
)
)

Anyway I have pointed to my ???'s and comments in red

(defun C:Revision (/ strLayer strRevision arc inp
COOR ID lp txt delta CLIST
INTREVISION P1 P2 REVBLOCK
)
(setq sysvarchanges;<<I like how you did this
(mapcar '(lambda (x) (list x (getvar x)))
'("osmode" "cmdecho" "attdia" "attreq")
)
)
(setvar "osmode" 767)
(setvar "cmdecho" 0)

(initget "English Metric") ;ASK USER FOR INPUT
(setq Inp (getkword "\nWhat are the units of measure? [English/Metric] <English>: "))
(or Inp (setq Inp "English") ;DEFAULT ENGLISH UNITS
)

(if (= Inp "Metric")
(SETVAR "MEASUREMENT" 1
) ;DO THIS IF METRIC
(SETVAR "MEASUREMENT" 0
) ;ELSE DO THIS IF ENGLISH
)

(if (zerop (getvar "measurement"));<<explain I assume it is zerop since the var can only be 0 or 1
(setq arc 10
RevBlock "E"
CList (list
(list '(1598.7021 -997.0960) ;_ Coordinates for Rev 1
'(1610.7021 -1002.0960) ;_ ID coordinates for Rev 1
)
(list '(1598.7021 -1021.0960) ;_ Coordinates for Rev 2
'(1610.7021 -1026.0960) ;_ ID coordinates for Rev 2
)
(list '(1598.7021 -1045.0960) ;_ Coordinates for Rev 3
'(1610.7021 -1050.0960) ;_ ID coordinates for Rev 3
)
)
)
(setq arc 254
RevBlock "M"
CList (list
(list '(40607.0 -25326.0) ;_ Coordinates for Rev 1
'(40894.0 -25453.0) ;_ ID coordinates for Rev 1
)
(list '(40607.0 -25936.0) ;_ Coordinates for Rev 2
'(40894.0 -26063.0) ;_ ID coordinates for Rev 2
)
(list '(40607.0 -26546.0) ;_ Coordinates for Rev 3
'(40894.0 -26673.0) ;_ ID coordinates for Rev 3
)
)
)
)
(initget 128 "Pline Rectangle Circle") ;_ ask for input why 128?
(setq Inp
(getkword
"\nSpecify cloud type [Pline/Rectangle/Circle] <Pline>: "
)
)
(cond
((= inp "Rectangle")
(setq p1 (getpoint "\nSpecify first corner point: "))
(setq p2 (getcorner p1 "\nSpecify other corner point: "))
(setvar "plinewid" 0)
(command "rectang" p1 p2)
(command "._REVCLOUD" "a" arc "" "O" (entlast) "N")
)
((= inp "Circle")
(setq p1 (getpoint "\nSpecify center of circle : "))
(setq p2 (getpoint p1 "\nSpecify outside edge: "))
(setvar "plinewid" 0)
(command "circle" p1 p2)
(command "._REVCLOUD" "a" arc "" "O" (entlast) "N")
)
(t ;_ The default setting is "Pline"
(command "_.PLINE")
(while (= (getvar "CMDNAMES") "PLINE")
(command pause)
)
(command "pedit" "l" "c" "")
(command "._REVCLOUD" "a" arc "" "O" (entlast) "N")
)
)

(initget 134 "1 2 3") ;why 134?
(setq strRevision (getint "\nSpecify revision number [1/2/3] <1>:"))
(if (numberp strRevision)
(setq intRevision (1- strRevision)
strRevision (itoa strRevision)
)
(setq intRevision 0
strRevision "1"
)
)
(setq coor (car (nth intRevision CList))
id (cadr (nth intRevision CList))
)


(setvar "attdia" 0)
(setvar "attreq" 1)
(command "-insert"
(strcat "Anno_Revision_" RevBlock)
pause
(getvar "dimscale")
""
0
strRevision
)

(setq lp (getvar "lastpoint"))

(if (zerop (getvar "measurement"))
(setq txt (list (car id) (- (cadr id) 12.0)))
(setq txt (list (car id)(- (cadr id) 305.)))
)

(setq delta (entlast))

(command "._copy" delta "" lp coor)

(command "text"
"s"
"dmt"
"j"
"bl"
id
0
(getstring T "\nInitials and Date, (ex. ABC - 4/16/93): ")
)
(setq id (entlast))
(command "._change" id "" "P" "la"
"border" "lt" "continuous" "lw" "0.0"
"c" "green" ""
)

(command "text"
"j"
"bl"
txt
0
(strcat "CHECKED BY: "
(getstring T "\nEnter text (ex. JGA): ")
)
)
(setq txt (entlast))
(command "._change" txt "" "P" "la"
"border" "lt" "continuous" "lw" "0.0"
"c" "green" ""
)
(foreach n syvarchanges
(setvar (car n) (cadr n))
)
(princ)
)__________________

Opie
2008-03-12, 07:24 PM
explain I assume it is zerop since the var can only be 0 or 1
The function ZEROP checks to see if the supplied value is 0. If it is zero, then it returns True, which in turn sets the variables to the desired values. Using the MEASUREMENT system variable properly means the drafter does not have to know whether they should use the Imperial or Metric function to perform the task they need.

why 128?
This will allow the user to simply press ENTER or otherwise an arbitrary value. Look up INITGET in the developer help for additional information.

why 134?
This should limit the input to a non-zero, non-negative number or an arbitrary value as above. It is bit-coded. Again, see the developer help for INITGET.

I noticed you added another check on the MEASUREMENT variable later in the code. You could easily add the desired value as a new variable to the beginning code where this is checked.

See additional comments in blue in following code.
(defun C:Revision (/ strLayer strRevision arc
COOR ID lp txt delta
CLIST INTREVISION P1 P2
REVBLOCK
)
(setq sysvarchanges
(mapcar '(lambda (x) (list x (getvar x)))
'("osmode" "cmdecho" "attdia" "attreq")
)
)
(setvar "osmode" 767)
(setvar "cmdecho" 0)


(if (null UnitOfMeasure) ;_ Check to see if this has been run during this drawing
;_ session. If so, do not run it again.
(progn
(initget 128 "English Metric")
(setq UnitOfMeasure
(getkword
"\nWhat are the units of measure? [English/Metric] <English>: "
)
)
;_ Removed OR statement to specify UnitOfMeasure as "English"
(if (= UnitOfMeasure "Metric")
(SETVAR "MEASUREMENT" 1) ;_ DO THIS IF METRIC
(SETVAR "MEASUREMENT" 0) ;_ ELSE DO THIS IF ENGLISH
)
)
)

(if (zerop (getvar "measurement")) ;<<explain I assume it is zerop since the var can only be 0 or 1
(setq arc 10
RevBlock "E"
txt 12.0
CList (list
(list '(1598.7021 -997.0960) ;_ Coordinates for Rev 1
'(1610.7021 -1002.0960) ;_ ID coordinates for Rev 1
)
(list '(1598.7021 -1021.0960) ;_ Coordinates for Rev 2
'(1610.7021 -1026.0960) ;_ ID coordinates for Rev 2
)
(list '(1598.7021 -1045.0960) ;_ Coordinates for Rev 3
'(1610.7021 -1050.0960) ;_ ID coordinates for Rev 3
)
)
)
(setq arc 254
RevBlock "M"
txt 305.0
CList (list
(list '(40607.0 -25326.0) ;_ Coordinates for Rev 1
'(40894.0 -25453.0) ;_ ID coordinates for Rev 1
)
(list '(40607.0 -25936.0) ;_ Coordinates for Rev 2
'(40894.0 -26063.0) ;_ ID coordinates for Rev 2
)
(list '(40607.0 -26546.0) ;_ Coordinates for Rev 3
'(40894.0 -26673.0) ;_ ID coordinates for Rev 3
)
)
)
)
(initget 128 "Pline Rectangle Circle") ;_ ask for input why 128?
(setq Inp
(getkword
"\nSpecify cloud type [Pline/Rectangle/Circle] <Pline>: "
)
)
(cond
((= inp "Rectangle")
(setq p1 (getpoint "\nSpecify first corner point: "))
(setq p2 (getcorner p1 "\nSpecify other corner point: "))
(setvar "plinewid" 0)
(command "rectang" p1 p2)
(command "._REVCLOUD" "a" arc "" "O" (entlast) "N")
)
((= inp "Circle")
(setq p1 (getpoint "\nSpecify center of circle : "))
(setq p2 (getpoint p1 "\nSpecify outside edge: "))
(setvar "plinewid" 0)
(command "circle" p1 p2)
(command "._REVCLOUD" "a" arc "" "O" (entlast) "N")
)
(t ;_ The default setting is "Pline"
(command "_.PLINE")
(while (= (getvar "CMDNAMES") "PLINE")
(command pause)
)
(command "pedit" "l" "c" "")
(command "._REVCLOUD" "a" arc "" "O" (entlast) "N")
)
)

(initget 134 "1 2 3") ;why 134?
(setq strRevision (getint "\nSpecify revision number [1/2/3] <1>:"))
(if (numberp strRevision)
(setq intRevision (1- strRevision)
strRevision (itoa strRevision)
)
(setq intRevision 0
strRevision "1"
)
)
(setq coor (car (nth intRevision CList))
id (cadr (nth intRevision CList))
txt
(list (car id) (- (cadr id) txt))

)



(setvar "attdia" 0)
(setvar "attreq" 1)
(command "-insert"
(strcat "Anno_Revision_" RevBlock)
pause
(getvar "dimscale")
""
0
strRevision
)

(setq lp (getvar "lastpoint"))

(setq delta (entlast))

(command "._copy" delta "" lp coor)

(command "text"
"s"
"dmt"
"j"
"bl"
id
0
(getstring T "\nInitials and Date, (ex. ABC - 4/16/93): ")
)
(setq id (entlast))
(command "._change" id "" "P" "la"
"border" "lt" "continuous" "lw" "0.0"
"c" "green" ""
)

(command "text"
"j"
"bl"
txt
0
(strcat "CHECKED BY: "
(getstring T "\nEnter text (ex. JGA): ")
)
)
(setq txt (entlast))
(command "._change" txt "" "P" "la"
"border" "lt" "continuous" "lw" "0.0"
"c" "green" ""
)
(foreach n syvarchanges
(setvar (car n) (cadr n))
)
(princ)
)

d_m_hopper
2008-03-13, 05:00 PM
Thank you Opie

The explanations and comments are very helpful to me with my learning.

I was able to take bits from this code and rewrite some exsting code