Jump to content

Convert dynamic block with visibility to static


yann.insa

Recommended Posts

Hello everyone,

I’m experiencing an issue with a block created by dlanorh. It seems not to work when dealing with dynamic blocks without attributes. I reached out to dlanorh for improvements, but it appears they have been inactive on the AutoCAD forum since 2021.

I want to use the undynamic script, but something goes wrong with the 2023 version of AutoCAD. It says "Nothing Found," even though a visibility state is present. I need the visibility state to be exploded in the block name.

I believe the issue occurs when I use a block without attributes (like the file attached) and the line (setq ss (ssget "_A" '((0 . "INSERT") (66 . 1) (410 . "Model")))). However, when I change this line, I also encounter a problem with the dbe function, which I don’t understand.

Any assistance would be greatly appreciated!

(defun LM:getvisibilityparametername ( blk / vis )
    (if (and (vlax-property-available-p blk 'effectivename)
             (setq blk (vla-item (vla-get-blocks (vla-get-document blk)) (vla-get-effectivename blk)))
             (= :vlax-true (vla-get-isdynamicblock blk))
             (= :vlax-true (vla-get-hasextensiondictionary blk))
             (setq vis (vl-some '(lambda (pair) (if (and (= 360 (car pair)) (= "BLOCKVISIBILITYPARAMETER" (cdr (assoc 0 (entget (cdr pair)))))) (cdr pair)))
                         (dictsearch (vlax-vla-object->ename (vla-getextensiondictionary blk)) "ACAD_ENHANCEDBLOCK")
                       )
             )
        )
        (cdr (assoc 301 (entget vis)))
    )
);end_defun

(defun LM:getvisibilitystate ( blk / vis ) (if (setq vis (LM:getvisibilityparametername blk)) (LM:getdynpropvalue blk vis)))

(defun LM:setdynpropvalue ( blk prp val )
    (setq prp (strcase prp))
    (vl-some '(lambda ( x ) (if (= prp (strcase (vla-get-propertyname x))) (vlax-put x 'value val))) (vlax-invoke blk 'getdynamicblockproperties)
    )
);end_defun

(defun LM:getdynpropvalue ( blk prp )
  (setq prp (strcase prp))
  (vl-some '(lambda ( x ) (if (= prp (strcase (vla-get-propertyname x))) (vlax-get x 'value))) (vlax-invoke blk 'getdynamicblockproperties))
);end_defun

(defun rh:has_tag (o tg) (vl-some '(lambda (x) (= tg (strcase (vla-get-tagstring x)))) (vlax-invoke o 'getattributes)))

(defun rh:dbe ( blk doc lyr )
  (vlax-for obj (vla-item (vlax-get-property doc 'blocks) blk)
    (cond ( (and (/= (vla-get-layer obj) lyr)
                 (not (wcmatch (strcase (vlax-get-property obj 'objectname)) "*ENTITY*"))
            )
            (vla-delete obj)
          )
    )
  )
);end_defun

(vl-load-com)

;Dynamic to Static Block By visibility state
(defun c:d2sbv ( / *error* c_doc c_spc sv_lst sv_vals tag flg obj o d vn en prp val f ss cnt obj bn ang vn ipt n_obj nbn)

  (defun *error* ( msg )
    (mapcar 'setvar sv_lst sv_vals)
    (if (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")) (princ (strcat "\nAn Error : " msg " occurred.")))
    (princ)
  );end_local_*error*_defun

  (setq c_doc (vlax-get-property (vlax-get-acad-object) 'activedocument)
        c_spc (vlax-get-property c_doc (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace))
        sv_lst (list 'cmdecho 'osmode)
        sv_vals (mapcar 'getvar sv_lst)
  );end_setq

  (mapcar 'setvar sv_lst '(0 0))

;user settings
  (setq tag "STATION-NO")

  (while (not flg)
    (setq obj (vlax-ename->vla-object (car (entsel "\nSelect One of the Dynamic Blocks to Convert : "))))
    (cond ( (and (= (setq o (vlax-get obj 'objectname)) "AcDbBlockReference")
                 (= :vlax-true (setq d (vlax-get-property obj 'isdynamicblock)))
                 (setq vn (LM:getvisibilitystate obj))
            );end_and
            (setq en (vlax-get obj 'effectivename) flg T prp (LM:getvisibilityparametername obj))
            (cond ( (or (= :vlax-false (vlax-property-available-p obj 'hasattributes)) (not tag) (not (rh:has_tag obj tag))) (setq val 0)))
          )
          (t 
            (cond ( (/= o "AcDbBlockReference") (setq msg "NOT a Block"))
                  ( (= d :vlax-false) (setq msg "NOT a Dynamic Block"))
                  ( (not vn) (setq msg "Block has NO Visibility Parameter"))
            );end_cond
            (alert msg)
          )
    );end_cond
  );end_while

  (setq ss (ssget "_A" '((0 . "INSERT") (66 . 1) (410 . "Model"))))
  (cond (ss
          (repeat (setq cnt (sslength ss))
            (setq obj (vlax-ename->vla-object (ssname ss (setq cnt (1- cnt))))
                  bn (vlax-get obj 'effectivename)
            );end_setq

            (cond ( (= bn en)
                    (setq ang (lm:getdynpropvalue obj "angle1")
                          vn (LM:getvisibilitystate obj)
                          ipt (vlax-get obj 'insertionpoint)
                          n_obj (vlax-invoke c_spc 'insertblock ipt en 1 1 1 0)
                    );end_setq

                    (LM:setdynpropvalue n_obj prp vn)

                    (if (numberp val)
                      (setq val (1+ val) nbn (strcat vn "_" (itoa val)))
                      (setq val (vl-some '(lambda (x) (if (= tag (strcase (vla-get-tagstring x))) (vla-get-textstring x))) (vlax-invoke obj 'getattributes))
                            nbn (strcat vn "_" val)
                      );end_setq
                    );end_if

                    (vla-converttostaticblock n_obj nbn)
                    (rh:dbe nbn c_doc "0")
                    (vla-delete n_obj)
                    (setq n_obj (vlax-invoke c_spc 'insertblock ipt nbn 1 1 1 ang))
                    (mapcar '(lambda (x) (vlax-put-property n_obj x (vlax-get-property obj x))) (list 'layer 'truecolor 'xscalefactor 'yscalefactor 'zscalefactor))
                    (vla-delete obj)
                  )
            );end_cond
          );end_repeat
        )
        (t (alert "Nothing Found"))
  );end_cond

  (vla-purgeall c_doc)
  (vla-regen c_doc acallviewports)

  (mapcar 'setvar sv_lst sv_vals)
  (princ)
);end_defun


Script in attachement and block file.

Thanks in advance

example bloc dynamic (1).dwg D2SBlockbyVisibility (2).lsp

Link to comment
Share on other sites

That ssget is select all blocks in the drawing's model space that have attributes.  Try switching it to select all dynamic blocks in model space.

(setq ss (ssget "_A" (list '(0 . "INSERT") (cons 2 "`*U*") '(410 . "Model"))))

 

 

 

 

 

Link to comment
Share on other sites

Hi,
Thanks for your answer, I have already test something (with help from chatGPT debug) like that but I have some issue with dbe funtion when I apply this line.
Error log : first execute nothing and second execute, block created but no scale and rotation and name kept. Code correctly works when attribute block is present. 
 

Commande: (LOAD "C:/Users/yann/Downloads/D2SBlockbyVisibility (2).lsp") C:D2SBV

Commande: D2SBV

Select One of the Dynamic Blocks to Convert :
An Error : Paramètre non facultatif occurred.
Commande:
Commande:
Commande: D2SBV

Select One of the Dynamic Blocks to Convert :
An Error : Erreur Automation Clé dupliquée occurred.

Link to comment
Share on other sites

Posted (edited)

Hi,
can I have  little help please:
I tried to add some princ message but currently : error An Error : type d'argument incorrect: FILE #<VLA-OBJECT IAcadBlockReference 000002a30e6ce8f8> occurred.

 

Commande: D2SBV

Select a dynamic block to convert:
Select One of the Dynamic Blocks to Convert :
Getting visibility state...
Getting visibility parameter name...
Getting dynamic property value...
Getting visibility parameter name...
Selecting dynamic blocks in model space...
Converting dynamic block to static block function...
Getting dynamic property value...
Getting visibility state...
Getting visibility parameter name...
Getting dynamic property value...
Setting dynamic property value...
An Error : type d'argument incorrect: FILE #<VLA-OBJECT IAcadBlockReference 000002a30e6ce8f8> occurred.

 

 

debug code:
 

(defun LM:getvisibilityparametername (blk / vis)
    (princ "\nGetting visibility parameter name...")
    (if (and (vlax-property-available-p blk 'effectivename)
             (setq blk (vla-item (vla-get-blocks (vla-get-document blk)) (vla-get-effectivename blk)))
             (= :vlax-true (vla-get-isdynamicblock blk))
             (= :vlax-true (vla-get-hasextensiondictionary blk))
             (setq vis (vl-some '(lambda (pair) (if (and (= 360 (car pair)) (= "BLOCKVISIBILITYPARAMETER" (cdr (assoc 0 (entget (cdr pair)))))) (cdr pair)))
                         (dictsearch (vlax-vla-object->ename (vla-getextensiondictionary blk)) "ACAD_ENHANCEDBLOCK")
                       )
             )
        )
        (cdr (assoc 301 (entget vis)))
    )
);end_defun

(defun LM:getvisibilitystate (blk / vis)
    (princ "\nGetting visibility state...")
    (if (setq vis (LM:getvisibilityparametername blk))
        (LM:getdynpropvalue blk vis)
    )
)

(defun LM:setdynpropvalue (blk prp val)
    (princ "\nSetting dynamic property value...")
    (setq prp (strcase prp))
    (vl-some '(lambda (x) (if (= prp (strcase (vla-get-propertyname x))) (vlax-put x 'value val))) (vlax-invoke blk 'getdynamicblockproperties))
);end_defun

(defun LM:getdynpropvalue (blk prp)
    (princ "\nGetting dynamic property value...")
    (setq prp (strcase prp))
    (vl-some '(lambda (x) (if (= prp (strcase (vla-get-propertyname x))) (vlax-get x 'value))) (vlax-invoke blk 'getdynamicblockproperties))
);end_defun

(defun rh:dbe (blk doc lyr)
    (princ "\nDeleting objects on layer...")
    (vlax-for obj (vla-item (vlax-get-property doc 'blocks) blk)
        (cond ((and (/= (vla-get-layer obj) lyr)
                    (not (wcmatch (strcase (vlax-get-property obj 'objectname)) "*ENTITY*")))
               (vla-delete obj))
        )
    )
);end_defun

(vl-load-com)

;Dynamic to Static Block By visibility state
(defun c:d2sbv (/ *error* c_doc c_spc sv_lst sv_vals flg obj o d vn en prp val f ss cnt obj bn ang vn ipt n_obj nbn)

    (defun *error* (msg)
        (mapcar 'setvar sv_lst sv_vals)
        (if (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*")) (princ (strcat "\nAn Error : " msg " occurred.")))
        (princ)
    );end_local_*error*_defun

    (setq c_doc (vlax-get-property (vlax-get-acad-object) 'activedocument)
          c_spc (vlax-get-property c_doc (if (= 1 (getvar 'cvport)) 'paperspace 'modelspace))
          sv_lst (list 'cmdecho 'osmode)
          sv_vals (mapcar 'getvar sv_lst)
    );end_setq

    (mapcar 'setvar sv_lst '(0 0))

    (while (not flg)
        (princ "\nSelect a dynamic block to convert: ")
        (setq obj (vlax-ename->vla-object (car (entsel "\nSelect One of the Dynamic Blocks to Convert : "))))
        (cond ((null obj)
               (alert "No object selected.")
               (setq flg T)
        )
              ((and (= (setq o (vlax-get obj 'objectname)) "AcDbBlockReference")
                    (= :vlax-true (setq d (vlax-get-property obj 'isdynamicblock)))
                    (setq vn (LM:getvisibilitystate obj)))
               (setq en (vlax-get obj 'effectivename) flg T prp (LM:getvisibilityparametername obj))
        )
              (t
               (cond ((/= o "AcDbBlockReference") (setq msg "NOT a Block"))
                     ((= d :vlax-false) (setq msg "NOT a Dynamic Block"))
                     ((not vn) (setq msg "Block has NO Visibility Parameter"))
               );end_cond
               (alert msg)
        )
        );end_cond
    );end_while

    (princ "\nSelecting dynamic blocks in model space...")
    (setq ss (ssget "_A" (list '(0 . "INSERT") (cons 2 "`*U*") '(410 . "Model"))))
    (cond (ss
           (repeat (setq cnt (sslength ss))
               (setq obj (vlax-ename->vla-object (ssname ss (setq cnt (1- cnt))))
                     bn (vlax-get obj 'effectivename)
               );end_setq

                (cond ((= bn en)
           (princ "\nConverting dynamic block to static block function...")

           (setq ang (lm:getdynpropvalue obj "angle1")
                 vn (LM:getvisibilitystate obj)
                 ipt (vlax-get obj 'insertionpoint)
                 n_obj (vlax-invoke c_spc 'insertblock ipt en 1 1 1 0)
           );end_setq
                      (LM:setdynpropvalue n_obj prp vn)

                      (if (numberp val)
                          (setq val (1+ val) nbn (strcat vn "_" (itoa val)))
                          (setq val 0 nbn (strcat vn "_" (itoa val)))
                      )

                      (princ "\nn_obj: " n_obj)
                      (princ "\nnbn: " nbn)
                      (vla-converttostaticblock n_obj nbn)
                      (rh:dbe nbn c_doc "0")
                      (vla-delete n_obj)
                      (setq n_obj (vlax-invoke c_spc 'insertblock ipt nbn 1 1 1 ang))
                      (mapcar '(lambda (x) (vlax-put-property n_obj x (vlax-get-property obj x))) (list 'layer 'truecolor 'xscalefactor 'yscalefactor 'zscalefactor))
                      (vla-delete obj)
               )
           );end_cond
           );end_repeat
    )
          (t (alert "Nothing Found"))
    );end_cond

    (princ "\nPurging and regenerating document...")
    (vla-purgeall c_doc)
    (vla-regen c_doc acallviewports)

    (mapcar 'setvar sv_lst sv_vals)
    (princ)
);end_defun

 

Thanks in advance

Sincerly

Edited by yann.insa
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...