Jump to content

Recommended Posts

Posted (edited)

Hi all,

 

I have this piece of code to try to create a layer filter to show only non-xref layers. It works sort of but it seems to toggle the filter and not set it every time it's run. I've looked at several threads on this topic but couldn't find a way to fix this.

 

Problem: Run this once and it creates the filter, 2nd run it sets the filter, on 3rd run it toggles off the filter. 

 

The solution I was hoping for: I would like to do create the filter and set it and any subsequent runs set the filter.

 

Credit to EC-CAD for the _getlayerfilternames function.

 

 


(setq layfltrName "Non-Xref Layers")

; (princ (_getlayerfilternames))

(if (member (strcase layfltrName) (_getlayerfilternames)) 

  ; If the layer filter exists
  (progn 
    (princ (strcat "Setting filter to " layfltrName))
    (command "+LAYER" layfltrName) ;; Undocumented command
    (command "-LAYER" "_FILTER" "_S" layfltrName "_X" "")
  )

  ; If the layer filter doesn't exist
  (progn 

    (princ (strcat "Creating and Setting filter to " layfltrName))
    (command "-LAYER" "_filter" "N" "P" "All" "NAME==\"~*|*\"" layfltrName "_X" "")
    (command "+LAYER" layfltrName) ;; Undocumented command
    (command "-LAYER" "_FILTER" "_S" layfltrName "_X" "")
  )
)

  ;; Check for All Non-Xref Layers filter and set it.
  ;;----------------------------------------------------------------------;;
  ;; Search Layer Filters by EC-CAD
  ;; https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/table-search-for-layer-filter/m-p/2295197#M263717
(defun _getlayerfilternames (/ collection) 
  (setq names (list ""))
  (if 
    (not 
      (vl-catch-all-error-p 
        (setq collection (vl-catch-all-apply 
                           (function 
                             (lambda () 
                               (vla-item 
                                 (vla-getextensiondictionary 
                                   (vla-get-layers (vla-get-activedocument (vlax-get-acad-object)))
                                 )
                                 "ACAD_LAYERFILTERS"
                               )
                             )
                           )
                         )
        )
      )
    )
    (vlax-for item collection 
      (setq names (cons (strcase (vla-get-name item)) names))
    )
  )
  names
)

 

Edited by 3dwannab
Posted (edited)

Well. This seems to work to use the built-in filters and set it to the All non-Xref Layers filter 🧐 Seem to have to set it to the All Used Layers first and set the other.

 

(command "+layer" "All Used Layers")
(command "-LAYER" "_FILTER" "_S" "All Used Layers" "_X" "")

(command "+layer" "All non-Xref Layers")
(command "-LAYER" "_FILTER" "_S" "All non-Xref Layers" "_X" "")

 

Edit:

No need for the undocumented +layer command as it shows the layers palette if shown and rolled in.

 

This seems to do the business.

; (command "+layer" "All Used Layers") ;; Makes the layer prop manager pop out if shown. Don't need this anyway.
(command "-LAYER" "_FILTER" "_S" "All Used Layers" "_X" "")

; (command "+layer" "All non-Xref Layers") ;; Makes the layer prop manager pop out if shown. Don't need this anyway.
(command "-LAYER" "_FILTER" "_S" "All non-Xref Layers" "_X" "")

 

Just to note. That All non-Xref Layers filter only gets created when an Xref is inserted.

 

I got around this by doing this:

(command "-LAYER" "_FILTER" "_S" "All Used Layers" "_X" "") ;; Set to this filter first

(if (> (_XRef_Count) 0) 
  (command "-LAYER" "_FILTER" "_S" "All non-Xref Layers" "_X" "") ;; Then set to the non-Xref one. This prevent the toggling bug that existed with the first version.
)

;;----------------------------------------------------------------------;;
;; XREF COUNT FUNCTION

(defun _XRef_Count (/ i x) 
  (setq i 0)
  (vlax-for x 
    (vla-get-blocks 
      (vla-get-ActiveDocument (vlax-get-acad-object))
    )
    (if (= :vlax-true (vla-get-isxref x)) 
      (setq i (1+ i))
    )
  )
  i
)

 

Edited by 3dwannab

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...