MText Unable to update

MText Unable to update

Anonymous
Not applicable
874 Views
3 Replies
Message 1 of 4

MText Unable to update

Anonymous
Not applicable

Hi Team,

 

Unable to update Mtext please find below code.

 

Note: Please advice me there is any way to debug command code .

 

 

 

 

	[CommandMethod("MyTestCommands", "test", CommandFlags.Modal)]
        public void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;
            var res1 = ed.GetFileNameForOpen("Specify parameter file");
            if (res1.Status != Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
                return;

            var res2 = ed.GetString("Specify output sub-folder name");
            if (res2.Status != Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
                return;
            try
            {
                var db = doc.Database;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

                    foreach (ObjectId id in btr)
                    {
                        Entity currentEntity = tr.GetObject(id, OpenMode.ForWrite, false) as Entity;
                        if (currentEntity == null)
                        {
                            continue;
                        }
                        if (currentEntity.GetType() == typeof(MText))
                        {
                            ((MText)currentEntity).Contents = "BlahBlah";
                        }
                        else
                        {
                            ((DBText)currentEntity).TextString = "BlahBlah";
                        }
                    }
                    tr.Commit();
                }

            }
            catch (System.Exception e)
            {
                using (var writer = File.CreateText(Path.Combine(res2.StringResult, "error.txt")))
                {
                    writer.WriteLine(e.ToString());
                }
                ed.WriteMessage("Error: {0}", e.ToString());
            }
        }
0 Likes
Accepted solutions (1)
875 Views
3 Replies
Replies (3)
Message 2 of 4

_gile
Consultant
Consultant
Accepted solution

Hi,

 

There're some mistakes in your code.

 

BlockTableRecord btr = (BlockTableRecord)tr.GetObject(db.BlockTableId, OpenMode.ForRead);

db.BlockTableId is the ID of the database block table which type is BlockTable, not a BlockTableRecord

Assuming you want to get the model space block table record, you can do it this way:

var bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
var btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);

or, using SymbolUtilityServices as in the above code.

 

While iterating the model space entities you cast all entities which are not MText into DBText, this will throw an error with any entity which does not derive from DBText.

 

Here's a little example on how to achieve what you're trying to do:

 

        [CommandMethod("MyTestCommands", "test", CommandFlags.Modal)]
        public void Test()
        {
            var doc = Application.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;
            var res1 = ed.GetFileNameForOpen("Specify parameter file");
            if (res1.Status != Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
                return;

            var res2 = ed.GetString("Specify output sub-folder name");
            if (res2.Status != Autodesk.AutoCAD.EditorInput.PromptStatus.OK)
                return;
            try
            {
                var db = doc.Database;
                using (Transaction tr = db.TransactionManager.StartTransaction())
                {
                    // open the model space block table record for read
                    BlockTableRecord btr = (BlockTableRecord)tr.GetObject(
                        SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead);

                    var textClass = RXObject.GetClass(typeof(DBText));
                    var mtextClass = RXObject.GetClass(typeof(MText));
                    foreach (ObjectId id in btr)
                    {
                        // if this is the ID of a DBText
                        if (id.ObjectClass.IsDerivedFrom(textClass))
                        {
                            var text = (DBText)tr.GetObject(id, OpenMode.ForWrite);
                            text.TextString = "BlahBlah";
                        }
                        // if this is the ID of a MText
                        else if (id.ObjectClass.IsDerivedFrom(mtextClass))
                        {
                            var mtext = (MText)tr.GetObject(id, OpenMode.ForWrite);
                            mtext.Contents = "BlahBlah";
                        }
                    }
                    tr.Commit();
                }

            }
            catch (System.Exception e)
            {
                using (var writer = File.CreateText(Path.Combine(res2.StringResult, "error.txt")))
                {
                    writer.WriteLine(e.ToString());
                }
                ed.WriteMessage("Error: {0}", e.ToString());
            }
        }


Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 4

_gile
Consultant
Consultant

@Anonymous wrote:

Note: Please advice me there is any way to debug command code .

 


You should display the Exception.StackTrace to have more details on the exception

 

 try
{
    // ...
}
catch (System.Exception e)
{
    ed.WriteMessage("\nError: {0}\n{1}", e.Message, e.StackTrace);
}

You can also use break points in you code, and run it step by step in debug mode.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 4 of 4

Anonymous
Not applicable

Hi Gilles Chanteau,

 

Thank you for your replay. My source file text not changing.

 

 

am doing below process.

 

1. Create command dll as zip

2. upload zip and create new package in autodesk design automation API.

3.and creating Activity in design automation API.

4. submitting work item.

5. download the source file from autodesk cloud

 

as given solution changes  not affecting the source file.

 

Regards,

Ragunath.s

 

0 Likes