Step trough entity list

Step trough entity list

andreas7ZYXQ
Advocate Advocate
1,241 Views
8 Replies
Message 1 of 9

Step trough entity list

andreas7ZYXQ
Advocate
Advocate

I´m trying to figure out how to step through my entity list when i have more than one dxf code thats the same.

In the list below ive marked the dxf code 300. I would like to step through the list and read those codes.
How do i read the 300 code in a specific position in the list? Im new to reading lists that have duplictes.

 

 (
    (-1 . <Entity name: 1bbbdd01050>)
    (0 . "MAGIELECTRICALDIMTEXT")
    (330 . <Entity name: 1bbecf791f0>)
    (5 . "7CD")
    (100 . "AcDbEntity")
    (67 . 0)
    (410 . "Model")
    (8 . "E-64ED-HT-")
    (100 . "MAGIELECTRICALENTITY")
    (70 . 801)
    (71 . 0)
    (72 . 1)
    (40 . 1.0)
    (41 . 1.0)
    (73 . 0)
    (74 . 0)
    (75 . 1)
    (330 . <Entity name: 1bbbdd01040>)
    (76 . 0)
    (70 . 4)
    (70 . 2003)
    (70 . 0)
    (70 . 4103)
    (40 . 1.0)
    (70 . 1018)
    (280 . 1)
    (70 . 6055)
    (300 . "R3;R2;R1;;X2;")
    (70 . 5)
    (70 . 2001)
    (70 . 1201)
    (70 . 1011)
    (280 . 0)
    (70 . 6046)
    (300 . "0FKHPsygjBC8K6WbT1zI28")
    (70 . 2015)
    (70 . 1)
    (70 . 6024)
    (300 . "RN,BN,AL,CI")
    (100 . "MAGIELECTRICALDIMTEXT")
    (10 8900.0 8450.0 200.0)
    (210 -6.12323e-17 -1.0 0.0)
    (211 0.0 0.0 1.0)
    (212 0.0 0.0 1.0)
    (70 . 0)
    (340 . <Entity name: 1bbecf745a0>)
    (340 . <Entity name: 1bbbf855ce0>)
    (70 . 256)
    (40 . 125.0)
    (41 . 0.0)
    (71 . 0)
    (72 . 0)
    (73 . 32)
    (74 . 0)
    (70 . 6)
    (71 . 213)
    (72 . 0)
    (73 . 0)
    (74 . 0)
    (300 . "+D?S?")
    (71 . 212)
    (72 . 0)
    (73 . 1)
    (74 . 0)
    (300 . "M??:")
    (71 . 211)
    (72 . 0)
    (73 . 2)
    (74 . 0)
    (300 . "16")
    (71 . 0)
    (72 . 0)
    (73 . 3)
    (74 . 0)
    (300 . "-")
    (71 . 226)
    (72 . 0)
    (73 . 4)
    (74 . 0)
    (300 . "17")
    (71 . 0)
    (72 . 0)
    (73 . 5)
    (74 . 0)
    (300 . "X")
  )
0 Likes
Accepted solutions (3)
1,242 Views
8 Replies
Replies (8)
Message 2 of 9

ВeekeeCZ
Consultant
Consultant

The most simple way, use foreach:

(foreach pair lst
  (if (= (car pair) 300)
    (setq lst300 (cons pair lst300))))

Note: lst300 is in reversed order!

 

The most popular way, use vl-remove-if-not

(vl-remove-if-not
  '(lambda (p)
     (= 300 (car p)))
  lst)

 

 

 

0 Likes
Message 3 of 9

_Tharwat
Advisor
Advisor

Hi,

To step through a list, here is one way to do so:

NOTE: Replace the variable 'lst' with your dxf list of items.

(foreach dxf lst
  (if (= (car dxf) 300)
    (setq l (cons dxf l))
    )
  )

To pick an item based on the position then here is one way to do so:

NOTE: the variable 'num' represents an integer starts from zero as the first item.

(nth num l)
0 Likes
Message 4 of 9

andreas7ZYXQ
Advocate
Advocate

Thanks

 

So if i would like to pick the value (300 . "M??:"), how would you prefer to do that? 
If you could give me an example of extracting that one i would be very thankful.

 

(300 . "M??:")

 

0 Likes
Message 5 of 9

_Tharwat
Advisor
Advisor
Accepted solution

Give this a go.

    (while (and lst (not fnd))
      (and (= (caar lst) 300) (= (setq val (cdr (car lst))) "M??:") (setq fnd val))
      (setq lst (cdr lst))
      )
0 Likes
Message 6 of 9

ВeekeeCZ
Consultant
Consultant
Accepted solution

Then try this function

(defun miassoc (key idx lst)   ; idx 0 for 1st
 (nth idx (vl-remove-if-not '(lambda (p) (= key (car p))) lst)))

Usage:

(miassoc 300 1 (entget (car (entsel))))

 

 

0 Likes
Message 7 of 9

ВeekeeCZ
Consultant
Consultant
Accepted solution

@andreas7ZYXQ wrote:

Thanks

 

So if i would like to pick the value (300 . "M??:"), how would you prefer to do that? 
If you could give me an example of extracting that one i would be very thankful.

 

(300 . "M??:")

 


 

Do you search by fixed order in the list or by given content?

My function is for the first case, @_Tharwat  for the latter.

0 Likes
Message 8 of 9

andreas7ZYXQ
Advocate
Advocate

I will test and try both functions. Thanks alot!

0 Likes
Message 9 of 9

_Tharwat
Advisor
Advisor

You're welcome and happy coding.

0 Likes