Jump to content

Change Color of Dynamic Block Sub Entities


handasa

Recommended Posts

Hello Everyone

i use to this lisp to change color of block's subentities .

(defun colorb (x0 / doc blk)
(setq col (cdr (assoc 62 (acad_truecolordlg 2))))
 (setq doc (vla-get-activedocument (vlax-get-acad-object)))
 (setq blk (vla-item (vla-get-blocks doc) (vla-get-Effectivename x0)))
 (vlax-for x blk
  (if (eq (vla-get-objectname x) "AcDbBlockReference")
   (progn (colorb x)) ; recursion
   (vla-put-color x col)
  )
 )
)

it works well for regular blocks but in case of dynamic blocks the new color won't take effect until i use "bedit" command  like this

(defun update (block)
(setq blockname (LM:al-effectivename block))
(COMMAND "-BEDIT" "" blockname)
(command "zoom" "e")
(COMMAND "_BCLOSE" "SAVE")
)

any ideas to change dynamic block subentities without using the bedit command 

thanks in advance

Link to comment
Share on other sites

12 minutes ago, Tharwat said:

Hi,

Replace the function vla-get-effectivename with vla-get-name and try again.

this didn't change the color of the block subentities 

Edited by handasa
Link to comment
Share on other sites

Just now, Tharwat said:

You may need to regen the active space / drawing to see the changes.

 

regen didn't work also.the only way to have the changes take effect is to bedit the block and zoom inot it and exit the bedit

Link to comment
Share on other sites

2 hours ago, handasa said:

(defun update (block / blockname)
(setq blockname (LM:al-effectivename block))
(vl-cmdf "_.attsync" "_N" blockname)
)

 

 

Try the above

Edited by dlanorh
Link to comment
Share on other sites

7 minutes ago, dlanorh said:

 

Try the above

block doesn't have attribute definitions.so this command gives error _N Unknown command "N" 

Link to comment
Share on other sites

The only other way i can think of without opening the block editor is to ssget "_X"  the blocks in the drawing, and iterate the pickset filtering blocks with the same effective name into a list.

Then for each block in the list  get ALL the dynamic block properties and values, use the resetblock command and then reinstate the saved dynamic block property values.

 

Example functions to do this can be found HERE

 

This is also worth a  READ

 

Link to comment
Share on other sites

Strange that a block definition does not have an EffectiveName property.

But with some 'classic' Lisp you can create a workaround.

I also tried to fix some issues with the recursion.


; Blk can be a block name or the ename of a "BLOCK" entity.
(defun KGA_BlockClassic_EffectiveName (blk / elst blkRecHnd)
  (setq elst
    (entget (if (= 'ename (type blk)) blk (tblobjname "block" blk)))
  )
  (if
    (and
      (= "*" (substr (cdr (assoc 2 elst)) 1 1))
      (setq blkRecHnd (cdr (assoc 1005 (cdadr (assoc -3 (entget (cdr (assoc 330 elst)) '("AcDbBlockRepBTag")))))))
    )
    (cdr (assoc 2 (entget (handent blkRecHnd))))
    (cdr (assoc 2 elst))
  )
)

; (colorb (vla-get-name (vlax-ename->vla-object (car (entsel)))) (acad_colordlg 2))
(defun colorb (nme col / N_Mod blks i nmeLst)

  (defun N_Mod (blk)
    (vlax-for obj blk
      (if
        (and
          (= "AcDbBlockReference" (vla-get-objectname obj))
          (not (vl-position (strcase (vla-get-effectivename obj)) nmeLst))
        )
        (setq nmeLst (append nmeLst (list (strcase (vla-get-effectivename obj))))) ; Append required.
        (vla-put-color obj col)
      )
    )
  )

  (setq blks (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))))
  (setq nmeLst (list (strcase (KGA_BlockClassic_EffectiveName nme))))
  (setq i 0)
  (while (< i (length nmeLst))
    (setq nme (nth i nmeLst))
    (vlax-for blk blks
      (if (= nme (strcase (KGA_BlockClassic_EffectiveName (vla-get-name blk))))
        (N_Mod blk)
      )
    )
    (setq i (1+ i))
  )
)

 

Edited by Roy_043
  • Thanks 1
Link to comment
Share on other sites

54 minutes ago, Roy_043 said:

Strange that a block definition does not have an EffectiveName property.

But with some 'classic' Lisp you can create a workaround.

I also tried to fix some issues with the recursion.


; Blk can be a block name or the ename of a "BLOCK" entity.
(defun KGA_BlockClassic_EffectiveName (blk / elst blkRecHnd)
  (setq elst
    (entget (if (= 'ename (type blk)) blk (tblobjname "block" blk)))
  )
  (if
    (and
      (= "*" (substr (cdr (assoc 2 elst)) 1 1))
      (setq blkRecHnd (cdr (assoc 1005 (cdadr (assoc -3 (entget (cdr (assoc 330 elst)) '("AcDbBlockRepBTag")))))))
    )
    (cdr (assoc 2 (entget (handent blkRecHnd))))
    (cdr (assoc 2 elst))
  )
)

; (colorb (vla-get-name (vlax-ename->vla-object (car (entsel)))))
(defun colorb (nme / N_Mod blks col i nmeLst)

  (defun N_Mod (blk)
    (vlax-for obj blk
      (if
        (and
          (= "AcDbBlockReference" (vla-get-objectname obj))
          (not (vl-position (strcase (vla-get-effectivename obj)) nmeLst))
        )
        (setq nmeLst (append nmeLst (list (strcase (vla-get-effectivename obj))))) ; Append required.
        (vla-put-color obj col)
      )
    )
  )

  (setq blks (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))))
  (setq col (acad_colordlg 2))
  (setq nmeLst (list (strcase (KGA_BlockClassic_EffectiveName nme))))
  (setq i 0)
  (while (< i (length nmeLst))
    (setq nme (nth i nmeLst))
    (vlax-for blk blks
      (if (= nme (strcase (KGA_BlockClassic_EffectiveName (vla-get-name blk))))
        (N_Mod blk)
      )
    )
    (setq i (1+ i))
  )
)

 

 

this is what happen when using the code above

 

Link to comment
Share on other sites

Seems like a simple regen issue. Notice that the main function is not a command ('C:') function.

I have renamed the main function and added an example of a command function:

; Blk can be a block name or the ename of a "BLOCK" entity.
(defun KGA_BlockClassic_EffectiveName (blk / elst blkRecHnd)
  (setq elst
    (entget (if (= 'ename (type blk)) blk (tblobjname "block" blk)))
  )
  (if
    (and
      (= "*" (substr (cdr (assoc 2 elst)) 1 1))
      (setq blkRecHnd (cdr (assoc 1005 (cdadr (assoc -3 (entget (cdr (assoc 330 elst)) '("AcDbBlockRepBTag")))))))
    )
    (cdr (assoc 2 (entget (handent blkRecHnd))))
    (cdr (assoc 2 elst))
  )
)

; (ChangeDynBlockColor (vla-get-name (vlax-ename->vla-object (car (entsel)))) (acad_colordlg 2))
(defun ChangeDynBlockColor (nme col / N_Mod blks i nmeLst)

  (defun N_Mod (blk)
    (vlax-for obj blk
      (if
        (and
          (= "AcDbBlockReference" (vla-get-objectname obj))
          (not (vl-position (strcase (vla-get-effectivename obj)) nmeLst))
        )
        (setq nmeLst (append nmeLst (list (strcase (vla-get-effectivename obj))))) ; Append required.
        (vla-put-color obj col)
      )
    )
  )

  (setq blks (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))))
  (setq nmeLst (list (strcase (KGA_BlockClassic_EffectiveName nme))))
  (setq i 0)
  (while (< i (length nmeLst))
    (setq nme (nth i nmeLst))
    (vlax-for blk blks
      (if (= nme (strcase (KGA_BlockClassic_EffectiveName (vla-get-name blk))))
        (N_Mod blk)
      )
    )
    (setq i (1+ i))
  )
)

(defun c:Test ( / col doc enm obj)
  (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  (vla-endundomark doc)
  (vla-startundomark doc)
  (if
    (and
      (setq enm (car (entsel)))
      (setq obj (vlax-ename->vla-object enm))
      (= "AcDbBlockReference" (vla-get-objectname obj))
      (setq col (acad_colordlg 2))
    )
    (progn
      (ChangeDynBlockColor (vla-get-name obj) col)
      (vla-regen doc acactiveviewport)
    )
  )
  (vla-endundomark doc)
  (princ)
)

 

  • Thanks 2
Link to comment
Share on other sites

29 minutes ago, Roy_043 said:

Seems like a simple regen issue. Notice that the main function is not a command ('C:') function.

I have renamed the main function and added an example of a command function:


; Blk can be a block name or the ename of a "BLOCK" entity.
(defun KGA_BlockClassic_EffectiveName (blk / elst blkRecHnd)
  (setq elst
    (entget (if (= 'ename (type blk)) blk (tblobjname "block" blk)))
  )
  (if
    (and
      (= "*" (substr (cdr (assoc 2 elst)) 1 1))
      (setq blkRecHnd (cdr (assoc 1005 (cdadr (assoc -3 (entget (cdr (assoc 330 elst)) '("AcDbBlockRepBTag")))))))
    )
    (cdr (assoc 2 (entget (handent blkRecHnd))))
    (cdr (assoc 2 elst))
  )
)

; (ChangeDynBlockColor (vla-get-name (vlax-ename->vla-object (car (entsel)))) (acad_colordlg 2))
(defun ChangeDynBlockColor (nme col / N_Mod blks i nmeLst)

  (defun N_Mod (blk)
    (vlax-for obj blk
      (if
        (and
          (= "AcDbBlockReference" (vla-get-objectname obj))
          (not (vl-position (strcase (vla-get-effectivename obj)) nmeLst))
        )
        (setq nmeLst (append nmeLst (list (strcase (vla-get-effectivename obj))))) ; Append required.
        (vla-put-color obj col)
      )
    )
  )

  (setq blks (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))))
  (setq nmeLst (list (strcase (KGA_BlockClassic_EffectiveName nme))))
  (setq i 0)
  (while (< i (length nmeLst))
    (setq nme (nth i nmeLst))
    (vlax-for blk blks
      (if (= nme (strcase (KGA_BlockClassic_EffectiveName (vla-get-name blk))))
        (N_Mod blk)
      )
    )
    (setq i (1+ i))
  )
)

(defun c:Test ( / col doc enm obj)
  (setq doc (vla-get-activedocument (vlax-get-acad-object)))
  (vla-endundomark doc)
  (vla-startundomark doc)
  (if
    (and
      (setq enm (car (entsel)))
      (setq obj (vlax-ename->vla-object enm))
      (= "AcDbBlockReference" (vla-get-objectname obj))
      (setq col (acad_colordlg 2))
    )
    (progn
      (ChangeDynBlockColor (vla-get-name obj) col)
      (vla-regen doc acactiveviewport)
    )
  )
  (vla-endundomark doc)
  (princ)
)

 

this worked fine..thank you very much ,Roy 

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