PDA

View Full Version : How to use the "WHILE" and use "ENTER" to end the command ?


stephen.coff
2007-07-22, 02:51 PM
Guys,
I have routine by Fixo that will allow you to select a polyline and display it's area. I have altered it some what to place the area on a particular layer and style. I want it to be able to select multiple polylines and label each one selcted with a number ie "Zone 1" Zone 2" etc. I think I have it correct so it will count out the zones. How ever, I want to get the "WHILE" function to work with "ENTER" after a polyline selection to be the condition to exit the "WHILE" function.
Unother words, while you keep selecting polylines it will keep running and label the next polylines as a zone number higher than the previous. When the enter button is pressed after selecting the polyline the routine will end.

See below for the routine as it stands:

;;; Altered ARL.lsp by FIXO (AUGI) 22.07.07

(defun c:Atxt (/ ent adoc acsp obj mtx myNum)
(vl-load-com)
(if (= (getvar "USERI1") 0)
(setvar "USERI1" 1)
(progn
(setvar "USERI1" 1)
(alert "\n USERI1 Has Been Reset To: 1 ")
)
)

(setq oldunits (getvar "insunits"))
(command "-layer" "m" "-M-AREA" "C" "20"
"-M-AREA" "Lt" "DASHED2" "-M-AREA" "p"
"n" "-M-AREA" ""
)
(command "_style" "AREA" "arial" "500" "1" "0" "n" "n")

(or adoc
(setq adoc (vla-get-activedocument
(vlax-get-acad-object)
)
)
)

(or acsp
(setq acsp (vla-get-modelspace
adoc
)
)
)
(while
(setq ent
(entsel
"\nSelect The Polyline You Wish To Calculate Area For (ENTER to exit): "
)
)
(setq obj (vlax-ename->vla-object (car ent)))
(if (wcmatch (vla-get-objectname obj) "*Polyline")
(progn
(setq myNum (getvar "USERI1"))
(setq cpt (getpoint "\PICK LOCATION FOR AREA TEXT"))
(setq pt1 (mapcar '+ cpt (list 0 175 0)))
(setq pt2 (mapcar '+ cpt (list 0 -175 0)))
(setq txt1 (strcat "Zone " myNum))
(command "_text" "style" "area" "justify" "bc" pt1 "0" txt1)

;;: Displayed In Meters To 1 Decimal Place.
(setq newunits (setvar "insunits" 4))
(setq
txt2 (strcat "%<\\AcObjProp Object (%<\\_ObjId "
(itoa (vlax-get obj 'ObjectID))
">%).Area \\f \"%lu6%qf1%ct8%pr1[1e-006]\">%"
)
)
(alert "THIS IS NOT A POLYLINE")
)
)
(command "_text" "style" "area" "justify" "Tc" pt1 "0" txt2)
(vla-regen adoc acallviewports)
(setvar "USERI1" (+ myNum 1))
(setq myNum (itoa myNum))
)
(setvar "insunits" oldunits)
(setvar "USERI1" 0)
(princ)
)

rkmcswain
2007-07-22, 04:24 PM
Example:
This will report the entity type selected, as long as you keep selecting objects. If you press enter to quit selecting, or miss a pick, then the (while) is exited.

So basically, put all your code in between the (while and it's closing paren.


(while (setq ent (entsel))
(alert (cdr (assoc 0 (entget (car ent)))))
)

stephen.coff
2007-07-23, 01:40 AM
rkmcswain,
I am grateful of your reply. I have a problem still, I am truely missing/ not understand something. This is what is really bugging me, I thought I had all the required code inside of the closing paren ? I thouunderstand the closing paren to be before (setvar "insunits" oldunits) which is a few lines up from the end of the code ?

Stephen

rkmcswain
2007-07-23, 03:07 PM
This:
(setq txt1 (strcat "Zone " myNum))

...doesn't work. It should read:

(setq txt1 (strcat "Zone " (itoa myNum)))

Also, the (progn (inside the while) should close before the warning about "this is not a polyline"....

Here is revised version.


;;; Altered ARL.lsp by FIXO (AUGI) 22.07.07

(defun c:Atxt (/ ent adoc acsp obj mtx myNum CPT NEWUNITS OLDUNITS PT1 PT2 TXT1 TXT2)
(vl-load-com)
(if (= (getvar "USERI1") 0)
(setvar "USERI1" 1)
(progn
(setvar "USERI1" 1)
(alert "\n USERI1 Has Been Reset To: 1 ")
)
)

(setq oldunits (getvar "insunits"))
(command "-layer" "m" "-M-AREA" "C" "20"
"-M-AREA" "Lt" "DASHED2" "-M-AREA" "p"
"n" "-M-AREA" ""
)
(command "_style" "AREA" "arial" "500" "1" "0" "n" "n")

(or adoc
(setq adoc (vla-get-activedocument
(vlax-get-acad-object)
)
)
)

(or acsp
(setq acsp (vla-get-modelspace
adoc
)
)
)
(while
(setq ent
(entsel
"\nSelect The Polyline You Wish To Calculate Area For (ENTER to exit): "
)
)
(setq obj (vlax-ename->vla-object (car ent)))
(if (wcmatch (vla-get-objectname obj) "*Polyline")
(progn
(setq myNum (getvar "USERI1"))
(setq cpt (getpoint "\PICK LOCATION FOR AREA TEXT"))
(setq pt1 (mapcar '+ cpt (list 0 175 0)))
(setq pt2 (mapcar '+ cpt (list 0 -175 0)))
(setq txt1 (strcat "Zone " (itoa myNum)))
(command "_text" "style" "area" "justify" "bc" pt1 "0" txt1)

;;: Displayed In Meters To 1 Decimal Place.
(setq newunits (setvar "insunits" 4))
(setq
txt2 (strcat "%<\\AcObjProp Object (%<\\_ObjId "
(itoa (vlax-get obj 'ObjectID))
">%).Area \\f \"%lu6%qf1%ct8%pr1[1e-006]\">%"
)
))
(alert "THIS IS NOT A POLYLINE")

)
(command "_text" "style" "area" "justify" "Tc" pt1 "0" txt2)
(vla-regen adoc acallviewports)
(setvar "USERI1" (+ myNum 1))
(setq myNum (itoa myNum))
)
(setvar "insunits" oldunits)
(setvar "USERI1" 0)
(princ)
)

stephen.coff
2007-07-25, 03:29 AM
rkmcswain,
Thank you very much for the revised routine and more so for the explaination to help me understand where I have gone wrong.

Stephen