Group selection set based on dxf codes (layer name, color or layer type)

Group selection set based on dxf codes (layer name, color or layer type)

qwikdraft
Observer Observer
298 Views
1 Reply
Message 1 of 2

Group selection set based on dxf codes (layer name, color or layer type)

qwikdraft
Observer
Observer

As a beginner, I have done something I have been trying for a long time: group a full selection set based on their dxf code values. If you think this can be more clean, please feel free to let me know.

Attaching the code for anyone's reference:

(defun c:slection_sets()
  (setvar "osmode" 0)
  (alert "Select your Ground floor plan")
  (setq groundfloor (ssget))
  (setq entities (list))
  (setq entities (append (list (ssname groundfloor 0)) entities))
  (setq in 0)
(while (< in (sslength groundfloor))
(setq count 1)
(foreach k entities
  (if (and (equal (assoc 0 (entget (ssname groundfloor in))) (assoc 0 (entget k))) (equal (assoc 8 (entget (ssname groundfloor in))) (assoc 8 (entget k))) (equal (assoc 62 (entget (ssname groundfloor in))) (assoc 62 (entget k))))
    (progn
      (princ "T")
      (setq count (+ count 0))
      )
    (progn
      (princ "F")
      (setq count (1+ count))
      )
    )
  )
  (princ "\n")
(if (> count (length entities))
(setq entities (append (list (ssname groundfloor in)) entities))
)
  (setq in (1+ in))
)
  )

 

0 Likes
299 Views
1 Reply
Reply (1)
Message 2 of 2

Moshe-A
Mentor
Mentor

@qwikdraft  hi,

 

here is my simple version for you to learn (understand)

the idea is first to select the object according all following will be matched.

 

line4 : (entsel) ; pick first object

line 8: (ssget) ; select the following objects

 

line 16-17:  check for match object + layer

 

for matching the color you have to careful cause if the color is BYLAYER, (entget) does not return the color dotpair and calling (assoc '62 elist) will return nil so nil could break the program. 

 

line 18-21: check to see if dxf code 62 exist and if both object are nil this is a match otherwise both colors mush be equal.

 

line 29:  the (cons) function construct the list. cause variable entities is defines as local it is reset to nil at start and nil is total reset for a list 😀 

 

line 37: echo the list to cmd line, a (reverse) function is used cause the (cons) function construct the list in reverse mode. 

 

if you want to know the amount of entities, call (length entities)

 

enjoy

Moshe

 

 

 

; construct a list of objects share the same layer and color, first selected object will lead
(defun c:grpsel (/ pick0 ename0 elist0 ss1 i ename1 elist1 entities)
 (if (and
       (setq pick0 (entsel "\nPick first object: "))
       (setq ename0 (car pick0))
       (not (redraw ename0 3))
       (setq elist0 (entget ename0))
       (setq ss1 (ssget))
     )
  (progn
   (setq i -1)
   (repeat (sslength ss1)
    (setq elist1 (entget (setq ename1 (ssname ss1 (setq i (1+ i))))))
    
    (if (and  
          (eq (cdr (assoc '0 elist0)) (cdr (assoc '0 elist1)))
          (eq (cdr (assoc '8 elist0)) (cdr (assoc '8 elist1)))
	  (or
	    (and (not (cdr (assoc '62 elist0))) (not (cdr (assoc '62 elist1))))
	    (eq (cdr (assoc '62 elist0)) (cdr (assoc '62 elist1)))
	  ); or
        ); and
     (setq entities (cons ename1 entities)) ; construct the list of objects
    ); if
     
   ); repeat

   (if (not (ssmemb ename0 ss1))
    (setq entities (cons ename0 entities)) ; add the first
   )    

   (redraw ename0 4)
  ); progn
 ); if

 (terpri) 
 (princ (reverse entities))
 (princ)
); c:grpsel

 

 

 

0 Likes