Jump to content

Recommended Posts

Posted (edited)

Hello!

 

I’m trying to do a lisp which runs through all lines and sorts them by color, so the heaviest color ends up on top.

This works fine for when there are lines to move but if the selection is empty (in case no green lines exists in the drawing for example) the script stops and waits for an selection but I want it to

just move on in case no lines exist.

This is for cleaning up dwg exports from Revit so sometimes all colors needs to be sorted, sometimes on 3 or 4 depending on which drawing is exported.

(defun ssfilter-by-color (color / lay layers)
 ;; Get all the layer names whi?ch are set to the color
 (setq lay    (tblnext "LAYER" t) ;Get the 1st layer
       layers "" ;Initialize the layer names filter string
 )
 (while lay ;Step through all layers
   ;; Check if current layer is set to color
   (if (= (cdr (assoc 62 lay)) color)
     (setq layers (strcat "," (cdr (assoc 2 lay)) layers)) ;Add to filter string
   )
   (setq lay (tblnext "LAYER")) ;Get the next layer
 )
 (if (= layers "")
   (list (cons 62 color))
   (list '(-4 . "<OR")
         '(-4 . "<AND")
         (cons 8 (substr layers 2))
         '(62 . 256)
         '(-4 . "AND>")
         (cons 62 color)
         '(-4 . "OR>")
   )
 )
)
(sssetfirst nil (ssget "_x" (ssfilter-by-color 7))) ;;nr=indexcolor
(command "_draworder" "_B")

(sssetfirst nil (ssget "_x" (ssfilter-by-color 3))) 
(command "_draworder" "_B")

(sssetfirst nil (ssget "_x" (ssfilter-by-color 1))) 
(command "_draworder" "_B")

(sssetfirst nil (ssget "_x" (ssfilter-by-color 4))) 
(command "_draworder" "_B")

(princ)

 

Edited by HHL
Posted

Try:

(defun ssfilter-by-color (color / lay layers)
 ;; Get all the layer names which are set to the color
 (setq lay    (tblnext "LAYER" t) ; Get the 1st layer
       layers ""                  ; Initialize the layer names filter string
 )
 (while lay ; Step through all layers
   ;; Check if current layer is set to color
   (if (= (cdr (assoc 62 lay)) color)
     (setq layers (strcat "," (cdr (assoc 2 lay)) layers)) ; Add to filter string
   )
   (setq lay (tblnext "LAYER")) ; Get the next layer
 )
 (if (= layers "")
   (list (cons 62 color))
   (list '(-4 . "<OR")
         '(-4 . "<AND")
         (cons 8 (substr layers 2))
         '(62 . 256)
         '(-4 . "AND>")
         (cons 62 color)
         '(-4 . "OR>")
   )
 )
)

(defun ssfilter-by-space ()
  (list
    (cons 410 (if (= 1 (getvar 'cvport)) (getvar 'ctab) "Model"))
    '(-4 . "<NOT")
      '(-4 . "<AND")
        '(0 . "VIEWPORT")
        '(69 . 1)
      '(-4 . "AND>")
    '(-4 . "NOT>")
  )
)

(defun c:DrawOrderByColor ( / spc ss)
  (setq spc (ssfilter-by-space))
  (foreach aci '(7 3 1 4)
    (if (setq ss (ssget "_X" (append spc (ssfilter-by-color aci))))
      (command "_draworder" ss "" "_back")
    )
  )
  (princ)
)

(c:DrawOrderByColor)

The ssfilter-by-color function was not modified.

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...