Change layer block inserted on

Change layer block inserted on

tevansERD
Advocate Advocate
3,180 Views
11 Replies
Message 1 of 12

Change layer block inserted on

tevansERD
Advocate
Advocate

we are having to change our block structure to where all line work is drawn on layer 0, color and linetype bylayer. I have done this with a script. The linework was on the layers such as E-elc etc and inserted on layer 0. I have a lisp routine that i use to redefine the blocks in all of our drawings to update them. I now need to change the layer the blocks are inserted on E-elec and not layer 0. Does anyone know of a lisp routine that will do this? The lisp routine i used to redefine the blocks used a script with all of the block names to redefine. I am hoping that a similar routine is out there to move the blocks perhaps with a list.

Thank you in advance for your assistance.

0 Likes
Accepted solutions (1)
3,181 Views
11 Replies
Replies (11)
Message 2 of 12

leothebuilder
Advisor
Advisor

As a suggestion, rather than create a script or lisp to capture all the block names why not right click a block, select similar and change the layer

in the properties palette.

0 Likes
Message 3 of 12

David_W_Koch
Mentor
Mentor

@leothebuilder wrote:

As a suggestion, rather than create a script or lisp to capture all the block names why not right click a block, select similar and change the layer

in the properties palette.


I believe he is looking for a custom function that would take a list of block names and, for each name, extract the layer name of one of the objects in the block definition and then move each block reference in the drawing to that layer.  This assumes that all current blocks have all of their nested objects on the same layer.  That would have to be run first; then his other routine for changing all of the nested objects to layer 0 with ByLayer color and linetype could be run.

 

It would fairly easy to write such a routine, and then you could batch process multiple drawings.  If it were just one drawing and a small number of different block references, then your suggestion would be the better way to go.

 

Unfortunately, I do not have such a routine already written.  If no one else posts back, perhaps I will take a stab at a bare-bones routine that would do this.


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

0 Likes
Message 4 of 12

leothebuilder
Advisor
Advisor

David, I would agree that my suggestion would only be suitable for a one-off or maybe two drawing files.

If you wanted to edit a lot of files, yes you would benefit from a script or lisp for batch processing.

Your block names would have to match in each file for it to work.

I have seen many files where cut and pasting of blocks have resulted in renaming them something crazy like A$C20462A83.

 

0 Likes
Message 5 of 12

David_W_Koch
Mentor
Mentor

@leothebuilder wrote:

I have seen many files where cut and pasting of blocks have resulted in renaming them something crazy like A$C20462A83.

 


I hate those.  Especially when they then explode the block, but do not purge the definition.  Why use PASTEBLOCK if you are going to explode it, anyway.  And if there is a reason to keep the contents as a block, then rename it to something that has meaning.


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

0 Likes
Message 6 of 12

David_W_Koch
Mentor
Mentor

No promises that this will do exactly what you want.  I have not extensively tested it, but it did work for me in the limited testing I did.  I would suggest making copies of several representative files and testing it on them to make certain nothing unfortunate occurs before running it on the actual files.

 

In addition to the attached AutoLISP file, you will need a plain text file that has the name of each block definition you wish to process, one name per line.  The file can be saved to any name you like, but must have a TXT file extension.  The routine will present a dialog that will allow you to navigate to and select your TXT file.  Then select the Open button to start the processing.2017-06-23_SelectTextFileWithListOfBlockNamesDialog.png

 

If the file includes the name of a block definition that does not exist in the current file, it will write a note to that effect to the command line, and move on to the next name.  If the file includes the name of a block definition that is defined in the current file, but which does not have any placed instances in the current file, the routine will write a note to that effect to the command line, and move on to the next name.  For example:

Command: BTNL
Block NoSuchBlockDefinition10 is not defined in this drawing.
No instances of block Strike2 were found in this drawing.
Block SpirographicBogus7 is not defined in this drawing.
Block To Nested Layer function complete.

David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

0 Likes
Message 7 of 12

tevansERD
Advocate
Advocate

Yes I am looking to change the blocks in many drawings, that is why I am looking for a routine. I created a script that I ran on all of the blocks to change the linework to layer 0. I will give this a shot to see if it will move the blocks to the correct layers.

thanks for everyone's input

0 Likes
Message 8 of 12

David_W_Koch
Mentor
Mentor

If you have already converted all of the nested graphics to layer 0, then this routine will not help.  It needs to be run while the nested graphics are still on the layer that you want for the main block.

 

If the blocks have all had their nested objects changed to layer 0, then a modified routine could be written, provided you know what layer name should be used for each block on your list.  If you set up the text file so that each line looks like this:

("BLOCKNAME" "LAYERNAME")

then the routine could be modified to read each line in as a list, the parts where the existing layer name is extracted could be deleted, and the change in layer name could be done using the data in the list.  This all assumes that the layers already exist, although if desired, the routine could create a new layer for any that do not exist.  Unless you want to key in all the layer attribute data, it would probably suffice to have any new layer created with default values for color, linetype, etc.  A simple layer script could be run to fix those layers to your standards after the fact.


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

0 Likes
Message 9 of 12

David_W_Koch
Mentor
Mentor
Accepted solution

Here is a routine that does not rely on extracting the layer from the nested entities.  It does require that you provide both the block name and the layer name in the text file.  As noted in my previous post, each line of the text file needs to be formatted like this:

 

("BLOCKNAME" "LAYERNAME")

So, each line begins with an open parenthesis, followed by the name of a block to be processed, enclosed in double quotation marks, followed by a space, followed by the name of the layer onto which all instances of that particular block are to be placed, enclosed in double quotation marks, followed by a close parenthesis.  The routine does no error checking on what is read in for each line, so any deviation from that format could result in the routine crashing.

 

 

The routine starts with a dialog so that you can select the text file that contains the block name/layer name pairs.  It works as described for the previous routine.

 

The attached ZIP file includes the BTL.lsp file, along with a sample drawing file and text file that works with the drawing file.  The drawing file includes five block definitions and placed instances of four of those.  (Including four instances in paper space - those will have their layers changed too, even if the routine is run from the Model tab.)  The text file has lines for all five of those block definitions as well as a sixth that is not defined in the file.  Block5 will trigger the no instances in the file command line message; Block6 triggers the not defined in the file command line message.


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

Message 10 of 12

Anonymous
Not applicable

Hello and thank you for your Lisp which is very useful. I have a question, do you think it is possible to have the same Lisp but instead of having  "BLOCK1 "  "BLOCK2 " etc... have  "* CK1 "  "* CK2 " etc...
to fetch a block containing such or such part of the word in the. txt file

0 Likes
Message 11 of 12

David_W_Koch
Mentor
Mentor

The previously posted code could be modified to allow for wildcard characters in the block name.  That might best be done by a separate subroutine, which, if a block name string with one or more wildcard characters is found, would be called with the block name string as an argument.  The routine would then iterate through the Block Table, checking for Block Definition names that match the wildcard string.  When done, the routine would return a list of strings containing the names of the Block Definitions that matched the wildcard string or nil if none were found.  If the list is non-nil, then the main routine would need to run the layer changing code in a loop, processing each item on the list.

 

I do not have time right now to write the code to do that, but it is possible.


David Koch
AutoCAD Architecture and Revit User
Blog | LinkedIn
EESignature

0 Likes
Message 12 of 12

Anonymous
Not applicable

really thank you already for doing this routine. I will try to understand how the Lisp works

0 Likes