0

Is there any way to get the position of a specified AcadAttribute in a specified title block in an existing drawing with C#?

Edit : My code is something like bellow,

AcadBlock myBlock = myAcadDoc.Database.Blocks.Items(block_Name);

AcadAttribute myAtt;
foreach(AcadEntity entity in myBlock)
{
    myAtt = entity as AcadAttribute;

    if(myAtt == null) continue;

    if(myAtt.TagString == "Specified_String")
    {
        //Now i want to insert an image exactly where the myAtt attribute is exists
        myAcadDoc.Database.ModelSpace.AddRoster("My image path", myAtt.Position /*myAtt does not have Position property*/, 50.0, 0)
    }
}

I want to insert an image exactly where the myAtt attribute is exists, and it is the reason of why i need the position of the AcadAttribute.

M_Mogharrabi
  • 1,369
  • 5
  • 29
  • 57

1 Answers1

4

AutoDesk's cryptic API has tripped you up a little. You've got to remember that when working with blocks, there is both a definition and a reference. The definition is what you define when you create a block. You tell it what entities describe it, including any attributes. When you insert that block into a drawing, it is a BlockReference, which inherits from Entity. In your case, you're interested in AttributeReference when walking through the block, because you care about it's position relative to the model space, not just the block it's defined in.

foreach (AcadEntity ent in doc.ModelSpace)
{
    var block = ent as AcadBlockReference;
    if (block == null || block.Name != block_Name)
        continue;

    foreach (AcadAttributeReference att in block.GetAttributes())
    {
        if (att.TagString != "Specified_String")
            continue;

        doc.ModelSpace.AddRaster(
            @"C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg",
            att.InsertionPoint, 1, 0);
        break;
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Parrish Husband
  • 3,148
  • 18
  • 40
  • Thanks Locke, It can be my solution, but it has a little problem. The doc.ModelSpace just contains of the block reference of active document but not the blocks which are defined in the active document.In other words the ent object just set to active document. – M_Mogharrabi Mar 17 '14 at 07:07