Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Lists from dotted pairs

7 REPLIES 7
Reply
Message 1 of 8
Anonymous
469 Views, 7 Replies

Lists from dotted pairs

If I have a dotted pair list as shown below



Lst = (( Polyline . myLayer2 ) ( Line . mylayer1 ) ( Circle . myLayer5 )
( Block . MyLayer8 ))



The above list is in the correct order. Sometimes there may only be 1 pair
in the list and sometimes more.



I want the final result to be : (princ or alert string) :



" Level 1 object is a 'Polyline' on layer 'myLayer2'

Level 2 object is a 'Line' on layer 'myLayer1'

Level 3 object is a 'Circle' on layer 'myLayer5'

Level 4 object is a 'Block' on layer 'myLayer8' "



The problem I am having is list manipulation. I guess I need to sort the
list

into separate lists?

So objects are in one list and layers in another list? If so, how is it
done?

And then how do make it so I can place them into the string with the
corresponding level?



I am all googled out on looking for examples ...



Thanks for your help



Russ
7 REPLIES 7
Message 2 of 8
st0neface
in reply to: Anonymous


(defun test (lst / elem cnt)

(setq cnt 1)
(foreach elem lst
(princ (strcat "\nLevel "
(itoa cnt)
" Object is a "
(vl-princ-to-string (car elem))
" on Layer "
(vl-princ-to-string (cdr elem))
)
)
(setq cnt (1+ cnt))
)
(princ)
)


HTH,
Kurt
Message 3 of 8
H.vanZeeland
in reply to: Anonymous

Hi,



Or this



(setq i 0 lst '(("Polyline" . "myLayer2") ("Line" . "mylayer1") ("Circle" . "myLayer5")

("Block" . "MyLayer8")))





(alert (apply 'strcat (mapcar (function (lambda(obj layer)

(strcat "\nLevel " (itoa (setq i (1+ i)))" Object is a <" obj "> on layer \"" layer "\"")

))(mapcar 'car lst)(mapcar 'cdr lst))))



Cheers

Harrie
Message 4 of 8
Anonymous
in reply to: Anonymous

Yes thanks Kurt thats great!
Just one question if I was to use "alert" instead of "princ" how would I
place all the string onto one alert box? ie if there are 3 dotted pairs
there would be 3 alert boxes - how can I place it onto one?
Russ
wrote in message news:6150477@discussion.autodesk.com...
(defun test (lst / elem cnt) (setq cnt 1) (foreach elem lst (princ (strcat
"\nLevel " (itoa cnt) " Object is a " (vl-princ-to-string (car elem)) " on
Layer " (vl-princ-to-string (cdr elem)) ) ) (setq cnt (1+ cnt)) ) (princ) )
HTH, Kurt
Message 5 of 8
st0neface
in reply to: Anonymous



(defun test (lst / elem cnt msg)
(setq cnt 1 msg "")
(foreach elem lst
(setq msg (strcat msg
(strcat "\nLevel "
(itoa cnt)
" Object is a "
(vl-princ-to-string (car elem))
" on Layer "
(vl-princ-to-string (cdr elem))
)
))
(setq cnt (1+ cnt))
)
(alert msg)
)


also take a look at Harrie's solution.
Message 6 of 8
Anonymous
in reply to: Anonymous

Thanks both examples work perfectly!

Just one other question the object names in my dotted pair list have object
names like "AcDbPolyline" and "AcDbBlockReference"
I would like to change the names to something more user friendly. How would
I do that? - Do I need to modify the list or change the final string ?


(setq obj EntityName (cond
((= EntityName "AcDbPolyline") "a Polyline")
((= EntityName "AcDbBlockReference" ) "a Block" )
((= EntityName "AcDbLine" ) "a Line" )
((= EntityName "AcDbCircle" ) "a Circle" )
);cond
)

cheers Russ
Message 7 of 8
H.vanZeeland
in reply to: Anonymous

Hi Rus



You can do something like this

and add those names you want to the function ConvToUserFriendly




(defun ConvToUserFriendly (Objname)

(cond

((= Objname "AcDbPolyline") "Polyline")

((= Objname "AcDbBlockReference" ) "Block" )

((= Objname "AcDbLine" ) "Line" )

((= Objname "AcDbCircle" ) "Circle" )

;;; ((= Objname "AcDbMore" ) "More" )

(T Objname)

);cond

)



(defun alertDottedList (lst / i)

(setq i 0)

(alert (apply 'strcat (mapcar (function (lambda(obj layer)

(strcat "\nLevel " (itoa (setq i (1+ i)))" Object is a <" (ConvToUserFriendly obj) "> on layer \"" layer "\"")

))(mapcar 'car lst)(mapcar 'cdr lst))))

)





(alertDottedList '(("AcDbPolyline" . "myLayer2") ("AcDbLine" . "mylayer1") ("AcDbCircle" . "myLayer5")

("AcDbBlockReference" . "MyLayer8")("AcDbText" . "mylayer9")))



Cheers

Harrie

Message 8 of 8
Anonymous
in reply to: Anonymous

Fantastic Harrie!,
that's brilliant 🙂
cheers
Russ

I wrote in message news:6151189@discussion.autodesk.com...
Hi Rus



You can do something like this

and add those names you want to the function ConvToUserFriendly





(defun ConvToUserFriendly (Objname)

(cond

((= Objname "AcDbPolyline") "Polyline")

((= Objname "AcDbBlockReference" ) "Block" )

((= Objname "AcDbLine" ) "Line" )

((= Objname "AcDbCircle" ) "Circle" )

;;; ((= Objname "AcDbMore" ) "More" )

(T Objname)

);cond

)




(defun alertDottedList (lst / i)

(setq i 0)

(alert (apply 'strcat (mapcar (function (lambda(obj layer)

(strcat "\nLevel " (itoa (setq i (1+ i)))" Object is a <"
(ConvToUserFriendly obj) "> on layer \"" layer "\"")

))(mapcar 'car lst)(mapcar 'cdr lst))))

)






(alertDottedList '(("AcDbPolyline" . "myLayer2") ("AcDbLine" . "mylayer1")
("AcDbCircle" . "myLayer5")

("AcDbBlockReference" . "MyLayer8")("AcDbText" . "mylayer9")))



Cheers

Harrie

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost