.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

how can add multiple attribute tags

4 REPLIES 4
Reply
Message 1 of 5
OsamaElewa
314 Views, 4 Replies

how can add multiple attribute tags

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();
            }
        }

 

4 REPLIES 4
Message 2 of 5
hgasty1001
in reply to: OsamaElewa

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

Message 3 of 5
OsamaElewa
in reply to: hgasty1001

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

Message 4 of 5
Ron_M
in reply to: OsamaElewa

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);

Message 5 of 5
hgasty1001
in reply to: OsamaElewa

Hi,

 

Try this:

 

Public Shared Sub InsertAttibuteInBlockRef(ByVal blkRef As BlockReferenceByVal attributeTag As StringByVal attributeText As StringByVal 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

 

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost