Author Topic: Change text object in block reference  (Read 4558 times)

0 Members and 1 Guest are viewing this topic.

Offline fixo

  • Full Member
  • ***
  • Posts: 135
  • Karma: +4/-0
  • Gender: Male
    • prefered language: C
    • Prog expertise: Good
    • View Profile
Change text object in block reference
« on: March 15, 2013, 04:57:16 PM »
   C#
Code: [Select]
     
        // Change the text object in the BlockTableRecord by selecting them on screen
        // This method may have any name
        [CommandMethod("cato", CommandFlags.UsePickSet | CommandFlags.Redraw)]
        public static void ChangeTextInBlockDef()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;

            Database db = doc.Database;

            Editor ed = doc.Editor;

            Transaction tr = db.TransactionManager.StartTransaction();
            using (tr)
            {
                doc.TransactionManager.EnableGraphicsFlush(true);
                PromptNestedEntityOptions pno = new PromptNestedEntityOptions("\nSelect text within block instance: ");
                pno.AllowNone = true;
                pno.UseNonInteractivePickPoint = false;
                PromptNestedEntityResult rs = ed.GetNestedEntity(pno);
                if (rs.Status != PromptStatus.OK) return;
                Entity selent = (Entity)tr.GetObject(rs.ObjectId, OpenMode.ForRead);
                ed.WriteMessage("\nSelected type of {0}", selent.GetType().Name);

                PromptStringOptions pso = new PromptStringOptions("\nEnter new text: ");
                pso.AllowSpaces = true;
                PromptResult res;
                res = ed.GetString(pso);
                if (res.Status != PromptStatus.OK) return;
                string newstr = res.StringResult;
                ed.WriteMessage("\nNew Text Entered:\t{0}", newstr);

                BlockTableRecord btrec = tr.GetObject(selent.OwnerId, OpenMode.ForWrite) as BlockTableRecord;
                // prevent work with attributes and embedded blocks
                if (selent.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(AttributeReference))) |
                    selent.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(BlockReference)))) return;
                if (selent.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(DBText))))
                {
                    DBText txt = selent as DBText;
                    string oldstr = txt.TextString;

                    BlockTableRecord owner = (BlockTableRecord)tr.GetObject(txt.OwnerId, OpenMode.ForRead);
                    ed.WriteMessage("\nName:\t{0}", owner.Name);
                    owner.UpgradeOpen();
                    foreach (ObjectId id in owner)
                    {
                        if (!id.ObjectClass.IsDerivedFrom(RXClass.GetClass(typeof(DBText)))) continue;
                        DBObject obj = tr.GetObject(id, OpenMode.ForRead);
                        DBText blktxt = obj as DBText;
                        if (blktxt.TextString != oldstr) continue;
                        blktxt.UpgradeOpen();
                        blktxt.TextString = newstr;

                    }

                    //--------------------------------------------------------------
                    // add other possible types such as mtext, dimension etc here
                    //--------------------------------------------------------------


                    tr.TransactionManager.QueueForGraphicsFlush();

                    doc.TransactionManager.FlushGraphics();

                    tr.Commit();

                    ed.Regen();
                }
            }
        }

   VB.NET
Code: [Select]
        ' Change the text object in the BlockTableRecord by selecting them on screen
        ' This method may have any name
        <CommandMethod("cato", CommandFlags.UsePickSet Or CommandFlags.Redraw)> _
        Public Shared Sub ChangeTextInBlockDef()
            Dim doc As Document = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument

            Dim db As Database = doc.Database

            Dim ed As Editor = doc.Editor

            Dim tr As Transaction = db.TransactionManager.StartTransaction()
            Using tr
                doc.TransactionManager.EnableGraphicsFlush(True)
                Dim pno As New PromptNestedEntityOptions(vbLf & "Select text within block instance: ")
                pno.AllowNone = True
                pno.UseNonInteractivePickPoint = False
                Dim rs As PromptNestedEntityResult = ed.GetNestedEntity(pno)
                If rs.Status <> PromptStatus.OK Then
                    Return
                End If
                Dim selent As Entity = DirectCast(tr.GetObject(rs.ObjectId, OpenMode.ForRead), Entity)
                ed.WriteMessage(vbLf & "Selected type of {0}", selent.[GetType]().Name)

                Dim pso As New PromptStringOptions(vbLf & "Enter new text: ")
                pso.AllowSpaces = True
                Dim res As PromptResult
                res = ed.GetString(pso)
                If res.Status <> PromptStatus.OK Then
                    Return
                End If
                Dim newstr As String = res.StringResult
                ed.WriteMessage(vbLf & "New Text Entered:" & vbTab & "{0}", newstr)

                Dim btrec As BlockTableRecord = TryCast(tr.GetObject(selent.OwnerId, OpenMode.ForWrite), BlockTableRecord)
                ' prevent work with attributes and embedded blocks
                If selent.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(GetType(AttributeReference))) Or selent.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(GetType(BlockReference))) Then
                    Return
                End If
                If selent.ObjectId.ObjectClass.IsDerivedFrom(RXClass.GetClass(GetType(DBText))) Then
                    Dim txt As DBText = TryCast(selent, DBText)
                    Dim oldstr As String = txt.TextString

                    Dim owner As BlockTableRecord = DirectCast(tr.GetObject(txt.OwnerId, OpenMode.ForRead), BlockTableRecord)
                    ed.WriteMessage(vbLf & "Name:" & vbTab & "{0}", owner.Name)
                    owner.UpgradeOpen()
                    For Each id As ObjectId In owner
                        If Not id.ObjectClass.IsDerivedFrom(RXClass.GetClass(GetType(DBText))) Then
                            Continue For
                        End If
                        Dim obj As DBObject = tr.GetObject(id, OpenMode.ForRead)
                        Dim blktxt As DBText = TryCast(obj, DBText)
                        If blktxt.TextString <> oldstr Then
                            Continue For
                        End If
                        blktxt.UpgradeOpen()

                        blktxt.TextString = newstr
                    Next

                    '--------------------------------------------------------------
                    ' add other possible types such as mtext, dimension etc here
                    '--------------------------------------------------------------


                    tr.TransactionManager.QueueForGraphicsFlush()

                    doc.TransactionManager.FlushGraphics()

                    tr.Commit()

                    ed.Regen()
                End If
            End Using
        End Sub