Jump to content

Change Layers inside a Block


JoeyG_77

Recommended Posts

Hey everyone ... Im looking for a lisp that would take a block open it and change all the layers & line types to "bylayer" & then change it to a specific layer and close the block after editing it.

 

Thanks

Joey G

Link to comment
Share on other sites

I had this in my library - modified it from ByBlock to ByLayer for your purposes... Untested though...

 


(defun c:BlksByLayer ( / *error* BlkByLayer *adoc* ss i lst )

  (vl-load-com)

  (defun *error* ( m )
    (vla-endundomark *adoc*)
    (if m
      (prompt m)
    )
    (princ)
  )

  (defun BlkByLayer ( objSelection / blkdef )
    (if (= (type objSelection) 'ENAME)
      (setq objSelection (vlax-ename->vla-object objSelection))
    )
    (if (wcmatch (strcase (vla-get-objectname objSelection)) "*BLOCK*")
      (if (and
            (setq blkdef (vla-item (vla-get-blocks *adoc*) (vla-get-name objSelection)))
            ;;;(= (vla-get-isdynamicblock blkdef) :vlax-false) ;;; - if you need to process dynamic blocks as well - if you don't remove comments
            (= (vla-get-isxref blkdef) :vlax-false)
            (not (vl-position (vla-get-name objSelection) lst))
          )
        (progn
          (setq lst (cons (vla-get-name objSelection) lst))
          (vlax-for objBlock blkdef
            (vla-put-color objBlock 256)
            (vla-put-linetype objBlock "ByLayer")
            (vla-put-Lineweight objBlock -1)
          )
        )
      )
    )
  )

  (setq *adoc* (vla-get-activedocument (vlax-get-acad-object)))
  (if (= 8 (logand 8 (getvar 'undoctl)))
    (vla-endundomark *adoc*)
  )
  (vla-startundomark *adoc*)
  (if (setq ss (ssget "_:L" '((0 . "INSERT"))))
    (repeat (setq i (sslength ss))
      (BlkByLayer (ssname ss (setq i (1- i))))
      (entupd (ssname ss i))
    )
  )
  (vla-regen *adoc* acactiveviewport)
  (*error* nil)
)

 

HTH., M.R.

Link to comment
Share on other sites

Marko ... Thanks for the response ! It worked great to change everything to bylayer but I needed it to change it to a specific layer after it changed to bylayer .

 

Thanks for the help 

Link to comment
Share on other sites

Try changing this part :

  (if (setq ss (ssget "_:L" '((0 . "INSERT"))))
    (repeat (setq i (sslength ss))
      (BlkByLayer (ssname ss (setq i (1- i))))
      (entupd (ssname ss i))
    )
  )

To something like this :

  (if (setq ss (ssget "_:L" '((0 . "INSERT"))))
    (repeat (setq i (sslength ss))
      (BlkByLayer (ssname ss (setq i (1- i))))
      (entupd (cdr (assoc -1 (entmod (subst (cons 8 "LayerName") (assoc 8 (entget (ssname ss i))) (entget (ssname ss i)))))))
    )
  )
Link to comment
Share on other sites

  • 4 years later...

hi Marko_ribar, I want to do the same trick to change all the nested elements inside a block in layer "0" and color Bylayer , Ltype bylayer etc (all things bylayer).

Can You help Me

 

i also have this lisp 

 

; Written By: Peter Jamtgaard 12/20/2006 modified by Anakozza 05/03/2023
;^P(or C:BBL (load "BBL.lsp"));BBL
(defun C:BBL (/ colBlockReference
                    ActDoc dprSelection
                    objSelection strBlockName
                 )
 (if (setq dprSelection (entsel "\nSelect Block: "))
  (progn
   (setq ActDoc (vla-get-ActiveDocument (vlax-get-Acad-Object))
         dprSelection (car dprSelection)
         objSelection (vlax-ename->vla-object dprSelection)
   )
   (vla-StartUndoMark ActDoc)
   (BlkByLayer objSelection)
   (entupd dprSelection)
   (vla-EndUndoMark ActDoc)
  )
 )
 (prin1)
)

(defun BlkByLayer (objSelection / colBlockReference objBlock
                    strBlockName
                 )
 (if (= (type objSelection) 'ENAME)
  (setq objSelection (vlax-ename->vla-object objSelection)))
 (if (wcmatch (strcase (vla-get-objectname objSelection)) "*BLOCK*")
  (progn
   (vlax-for objBlock (vla-item
                       (vla-get-blocks ActDoc)
                       (vla-get-name objSelection)
                      )

    (vla-put-Color objBlock "ByLayer")
    (vla-put-Layer objBlock "0")
    (vla-put-linetype objBlock "ByLayer")
    (vla-put-Lineweight objBlock -1)
    (vla-put-PlotStyleName objBlock "ByLayer")
   )
  )
 )
 (prin1)
)
(prin1)

Link to comment
Share on other sites

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