While loop with if command

While loop with if command

Anonymous
Not applicable
1,593 Views
7 Replies
Message 1 of 8

While loop with if command

Anonymous
Not applicable

(defun C:conlabel (/ ep seglen Yes No)
(while
  (INITGET 1 "Yes No")
  (OR
    (SETQ RETKWORD (GETKWORD "\nLabel New Line?: [Yes/No]:"))
    (SETQ RETKWORD "Yes")
    )
  (IF (= RETKWORD "Yes")
        (PROGN
 (setq ep (entsel))
 (setq seglen (getdist "\nSegment length"))
 (command "measure" ep "Block" "contour elevation label" "_Y" seglen)
 )
    )

  (IF (= RETKWORD "No")
    (PROGN
 (command "attsync" "s" pause "y")
 (princ)
      )
    )
  )
 

 

Ive written this code but when I load it there are no errors but when I type the command nothing happens. Any ideas?

0 Likes
Accepted solutions (1)
1,594 Views
7 Replies
Replies (7)
Message 2 of 8

CodeDing
Advisor
Advisor

@Anonymous ,

 

Untested, but try this:

(defun c:CONLABEL ( / kw ep seglen)
(while (eq "Yes"
       (progn
	 (initget 1 "Yes No")
	 (setq kw (getkword "\nLabel New Line?: [Yes/No]:"))
       ));progn/equal
  (setq ep (entsel))
  (initget 1) (setq seglen (getdist "\nSegment length: "))
  (command "MEASURE" ep "Block" "contour elevation label" "_Y" seglen)
);while
;finish up after "No"
(command "ATTSYNC" "s" pause "y")
(princ)
);defun

Best,

~DD

Message 3 of 8

andkal
Collaborator
Collaborator

"While" evaluates commands as long as test expression is true. In your case test expression (INITGET 1 "Yes No") returns nil so the program does nothing.


• www.autolisps.blogspot.com - Productivity plugins for Autocad and Autocad MEP
• Autodesk AppStore
Message 4 of 8

Anonymous
Not applicable

CodeDing,

 

This works very well, thank you!

 

Any way it can be set so after yes is selected you can select the first line and specify its length and then it will go back to a selection prompt and an end button? If not what you gave me is more than enough, just trying to make things take the least amount of clicks possible.

 

Thanks again,

Devin

0 Likes
Message 5 of 8

CodeDing
Advisor
Advisor
Accepted solution

@Anonymous ,

 

I'm not 100% sure what you're asking for. Can you elaborate? After looking again, perhaps this would suit you better:

(defun c:CONLABEL ( / e)
(while (setq e (car (entsel "\nSelect object to measure (ENTER to quit): ")))
  (command "MEASURE" e "Block" "contour elevation label" "y" pause)
);while
(command "ATTSYNC" "s" pause "y")
(prompt "\nCONLABEL Complete...")
(princ)
);defun

Is ATTSYNC in the correct position? Do you only run it on one block after you're finished with MEASURE?

 

Best,

~DD

0 Likes
Message 6 of 8

Anonymous
Not applicable

This is exactly what I was thinking thanks!

 

The attsync is where it is because after all the blocks have been placed they are still read as the same block so the one command will sync each instance, if that makes sense

0 Likes
Message 7 of 8

CodeDing
Advisor
Advisor

Oh, I don't use ATTSYNC much, but if that's the case you can just have it sync the last block. Try updating to this:

(command "ATTSYNC" "s" "l" "y")
0 Likes
Message 8 of 8

scot-65
Advisor
Advisor
(while (and (not (initget 1 "Yes No" ))...
can be used. As andkal has stated, INITGET returns nil.

???

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes