Author Topic: Rename Block on Insert?????  (Read 11678 times)

0 Members and 1 Guest are viewing this topic.

ML

  • Guest
Rename Block on Insert?????
« on: March 14, 2005, 05:56:50 PM »
I was wondering if anyone had an example (assuming it is possible) that could rename a block on insert if it sees that the name already exists?

So, the drawing has a block called block1, the user inserts block1 again, the codes sees that block1 already exists and renames the one being inserted to -2 and so on.

Is this possible and does anyone have any examples that I can draw from?

Thank you

Mark

TR

  • Guest
Rename Block on Insert?????
« Reply #1 on: March 14, 2005, 06:13:27 PM »
Why in the world would you want to do that? Use invisible attributes in the block if you're using it for some sort of counting function. It would make everyones life easier.

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Rename Block on Insert?????
« Reply #2 on: March 14, 2005, 06:38:30 PM »
Mark, I've got to agree with Tim.

The whole purpose of having a block is so it can be inserted many times but only take up the space to define it once. By 'renaming' each insert, you would need to copy the block in the block table as the new named block and insert that......do that 50 times and you've got the exact same block defined 50 times in the block table...the only difference being in the names.

Now what if one thing needs to change in the master block? Yep, you gotta go change each one of those blocks with a different name, too.

ML

  • Guest
Rename Block on Insert?????
« Reply #3 on: March 14, 2005, 06:50:49 PM »
Hey guys

I can see why that would seem silly at first.

Jeff, you will recall because you helped me work through the code in the previous attempts:

I am inserting a block (at a speciific scale), I want to rename its layers for very specific reason but not necessarily explode it.

I then may want to use the same block again but at a different scale and then rename its layers as well, please don't ask why  :shock:

As it stands, after I insert the second block, I am asked to redefine the block, after I redefine the block, there becomes a sharing problem with the layers, then layer control becomes impossible.

I inserted the block earlier at 48, ran my macro, it renamed the layers accordingly, I then copied the block, inserted it in, ran my macro and it worked perfectly.

So, without a longer explanation, I will ask again, can this be done?

Thanks again

Mark

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Rename Block on Insert?????
« Reply #4 on: March 14, 2005, 08:31:14 PM »
Quote from: ML
....., I will ask again, can this be done?

Thanks again

Mark

Mark, even though I helped in your previous attempts I never was clear on exactly what you were trying to do. I think I may now have a clue :D

You have BlockA whose entities were created on a specific layer "Layer1".

You now want to have sepearte block definitions based on the block's scale factor with the block's entites to be on the matching layer name, such as BlockA-48 & "Layer1-48".

When you insert this block at 48 scale you first need to see if it exists, if not you need to create the new block by copying the data from the main block, BlockA, while also creating the associated layer(s).

You DON'T want to actually rename the block everytime it's inserted....we already covered that this is not possible.(Not yet anyway, have a look at the specs for R2006......it looks like dynamic blocks may be what you are looking for).

So, to start coding we need to get from the user, or calling function or whereever, the block name to insert and the scale:
Code: [Select]
'NOTE that this is just sample coding that lacks any style, formatting, error checking or correctness.
'It is merely for the ideas...

bname = utility.getstring(vbcr & "Block to insert: ")
bscale = utility.getreal(vbcr & "Value to scale the block: ")

'Now check whether the block has already been placed:
dim oBlock as AcadBlock
On error reume next
Set oBlock = thisdrawing.blocks.item(bname & "-" & bscale)
If Err then
  set oBlock = ThisDrawing.Blocks.Add(bname & "-" & bscale)
  err.clear
  Set oLayer = ThisDrawing.Layers.Add(the desired new layer....)
'Use the copyobjects method to copy all of the entities from the main block to the new one and change all the entities' layers in the new block to match the new layer(s)
End If
'Now insert the new block as desired on the layer desired.

Is THAT somewhat close to what you are thinking of?

ML

  • Guest
Rename Block on Insert?????
« Reply #5 on: March 15, 2005, 09:09:18 AM »
Hey Jeff

I thought that I had e-mailed you my code with an explanation via PM?
I'm sorry I never explained but you are very close.

OK,
THe Block is actually 4 Legends. On each legend are multiple symbols which are also blocks.

Each Block with all of its symbols are on one layer.
Lets say BA-SYMB.

So, I insert the block at 48, I want BA-SYMB to become BA-SYMB-48. It is very simple.

Suppose I insert another one at 96, I then want those layers to be suffixed BA-SYMB-96.

I have the code working real well after the first one is inserted, the problem is after the second one is inserted, all layers are renamed accordingly but because the block has been redefined, all blocks will be controlled by The BA-SYMB-96 layer because that was the last inserted.

I need to be able to freeze blocks on BA-SYMB-48 and on BA-SYMB-96 individually.

The only solutions that come to mind are to either explode on insert or  rename the second block which was the reason for this post.

Does this make sense?

Check out the below code

Thanks

Mark

Code: [Select]

Sub LegendLayers()

Dim BlkRef As AcadBlockReference
Dim layer As AcadLayer
Dim layers As AcadLayers

Set layers = ThisDrawing.Layers


On Error Resume Next


 For Each BlkRef In ThisDrawing.ModelSpace
 
'Use the scale factor of the inserted block to suffix the existing layer names

'Legend Blocks Layers
 For Each layer In layers
   Select Case layer.Name
     Case Is = ("BA-SYMB")
       layer.Name = ("BA-SYMB") & "-" & BlkRef.XScaleFactor
     Case Is = ("CA-SYMB")
       layer.Name = ("ADT-CA-SYMB") & "-" & BlkRef.XScaleFactor
     Case Is = ("D-SYMB")
       layer.Name = ("D-SYMB") & "-" & BlkRef.XScaleFactor
     Case Is = ("FA-SYMB")
       layer.Name = ("FA-SYMB") & "-" & BlkRef.XScaleFactor
     Case Is = ("I-SYMB")
       layer.Name = ("I-SYMB") & "-" & BlkRef.XScaleFactor
     Case Is = ("TV-SYMB")
       layer.Name = ("TV-SYMB") & "-" & BlkRef.XScaleFactor
 'Text Layers
     Case Is = ("BA-SYMB-TEXT")
       layer.Name = ("BA-SYMB-TEXT") & "-" & BlkRef.XScaleFactor
     Case Is = ("CA-SYMB-TEXT")
       layer.Name = ("CA-SYMB-TEXT") & "-" & BlkRef.XScaleFactor
     Case Is = ("D-SYMB-TEXT")
       layer.Name = ("D-SYMB-TEXT") & "-" & BlkRef.XScaleFactor
     Case Is = ("FA-SYMB-TEXT")
       layer.Name = ("FA-SYMB-TEXT") & "-" & BlkRef.XScaleFactor
     Case Is = ("I-SYMB-TEXT")
       layer.Name = ("I-SYMB-TEXT") & "-" & BlkRef.XScaleFactor
     Case Is = ("TV-SYMB-TEXT")
      layer.Name = ("TV-SYMB-TEXT") & "-" & BlkRef.XScaleFactor
   End Select
  Next
 Next

End Sub
 

daron

  • Guest
Rename Block on Insert?????
« Reply #6 on: March 15, 2005, 10:03:59 AM »
You want to rename your layers, insert your blocks as xrefs, change the name and insert it again. Each layer will have a different name.

ML

  • Guest
Rename Block on Insert?????
« Reply #7 on: March 15, 2005, 10:11:23 AM »
No, that won't work Daron.
The symbols in the legend will be attributed at a later time so I will need to keep them as blocks.

The above code I put together works real well, I just have the feeling i will need to insert as exploded blocks

Thanks

Mark

ML

  • Guest
Rename Block on Insert?????
« Reply #8 on: March 15, 2005, 10:13:05 AM »
Actually, that will not work either.
If it is exploded, then the code will fail as it is looking for a blockreference.

This takes me back to the very first question

How can I rename a block upon insertion?

Thank you

Mark

daron

  • Guest
Rename Block on Insert?????
« Reply #9 on: March 15, 2005, 10:57:34 AM »
(command "rename" oldname newname)
sendcommand rename etc.

Then insert the block again and rename again.

would either of those work?

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Rename Block on Insert?????
« Reply #10 on: March 15, 2005, 12:19:50 PM »
Quote from: ML
Hey Jeff

I thought that I had e-mailed you my code with an explanation via PM?
I'm sorry I never explained but you are very close. You did, but I have a hard time following my own train of thought....imagine how it is for me to follow yours  :?

OK,
THe Block is actually 4 Legends. On each legend are multiple symbols which are also blocks. Right

Each Block with all of its symbols are on one layer.
Lets say BA-SYMB. Check, I'm still with you

So, I insert the block at 48, I want BA-SYMB to become BA-SYMB-48. It is very simple. Heh, if it was very simple your code would be done and working..... :D

Suppose I insert another one at 96, I then want those layers to be suffixed BA-SYMB-96. Check, again

I have the code working real well after the first one is inserted, the problem is after the second one is inserted, all layers are renamed accordingly but because the block has been redefined, all blocks will be controlled by The BA-SYMB-96 layer because that was the last inserted.
OK, this is where you keep losing me..... :idea: I think I finally see a solution......see below, after the quote..

I need to be able to freeze blocks on BA-SYMB-48 and on BA-SYMB-96 individually. Check, again

The only solutions that come to mind are to either explode on insert or  rename the second block which was the reason for this post.

Does this make sense?  Yes and No......

Check out the below code

Thanks

Mark


OK, so the problem is that you are renaming a block in the drawing and then trying to insert THAT block at another scale...therein lies your problem. You need to:
1. Save your base block BA-SYMB to disk
2. When you go to insert it, get the scale and check if the block BA-SYMB- & SCALE is in the drawing.
3. If not, insert the block from the disk file, NOT ANY VERSION ALREADY IN THE DRAWING. If it already exists skip to 5
4. Run the block & layer rename routine. Now you will won't have the BA-SYMB in your drawing again, so the next time you insert from the FILE, your code to rename will work just fine.
5. Insert the block as desired.......

So if all goes well, and you've inserted your block at both 48 & 96 scales, your block table would show: "BA-SYMB-48" & "BA-SYMB-96" but there would be NO "BA-SYMB"
Does that makes sense?

Jeff

ML

  • Guest
Rename Block on Insert?????
« Reply #11 on: March 15, 2005, 09:56:18 PM »
Hey Jeff

Thanks again

I am not sure if you are following me exactly but almost.
If you look at the code that I posted, I think it pretty much explains it.

OK,

Insert block at 48, the layer names in that block become layernames-48

Insert that block again at 96, those layers become layernames-96

Now, I want to be able to freeze, turn off etc each layer without effecting other layers but that isn't happening.
I beleive it is due to the fact that after the second block is inserted, you need to redefine it, therefore all layers take the side of the last block insert.

Does that make better sense?


Thanks again

Mark

ML

  • Guest
Rename Block on Insert?????
« Reply #12 on: March 16, 2005, 06:58:06 AM »
Jeff,

I am trying this approach: User inserts first block to scale, let's say 48,
Macro is ran. The layers get made accordingly,all is working well.
The problem is when we insert another one, so I try moving the firsrt one into place and exploding it, then I insert the second one, run the macro. The layers (the way I want them) are not created.

If I delete the first block that is exploded, then the layers for the second one will be created (layername-xscalefactor) which is no good, I need both to work.

So, my conclusion is that there is objects in the exploded block that were exposed after the block was exploded that my code doesn't like

I've since added a condition:

Code: [Select]

If blkref.objectname = "acblockreference" Then
continue


Still, I am getting a type mismatch error.

Any ideas on that as well?

Thank you

Mark

ML

  • Guest
Rename Block on Insert?????
« Reply #13 on: March 16, 2005, 11:00:29 AM »
Daron

That rename command may just do the trick!

Thank you

Mark

Jeff_M

  • King Gator
  • Posts: 4087
  • C3D user & customizer
Rename Block on Insert?????
« Reply #14 on: March 16, 2005, 12:37:10 PM »
Quote from: ML
Hey Jeff

Thanks again Sure Thing!

I am not sure if you are following me exactly but almost. Nope, I'm sure I'm following you now.....
If you look at the code that I posted, I think it pretty much explains it. If that's the case then you aren't understanding the problem.....

OK,

Insert block at 48, the layer names in that block become layernames-48 Yes, I know

Insert that block again at 96, those layers become layernames-96 Which is exactly how I explained it....

Now, I want to be able to freeze, turn off etc each layer without effecting other layers but that isn't happening. If you followed my example they would....
I beleive it is due to the fact that after the second block is inserted, you need to redefine it, therefore all layers take the side of the last block insert. No, you need to insert and rename from FILE, OR have your base block in the drawing but use CopyObjects tor create a new block.

Does that make better sense? Nope, I had it last time. If you want to have  only one block definition for multiple blocks that have different characteristics, it CANNOT be done...although, as I said before, R2006 looks like it might be possible.


Thanks again You're welcome......you know, upon further reflection, you would be better off to have your different blocks saved to file and insert those, rather than have this convoluted layer & block rename deal.....

Mark