+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 18
  1. #1
    Full Member baker's Avatar
    Using
    Civil 3D 2012
    Join Date
    May 2011
    Posts
    57

    Default All blocks to be set to ByLayer (color and linetype)

    Registered forum members do not see this ad.

    I searched and did not really find the results i was looking for.

    I am looking for a Lisp that when called, will change every block in the DWG to ByLayer color and line type.

    I have one i found recently that works, but you have to select each block.

    this is the Lisp i found..

    Code:
        ;   File Name: FIXBLOCK.LSP
        ;   Description: Puts all of a blocks sub-entities on layer 0 with color and
        ;					  linetype set to BYBLOCK. The block, itself, will remain on
        ;					  its' original layer.
        ;
        ;*******************************************************************************
    
    
    (defun d_FixBlock (/             eBlockSel ; Block selection
                       lInsertData ; Entity data
                       sBlockName ; Block name
                       lBlockData ; Entity data
                       eSubEntity ; Sub-entity name
                       lSubData ; Sub-entity data
                       iCount ; Counter
                      )
    
      ;; Redefine error handler
    
      (setq
        d_#error *error*
        *error*  d_FB_Error
      ) ;_ end setq
    
      ;; Set up environment
    
      (setq #SYSVARS (#SaveSysVars (list "cmdecho")))
    
      (setvar "cmdecho" 0)
      (command "._undo" "_group")
    
      ;; Get block from user and make sure it's an INSERT type
    
      (if (setq eBlockSel (entsel "\nSelect block to change :"))
        (progn
          (if (setq lInsertData (entget (car eBlockSel)))
            (if (= (cdr (assoc 0 lInsertData)) "INSERT")
              (setq sBlockName (cdr (assoc 2 lInsertData)))
              (progn
                (alert "Entity selected is not a block!")
                (exit)
              ) ;_ end progn
            ) ;_ end if
            (progn
              (alert "Invalid Block Selection!")
              (exit)
            ) ;_ end progn
          ) ;_ end if
    
          ;; Get block info from the block table
    
          (setq
            lBlockData (tblsearch "BLOCK" sBlockName)
            eSubEntity (cdr (assoc -2 lBlockData))
          ) ;_ end setq
    
          ;; Make sure block is not an Xref
    
          (if (not (assoc 1 lBlockData))
            (progn
              (princ "\nProcessing block: ")
              (princ sBlockName)
    
              (princ "\nUpdating blocks sub-entities. . .")
    
              ;; Parse through all of the blocks sub-entities
    
              (while eSubEntity
    
                (princ " .")
                (setq lSubData (entget eSubEntity))
    
                ;; Update layer property
    
                (if (assoc 8 lSubData)
                  (progn
                    (setq lSubData
                           (subst
                             (cons 8 "0")
                             (assoc 8 lSubData)
                             lSubData
                           ) ;_ end subst
                    ) ;_ end setq
                    (entmod lSubData)
                  ) ;_ end progn
                ) ;_ end if
    
                ;; Update the linetype property
    
                (if (assoc 6 lSubData)
                  (progn
                    (setq lSubData
                           (subst
                             (cons 6 "BYBLOCK")
                             (assoc 6 lSubData)
                             lSubData
                           ) ;_ end subst
                    ) ;_ end setq
                    (entmod lSubData)
                  ) ;_ end progn
                  (entmod (append lSubData (list (cons 6 "BYBLOCK"))))
                ) ;_ end if
    
                ;; Update the color property
    
                (if (assoc 62 lSubData)
                  (progn
                    (setq lSubData
                           (subst
                             (cons 62 0)
                             (assoc 62 lSubData)
                             lSubData
                           ) ;_ end subst
                    ) ;_ end setq
                    (entmod lSubData)
                  ) ;_ end progn
                  (entmod (append lSubData (list (cons 62 0))))
                ) ;_ end if
    
                (setq eSubEntity (entnext eSubEntity))
        ; get next sub entity
    
              ) ; end while
    
              ;; Update attributes
    
              (idc_FB_UpdAttribs)
    
            ) ; end progn
            (alert "XREF selected. Not updated!")
          ) ; end if
        ) ; end progn
        (alert "Nothing selected.")
      ) ; end if
    
    ;;; Pop error stack and reset environment
    
      (idc_RestoreSysVars)
    
      (princ "\nDone!")
    
      (setq *error* d_#error)
    
      (princ)
    
    )   ; end defun
    
        ;*******************************************************************************
        ; Function to update block attributes
        ;*******************************************************************************
    (defun idc_FB_UpdAttribs ()
    
      ;; Update any attribute definitions
    
      (setq iCount 0)
    
      (princ "\nUpdating attributes. . .")
      (if (setq ssInserts (ssget "x"
                                 (list (cons 0 "INSERT")
                                       (cons 66 1)
                                       (cons 2 sBlockName)
                                 ) ;_ end list
                          ) ;_ end ssget
          ) ;_ end setq
        (repeat (sslength ssInserts)
    
          (setq eBlockName (ssname ssInserts iCount))
    
          (if (setq eSubEntity (entnext eBlockName))
            (setq
              lSubData (entget eSubEntity)
              eSubType (cdr (assoc 0 lSubData))
            ) ;_ end setq
          ) ;_ end if
    
          (while (or (= eSubType "ATTRIB") (= eSubType "SEQEND"))
    
            ;; Update layer property
    
            (if (assoc 8 lSubData)
              (progn
                (setq lSubData
                       (subst
                         (cons 8 "0")
                         (assoc 8 lSubData)
                         lSubData
                       ) ;_ end subst
                ) ;_ end setq
                (entmod lSubData)
              ) ;_ end progn
            ) ;_ end if
    
            ;; Update the linetype property
    
            (if (assoc 6 lSubData)
              (progn
                (setq lSubData
                       (subst
                         (cons 6 "BYBLOCK")
                         (assoc 6 lSubData)
                         lSubData
                       ) ;_ end subst
                ) ;_ end setq
                (entmod lSubData)
              ) ;_ end progn
              (entmod (append lSubData (list (cons 6 "BYBLOCK"))))
            ) ;_ end if
    
            ;; Update the color property
    
            (if (assoc 62 lSubData)
              (progn
                (setq lSubData
                       (subst
                         (cons 62 0)
                         (assoc 62 lSubData)
                         lSubData
                       ) ;_ end subst
                ) ;_ end setq
                (entmod lSubData)
              ) ;_ end progn
              (entmod (append lSubData (list (cons 62 0))))
            ) ;_ end if
    
            (if (setq eSubEntity (entnext eSubEntity))
              (setq
                lSubData (entget eSubEntity)
                eSubType (cdr (assoc 0 lSubData))
              ) ;_ end setq
              (setq eSubType nil)
            ) ;_ end if
    
          ) ; end while
    
          (setq iCount (1+ iCount))
    
        ) ; end repeat
    
      ) ; end if
      (command "regen")
    )   ; end defun
    
        ;*******************************************************************************
        ; Function to save a list of system variables
        ;*******************************************************************************
    (defun #SaveSysVars (lVarList / sSystemVar)
      (mapcar
        '(lambda (sSystemVar)
           (setq lSystemVars
                  (append lSystemVars
                          (list (list sSystemVar (getvar sSystemVar)))
                  ) ;_ end append
           ) ;_ end setq
         ) ;_ end lambda
        lVarList
      ) ;_ end mapcar
    
      lSystemVars
    
    ) ;_ end defun
        ;*******************************************************************************
        ; Function to restore a list of system variables
        ;*******************************************************************************
    (defun idc_RestoreSysVars ()
      (mapcar
        '(lambda (sSystemVar)
           (setvar (car sSystemVar) (cadr sSystemVar))
         ) ;_ end lambda
        #SYSVARS
      ) ;_ end mapcar
    ) ;_ end defun
        ;*******************************************************************************
        ; Error Handler
        ;*******************************************************************************
    (defun d_FB_Error (msg)
    
      (princ "\nError occurred in the Fix Block routine...")
      (princ "\nError: ")
      (princ msg)
    
      (setq *error* d_#error)
      (if *error*
        (*error* msg)
      ) ;_ end if
    
      (command)
    
      (if (/= msg "quit / exit abort")
        (progn
          (command "._undo" "_end")
          (command "._u")
        ) ;_ end progn
      ) ;_ end if
    
      (idc_RestoreSysVars)
    
      (princ)
    
    ) ;_ end defun
        ;*******************************************************************************
    
    (defun FB () (d_FixBlock))
    
    (fb)
    (princ)

    also, i cannot get this lisp to work without apploading each time i need it.

  2. #2
    Forum Deity BlackBox's Avatar
    Using
    Civil 3D 2011
    Join Date
    Nov 2009
    Posts
    3,945

    Default

    Why not just use the SETBYLAYER command?
    "Potential has a shelf life." - Margaret Atwood

  3. #3
    Full Member baker's Avatar
    Using
    Civil 3D 2012
    Join Date
    May 2011
    Posts
    57

    Default

    Quote Originally Posted by RenderMan View Post
    Why not just use the SETBYLAYER command?
    because i forgot about that command.. lol.

  4. #4
    Forum Deity BlackBox's Avatar
    Using
    Civil 3D 2011
    Join Date
    Nov 2009
    Posts
    3,945

    Default

    Quote Originally Posted by baker View Post
    because i forgot about that command.. lol.
    lmfao

    ... Happy to help!
    "Potential has a shelf life." - Margaret Atwood

  5. #5
    Forum Deity Tharwat's Avatar
    Discipline
    Mechanical
    Tharwat's Discipline Details
    Occupation
    MEP AutoCAD Draftsman
    Discipline
    Mechanical
    Using
    AutoCAD 2014
    Join Date
    Oct 2009
    Location
    Lives in Abu Dhabi
    Posts
    2,627

    Default

    Quote Originally Posted by baker View Post
    because i forgot about that command.. lol.
    Me too , every time Renderman reminds me of it by his precious words and I keep on forgetting it .

    My routine is ready to delivery but this command hold my energy LOL....
    - When aim is being settled in my mind , I have to reach it and get it in hand whatever it costs and wherever it is and will never give up . Tharwat said

  6. #6
    Super Member David Bethel's Avatar
    Discipline
    Multi-disciplinary
    David Bethel's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Commercial Food Service
    Using
    AutoCAD pre 2000
    Join Date
    Dec 2003
    Location
    Newport News, Virginia
    Posts
    1,926

    Default

    I honestly don't know much about setbyblock, but this should give a bit of flexibility as well


    Code:
    (defun c:blk2def (/ bl tdef fe fd)
    
    ;;;GROUP LIST Group_Number Sysvar_Name New_Value
    
      (setq bl '(( 8 "CLAYER"      "0")
                 ( 6 "CELTYPE"     "BYLAYER")
                 (39 "THICKNESS"    0.0)
                 (48 "CELTSCALE"    1)
                 (62 "CECOLOR"      256)))
    
      (while (setq tdef (tblnext "BLOCK" (not tdef)))
             (setq fe (cdr (assoc -2 tdef)))
             (princ (strcat "\n" (cdr (assoc 2 tdef))))
             (entmake tdef)
             (while fe
               (setq fd (entget fe))
               (foreach g bl
                  (cond ((not (getvar (nth 1 g))))
                        ((assoc (nth 0 g) fd)
                         (setq fd (subst (cons (nth 0 g) (nth 2 g))
                                         (assoc (nth 0 g) fd) fd)))
                        (T
                         (setq fd (append fd (list (cons (nth 0 g) (nth 2 g))))))))
               (entmake fd)
               (setq fe (entnext fe)))
             (entmake (list (cons 0 "ENDBLK")(cons 8 "0"))))
    
      (command "_.REGENALL")
      (prin1))

    You can manipulate the list bl to your needs.
    • SNVALID Layer Name
    • Linetypes must exist prior to calling this for it work properly.
    • Colors 0-256

    It does not address existing ATTRIButes.


    -David
    R12 (Dos) - A2K

  7. #7
    Full Member baker's Avatar
    Using
    Civil 3D 2012
    Join Date
    May 2011
    Posts
    57

    Default

    ahhhh... now i remember.. i want the linework in the blocks to move to layer 0 as well as bylayer for everything else..

  8. #8
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,718

    Default

    Give this a try:

    Code:
    (defun c:test ( / acdoc )
    
        ;;-----------------------------------------------------------
        ;; All block objects to Layer "0", Color/Linetype ByLayer
        ;; Lee Mac 2011  -  www.lee-mac.com
        ;;-----------------------------------------------------------
      
        (vlax-for block
            (vla-get-blocks
                (setq acdoc (vla-get-activedocument (vlax-get-acad-object)))
            )
            (if
                (and
                    (eq :vlax-false (vla-get-islayout block))
                    (eq :vlax-false (vla-get-isxref block))
                )
                (vlax-for object block
                    (mapcar
                        (function
                            (lambda ( property value )
                                (vl-catch-all-apply 'vlax-put-property (list object property value))
                            )
                        )
                       '(layer color linetype)
                        (list "0" acbylayer "BYLAYER")
                    )
                )
            )
        )
        (vla-regen acdoc acallviewports)
        (princ)
    )
    (vl-load-com) (princ)
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  9. #9
    Full Member baker's Avatar
    Using
    Civil 3D 2012
    Join Date
    May 2011
    Posts
    57

    Default

    Lee,

    you are going to kill me. That is great and i will use that lisp.. Is there a way to have another lisp (or the option in this lisp) to put the block's objects to the layer the block is inserted to?

  10. #10
    Forum Deity BlackBox's Avatar
    Using
    Civil 3D 2011
    Join Date
    Nov 2009
    Posts
    3,945

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by baker View Post
    Lee,

    you are going to kill me. That is great and i will use that lisp.. Is there a way to have another lisp (or the option in this lisp) to put the block's objects to the layer the block is inserted to?
    Consider replacing this line:

    Code:
    (list "0" acbylayer "BYLAYER")
    ... with this one:

    Code:
    (list (vla-get-layer object) acbylayer "BYLAYER")
    "Potential has a shelf life." - Margaret Atwood

Similar Threads

  1. Default color of [ Bylayer ]
    By basiony in forum AutoCAD Beginners' Area
    Replies: 1
    Last Post: 23rd Nov 2010, 08:41 am
  2. Lisp to change linetype in block bylayer
    By mxpxh2o in forum AutoLISP, Visual LISP & DCL
    Replies: 12
    Last Post: 7th Jun 2010, 04:13 pm
  3. color bylayer
    By woodman78 in forum AutoLISP, Visual LISP & DCL
    Replies: 1
    Last Post: 16th Oct 2009, 02:07 pm
  4. from bylayer to by color
    By russell84 in forum AutoLISP, Visual LISP & DCL
    Replies: 7
    Last Post: 17th Jul 2008, 02:59 pm
  5. set color & lintype back to bylayer after drawing a line
    By raj banerjee in forum AutoLISP, Visual LISP & DCL
    Replies: 1
    Last Post: 19th Sep 2007, 10:16 am

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts