Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Change Dynamic Block to Regular Block

18 REPLIES 18
SOLVED
Reply
Message 1 of 19
burniksapwet
6954 Views, 18 Replies

Change Dynamic Block to Regular Block

Guys I found a .lsp that would change all dynamic blocks to regular block. The problem I have with it is the block names are being renamed to "Static XX (XX being different numbers)  Is there a way to update this .lsp so that I retains the block names? Thank you so much in advance.

 

(defun c:UnDynamic

    (   /
        _get_item
        _right
        _make_key
        _dynamic->static_block
        _get_locked
        _get_dynamic_inserts
        _main
    )

    (defun _get_item ( collection key / item )
        (vl-catch-all-apply
           '(lambda ( ) (setq item (vla-item collection key)))
        )
        item
    )

    (defun _right ( str n / len )
        (if (< n (setq len (strlen str)))
            (substr str (1+ (- len n)))
            str
        )
    )

    (defun _make_key ( collection prefix len / key )
        (   (lambda ( i pad )
                (while
                    (_get_item collection
                        (setq key
                            (strcat prefix
                                (_right
                                    (strcat pad (itoa (setq i (1+ i))))
                                    len
                                )
                            )
                        )
                    )
                )
                key
            )
            0
            (   (lambda ( pad )
                    (while (< (strlen pad) len)
                        (setq pad (strcat "0" pad))
                    )
                    pad
                )
                ""
            )
        )
    )

    (defun _dynamic->static_block ( blocks insert len )
        (vla-ConvertToStaticBlock
            insert
            (_make_key blocks "STATIC_" len)
        )
    )

    (defun _get_locked ( layers / locked )
        (vlax-for layer layers
            (if (eq :vlax-true (vla-get-lock layer))
                (setq locked (cons layer locked))
            )
        )
        locked
    )

    (defun _get_dynamic_inserts ( blocks / inserts )
        (vlax-for block blocks
            (vlax-for object block
                (if (eq "AcDbBlockReference" (vla-get-objectname object))
                    (if (eq :vlax-true (vla-get-isdynamicblock object))
                        (setq inserts (cons object inserts))
                    )
                )
            )
        )
        inserts
    )

    (defun _main ( document / blocks inserts locked len )
        (if
            (setq inserts
                (_get_dynamic_inserts
                    (setq blocks (vla-get-blocks document))
                )
            )
            (progn
                (foreach layer (setq locked (_get_locked (vla-get-layers document)))
                    (vla-put-lock layer :vlax-false)
                )
                (setq len (strlen (itoa (length inserts))))
                (foreach insert inserts
                    (_dynamic->static_block blocks insert len)
                )
                (foreach layer locked
                    (vla-put-lock layer :vlax-true)
                )
            )
        )
        (princ)
    )

    (_main (vla-get-activedocument (vlax-get-acad-object)))

)

18 REPLIES 18
Message 2 of 19
pbejse
in reply to: burniksapwet


@burniksapwet wrote:

Guys I found a .lsp that would change all dynamic blocks to regular block. The problem I have with it is the block names are being renamed to "Static XX (XX being different numbers)  Is there a way to update this .lsp so that I retains the block names? Thank you so much in advance.

 


Doable BUT what of two or more instances of that block present on the drawing? and both have different "dynamic" settings?  Both of them cannot have the same name. One would inherit the original name, but one would be different is what  i'm saying.

 

EDIT: hang on, so what you are wanting is replace the word STATIC with the original block name? if YES its easy

 

BTW, who is the original author of the code?

 

Message 3 of 19
pbejse
in reply to: pbejse


@pbejse wrote:

EDIT: hang on, so what you are wanting is replace the word STATIC with the original block name? if YES its easy

 

BTW, who is the original author of the code?


(defun _dynamic->static_block ( blocks insert len )
        (vla-ConvertToStaticBlock
            insert
            (_make_key blocks (strcat (vla-get-effectivename insert)" ") len)
        )
    )
Message 4 of 19
burniksapwet
in reply to: pbejse

I found it here. http://www.theswamp.org/index.php?topic=32681.msg382548#msg382548

 

For duplicate valves if you can maybe add a number at the end of the block name.  If that is not possible i guess i will just have to make sure that there are only one type of block in that drawing. Thank you for replying.

Message 5 of 19
burniksapwet
in reply to: pbejse

And yes I would like for it to keep the block name instead of changing it to static. Thank you.

Message 6 of 19
burniksapwet
in reply to: burniksapwet

Also can it not have the number after it?
Message 7 of 19
pbejse
in reply to: burniksapwet


burniksapwet wrote:

 

...For duplicate valves if you can maybe add a number at the end of the block name...

 

...And yes I would like for it to keep the block name instead of changing it to static. Thank you...


 

Did you try the snippet posted at post #3.

 

Now if there are duplicates, leaving one block to retain its original name would take some more coding. We need to identify which one right after all the blocks are processed AND redefine the block keeping its current state as a static block.

 

 

 

Message 8 of 19
burniksapwet
in reply to: burniksapwet

I did but I think I was doing something wrong because it didn't work for me. If it's not to much to ask would you mind updating the full lsp so that I could just copy and paste it? I don't know much about creating lsp and don't know how to troubleshoot what I did wrong. Thank you.
Message 9 of 19
pbejse
in reply to: burniksapwet


burniksapwet wrote:

 

Also can it not have the number after it?

 

....If it's not to much to ask would you mind updating the full lsp so that I could just copy and paste it? ...


Only when there is no duplicate.
Try the attached lisp file
HTH
Message 10 of 19
burniksapwet
in reply to: pbejse

Thank you so much sir.  This is exactly what I am looking for.  We really do appreciate the time you took to help us out. More power to you.

Message 11 of 19
akaterr55
in reply to: pbejse

pbejse,

Tried out your undynamic.lsp and it works great! 

Wrestled all weekend trying to figure out how to limit its effects to just model space dynamic blocks (without success).

I'm barely limping along trying to figure out regular lisp, so everything i tried in your vlisp code really was just stabbing in the dark.  A whole lot of reading, trying stuff, reading, trying stuff.  Not even a hint of success.  Luckily, the worst i could make it do was nothing at all.

Just what does it take to make the program ignore or filter out the paper space dynamics?

Thanks 

 

Message 12 of 19
akaterr55
in reply to: akaterr55

Well, there went another 6 hours wasted.  I'm beginning to think this isn't possible...

Message 13 of 19
akaterr55
in reply to: akaterr55

I give up.

the best I could do for getting just the blocks in model space was

 

(setq blocks (ssget "_x" (list '(0 . "insert")'(410 . "Model"))))(vla-get-activeselectionset (vla-get-activedocument (vlax-get-acad-object)))

 

which does get the set into vlisp as a vla-object, but as a selection set

#<VLA-OBJECT IAcadSelectionSet 2bba3c84>

 

whereas (setq blocks (vla-get-blocks(vla-get-activedocument (vlax-get-acad-object))))
#<VLA-OBJECT IAcadBlocks 28436304>

 

which apparently is what the program is looking for.  I have not been able to get the iacadselectionset to become iacadblocks...

 

cheers

Message 14 of 19
akaterr55
in reply to: akaterr55

I may go ahead and try starting a new thread on this since it seems like i'm the only one keeps checking on it.

Message 15 of 19
Anonymous
in reply to: burniksapwet

Dear,

Thanks a lot ... 

Message 16 of 19
Anonymous
in reply to: pbejse

I have hundreds of dynamic blocks of exactly the same name and I would like their names to remain exactly same when they change to static blocks. My blocks are basically rectangular shapes with different rotations. 


@pbejse wrote:

burniksapwet wrote:

 

Also can it not have the number after it?

 

....If it's not to much to ask would you mind updating the full lsp so that I could just copy and paste it? ...


Only when there is no duplicate.
Try the attached lisp file
HTH

 

Message 17 of 19
pbejse
in reply to: Anonymous


@Anonymous wrote:

I have hundreds of dynamic blocks of exactly the same name and I would like their names to remain exactly same when they change to static blocks. My blocks are basically rectangular shapes with different rotations. 

EXACTLY the same name? You mean it LOOKS exactly the same geometry, may be rotated or scaled or a different color but NOT with an anonymous name like *U5"? At its unaltered state.?

 

 

 

 

Message 18 of 19
mikeshick
in reply to: pbejse

Using Architecture 2022, I get automation error. key not found.  Can you please assist?

Mike Shick
www.medesigns.us
Message 19 of 19
lzedCB
in reply to: pbejse

Hello,

 

Is there any way to add a conversion of the attributes inside block to normal texts before getting it as regular blocks?

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report