Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
*Kiwi Russ
Message 1 of 8 (118 Views)

Lists from dotted pairs

118 Views, 7 Replies
03-26-2009 08:26 PM
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
Active Contributor
st0neface
Posts: 35
Registered: ‎03-17-2009
Message 2 of 8 (118 Views)

Re: Lists from dotted pairs

03-27-2009 12:26 AM in reply to: *Kiwi Russ

(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
Distinguished Contributor
H.vanZeeland
Posts: 664
Registered: ‎12-11-2003
Message 3 of 8 (118 Views)

Re: Lists from dotted pairs

03-27-2009 01:36 AM in reply to: *Kiwi Russ
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
*Kiwi Russ
Message 4 of 8 (118 Views)

Re: Lists from dotted pairs

03-27-2009 02:26 AM in reply to: *Kiwi Russ
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
Active Contributor
st0neface
Posts: 35
Registered: ‎03-17-2009
Message 5 of 8 (118 Views)

Re: Lists from dotted pairs

03-27-2009 05:12 AM in reply to: *Kiwi Russ


(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.
*Kiwi Russ
Message 6 of 8 (118 Views)

Re: Lists from dotted pairs

03-28-2009 02:46 AM in reply to: *Kiwi Russ
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
Distinguished Contributor
H.vanZeeland
Posts: 664
Registered: ‎12-11-2003
Message 7 of 8 (118 Views)

Re: Lists from dotted pairs

03-28-2009 08:35 AM in reply to: *Kiwi Russ
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

*Kiwi Russ
Message 8 of 8 (118 Views)

Re: Lists from dotted pairs

03-28-2009 01:35 PM in reply to: *Kiwi Russ
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
Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.
Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.