Jump to content

Redefine blocks in drawing by re-inserting them from a folder


Recommended Posts

Posted (edited)

MyBlocks.zip

(defun c:foo ( / a1 a2 oldCMDECHO s r pt folderPath BlockNames oldEnt newEnt blk )
  (setq oldCMDECHO (getvar "CMDECHO")
        a1 (getvar "ATTDIA")
        a2 (getvar "ATTREQ")
        pt (list 0.0 0.0)
        s 0.5
        r 0.0)
  (setvar "CMDECHO" 0)
  (setvar "ATTDIA" 0)
  (setvar "ATTREQ" 0)

  (setq folderPath "C:/MyBlocks/" BlockNames (list "BLK1.dwg" "BLK2.dwg" "BLK3.dwg"))

  (setq oldEnt (entlast))

  (foreach blk BlockNames
    (setq blk (strcat folderPath blk))
    (command "_.-insert" blk pt s s r)
    (setq newEnt (entlast))
    (if (and newEnt (/= oldEnt newEnt))
      (entdel newEnt)
    )
  )

  (setvar "ATTDIA" a1)
  (setvar "ATTREQ" a2)
  (setvar "CMDECHO" oldCMDECHO)

  (princ "\nBlocks inserted successfully.")
  (princ)
)
Edited by DavidP
Attach Blocks
Posted (edited)

I have drawing with blocks some of which have be change by the drafters... I want to re-insert & redefine the blocks from a local folder.

What I'm I missing?  My blocks are Dynamic blocks.

Edited by DavidP
Added additional info
Posted

you may try to change:

(command "_.-insert" blk pt s s r)

(setq newEnt (entlast))

(if (and newEnt (/= oldEnt newEnt)) (entdel newEnt) )

to

(command "_.-insert" (strcat (vl-filename-base blk) "=" blk) pt)

(while (> (getvar "CMDACTIVE") 0) (command ""))

(entdel(entlast)) ; Delete the temp insert block

 

This will force AutoCAD to update a block from a drawing. you will need to make sure the block name is same as the DWG's name.

Since you are working wirth dynamic blocks, the safer way is to get the dynamic property values for each block reference before update it and apply the data back to blocks after the definition is updated, in case dynamic parameters may have been changed in definition.

Posted

Hi @DavidP

 

From this part of code, you're file path is incorrect. Instend of "C:/MyBlocks/" need to be "C:/Users/"Your Name"/Desktop/MyBlocks/" or do Support File Search Path in Option -> File inside the AutoCAD to add that folder "MyBlocks".

 

10 hours ago, DavidP said:
(setq folderPath "C:/MyBlocks/" BlockNames (list "BLK1.dwg" "BLK2.dwg" "BLK3.dwg"))

 

From this part of code, if I open the blank drawing where the nothing is inside, the inserted blocks "BLK1, BLK2 and BLK3" will be erased, because there isn't "oldEnt" inside the drawing.

 

10 hours ago, DavidP said:
(if (and newEnt (/= oldEnt newEnt))
      (entdel newEnt)
    )

 

So, can you provide the drawing where you want to insert desired blocks?

Posted

Found it -

;; Redefine All Blocks  -  Lee Mac
(defun c:redefall ( / bln dir doc dwg lst obj org spc )

    (setq dir nil) ;; Directory of Block Library; nil to use Support Path

    (if dir
        (setq dir (strcat (vl-string-right-trim "\\" (vl-string-translate "/" "\\" dir)) "\\"))
        (setq dir "")
    )
    (cond
        (   (= 4 (logand 4 (cdr (assoc 70 (tblsearch "layer" (getvar 'clayer))))))
            (princ "\nCurrent layer locked.")
        )
        (   (setq doc (vla-get-activedocument (vlax-get-acad-object))
                  spc (vla-get-modelspace doc)
                  org (vlax-3D-point 0 0)
            )
            (vlax-for blk (vla-get-blocks doc)
                (if
                    (and
                        (= :vlax-false (vla-get-isxref blk))
                        (= :vlax-false (vla-get-islayout blk))
                        (not (wcmatch (setq bln (vla-get-name blk)) "`**,_*,*|*"))
                        (setq dwg (findfile (strcat dir bln ".dwg")))
                    )
                    (progn
                        (setq obj (vla-insertblock spc org dwg 1.0 1.0 1.0 0.0))
                        (if (= :vlax-true (vla-get-hasattributes obj))
                            (setq lst (vl-list* "," bln lst))
                        )
                        (vla-delete obj)
                    )
                )
            )
            (if lst (vl-cmdf "_.attsync" "_N" (apply 'strcat (cdr lst))))
            (vla-regen doc acallviewports)
        )
    )
    (princ)
)
(vl-load-com) (princ)

 

 

Posted

That works perfect Lee that's exactly what I was looking for...   

 

Regarding the questions about paths, the reason I chose to pass the path is because we complete drawings for various clients and most of them use similar or even the same  block names with minor graphical variations, in some cases may just be attribute tag names, so I want to ensure to re-insert from that client's directory. 

 

 

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