Author Topic: How to edit inserted dynamic block's attribute textstring and parameter's value  (Read 2429 times)

0 Members and 1 Guest are viewing this topic.

mrl989

  • Guest
I can use VB.NET to modify inserted normal block reference's attribute text string but it's not working for inserted dynamic block. pls help
Best regard.

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
You're more likely to get a response if you post us the code you've got so far, even if it's not working.

We like reading code here.  :-)

mrl989

  • Guest
Code: [Select]
    Public Sub UpdateAtt(ByVal tr As Transaction, ByVal objID As ObjectId)       
            Dim br As BlockReference = DirectCast(tr.GetObject(objID, OpenMode.ForWrite), BlockReference)
            Dim btr As BlockTableRecord = DirectCast(tr.GetObject(br.DynamicBlockTableRecord, OpenMode.ForWrite), BlockTableRecord)
     
            For Each attID As ObjectId In btr
                Dim ent As DBObject = tr.GetObject(attID, OpenMode.ForWrite)
                If TypeOf ent Is AttributeDefinition Then
                    Dim AttDef As AttributeDefinition = ent
                    If AttDef.Tag = "SH" Then AttDef.TextString = "111"                   
                End If            Next
If br is not dynamic block, br's attribute can edit by br's attribute collection, but it's not working for dynamic block, so i think this work can do by attribute definition but it's not working too, thanks for read...

Atook

  • Swamp Rat
  • Posts: 1029
  • AKA Tim
I don't speak VB, but it looks like you're iterating through the attdefs in the block definition, not the blockref. Following is code I use to update attributes in blocks. Note that I'm iterating through the attributes in the blockref.AttributeCollection. Hope this helps.

Code - C#: [Select]
  1. /// <summary>
  2. /// Updates the attributes.
  3. /// </summary>
  4. /// <param name="blockRefID">The block reference objectID.</param>
  5. /// <param name="atts">Dictionary where the key is the tag, the value the textstring</param>
  6. public static int UpdateAttributes(ObjectId blockRefID, Dictionary<string, string> atts)
  7. {
  8.         int updatedAtts = 0;
  9.         using (LockedTransaction acTr = Active.Document.TransactionManager.StartLockedTransaction())
  10.         {
  11.                 BlockReference blockRef = acTr.GetObject(blockRefID, OpenMode.ForRead,true) as BlockReference;
  12.                 if (blockRef!=null)
  13.                 {
  14.                         foreach (KeyValuePair<string, string> att in atts)
  15.                         {
  16.                                 foreach (ObjectId attId in blockRef.AttributeCollection)//<- iterate through the blockref attributes
  17.                                 {
  18.                                         AttributeReference ar = acTr.GetObject(attId, OpenMode.ForRead) as AttributeReference;
  19.                                         if (ar != null && string.Equals(ar.Tag, att.Key, StringComparison.CurrentCultureIgnoreCase))
  20.                                         {
  21.                                                 ar.UpgradeOpen();
  22.                                                 ar.TextString = att.Value;
  23.                                                 ar.DowngradeOpen();
  24.                                                 updatedAtts++;
  25.                                         }
  26.                                 }
  27.  
  28.                         }
  29.  
  30.                 }
  31.                 acTr.Commit();
  32.         }
  33.         return updatedAtts;
  34. }
  35.  

mrl989

  • Guest
you're right, i edit attribute textstring through attref but it's not working for dynamic block, but so stranger, it's working now, maybe i did some mistake on my code before, so thanks Atook for help...many thanks

dublinwesley

  • Guest
How would you delete all text from inside blocks within a drawing?  I have tried to write a command for this which is below and while it compiles it does not run properly:

Code: [Select]

[CommandMethod("DELTEXT")]
        public void DelText()
        {
            // Get the current document and database, and start a transaction
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            Database acCurDb = acDoc.Database;
            using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
            {
                // Open the Block table record for read
                BlockTable acBlkTbl;
                acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId,OpenMode.ForRead) as BlockTable;
                // Open the Block table record Model space for read

                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace],OpenMode.ForRead) as BlockTableRecord;

                int nCnt = 0;
                acDoc.Editor.WriteMessage("\nModel space objects: ");

                // Step through each object in Model space and display the Name of the Object

                foreach (ObjectId acObjId in acBlkTblRec)
                {
                    //If the ObjectClass.DxFName is INSERT, then this block is a reference.
                    if (acObjId.ObjectClass.DxfName == "INSERT")
                    {
                        //acDoc.Editor.WriteMessage("\n" + myBlk.Name);
                        //acDoc.Editor.WriteMessage("\n" + myBlk.BlockName);
                        //acDoc.Editor.WriteMessage("\n" + myBlk.ObjectId.ObjectClass.DxfName);
                        //acDoc.Editor.WriteMessage("\n" + myBlk.ObjectId.ObjectClass.Name);

                        //Open the Block Reference
                        BlockReference myBlkReference = acTrans.GetObject(acObjId, OpenMode.ForRead) as BlockReference;                   
                        BlockTableRecord xRefBlkTableRecs = acTrans.GetObject(myBlkReference.BlockTableRecord,OpenMode.ForRead) as BlockTableRecord;
                       
                        //For each Object in the Block Table Record of the Block Reference
                        foreach (ObjectId myBlkObjId in xRefBlkTableRecs)
                        {
                            acDoc.Editor.WriteMessage("\n Object Class Name: " + myBlkObjId.ObjectClass.Name);
                            acDoc.Editor.WriteMessage("\n Object Class DxFName: " + myBlkObjId.ObjectClass.DxfName);

                            //Check if the object is a text object
                            if (myBlkObjId.ObjectClass.DxfName.ToString().Equals("TEXT"))
                            {
                                acDoc.Editor.WriteMessage("\n This is a Text Item and Will be Deleted");
                               //This is certainly a text object - count it.
                                nCnt = nCnt + 1;
                                //Delete it
                                myBlkReference.UpgradeOpen();
                                myBlkObjId.GetObject(OpenMode.ForWrite).Erase();                           
                                acTrans.Commit();
                                //acDoc.Editor.Regen();
                            }
                        }


                    }
                    else
                    {
                        acDoc.Editor.WriteMessage("\n" + acObjId.ObjectClass.Name.ToString());
                        acDoc.Editor.WriteMessage("\n" + acObjId.ObjectClass.DxfName.ToString());

                    }
                    //acTrans.Commit();
                    //acDoc.Editor.Regen();
                }
                acDoc.Editor.WriteMessage("\n" + nCnt + "Text Objects in Document");

                // If no objects are found then display a message
                if (nCnt == 0)
                {
                    acDoc.Editor.WriteMessage("\n No Text objects found");
                }

            }
        }

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Hi,

You should ahve started another topic.

Anyway, try this (no need to iterate the model space, just iterate the block table):

Code - C#: [Select]
  1.         [CommandMethod("DELTEXT")]
  2.         public void DelText()
  3.         {
  4.             var doc = Application.DocumentManager.MdiActiveDocument;
  5.             var db = doc.Database;
  6.             var ed = doc.Editor;
  7.             var textClass = RXObject.GetClass(typeof(DBText));
  8.             using (var tr = db.TransactionManager.StartTransaction())
  9.             {
  10.                 var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
  11.                 foreach (ObjectId btrId in bt)
  12.                 {
  13.                     var btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
  14.                     if (!(btr.IsLayout || btr.IsFromExternalReference || btr.IsDependent))
  15.                     {
  16.                         foreach (ObjectId id in btr)
  17.                         {
  18.                             if (id.ObjectClass == textClass)
  19.                             {
  20.                                 var Text = (DBText)tr.GetObject(id, OpenMode.ForWrite, false, false);
  21.                                 Text.Erase();
  22.                             }
  23.                         }
  24.                     }
  25.                 }
  26.                 tr.Commit();
  27.             }
  28.             ed.Regen();
  29.         }
Speaking English as a French Frog

dublinwesley

  • Guest
Beautiful - works like a charm!  Thank you!   :yay!: