Jump to content

How to write script for blockreplace


1badsti

Recommended Posts

Hi all,

 

I am new with autocad scripting. I want to replace a old titleblock to a new one and keep all the attributes of the oldtitle block.

 

Can you guys help me out how to write the script to change the titleblock.

 

I stuck after I insert the new titleblock into the drawing

 

Here is the code

 

-layer

n

0

s

0

-insert

K:\titleblock\titleblock-D-1.dwg

900,0

1

1

0

 

What i suppose to do next? Please help

Link to comment
Share on other sites

that's it, start with the tricky one :) Attributed blocks can be a pain.

 

First the generalities, the most obvious way to deal with attributes is to supply all the attribute values in your script. I prefer to accept all the default values by setting ATTREQ = 0.

 

Now in your case. Is the change to the title block just graphical with no change in attribute tags or position? If so you should be able to implement your change with....

 
-insert
titleblock-d=k:\titleblock\titleblock-D-1.dwg

 

although I haven't tried it.

 

alternatively I rename the existing block to the new block name and then do something like

-insert
titleblock-d-1=

 

you will need to accept a "redefine?" question somewhere which will redefine your existing block and then escape inserting the new definition. There is a simple way to escape (I know, alant posted it a few weeks back) but I can't remember what it is so I aways complete the insert and then ERASE L to leave only one block.

 

If attributes have moved you need to ATTSYNC the new definition to get the attributes into their correct position.

 

However, if you have added attributes or theyhave been renamed I would advise against using a script.

Link to comment
Share on other sites

I tried but still don't know how to copy all attributes from old titleblock to new one. I tried this code and i am able to insert the new title block into a blank area in model space.

 

_-insert NEWTITLEBLOCK-D-1=K:\TITLEBLOCK\NEWTITLEBLOCK-D-1

900,0

1

1

0

_-BLOCKREPLACE ??????

 

I stuck at this blockreplace command. What should i do next in order for my script to copy the old block's attributes and put it to the new block. Thank you for all your helps.

Link to comment
Share on other sites

What should i do next in order for my script to copy the old block's attributes and put it to the new block. Thank you for all your helps.
I was trying to say there is no easy way to do that with a simple script.

 

I haven't tried Lee's code but his other code is good so that may work for you.

Link to comment
Share on other sites

This might help you when writing scripts:

 

http://www.cadtutor.net/forum/showthread.php?t=48570

 

 

Hi Lee,

 

Thanks you for your links. It is very difficult to have the script running. I've read a lot of your posts, and I know you are an expert of LSP program. I am very new to autocad and its programming. If you can help me out, It would be greatly appreciate.

 

Are there anyway to write a lsp program to swap the title block and keep the old titleblock attributes? And run the lsp program as a batch file because i have more than 100 drawings need to update the titleblock.

 

STi

Link to comment
Share on other sites

if all your attributes have the same tag name in the new block as the old block it is simple as I tried to explain above and I am sure Lee's code would do equally as well. If however you want to grab the value from an old attribute and put it in the new block that has an attribute with a different name then it is very difficult (for me). What exactly are you trying to do?

Link to comment
Share on other sites

Ok, bear in mind that the program I linked you to was just to ease in running the script - meaning that you only had to write the first line, and my program will do the rest.

 

As far as the script itself is concerned, if I remember, if the block has the same name as that you wish to replace, you can use vla-InsertBlock, (and perhaps attsync) and all attribute values will stay in tact.

 

I don't have access to CAD at the moment, but I will post an example when I get a minute.

 

I think I may have posted an example on here already, see here:

 

http://www.cadtutor.net/forum/showpost.php?p=320205&postcount=18

 

Try the above on a single drawing - (without using a script) if it works for you, I could modify it for use in a script.

 

Lee

Link to comment
Share on other sites

For the Script, try this:

 

(defun ReDef ( block / *error* oc spc doc block )
 (vl-load-com)
 ;; © Lee Mac  ~  21.06.10

 (defun *error* ( msg )
   (and oc (setvar 'CMDECHO oc))
   (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
       (princ (strcat "\n** Error: " msg " **")))
   (princ)
 )

 (setq spc
   (if
     (or
       (eq AcModelSpace
         (vla-get-ActiveSpace
           (setq doc
             (vla-get-ActiveDocument
               (vlax-get-acad-object)
             )
           )
         )
       )
       (eq :vlax-true (vla-get-MSpace *doc))
     )
     (vla-get-ModelSpace doc)
     (vla-get-PaperSpace doc)
   )
 )

 (setq oc (getvar 'CMDECHO)) (setvar 'CMDECHO 0)  

 (if (setq block (findfile block))
   (progn
     (vla-delete
       (vla-insertblock spc
         (vlax-3D-point '(0. 0. 0.)) block 1. 1. 1. 0.
       )
     )
     (vl-cmdf "_.attsync" "_N" (vl-filename-base block))
   )
 )
 
 (setvar 'CMDECHO oc)
 (princ)
)

The Script line, (when using my ScriptWriter program, would be something like this) :

 

_.open *file* (load "ReDef.lsp" "Load Failed.") (if ReDef (ReDef "C:\\Users\\BadSti\\NewBlock.dwg")) _.saveas  *file* _.close

Let me know if you need help.

 

Lee

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