Question

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.

Was it helpful?

Solution

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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top