DesmetMartin 发表于 2022-7-5 17:02:33

Un-dynamic a block - LISP

Hello all,
 
I should get the following code changed so that you can select the blocks you want to 'undynamic" first.
 
Can somebody help me?
 

(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 (strcat (vla-get-EffectiveName insert) "_00") 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))))
 
Credits to TP from theswamp.org.
 
Thanks,
Martin.

Grrr 发表于 2022-7-5 17:08:09

Not so pretty as his, but gets the job done:

(defun C:test ( / ErrCatcher SS i o )(defun ErrCatcher ( func varLst / rtn ) (if (not (vl-catch-all-error-p (setq rtn (vl-catch-all-apply func varLst)))) rtn))(prompt "\nSelect blocks to convert them into static ones:" )(if (setq SS (ssget "_:L" (list (cons 0 "INSERT"))))        (repeat (setq i (sslength SS))                (if (eq :vlax-true (vla-get-IsDynamicBlock (setq o (vlax-ename->vla-object (ssname SS (setq i (1- i)))))))                        (ErrCatcher 'vla-ConvertToStaticBlock (list o (vla-get-EffectiveName o)))                )        ))(princ));| defun |; (or (vlax-get-acad-object) (vl-load-com)) (princ)

DesmetMartin 发表于 2022-7-5 17:12:50

Thanks, it's this kind of thing I want to do.
The only problem is that the name get's changed and you can't edit you static block anymore.

DesmetMartin 发表于 2022-7-5 17:13:56

So what I'm trying to say is that the code in the 1st post should be used.
Only thing that should be added in the code is that you select the items you want to undynamic instead of all the blocks.
 
Thanks,
Martin

Roy_043 发表于 2022-7-5 17:21:12

@Grrr:
IMO this:

(or (vlax-get-acad-object) (vl-load-com))
Should be:

(or vlax-get-acad-object (vl-load-com))

Grrr 发表于 2022-7-5 17:21:22

 
Hi Roy,
Yes it seems that it makes more sence:

_$ vlax-get-acad-object#_$ (vlax-get-acad-object)#_$
Thanks!

Roy_043 发表于 2022-7-5 17:27:05

It is not about 'more sense'. I expect that (vlax-get-acad-object) will fail if the VL extensions have not been loaded. I am using the word 'expect' because I am using BricsCAD where (vl-load-com) is a dummy function and VL functions are available by default.

Grrr 发表于 2022-7-5 17:31:27

Yes, I mean that I haven't noticed that instead of checking for such function, I'm instead running it with the possibility of triggering an error, such as:

_$ (defun C:test () (strcat 1))C:TEST_$ C:test#_$ (C:test)_$ (C:test)Error: bad argument type: stringp 1_1$
You have sharp eye.

Roy_043 发表于 2022-7-5 17:34:41

Testing the (vla-ConvertToStaticBlock) function I have noticed something strange (note: as previously mentioned I use BricsCAD).
My test:
1. Insert the same dynamic block twice.
2. Make identical changes to one or more dynamicproperties for each insert.
3. The inserts now look the same but reference different anonymous definitions.
4. Copy one of the inserts.
5. The copy references the same anonymous definition as its original.
6. Only convert the copy to a static block.
7. Result: The original of the copy has also become a static block (probably due to what was mentioned under #5).
 
If AutoCAD has the same behavior, converting only selected inserts to static blocks would require a more complex solution than the code posted in this thread.

Lee Mac 发表于 2022-7-5 17:37:52

 
This differs in AutoCAD: inserting two references of the same dynamic block and modifying their dynamic parameters to use the same values results in the two blocks referencing the same anonymous definition.
 
 
AutoCAD has the same behaviour (as to be expected, based on the above observation).
 
 
This is not the case in AutoCAD: converting one of the dynamic block references has no effect on the others.
页: [1] 2
查看完整版本: Un-dynamic a block - LISP