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

Search and replace predefined Blocks

11 REPLIES 11
Reply
Message 1 of 12
RockyBrown4134
975 Views, 11 Replies

Search and replace predefined Blocks

Old dog is overthinking something simple or has just plain forgot. I need help with the following Lisp.

 

1) I want to search the drawing for the pre-define block names given in the proram.

2) If the are there, I want to replace them with the new version.

3) Program replaces the firest block it finds, then tries to insert that block.

 

What am I missing?

Is ther a easier way to do this?

All help is greatly appreciated.

 

Code is attached.

 

If this response answers your question, Please mark this response as "Accept as Solution"

Rocky Brown
AutoCAD 2020 / Inventor 2020 / Plant 3D
11 REPLIES 11
Message 2 of 12


@RockyBrown4134 wrote:

.... 

1) I want to search the drawing for the pre-define block names given in the proram.

2) If the are there, I want to replace them with the new version.

3) Program replaces the firest block it finds, then tries to insert that block.

 

What am I missing?

Is ther a easier way to do this?

....


As for the incorrect operation, I tried your typical (progn) insertion series using a Block here that I had redefined, and it worked.  A possibility that occurs to me is that you may be working with a redefined Insert command.  Try prefixing it with a period/decimal point to force it to use the native AutoCAD command instead.  [I also added the prefixed underscore character in the suggestion below for language-independent operation.]

 

As for easier ways, yes there are several things you can do.  Mostly you can eliminate all that duplication by stepping through a list of Block names with (foreach), processing each in the same way, rather than spelling it all out separately for each.  Also, if the Block name is the same as the drawing file you want to use to replace its definition in the current drawing, you can omit repeating the name after the = sign.

 

Give this a try:

 

(foreach blk
  '("carbis" "CORE_TB" "Alco-Lite-ind" "Calico" "Detail"
    "Carbis-Loadtec" "COPYRIGHT" "Alco-Lite-ind_COPYRIGHT"
    "COPYRIGHT_ALCO" "COPYRIGHT_CALICO" "CORE_COPYRIGHT"
  ); end quoted list
  (if (tblsearch "block" blk)
    (progn (command "_.insert" (strcat blk "=") "_yes") (command) (command))
  ); end if
); end foreach

 

[In addition, it may be that you don't need both those no-argument (command) functions to close the Insert command without actually Inserting something, but I'll let you experiment with that.]

Kent Cooper, AIA
Message 3 of 12
_Tharwat
in reply to: RockyBrown4134

If you have the express tools installed , you can use the command "Replace Block to another Block"
Message 4 of 12

Kent;

Thanks for the help. Especially the "Education".

That streamlines it alot.

 

"[In addition, it may be that you don't need both those no-argument (command) functions to close the Insert command without actually Inserting something, but I'll let you experiment with that.]"

 

I tried it and it with and without the (command) (command) no-argument. I get a block that still wants me to insert it. I would like it cancel or escape out, but I can live with the frustration for now. Eventually, I want to put this in as a startup command and change the blocks out automatically.

 

If this response answers your question, Please mark this response as "Accept as Solution"

Rocky Brown
AutoCAD 2020 / Inventor 2020 / Plant 3D
Message 5 of 12

Kent,
I went back to an old post.
http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Canceling-or-ESC-key-in-the-middle-of...
There are a couple of things you mentioned in a linked post that may work as well. I'll post what I find out the results.

Thanks
If this response answers your question, Please mark this response as "Accept as Solution"

Rocky Brown
AutoCAD 2020 / Inventor 2020 / Plant 3D
Message 6 of 12

FYI......

 

After going back and reading one of your post

http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/All-of-a-sudden-this-code-has-stopped...

 

I tried some of the suggestions listed in the last couple of post. The result is still the same. Haven't given up yet. I'll continue to try ideas as I get them.

If this response answers your question, Please mark this response as "Accept as Solution"

Rocky Brown
AutoCAD 2020 / Inventor 2020 / Plant 3D
Message 7 of 12


@RockyBrown4134 wrote:

.... 

I tried some of the suggestions listed in the last couple of post. The result is still the same. ....


Three (command)-with-no-argument entries, perhaps?  [Grabbing at straws here....]

Kent Cooper, AIA
Message 8 of 12

After several mnths, I have re-written this routine, tried very sugeestion, and then some.

 

The routine worked in AutoCAD 2008, but does not in AutoCAD 2012. It will not stop/cancel the command when (command) (command) is put in the code.

 

Anybody else come across this problem? If so, any solutions?

 

 

If this response answers your question, Please mark this response as "Accept as Solution"

Rocky Brown
AutoCAD 2020 / Inventor 2020 / Plant 3D
Message 9 of 12

I did a couple of test to check if each line of the code work. In AutoCAD 2012, I entered the following on the command line:

 

(COMMAND "-insert" "carbis=carbis" "Y")

 

I get the following in the AutoCAD Text Window.

 

Command: (COMMAND "-insert" "carbis=carbis" "Y")
-insert Enter block name or [?]: carbis=carbis Duplicate definition of block
_OBLIQUE  ignored.
Duplicate definition of block COPYRIGHT  ignored.
Block "carbis" redefined

Units: Inches   Conversion:     0'-1"
Specify insertion point or [Basepoint/Scale/Rotate]: Y
Point or option keyword required.
; error: Function cancelled
Specify insertion point or [Basepoint/Scale/Rotate]:

 

What is confusing me is the "; error: Function cancelled" response by AutoCAD. The code line dose not have anything canceling the command at this point. in the program I had the (command)(command) to cancel at this point, however, I have not entered that line in the command line in the test, I have removed all the white spaces and re-wriiten the line of code manually, nothing works.

 

I even entered the cod in AutoCAD step by step, to see if the order worked.

 

-insert <Return>

carbis=carbis <Return>

y <return>

(Command) <Return>

 

Step by step woks, block redefined and command is canceled. Could whatever is causing the "; error: Function cancelled" be the problem?

 

Any Suggestions?

 

FYI....I had tested the program in AutoCAD 2008 and it worked without any problems.

 

 

If this response answers your question, Please mark this response as "Accept as Solution"

Rocky Brown
AutoCAD 2020 / Inventor 2020 / Plant 3D
Message 10 of 12


@RockyBrown4134 wrote:

I did a couple of test to check if each line of the code work. In AutoCAD 2012, I entered the following on the command line:

 

(COMMAND "-insert" "carbis=carbis" "Y")

 

I get the following in the AutoCAD Text Window.

 

Command: (COMMAND "-insert" "carbis=carbis" "Y")
-insert Enter block name or [?]: carbis=carbis Duplicate definition of block
_OBLIQUE  ignored.
Duplicate definition of block COPYRIGHT  ignored.
Block "carbis" redefined

Units: Inches   Conversion:     0'-1"
Specify insertion point or [Basepoint/Scale/Rotate]: Y
Point or option keyword required.
; error: Function cancelled
Specify insertion point or [Basepoint/Scale/Rotate]:

.... 


It is not asking whether you want to replace the definition of the Block, so the "Yes" answer to that question is being fed in to answer the insertion-point prompt.  That could be because: A) you have the EXPERT System Variable set so that it won't ask, or B) the operation of Insert in a (command) function is different in newer versions, or C) you're using a redefined Insert command, or D) you're using this when that Block name is not already defined in the drawing.

 

I think the "Function cancelled" message is about the Lisp (command) function, but it's still leaving you in the not-yet-completed Insert command that it started.

Kent Cooper, AIA
Message 11 of 12

I checked for what you indicated and listed below:

 

A) EXPERT System Variable set to 0

 

B) Order of operation of insert command "appears" to be the same. (I tested the order and it works in AutoCAD 2012)

 

C) Redefined Insert command - Command has not been redefined. I checked acad.pgp, acaddoc.lsp, acad.cuix and any other program loaded during startup.

 

D) Block is in drawing. There is a statement in the program to check for the block.

 

I first thought it acted like there was an extra Return in the code, due a ";" or "White space" or a misplaced parenthesis. I checked all three cases and even removed all of the white spaces.  

 

With Logos and title block format having changed for the next fiscal year, I need to finalize a way to change the blocks out without having to manually do it in each drawing. There are Way to Many drawings to do this to.

 

 

If this response answers your question, Please mark this response as "Accept as Solution"

Rocky Brown
AutoCAD 2020 / Inventor 2020 / Plant 3D
Message 12 of 12


@Kent1Cooper wrote:

It is not asking whether you want to replace the definition of the Block, so the "Yes" answer to that question is being fed in to answer the insertion-point prompt.  That could be because: A) you have the EXPERT System Variable set so that it won't ask, or B) the operation of Insert in a (command) function is different in newer versions, or C) you're using a redefined Insert command, or D) you're using this when that Block name is not already defined in the drawing.

 

I think the "Function cancelled" message is about the Lisp (command) function, but it's still leaving you in the not-yet-completed Insert command that it started.


Sorry that it has taken me so long to get back to this.

 

Answer to your questions:

A) EXPERT is set to "0", Issues all prompts normally.

B) I'm not sure but See below.

C) "Insert" command, to my knowledge, is not redefined. It is OTB.

D)  Using "(foreach BLK" statment and "(if (tblsearch "block" blk)" tosearch drawing for the block before starting the insert command.

 

Here is what I recently did and for some reason it works.

The code that was giing me the problem was: (command "_.insert" (strcat blk "=") "_yes")(command)(command), hanging up on the "Redefine" prompt with a "Y" or "Yes" answer.I decided to step through the command and see if each step worked. When I removed the "_yes" from the stament, the code worked as expected. I don't know why, but it works.I thought it was the "EXPERT" setting was changed at first, but I ruled that out when I checked it and it was set to "0".

 

Here is what is a mystery. I tried the code as originally written on a system that has AutoCAD 2010 and it works. But it did not work on  a system with AutoCAD 2012 until i removed the "_yes" from the statment.

 

 

If this response answers your question, Please mark this response as "Accept as Solution"

Rocky Brown
AutoCAD 2020 / Inventor 2020 / Plant 3D

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

Post to forums  

Autodesk Design & Make Report

”Boost