Jump to content

Replacing a Dynamic Block with another


JoeyG_77

Recommended Posts

Hey everyone .. Anyone know or have a lisp that will replace one dynamic block with another and take its properties if all of the criteria matches in that block.

 

Thanks

Joey G

Link to comment
Share on other sites

Lee-mac has a good dynamic block lisp to get the original properties like dynamic name and value. Then erase block insert new and use those values in a new block.

 

You could do a match list of dyn names ((dyn1 dynx)(dyn2 dyny)….. if same order of creation even easier.

Link to comment
Share on other sites

Not sure if it works for every prop that you need, but try this

 

Command RDP

 


;; http://www.lee-mac.com/dynamicblockfunctions.html

;; Get Dynamic Block Properties  -  Lee Mac
;; Returns an association list of Dynamic Block properties & values.
;; blk - [vla] VLA Dynamic Block Reference object
;; Returns: [lst] Association list of ((<prop> . <value>) ... )
(defun LM:getdynprops ( blk )
    (mapcar '(lambda ( x ) (cons (vla-get-propertyname x) (vlax-get x 'value)))
        (vlax-invoke blk 'getdynamicblockproperties)
    )
)

;; Set Dynamic Block Properties  -  Lee Mac
;; Modifies values of Dynamic Block properties using a supplied association list.
;; blk - [vla] VLA Dynamic Block Reference object
;; lst - [lst] Association list of ((<Property> . <Value>) ... )
;; Returns: nil
(defun LM:setdynprops ( blk lst / itm )
    (setq lst (mapcar '(lambda ( x ) (cons (strcase (car x)) (cdr x))) lst))
    (foreach x (vlax-invoke blk 'getdynamicblockproperties)
        (if (setq itm (assoc (strcase (vla-get-propertyname x)) lst))
            (vla-put-value x (vlax-make-variant (cdr itm) (vlax-variant-type (vla-get-value x))))
        )
    )
)

;;;;;;;;;;;;;;;;;;;;;;;;;

;; https://www.cadtutor.net/forum/topic/18257-entmake-functions/
(defun drawInsert (pt Nme)
 (entmakex (list (cons 0 "INSERT")
                 (cons 2 Nme)
                 (cons 10 pt)))
)

;;;;;;;;;;;;;;;;;;;;;;;;;

;; returns the common props, so we don't set props that don't exist on the destination block
(defun common_props (props1 props2 / res)
  (setq res (list))
  (foreach a props1
    ;; see if that prop exists in props2
    (if (assoc (car a) props2)   ;; skip "Origin"
      (if (/= "Origin" (car a))
        (setq res (append res (list a)))
      )
    )
  )
  res
)

;; RDP for Replace Dynamic Block
(defun c:rdp ( / blk props1 props2 cprops bname pt dblok)
  ;; read source block
  (setq blk (car (entsel "\nSelect source block: ")))
  (setq props1 (LM:getdynprops  (vlax-ename->vla-object blk)))
  ;; insert destination block
  (setq bname (getstring "\nEnter blockname of the destination block: "))
  (setq pt (cdr (assoc 10 (entget blk))))
  (setq dblk (drawInsert pt bname))
  (setq props2 (LM:getdynprops  (vlax-ename->vla-object dblk)))
  ;; set common props
  (setq cprops (common_props props1 props2))
  (LM:setdynprops (vlax-ename->vla-object dblk) cprops)
  ;; delete source
  (entdel blk)
  (princ)
)

Link to comment
Share on other sites

  • 3 years later...
On 11/14/2019 at 3:08 PM, Emmanuel Delay said:

This replaces the dynamic block but does not match block properties like rotate, etc. Can you please help.

 

 

;; http://www.lee-mac.com/dynamicblockfunctions.html

;; Get Dynamic Block Properties  -  Lee Mac
;; Returns an association list of Dynamic Block properties & values.
;; blk - [vla] VLA Dynamic Block Reference object
;; Returns: [lst] Association list of ((<prop> . <value>) ... )
(defun LM:getdynprops ( blk )
    (mapcar '(lambda ( x ) (cons (vla-get-propertyname x) (vlax-get x 'value)))
        (vlax-invoke blk 'getdynamicblockproperties)
    )
)

;; Set Dynamic Block Properties  -  Lee Mac
;; Modifies values of Dynamic Block properties using a supplied association list.
;; blk - [vla] VLA Dynamic Block Reference object
;; lst - [lst] Association list of ((<Property> . <Value>) ... )
;; Returns: nil
(defun LM:setdynprops ( blk lst / itm )
    (setq lst (mapcar '(lambda ( x ) (cons (strcase (car x)) (cdr x))) lst))
    (foreach x (vlax-invoke blk 'getdynamicblockproperties)
        (if (setq itm (assoc (strcase (vla-get-propertyname x)) lst))
            (vla-put-value x (vlax-make-variant (cdr itm) (vlax-variant-type (vla-get-value x))))
        )
    )
)

;;;;;;;;;;;;;;;;;;;;;;;;;

;; https://www.cadtutor.net/forum/topic/18257-entmake-functions/
(defun drawInsert (pt Nme)
 (entmakex (list (cons 0 "INSERT")
                 (cons 2 Nme)
                 (cons 10 pt)))
)

;;;;;;;;;;;;;;;;;;;;;;;;;

;; returns the common props, so we don't set props that don't exist on the destination block
(defun common_props (props1 props2 / res)
  (setq res (list))
  (foreach a props1
    ;; see if that prop exists in props2
    (if (assoc (car a) props2)   ;; skip "Origin"
      (if (/= "Origin" (car a))
        (setq res (append res (list a)))
      )
    )
  )
  res
)

;; RDP for Replace Dynamic Block
(defun c:rdp ( / blk props1 props2 cprops bname pt dblok)
  ;; read source block
  (setq blk (car (entsel "\nSelect source block: ")))
  (setq props1 (LM:getdynprops  (vlax-ename->vla-object blk)))
  ;; insert destination block
  (setq bname (getstring "\nEnter blockname of the destination block: "))
  (setq pt (cdr (assoc 10 (entget blk))))
  (setq dblk (drawInsert pt bname))
  (setq props2 (LM:getdynprops  (vlax-ename->vla-object dblk)))
  ;; set common props
  (setq cprops (common_props props1 props2))
  (LM:setdynprops (vlax-ename->vla-object dblk) cprops)
  ;; delete source
  (entdel blk)
  (princ)
)

 

 

Link to comment
Share on other sites

Ahh, the question is in the quote I think

 

"This replaces the dynamic block but does not match block properties like rotate, etc. Can you please help."

Link to comment
Share on other sites

@Emmanuel @ Lee Mac Thank you for extending the help.

I am attaching a drawing of the blocks which we use for our daily work.

"PRE-D1200" is the block used for preliminary drawings. 

After finalization of door types, we would like to replace these blocks with DSL-1200.

I would like to replace the selected blocks in one go. 

Please help.

 

 

Sample Block Replace.dwg

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