Author Topic: Renaming a single instance of a block .. is it possible?  (Read 521 times)

0 Members and 1 Guest are viewing this topic.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16614
  • Superior Stupidity at its best
Renaming a single instance of a block .. is it possible?
« on: June 05, 2017, 07:12:58 pm »
I was going to write a dissertation on why this is necessary, but it was getting too long ... so here is the abbreviated version.

I am programmatically copying a layout to a new layout, including all geometry that is on the source layout. I want to rename the block references on the new layout because their definition will be modified programmatically and I don't want to affect the source blocks.

To complicate matters, each block may have between 2 and 67 attributes at various scales, locations and rotations within the source block. These will not be changing.

Consider this scenario:
Insert block "a" on a layout named "Current" then rename block "a" to "a-Current-n", where n is an index number;
Copy "Current" layout and rename it to "Future";
Rename all block references on "Future" layout to "a-Future-n" where n is the index number;

"a" will remain the same, n will remain the same, the only thing that changes is "Current" changes to "Future".

While I sit here and type this, I am considering if I can just clone the block defs and rename them, then rename the references to match.

Code not required, I'll address that later, a methodology is what I am looking for at this point ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Atook

  • Swamp Rat
  • Posts: 824
Re: Renaming a single instance of a block .. is it possible?
« Reply #1 on: June 05, 2017, 07:50:26 pm »
Still trying to sort out what you're trying to do.

Will the geometry of the block be changing, or just  attribute values?

If the geometry is changing, it seems like you'll need a unique block for every layout. You'd need to modify the geometry and give it a unique name then insert it on that layout. I like your naming idea, then you can parse for it later if  you need to access programmatically .

There is however, a good chance that I'm misunderstanding your post completely.  :brow:

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16614
  • Superior Stupidity at its best
Re: Renaming a single instance of a block .. is it possible?
« Reply #2 on: June 05, 2017, 10:18:31 pm »
I think you have the basic idea .. the geometry will be modified, similar to a dynamic block.

The crux of this project is that there are only 25 +/- blocks that can be configured in tens of thousands of ways to create a drawing. As the attributes change and are updated to various values, the blocks automatically reconfigure based on predefined parameters. Sort of like a dynamic block with parametric constraints that are tied to attribute values.

The user will select data points that are imported. Initially, there are no blocks in the drawing, but once a dataset has been imported, the user can then use that base model to generate other layouts and update the blocks on that particular layout with new data without changing the initial one.

So basically, the code create a block programmatically and gives it a unique name. It then inserts other blocks into it to create the geometry based on attribute values. If that block is copied, it needs to be renamed because two instances of the same block will cause data calculations to be incorrect.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Atook

  • Swamp Rat
  • Posts: 824
Re: Renaming a single instance of a block .. is it possible?
« Reply #3 on: June 05, 2017, 10:37:39 pm »
... If that block is copied, it needs to be renamed because two instances of the same block will cause data calculations to be incorrect.

This could be a sticking point. Would it be possible that a user copy this block while your software isn't running? If so, you'll have two instances of the same block and all the tomfoolery that goes along with it.

If not, you've laid out the problem pretty well. It's almost time to write some code for it. :)

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16614
  • Superior Stupidity at its best
Re: Renaming a single instance of a block .. is it possible?
« Reply #4 on: June 05, 2017, 10:58:41 pm »
The software will be loaded automatically when AutoCAD opens. Of course no amount of code can control what people do to break drawings, but if they are given the tools to do it correctly to begin with, the chances of breaking the drawing is minimized somewhat.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

n.yuan

  • Bull Frog
  • Posts: 226
Re: Renaming a single instance of a block .. is it possible?
« Reply #5 on: June 06, 2017, 09:37:04 am »
...
Consider this scenario:
Insert block "a" on a layout named "Current" then rename block "a" to "a-Current-n", where n is an index number;
Copy "Current" layout and rename it to "Future";
Rename all block references on "Future" layout to "a-Future-n" where n is the index number;

"a" will remain the same, n will remain the same, the only thing that changes is "Current" changes to "Future".
 ...

The problem I am having to understand your question is: you describe the scenario as CAD user, or as programmer? Since this a forum about programming, let's assume it is the latter. Then we programmer should always refer "block" by BlockTableRecord and/or BlockReference. When you say "insert block 'a'" and then "rename block 'a'", it is not clear "block 'a'" means BlockTableRecord", or "BlockReference": one can insert a block drawing as BlockTableRecord, and can also "insert" a BlockReference (technically, it is not insert, it is create, because the BlockTableRecord must exist/be inserted first). But BlockReference CANNOT be renamed, its takes the name of the BlockTableRecord. I am sure you know all this, but point it out for what I am trying to say as following;

Here is my brief translation of you want to do (with code)

  • On layout "Current", insert a block definition "a", then create a BlockReference to it with a lot attributes according to the "a" BlockTableRecord;
  • Copy everything (only one BlockReference, let's assume) on layout "Current" to layout "Future".
  • You want the BlockReference on layout Current" change its name to "a-xxx-index", and the one on Layout "Fututre" change its name to "a-yyyy-index" (or whatever name). However, the BlockTableRecord remains unchanged

As I mentioned above, BlockReference's name is not changeable (i.e. Read-only), so, the only way to do what you want is to create new BlockTableRecord with the wanted block name, and repoint a BlockReference's BlockTableRecord property to the new BlockTableRecord (i.e. the BlockReference is changed to be the reference of another BlockTableRecord).

So, you can do this:

1. Use code to create a new BlockTableRecord, which has the same geometries as BlockTableRecord "a", name it as needed.
2. set the target BlockReference's BlockTablerecord property to the newly created BlockTableRecord's ObjectId.
3. If the "renamed" BlockReference remains to be the only reference of the newly created BlockTableRecord (just for the naming purpose), you can omit creating all the AttributeDenitions in the new blocktablerecord as in block definition "a". That is, the newly created blocktablerecord not having all the attributedefinitions will not affect the existing BlockReference's attributereferences.
4. After you have all new blocktablerecords created and corresponding BlockReferences are RE-referenced, call Editor.Regen() to update the BlockReference view in editor.

Hopefully my understanding to your need is not off too much

Bryco

  • Water Moccasin
  • Posts: 1796
Re: Renaming a single instance of a block .. is it possible?
« Reply #6 on: June 06, 2017, 08:05:59 pm »
ditto
Programatically you can make the new btr using clone foe all the items in the existing btr,
then you can use the paperspace block to access the insertpts and rotations to insert the new brefs in.
So you are not copying the layout more like making a new layout and populating it.
This could involve more work if you have tricky viewports otherwise fairly simple

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16614
  • Superior Stupidity at its best
Re: Renaming a single instance of a block .. is it possible?
« Reply #7 on: June 06, 2017, 11:22:54 pm »
Yeah, I can see where it is confusing. I'll try to explain it in the simplest terms possible from the programming perspective.

I programmatically copy a layout, including all of the associated geometry, to a new layout.
Code - C#: [Select]
  1. acLayoutMgr.CopyLayout(source, target);

The new target layout is an exact copy of the source layout with the exception of the name (and tab order which I set elsewhere)

The source layout has multiple block references, each with multiple attributes that get copied to the target layout. I need these block references to have a unique name that consists of the base block name (it is one of 12 predefined names), the layout name (where the block reference resides), and an index that identifies that block uniquely in that layout. Thus, if the source layout was named "Current" and the target layout was named "Future", a block reference named "a-current-32" in the "Current" layout would be "a-future-32" in the "Future" layout.

I realize that is going to require a new BlockDefinition in the BlockTableRecord because in the end, there can only be one block reference for every block definition.

What I am envisioning is the following:
Open a user form;
Copy a layout to a new layout;
Get a collection of all Block References on the target layout;
For each Block Reference in Future layout,
       Copy the existing Block Definition (i.e. "a-Current-21" ) to a new Block Definition ("a-Future-21");
       Update the associated Block Reference ("a-Current-21") to point to the new Block Definition ("a-Future-21");
Regen the editor;
Close the userform;

I guess a real simple way to explain it would be to say "Replace all inserts on a specified layout with new inserts that reference a copy of the original block definition, while replacing the layout name in the source block definition name with the layout name of the target layout."

I really hope this makes sense because I am not sure I know how to explain it any different.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Bryco

  • Water Moccasin
  • Posts: 1796
Re: Renaming a single instance of a block .. is it possible?
« Reply #8 on: June 07, 2017, 10:44:31 pm »
/yes so you only need 1 sub  replaceblockrefs(). And you are using the paperspace block belonging to the layout to cycle thru the blockfrefs. Using  a dictionary to copy the atts.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16614
  • Superior Stupidity at its best
Re: Renaming a single instance of a block .. is it possible?
« Reply #9 on: June 07, 2017, 11:54:35 pm »
/yes so you only need 1 sub  replaceblockrefs(). And you are using the paperspace block belonging to the layout to cycle thru the blockfrefs. Using  a dictionary to copy the atts.
No code as of yet .. coming to terms with the best way to manage doing it. Only then will I write some code, but it is getting close!
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

ronjonp

  • Needs a day job
  • Posts: 6258
Re: Renaming a single instance of a block .. is it possible?
« Reply #10 on: June 08, 2017, 11:44:30 am »
Do the blocks have to be named differently? Could you use Xdata instead? If the objects are on different tabs, it's very easy to grab all objects per tab.

Windows 10 x64 - AutoCAD /C3D 2018

Custom Build PC

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16614
  • Superior Stupidity at its best
Re: Renaming a single instance of a block .. is it possible?
« Reply #11 on: June 08, 2017, 05:43:20 pm »
Do the blocks have to be named differently? Could you use Xdata instead? If the objects are on different tabs, it's very easy to grab all objects per tab.

Yes, they absolutely MUST be different blocks. No two block references to the same block definition should ever exist. The reason is that each block reference will have a varying number of attributes that will determine what the block ultimately looks like.

I can't divulge specifics, but it is kind of like a dynamic/parametric block.
Imagine you have a block definition that we'll call widget ... now that widget has some attributes that define its appearance ... lets imagine Widget has a "Shape" attribute that can have one of the following values: circle, square, triangle, hexagon, octagon; Widget might also have an attribute "Inscribed Diameter" that can be any positive real number ...
If we change the value of "Shape" to circle it will change to a circle, using "Inscribed Diameter" as the diameter of the circle. If we change the value of "Inscribed Diameter" to 10, the circle will resize to fit into a diameter of 10. If we then change the "Shape" attribute to "square" the widget will change to the shape of a square and will fit inside a circle with the diameter of 10. Because we are materially changing the objects within the block definition, it would affect all references equally.

Lets be clear, this is a significant dumbing down of the software in development. The previous example would be simple to do with a dynamic block, what I am doing, is leaps and bounds beyond dynamic blocks.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Keith Brown

  • Swamp Rat
  • Posts: 581
Re: Renaming a single instance of a block .. is it possible?
« Reply #12 on: June 08, 2017, 09:22:04 pm »
I might be late to the game and maybe have missed something but instead of copying a blockreference, can you just add the geometry to a new sided database and then add the side database as a new blocktablerecord and then add the blockreference?  That way you will not have to do any renaming or syncing?
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

kdub

  • SuperMod
  • Swamp Rat
  • Posts: 925
  • class keyThumper<T>:ILazy<T>
Re: Renaming a single instance of a block .. is it possible?
« Reply #13 on: June 08, 2017, 10:47:00 pm »
@KeithBrown
Hi Keith,
Without knowledge of the ultimate goal I'm loath to suggest alternative structures.

Regarding the original isolated goal of duplicating blocks referenced in the Layout ...
   My initial thought was to make a side database for renaming and editing the relevant blocks, then insert the 'new' content into the layout copy, programmatically replacing the references.

    form follows function .... but  the form depends on info not available.

   
called Kerry in my other life

Sometimes the question is more important than the answer.

Keith Brown

  • Swamp Rat
  • Posts: 581
Re: Renaming a single instance of a block .. is it possible?
« Reply #14 on: June 09, 2017, 10:55:12 am »
@KeithBrown
Hi Keith,
Without knowledge of the ultimate goal I'm loath to suggest alternative structures.   


I agree Kerry, but since no code was written yet I thought I would throw it out there since it is not really changing the overall process. It was just a suggestion to keep from renaming a block and syncing attributes.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013