.NET

.NET

Reply
Distinguished Contributor
HJohn1
Posts: 170
Registered: ‎09-26-2008
Message 1 of 4 (375 Views)
Accepted Solution

BlockReference from AttributeReference

375 Views, 3 Replies
12-04-2012 08:37 AM

I am using GetNestedEntity to select an AttributeReference and update the TextString.  However, I would like to modify the routine to include other AttributeReferences on other BlockReferences.  I would like to be able to access the BlockReference from the AttributeReference so I could implement different validations based on the Name of the BlockRefence.  I have been trying to find a way to get this to work but I have been unable to do it.  Can someone please indicate how to go about it.

I assume that you use PromptNestedEntityResult object to obtain user picked AttributeReference' ObjectId.

 

According to the document and ObjectBrowser, there is a method: PromptNestedEntityResult.GetContainers(). It looks like the one we could use to find out which blockreference object owns the picked attribute. However, I tried this method with a simple block ( a circle with a couple of attributes) without sucess: the PromptNestedEntityResult.GetContainers() returns no container at all, although the picked nested entity is correctly an attribute.

 

But there is still a way to get what you want: AttributeReference.OwnerId will identify the BlockReference that owns the attributereference:

 

The code will look like:

 

 Document dwg = Application.DocumentManager.MdiActiveDocument;
Editor ed = dwg.Editor;
Database db = dwg.Database;
//Pick nested object - Attribute in a block
PromptNestedEntityOptions opt = new
PromptNestedEntityOptions("\nPick an attribute:");
opt.AllowNone = false;
PromptNestedEntityResult res = ed.GetNestedEntity(opt);
if (res.Status == PromptStatus.OK)
{
using (Transaction tran = db.TransactionManager.StartTransaction())
{
AttributeReference att = tran.GetObject(res.ObjectId, OpenMode.ForRead) as AttributeReference;
ed.WriteMessage("\nOwner ({0}): {1}", att.OwnerId.ToString(), att.OwnerId.ObjectClass.DxfName);
BlockReference blk = tran.GetObject(att.OwnerId, OpenMode.ForRead) as BlockReference;
ed.WriteMessage("\nPicked attribute is owned by block \"{0}\"", blk.Name);
tran.Commit();
}
}

 

*Expert Elite*
norman.yuan
Posts: 1,064
Registered: ‎04-27-2009
Message 2 of 4 (362 Views)

Re: BlockReference from AttributeReference

12-04-2012 10:03 AM in reply to: HJohn1

I assume that you use PromptNestedEntityResult object to obtain user picked AttributeReference' ObjectId.

 

According to the document and ObjectBrowser, there is a method: PromptNestedEntityResult.GetContainers(). It looks like the one we could use to find out which blockreference object owns the picked attribute. However, I tried this method with a simple block ( a circle with a couple of attributes) without sucess: the PromptNestedEntityResult.GetContainers() returns no container at all, although the picked nested entity is correctly an attribute.

 

But there is still a way to get what you want: AttributeReference.OwnerId will identify the BlockReference that owns the attributereference:

 

The code will look like:

 

            Document dwg = Application.DocumentManager.MdiActiveDocument;
            Editor ed = dwg.Editor;
            Database db = dwg.Database;

            //Pick nested object - Attribute in a block
            PromptNestedEntityOptions opt = new 
                    PromptNestedEntityOptions("\nPick an attribute:"); 
                opt.AllowNone = false; 
  
            PromptNestedEntityResult res = ed.GetNestedEntity(opt);
            if (res.Status == PromptStatus.OK)
            {
                using (Transaction tran = db.TransactionManager.StartTransaction())
                {
                    AttributeReference att = tran.GetObject(res.ObjectId, OpenMode.ForRead) as AttributeReference;

                    ed.WriteMessage("\nOwner ({0}): {1}", att.OwnerId.ToString(), att.OwnerId.ObjectClass.DxfName);

                    BlockReference blk = tran.GetObject(att.OwnerId, OpenMode.ForRead) as BlockReference;
                    ed.WriteMessage("\nPicked attribute is owned by block \"{0}\"", blk.Name);

                    tran.Commit();
                }
            }

 

Mentor
khoa.ho
Posts: 224
Registered: ‎09-15-2011
Message 3 of 4 (351 Views)

Re: BlockReference from AttributeReference

12-04-2012 11:43 AM in reply to: HJohn1

An appendix to norman.yuan post:

 

AttributeReference.OwnerId gets the BlockReference that owns this AttributeReference.

 

var containers = PromptNestedEntityResult.GetContainers() gets the container(s) of the selected nested entity. If this entity is nested inside a block (innerBlock), and this block is nested inside another block (outerBlock), the result will be containers[0] = innerBlock.ObjectId, containers[1] = outerBlock.ObjectId. The first index is for the inner most nested block, the last index is for the outside overall block.

 

PromptNestedEntityResult.GetContainers() does NOT return anything if you pick on an AttributeReference (it may be a bug), but returns an array if you pick on any other entity (Line, Circle, DBText…). So GetContainers() will not use for attribute, instead we use AttributeReference.OwnerId to get the block reference of this attribute.

 

-Khoa

 

Distinguished Contributor
HJohn1
Posts: 170
Registered: ‎09-26-2008
Message 4 of 4 (317 Views)

Re: BlockReference from AttributeReference

12-05-2012 09:17 PM in reply to: norman.yuan

Thank you very much Norman.  AttributeReference.OwnerId is exactly what I was looking for.  I just needed to go back up one level to the BlockRefence.

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.