How to set location for AttributeReference

How to set location for AttributeReference

nguyenthanguth
Contributor Contributor
531 Views
2 Replies
Message 1 of 3

How to set location for AttributeReference

nguyenthanguth
Contributor
Contributor

Hi, I'm here to answer my questions. Hope everybody help please
when i insert a block into autocad drawing. I have a custom edit, it changes the position of position. I have repositioned the block but an error has appeared. attributes do not automatically move by block
below is my image and code
this is when i insert
1.png
this is the result i want.

 

2.png

 
sample my code: I was able to handle it with ed.Command("ATTSYNC", "Name", blockNameInsert); this is probably a bad idea. 

#region Insert block WallVertDowelType3
// Create a new block reference and add it to the current space
using (BlockReference br_Insert = new BlockReference(geoPosition, blockTable[blockNameInsert]))
{
    br_Insert.Layer = genLayer;
    br_Insert.ScaleFactors = getScale;
    br_Insert.Rotation = misRotation;

    // insert block WallVertDowelType3 vào CurrentSpaceId
    BlockTableRecord btrCurrentSpace = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
    btrCurrentSpace.AppendEntity(br_Insert);
    tr.AddNewlyCreatedDBObject(br_Insert, true);

    #region Set Attribute[] of Block Reference | if (btr.HasAttributeDefinitions)
    BlockTableRecord blockInsert = tr.GetObject(blockTable[blockNameInsert], OpenMode.ForRead) as BlockTableRecord;
    foreach (ObjectId oid_ATB in blockInsert)
    {
        DBObject dBObject = tr.GetObject(oid_ATB, OpenMode.ForRead);
        if (dBObject is AttributeDefinition)
        {
            AttributeReference attRef = new AttributeReference();

            // Add the attribute to the block reference
            AttributeDefinition attDef = dBObject as AttributeDefinition;
            attRef.SetAttributeFromBlock(attDef, br_Insert.BlockTransform);
            //attRef.Position = attDef.Position.TransformBy(br_Insert.BlockTransform);

            double temp_DIA = DIA.Contains("D") ? Convert.ToDouble(DIA.Split('D')[1]) : Convert.ToDouble(DIA);
            switch (attRef.Tag)
            {
                case "xxx": attRef.TextString = "xxx"; break;
            }
            // Add attribute to block reference
            br_Insert.AttributeCollection.AppendAttribute(attRef);
            tr.AddNewlyCreatedDBObject(attRef, true);
        }
    }
    #endregion

    #region Set Custom[] of Block Reference Dynamic | if (bref.IsDynamicBlock)
    DynamicBlockReferencePropertyCollection DBRPCollection = br_Insert.DynamicBlockReferencePropertyCollection;
    foreach (DynamicBlockReferenceProperty DBRProperty in DBRPCollection)
    {
        switch (DBRProperty.PropertyName)
        {
            case "xxx": DBRProperty.Value = canhB; break;
        }
    }
    br_Insert.Position = geoPosition; // error generated attribute is not in the correct position. Temporarily fixed with `ATTSYNC`
    #endregion
}
#endregion
ed.Command("ATTSYNC", "Name", blockNameInsert); //still usable but not optimal in terms of performance. i need another way

  

0 Likes
Accepted solutions (1)
532 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor
Accepted solution

Instead of changing Position property of a BlockReference, you use Matrix3d.Displacement() to transform a block for moving it, if the block reference owns attributes:

 

var mt=Matrix3d.Displacement(blk.Position.GetVectorTo([newLocation]);

blk.TransformBy(mt);

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3

nguyenthanguth
Contributor
Contributor

omg. great, excellent, wonderfull. I did it. Thank you very much

0 Likes