sadhu Posted November 10, 2011 Posted November 10, 2011 I have list of blocks BlktagValLst that contains list-elements organized as below: (blknme-A tag1 tagvlu1A) (blknme-A tag2 tagvlu2A) (blknme-A tag3 tagvlu3A) (blknme-B tag1 tagvlu1B) (blknme-B tag1 tagvlu1B) (blknme-B tag1 tagvlu1B) and so on for a large number of blocks. I need to access the tag values so I tried this : (and (= (car (assoc blknme-A BlktagValLst)) blknme-A) ;if block name is blknme-A (= (cadr (assoc blknme-A BlktagValLst)) tag1) ; and tag is tag1 (setq pntxt (caddr blknme-A BlktagValLst)))) ; ;save tag value in pntxt (and (= (car (assoc blknme-A BlktagValLst)) blknme-A) ;if block name is blknme-A (= (cadr (assoc blknme-A BlktagValLst)) [color=red]tag2[/color]) ; and tag is tag2 (setq [color=red]descrixt[/color] (caddr blknme-A BlktagValLst)))) ; ;save tag value in [color=red]descrixt[/color] Need help to make this funtional - or a better way to do it. Thanks Quote
MSasu Posted November 10, 2011 Posted November 10, 2011 The content of associated lists are dotted pairs not standard lists (with an exception for point entries); therefore the second entry should be accessed trough CDR not CADR. Also you don't need to validate the DXF key (the CAR item). Regards, Mircea Quote
Lee Mac Posted November 10, 2011 Posted November 10, 2011 I would suggest restructuring your block data to something like: (setq lst '( (blknme-A (tag1 tagvlu1A) (tag2 tagvlu2A) (tag3 tagvlu3A) ) (blknme-B (tag1 tagvlu1B) (tag2 tagvlu2B) (tag3 tagvlu3B) ) ) ) Then the task becomes: (cdr (assoc "TagName" (cdr (assoc "BlockName" lst)))) Quote
sadhu Posted November 10, 2011 Author Posted November 10, 2011 Thank Lee, I'm working on what you suggested. Quote
Lee Mac Posted November 11, 2011 Posted November 11, 2011 If you wanted to use your original data, here are three functions to retrieve the value from the block/tag names: (defun GetValue ( block tag lst / value ) (while (and (setq item (car lst)) (not value)) (if (and (eq block (car item)) (eq tag (cadr item))) (setq value (caddr item)) ) (setq lst (cdr lst)) ) value ) (defun GetValue2 ( block tag lst ) (vl-some '(lambda ( item ) (if (and (eq block (car item)) (eq tag (cadr item))) (caddr item))) lst) ) (defun GetValue3 ( block tag lst / _massoc ) (defun _massoc ( key lst / item ) (if (setq item (assoc key lst)) (cons (cdr item) (_massoc key (cdr (member item lst)))) ) ) (cadr (assoc tag (_massoc block lst))) ) e.g.: (setq lst '( ("Block1" "Tag1" "b1Value1") ("Block1" "Tag2" "b1Value2") ("Block1" "Tag3" "b1Value3") ("Block2" "Tag1" "b2Value1") ("Block2" "Tag2" "b2Value2") ("Block2" "Tag3" "b2Value3") ) ) _$ (GetValue "Block2" "Tag2" lst) "b2Value2" _$ (GetValue2 "Block2" "Tag2" lst) "b2Value2" _$ (GetValue3 "Block2" "Tag2" lst) "b2Value2" Quick speed test: (setq i 9) (repeat 9 (setq j 9) (repeat 9 (setq l (cons (list (strcat "Block" (itoa i)) (strcat "Tag" (itoa j)) (strcat "b" (itoa i) "Value" (itoa j))) l)) (setq j (1- j)) ) (setq i (1- i)) ) ( ("Block1" "Tag1" "b1Value1") ("Block1" "Tag2" "b1Value2") ("Block1" "Tag3" "b1Value3") ("Block1" "Tag4" "b1Value4") ("Block1" "Tag5" "b1Value5") ("Block1" "Tag6" "b1Value6") ("Block1" "Tag7" "b1Value7") ("Block1" "Tag8" "b1Value8") ("Block1" "Tag9" "b1Value9") ("Block2" "Tag1" "b2Value1") ("Block2" "Tag2" "b2Value2") ... ("Block9" "Tag9" "b9Value9") ) _$ (GetValue "Block6" "Tag7" l) "b6Value7" _$ (GetValue2 "Block6" "Tag7" l) "b6Value7" _$ (GetValue3 "Block6" "Tag7" l) "b6Value7" _$ (Benchmark '((GetValue "Block6" "Tag7" l) (GetValue2 "Block6" "Tag7" l) (GetValue3 "Block6" "Tag7" l))) Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s): (GETVALUE3 "Block6" "Tag7" L).....1045 / 2.13 <fastest> (GETVALUE "Block6" "Tag7" L)......1622 / 1.38 (GETVALUE2 "Block6" "Tag7" L).....2231 / 1 <slowest> Quote
sadhu Posted November 11, 2011 Author Posted November 11, 2011 I have opted to use this - it is the fastest isn't it. (defun GetValue3 ( block tag lst / _massoc ) (defun _massoc ( key lst / item ) (if (setq item (assoc key lst)) (cons (cdr item) (_massoc key (cdr (member item lst)))) ) ) (cadr (assoc tag (_massoc block lst))) ) It is integrated like this (setq txt1 (getvalue (nth Ucnt Ulst) "P/N" tagValLst)) (setq txt2 (getvalue (nth Ucnt Ulst) "DESCRI" tagValLst)) (setq txt3 (strcat (rtos pcnt 2 0)". ;"txt1";"txt2";qty. ;"(rtos counter 2 2))) ;(prc asso) (prc txt3) ; print Actually it is a part of a code that makes a simple BOM. (part number, description and quantity). It works perfectly. Thanks a lot LEE. I have tried your COUNT lisp - it does not print attributes so not my case. Now I'm looking into putting this data into a table that can be inserted into the drawing (like your COUNT lisp) Thanks agian. Quote
Lee Mac Posted November 11, 2011 Posted November 11, 2011 You're very welcome Sadhu! Good luck with your program Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.