Jump to content

lisp to change dynamic block visibility


woodman78

Recommended Posts

I am trying to change block visibility by lisp and have cobbled this together. It doesn't work though and I was wondering if someone could have a look for me. Thanks.

 

 
(defun c:Legend_Scale (/ LName BWild obj)
 (setq LName "dyn_legend") ;Get the layer's name
       (setq BWild (strcat "*" LName "*")) ;Default to LName
         (setq obj (ssget "_X" (list '(0 . "INSERT") (cons 2 BWild)))) ;And some blocks found
(chgdynprop obj "visibility" "1:1000")
(entupd obj)
(princ)
)
(defun chgdynprop (e propname newval / obj v vval sal tot i)
(setq obj (if (= (type e) 'vla-object) e (vlax-ename->vla-object e)))
(if (= (vlax-get-property obj 'isdynamicblock) :vlax-true)
(progn
(setq v (vla-getdynamicblockproperties obj)
vval (vlax-variant-value v)
sal (vlax-safearray->list vval)
tot (length sal)
i 0
);setq
(while (< i tot)
(if (= (vlax-get-property (nth i sal) "PropertyName")
propname)
(progn
(vlax-put-property (nth i sal) "Value" newval)
(setq i tot)
);progn
(setq i (1+ i))
);endif
);endwhile
);progn
);endif
);chgdynprop

Link to comment
Share on other sites

Give this a try:

 

(defun c:FOO ( / ss dynProps val)
 (vl-load-com)
 (if (setq ss (ssget "_x" '((0 . "INSERT") ([color=blue]2[/color] . "[color=blue]*[/color]DYN_LEGEND[color=blue]*[/color]"))))
   (progn
     (vla-startundomark
       (cond (*activeDoc*)
             ((setq *activeDoc*
                     (vla-get-activedocument
                       (vlax-get-acad-object))))))
     (vlax-for x (setq ss (vla-get-activeselectionset *activeDoc*))
       (if (and (= :vlax-true (vla-get-isdynamicblock x))
                (vl-string-search
                  "DYN_LEGEND"
                  (strcase (vla-get-effectivename x)))
                (setq dynProps (car (vlax-invoke
                                      x
                                      'getdynamicblockproperties)))
                (vl-position
                  (cond (val) ((setq val "1:1000")))
                  (vlax-get dynProps 'allowedvalues)))
          (vlax-put-property dynProps 'value val)))
     (vla-endundomark *activeDoc*)
     (vla-delete ss))
   (prompt "\n** Nothing selected ** "))
 (princ)) 

 

Note that if val ("1:1000") is not included in the dynamic block's AllowedValues, no action is taken.

Edited by BlackBox
Code revised
Link to comment
Share on other sites

Thanks RenderMan. I keep getting "nothing selected" though. I have tried renaming blocks and different things but no joy. Any ideas?

Link to comment
Share on other sites

Thanks RenderMan. I keep getting "nothing selected" though. I have tried renaming blocks and different things but no joy. Any ideas?

 

That means you have no blocks on the "DYN_LEGEND" layer. Perhaps I misinterpreted your original code... I took this line:

 

 
(defun c:Legend_Scale (/ LName BWild obj)
(setq LName "dyn_legend") ;Get the layer's name

 

... To mean that would be the block's layer name? Thus, I included this:

 

(if (setq ss (ssget "_x" '((0 . "INSERT") ([color=red]8 . "DYN_LEGEND"[/color]))))

 

... Where DXF code 8 is the entity's layer name. Try replacing the code in red with this:

 

(2 . "*DYN_LEGEND*")

 

... Otherwise, we'll just step through an SS of all blocks.

Link to comment
Share on other sites

  • 1 year later...

Hey guys,

 

I’m just discovering these visibility states of dynamic blocks myself, pretty interesting and could be very useful.

 

I’m just in the process of customizing a clients drawing sheet for use in our office and and just trying to understand how I can batch toggle the visibility state of a block in multiple drawings.

 

The routine above looks like it should do the trick – but unfortunately when I run it – the result is “** Nothing selected **”.

 

If I remove the block name filter from the selection set, the routine seems to run fine.

 

My working code is…

 

(defun c:test1 ( / ss dynProps val)
 (vl-load-com)
 (if (setq ss (ssget "_x" '((0 . "INSERT"))))
   (progn
     (vla-startundomark
       (cond (*activeDoc*)
             ((setq *activeDoc*
                     (vla-get-activedocument
                       (vlax-get-acad-object))))))
     (vlax-for x (setq ss (vla-get-activeselectionset *activeDoc*))
       (if (and (= :vlax-true (vla-get-isdynamicblock x))
                (vl-string-search
                  "4057_STICKER"
                  (strcase (vla-get-effectivename x)))
                (setq dynProps (car (vlax-invoke
                                      x
                                      'getdynamicblockproperties)))
                (vl-position
                  (cond (val) ((setq val "For Client Review")))
                  (vlax-get dynProps 'allowedvalues)))
          (vlax-put-property dynProps 'value val)))
     (vla-endundomark *activeDoc*)
     (vla-delete ss))
   (prompt "\n** Nothing selected ** "))
 (princ))

 

But I cannot understand why it doesn’t detect my block named "4057_STICKER". Does it have anything to do with the Anonymous Name?

 

Thanks for all your help.

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