Hi,
Dears kindly I am trying to add text to attribute block with multiple tags (the attribute block attached as dwg file in this post) but by using the code below the result is coming up like attached image.
so please how can i fill different values in the block attribute.
Always appreciate your help
Thanks a lot
public void CreateMyBlockRef() { Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; PromptPointOptions ppo = new PromptPointOptions("\nSpecify insertion point :"); PromptPointResult ppr = ed.GetPoint(ppo); if (ppr.Status != PromptStatus.OK) return; Point3d insertionPoint = ppr.Value; using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable; BlockTableRecord blockDef = bt["MyBlock"].GetObject(OpenMode.ForRead) as BlockTableRecord; BlockTableRecord ms = bt[BlockTableRecord.ModelSpace].GetObject (OpenMode.ForWrite) as BlockTableRecord; BlockReference blockRef = new BlockReference(insertionPoint, blockDef.ObjectId); ms.AppendEntity(blockRef); tr.AddNewlyCreatedDBObject(blockRef, true); foreach (ObjectId id in blockDef) { DBObject obj = id.GetObject(OpenMode.ForRead); AttributeDefinition attDef = obj as AttributeDefinition; if ((attDef != null) && (!attDef.Constant)) { AttributeReference attRef = new AttributeReference(); attRef.SetAttributeFromBlock (attDef, blockRef.BlockTransform); attRef.TextString = string.Format("x", "y", "z"); blockRef.AttributeCollection.AppendAttribute(attRef); tr.AddNewlyCreatedDBObject(attRef, true); } } tr.Commit(); } }
Hi,
It's not clear from your code if you are trying to insert a block (a block reference), and fill its attributes values (attribute references) or if you are trying to modify the block definition adding more attribute definitions. Please make clear what exactly is your goal.
Gaston Nunez
hi,
the block attribute in the dwg file contains 3 tags I want to add the data in the three tags every on indvidually,
how can i add data to the three tags in that block, the tags are (PLLN, PLIN and PLNUMB) what I want is to add the value in evry tag individually.
thanks a lot
You can change the text value of the attribute only after you've added it to the drawing. Move:
attRef.TextString = string.Format("x", "y", "z");
to after:
tr.AddNewlyCreatedDBObject(attRef, true);
Hi,
Try this:
Public Shared Sub InsertAttibuteInBlockRef(ByVal blkRef As BlockReference, ByVal attributeTag As String, ByVal attributeText As String, ByVal tr As Transaction) Dim btr As BlockTableRecord = DirectCast(tr.GetObject(blkRef.BlockTableRecord, OpenMode.ForRead), BlockTableRecord) For Each attId As ObjectId In btr Dim ent As Entity = DirectCast(tr.GetObject(attId, OpenMode.ForRead), Entity) If TypeOf ent Is AttributeDefinition Then Dim attDef As AttributeDefinition = DirectCast(ent, AttributeDefinition) Dim attRef As New AttributeReference() attRef.SetAttributeFromBlock(attDef, blkRef.BlockTransform) If attRef.Tag = attributeTag.ToUpper Then attRef.TextString = attributeText + "" Dim id As ObjectId = blkRef.AttributeCollection.AppendAttribute(attRef) tr.AddNewlyCreatedDBObject(attRef, True) attRef.Dispose() Exit For End If End If Next End Sub
Call this sub *after* adding the block reference to the database.
Gaston Nunez