Visual LISP, AutoLISP and General Customization

Reply
Valued Contributor
Posts: 73
Registered: ‎09-05-2013
Message 1 of 19 (2,577 Views)
Accepted Solution

Update blocks & attributes LISP

2577 Views, 18 Replies
09-05-2013 12:24 AM

Hey guys,

 

I'm new in this forum and l would need your help.

I'm using a blocklibrary, where I stored all my blocks, most of them have attributes.

Now I'd like to simplify my workflow and want to update the blocks that I inserted in a drawing trough LISP.

 

Is there a way to update all  inserted blocks and attributes of the whole drawing a once? The idea is, that the LISP checks the whole blocklibrary and updates the inserted blocks that to not match with the once from the library.

 

thanks for your help! :smileyhappy:

*Expert Elite*
3wood
Posts: 908
Registered: ‎03-25-2009
Message 2 of 19 (2,555 Views)

Re: Update blocks & attributes LISP

09-05-2013 04:07 AM in reply to: Klingi162

Welcome aboard!

Usually people use xrefs instead insert standard blocks.

I think we can skip the step of comparing block definition, just search the whole drawing, if there is any block name exists in the library, just insert it from the library and redefine the block in the drawing.

Valued Contributor
Posts: 73
Registered: ‎09-05-2013
Message 3 of 19 (2,546 Views)

Re: Update blocks & attributes LISP

09-05-2013 04:23 AM in reply to: 3wood

hey,

thanks for the quick reply.

The thing is I got many blocks in the drawing and don't want to search for every single block seperately.

We also want to use it in the office. so if I tell my colleges that I changed a block, they just need to press a shortcut and every block (including its attributes) is updated. therefore its the only way to make it with a LISP that compares the blocks from its library...

*Expert Elite*
3wood
Posts: 908
Registered: ‎03-25-2009
Message 4 of 19 (2,540 Views)

Re: Update blocks & attributes LISP

09-05-2013 04:42 AM in reply to: Klingi162

I guess there is way to attach additional information to each block definition to indicate its version (I saw it in AutoCAD Architectural?), but I am not familar with the method. Let's wait for other people's help.

*Expert Elite*
Lee_Mac
Posts: 1,051
Registered: ‎12-29-2009
Message 5 of 19 (2,533 Views)

Re: Update blocks & attributes LISP

09-05-2013 05:04 AM in reply to: Klingi162

The following code will redefine all blocks in the drawing for which a drawing file of the same name is found either within a specific directory, or within an AutoCAD Support File Search Path (SFSP):

 

;; Redefine All Blocks  -  Lee Mac
(defun c:redefall ( / bln dir doc dwg lst obj org spc )

    (setq dir nil) ;; Directory of Block Library; nil to use Support Path

    (if dir
        (setq dir (strcat (vl-string-right-trim "\\" (vl-string-translate "/" "\\" dir)) "\\"))
        (setq dir "")
    )
    (cond
        (   (= 4 (logand 4 (cdr (assoc 70 (tblsearch "layer" (getvar 'clayer))))))
            (princ "\nCurrent layer locked.")
        )
        (   (setq doc (vla-get-activedocument (vlax-get-acad-object))
                  spc (vla-get-modelspace doc)
                  org (vlax-3D-point 0 0)
            )
            (vlax-for blk (vla-get-blocks doc)
                (if
                    (and
                        (= :vlax-false (vla-get-isxref blk))
                        (= :vlax-false (vla-get-islayout blk))
                        (not (wcmatch (setq bln (vla-get-name blk)) "`**,_*,*|*"))
                        (setq dwg (findfile (strcat dir bln ".dwg")))
                    )
                    (progn
                        (setq obj (vla-insertblock spc org dwg 1.0 1.0 1.0 0.0))
                        (if (= :vlax-true (vla-get-hasattributes obj))
                            (setq lst (vl-list* "," bln lst))
                        )
                        (vla-delete obj)
                    )
                )
            )
            (if lst (vl-cmdf "_.attsync" "_N" (apply 'strcat (cdr lst))))
            (vla-regen doc acallviewports)
        )
    )
    (princ)
)
(vl-load-com) (princ)

 

Alter the value of the dir variable to source blocks from a specific directory, or set this variable to nil (the current value) to source blocks from the AutoCAD SFSP or working directory.

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
Valued Contributor
Posts: 73
Registered: ‎09-05-2013
Message 6 of 19 (2,523 Views)

Re: Update blocks & attributes LISP

09-05-2013 06:01 AM in reply to: Lee_Mac

hey Lee Mac,

 

thanks for the code.

It looks like that's the one I was looking for. The only problem is, that its for an english autoCAD version. I guess for an german one I would need to put some "_" somewhere in front of the comands I guess (but where??? :smileysad:), because when I run the script, AutoCAD just says: regenerating model

 

 

my second point is the directory of the block library: do you right it like that? -->

 

(setq dir "I:\AutoCAD block Library")

 

sorry for this questions but I'm not that fit with scripting.

*Expert Elite*
Lee_Mac
Posts: 1,051
Registered: ‎12-29-2009
Message 7 of 19 (2,520 Views)

Re: Update blocks & attributes LISP

09-05-2013 06:23 AM in reply to: Klingi162

Klingi162 wrote:

The only problem is, that its for an english autoCAD version. I guess for an german one I would need to put some "_" somewhere in front of the comands I guess, because when I run the script, AutoCAD just says: regenerating model


 

The program should work with all language versions of AutoCAD - the drawing will be automatically regenerated after all blocks have been redefined.

 


Klingi162 wrote:

my second point is the directory of the block library: do you right it like that? -->

 

(setq dir "I:\AutoCAD block Library")

 


 

Not quite -

 

Since the backslash character is an escape character in AutoLISP, you will need to use either single forward slashes or double-backslashes for filepath delimiters, e.g.:

 

(setq dir "I:\\AutoCAD block Library")

 Or alternatively:

 

(setq dir "I:/AutoCAD block Library")

 

I hope this helps!

Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
Valued Contributor
Posts: 73
Registered: ‎09-05-2013
Message 8 of 19 (2,483 Views)

Re: Update blocks & attributes LISP

09-06-2013 12:02 AM in reply to: Lee_Mac

It seems, that I'm still doing something wrong.

 

This is how I do it:

I load your LISP with "appload" and when I run the comand "redefall" it doesn't update my blocks from the library at all.

I checked it because I added some lines in the block and in the drawing where I inserted it I tried the LISP and the new lines didn't show up. only when I manually insert the block again you see the update.

The only result is "Regenerating model".

 

what am I doing wrong? :smileyindifferent:

*Expert Elite*
Lee_Mac
Posts: 1,051
Registered: ‎12-29-2009
Message 9 of 19 (2,463 Views)

Re: Update blocks & attributes LISP

09-06-2013 06:16 AM in reply to: Klingi162

I can report that the program is performing successfully in my testing, so perhaps the configuration is incorrect - please try this 'verbose' version which will provide more feedback to the user:

 

;; Redefine All Blocks  -  Lee Mac
(defun c:redefall ( / bln dir doc dwg lst obj org spc )

    (setq dir "I:\\AutoCAD block Library") ;; Directory of Block Library; nil to use Support Path

    (if dir
        (setq dir (strcat (vl-string-right-trim "\\" (vl-string-translate "/" "\\" dir)) "\\"))
        (setq dir "")
    )
    (cond
        (   (= 4 (logand 4 (cdr (assoc 70 (tblsearch "layer" (getvar 'clayer))))))
            (princ "\nCurrent layer locked.")
        )
        (   (setq doc (vla-get-activedocument (vlax-get-acad-object))
                  spc (vla-get-modelspace doc)
                  org (vlax-3D-point 0 0)
            )
            (terpri)
            (vlax-for blk (vla-get-blocks doc)
                (if
                    (and
                        (= :vlax-false (vla-get-isxref blk))
                        (= :vlax-false (vla-get-islayout blk))
                        (not (wcmatch (setq bln (vla-get-name blk)) "`**,_*,*|*"))
                    )
                    (if (setq dwg (findfile (strcat dir bln ".dwg")))
                        (progn
                            (princ (strcat "Redefining block: " dwg "\n"))
                            (setq obj (vla-insertblock spc org dwg 1.0 1.0 1.0 0.0))
                            (if (= :vlax-true (vla-get-hasattributes obj))
                                (setq lst (vl-list* "," bln lst))
                            )
                            (vla-delete obj)
                        )
                        (princ (strcat "Unable to locate block: " dir bln ".dwg\n"))
                    )
                )
            )
            (if lst
                (progn
                    (princ "Synchronising attributes for redefined blocks...\n")
                    (vl-cmdf "_.attsync" "_N" (apply 'strcat (cdr lst)))
                )
            )
            (textscr)
            (vla-regen doc acallviewports)
        )
    )
    (princ)
)
(vl-load-com) (princ)
Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)
Expert Elite
With Mathematics there is the possibility of perfect rigour, so why settle for less?
Valued Contributor
Posts: 73
Registered: ‎09-05-2013
Message 10 of 19 (2,475 Views)

Re: Update blocks & attributes LISP

09-06-2013 07:16 AM in reply to: Lee_Mac

thanks, but no change. :smileysad:

It just writes "Unable to locate block: I:\\AutoCAD block Library\testblock.dwg

 

Is it possible to integrate the LISP in any other way than trough "appload"?

Does it have a problem, because I'm using AutoCAD 2010 Architecture?

 

thanks for your patience

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community