If statement returns over top COND statement result

If statement returns over top COND statement result

Anonymous
Not applicable
984 Views
6 Replies
Message 1 of 7

If statement returns over top COND statement result

Anonymous
Not applicable

I have a few if statements that are at the top of my code that run if a certain variable is true. The problem i am having is the COND statement is running even know the condition is not met leaving some text on top of text. ln13 is a variable that gets created by another routine & is the state that the project is in. That part works, but the cond statement part is also being run & i am not sure why. The cities that are in the member call (ln7) are false when i dont choose NY but that code is still getting run.

 

(If (AND(= (strcase (GETVAR "PROJECTNAME")) "CDG")(= ln16 "BCK"))

(progn
(If (= ln13 "MD")
(Command "-layer" "s" "A-ANNO-TEXT" "" "insert" "H:\\Overlays\\BCK_McDonalds\\CoverinfoMD.dwg" "0,0" "1" "" "")
);end if MD
(If (= ln13 "PA")
(Command "-layer" "s" "A-ANNO-TEXT" "" "insert" "H:\\Overlays\\BCK_McDonalds\\CoverinfoPA.dwg" "0,0" "1" "" "")
);end if PA
(If (= ln13 "NJ")
(Command "-layer" "s" "A-ANNO-TEXT" "" "insert" "H:\\Overlays\\BCK_McDonalds\\CoverinfoNJ.dwg" "0,0" "1" "" "")
);end if NJ

(cond
((and (= ln13 "NY")
(member ln7 '("Manhattan" "Bronx" "Brooklyn" "Flushing" "Jackson Heights" "Queens" "New York" "Staten Island")))
(Command "-layer" "s" "A-ANNO-TEXT" "" "insert" "H:\\Overlays\\BCK_McDonalds\\CoverinfoNYC.dwg" "0,0" "1" "" "")
);cond 1
(t
(Command "-layer" "s" "A-ANNO-TEXT" "" "insert" "H:\\Overlays\\BCK_McDonalds\\CoverinfoNY.dwg" "0,0" "1" "" "")
);cond else
);cond
);end progn
(COMMAND "-MTEXT" "26.0466,16.8356" "S" "TITLE" "H" ".25" "J" "MC" "W" "" (STRCASE ln5) (STRCASE ln6) (STRCASE ln7a)"")

(COMMAND "QSAVE")

)

0 Likes
Accepted solutions (1)
985 Views
6 Replies
Replies (6)
Message 2 of 7

Kent1Cooper
Consultant
Consultant
Accepted solution

@Anonymous wrote:

I have a few if statements .... the cond statement part is also being run & i am not sure why. ....)


It's because it's an independent function following the (if) functions, and it has a default operation.  I don't understand why the three (if) statements are independent and not also within a (cond) function so that once it finds a condition satisfied, it ignores all the rest.

 

Also, I think your  );end progn  comes too early, if I'm correct in assuming you want the MTEXT and QSAVE to happen -- as you have it, the MTEXT part is the 'else' argument to the (if) function, and the QSAVE part is extraneous [are you not getting a too-many-arguments message?].

 

And any time I see so much code repeated over and over, I assume it can be consolidated.  I think you can structure it differently to contain all that repetitive stuff only once, and greatly shorten it.  Also, more than one command can be included in one (command) function.  Try something like this [untested]:

 

(if (and (= (strcase (GETVAR "PROJECTNAME")) "CDG") (= ln16 "BCK"))
  (progn ; then
    (command
      "_.layer" "s" "A-ANNO-TEXT" "" ; same for all
      "_.insert"
        (strcat ; assemble drawing name
          "H:\\Overlays\\BCK_McDonalds\\Coverinfo" ; same for all
          ln13 ; the State
          (if (member ln7 '("Manhattan" "Bronx" "Brooklyn" "Flushing" "Jackson Heights" "Queens" "New York" "Staten Island"))
            "C" ; then -- in the City [add letter]
            "" ; else -- [add nothing, whatever the State]          
          ); if
          ".dwg"
        ); strcat
        "0,0" "1" "" "" ; same for all
        "_.MTEXT" "26.0466,16.8356" "S" "TITLE" "H" ".25" "J" "MC" "W" "" (STRCASE ln5) (STRCASE ln6) (STRCASE ln7a) ""
        "_.QSAVE"
    ); command
  ); progn [then]
); if

 

 

Kent Cooper, AIA
0 Likes
Message 3 of 7

Anonymous
Not applicable

The reason as far as my thought process is concerned was that the reason for the condition statement for New York is if the city is part of the member list it gets the New York city code & if its still New York, but not part of that list it gets the New York state code. I was thinking if its not New York it would never get to the conditional statement

0 Likes
Message 4 of 7

Kent1Cooper
Consultant
Consultant

@Anonymous wrote:

..... I was thinking if its not New York it would never get to the conditional statement


Since it has only one thing to do if the condition is satisfied and one thing to do if it's not, your (cond) could just as well be another (if) function.  But the way to get it to not get to that if it's not New York  is to put all  of them into a collective (cond) function, so that once another State is recognized, it doesn't bother looking at any further possibilities:

....

  (cond
    ((= ln13 "MD")
      (Command ..... CoverinfoMD.dwg" "0,0" "1" "" "")
    );end MD condition
    ((= ln13 "PA")
      (Command ..... CoverinfoPA.dwg" "0,0" "1" "" "")
    );end PA condition
    ((= ln13 "NJ")
      (Command ..... CoverinfoNJ.dwg" "0,0" "1" "" "")
    );end NJ condition
    ( (and
        (= ln13 "NY")
        (member ln7 '("Manhattan" "Bronx" "Brooklyn" "Flushing" "Jackson Heights" "Queens" "New York" "Staten Island"))
      ); and
      (Command ..... CoverinfoNYC.dwg" "0,0" "1" "" "")
    );end NY + borough condition
    (t
      (Command ..... CoverinfoNY.dwg" "0,0" "1" "" "")
    );end none-of-the-above condition
  );cond

....

 

But since what's in ln13 is always [except for the Borough designations] the same as the last two letters of the drawing name to Insert, my earlier suggestion just used that fact, plugging it right into the name, and tacking on the "C" at the end only if the Borough designation was in the picture.  So much less code when you don't repeat that folder location and complete file name and insertion and scales and rotation for every single possibility.

Kent Cooper, AIA
0 Likes
Message 5 of 7

Anonymous
Not applicable

Ok thanks. yeah the way i was doing it was working for the other states but it was still running the new york stuff

0 Likes
Message 6 of 7

pbejse
Mentor
Mentor

You can use an association list if you have a disctinct value for the selected state, say  for example the value of ln7a .

...
(setq bn (strcat "H:\\Overlays\\BCK_McDonalds\\Coverinfo"               
               (cond
                     ((Setq f (assoc ln13 '(("MD" "Something")
                                           ("PA" "Other")
                                           ("NJ" "WHOA")))) ;< list can be a separate variable
                      		(setq ln7a (cadr f));<- value if assoc is T
                                 	ln13)
                     ((and (= ln13 "NY")
                           (member ln7 '("Manhattan" "Bronx" "Brooklyn" "Flushing"
	          			"Jackson Heights" "Queens" "New York" "Staten Island")))
                     		"NYC")
                     ( T ln13	)                      
                     ) ".dwg"
		)
      )
(Command "-layer" "s" "A-ANNO-TEXT" "" "insert" bn "0,0" "1" "" "")
...

if ln13 is "PA" the value for ln7a will be redefine as "other" otherwise it will remain as was before.

 

If you require another value not yet defined  you can use the value for something else that you may need unique to the selected state at later part ofthe code.

...
(setq distinctValueYouMayNeed (cadr f))
...

 

I will stick with using cond, so in the future you can just add other options

(setq bn (strcat "H:\\Overlays\\BCK_McDonalds\\Coverinfo"               
               (cond
                     ((member ln13 '("MD" "PA" "NJ")) ;< list can be a separate variable
                      		ln13)
                     ((and (= ln13 "NY")
                           (member ln7 '("Manhattan" "Bronx" "Brooklyn" "Flushing"
	          			"Jackson Heights" "Queens" "New York" "Staten Island")))
                     		"NYC")
                     ( T ln13	)                      
                     ) ".dwg"
		)
      )

HTH

 

0 Likes
Message 7 of 7

Anonymous
Not applicable

Its working pretty well.i have much more to learn with conditional statements. Thanks a lot

0 Likes