Author Topic: AutoCAD crashs even with try  (Read 1570 times)

0 Members and 1 Guest are viewing this topic.

Coder

  • Swamp Rat
  • Posts: 827
AutoCAD crashs even with try
« on: January 09, 2016, 06:26:36 AM »
Hello guys,

I am facing a crash of AutoCAD although the try method is used and that didn't help to pass over the sin :-). Can anyone clarify it please?

I know the culprit is the OpenMode.ForRead and must be OpenMode.ForWrite.

Thanks in advance.

Code - C#: [Select]
  1.         public static void changeFormatedMtextValue ()
  2.         {
  3.             Document doc = Application.DocumentManager.MdiActiveDocument;
  4.             Database db = doc.Database;
  5.             Editor ed = doc.Editor;
  6.  
  7.             using (Transaction tr = db.TransactionManager.StartTransaction())
  8.             {
  9.                 try
  10.                 {
  11.                     PromptEntityOptions entOP = new PromptEntityOptions("\nSelect formatted Mtext to strip :");
  12.                     entOP.AllowNone = false;
  13.                     entOP.AllowObjectOnLockedLayer = false;
  14.                     entOP.SetRejectMessage("\nMust select Mtext");
  15.                     entOP.AddAllowedClass(typeof(MText), false);
  16.  
  17.                     PromptEntityResult ent = ed.GetEntity(entOP);
  18.                     if (ent.Status == PromptStatus.OK)
  19.                     {
  20.                         MText mt = (MText)tr.GetObject(ent.ObjectId, OpenMode.ForRead);
  21.                         if (mt != null)
  22.                         {                          
  23.                             mt.Contents =  mt.Text;
  24.                         }    
  25.                     }
  26.                     tr.Commit();
  27.                 }
  28.                 catch (System.Exception exp)
  29.                 {
  30.                     tr.Abort();
  31.                     ed.WriteMessage(exp.Message);
  32.                 }  
  33.             }
  34.         }
  35.  

n.yuan

  • Bull Frog
  • Posts: 348
Re: AutoCAD crashs even with try
« Reply #1 on: January 09, 2016, 09:21:43 AM »
Your post is quite confusing: while you have already known that you need to use OpenMode.ForWrite to modify opened entity, yet you still use OpenMode.ForRead before modify the MText entity. Have you tried OpenMode.ForWrite? Or is there some reasons that make you think you cannot use OpenMode.ForWrite?

By the way, in your case, testing if the opened entity is null is not necessary, because

1. The PromptStatus.OK means a valid entity is selected;
2. the the direct casting ...=(MText)tr.GetObject(... only returns MText, if the casting succeed, or exception. if the casting fails (in your case, it will not fail because the selecting is set to only allow MText being selected!); it never returns NULL.

Coder

  • Swamp Rat
  • Posts: 827
Re: AutoCAD crashs even with try
« Reply #2 on: January 09, 2016, 11:22:37 AM »
Thank you,

The first time I wrote that program I forgot to open the object with OpenMode.ForWrite and used OpenMode.forRead but what's amazed me is that the try block did not pass over that error to the Exception message and caused the AutoCAD to crash which made me to think , what caused the try block not to obtain that error ? and I asked this question in this forum to get more clarifications about this odd thing.

And thanks for your descriptions about my check out on the object Mtext if it is not equal to null, really wonderful.

EDIT: a typo corrected.
« Last Edit: January 09, 2016, 11:43:01 AM by Coder »

n.yuan

  • Bull Frog
  • Posts: 348
Re: AutoCAD crashs even with try
« Reply #3 on: January 09, 2016, 11:33:08 AM »
It is not uncommon that try...catch... in AutoCAD API process does not catch some exceptions thrown by AutoCAD. It is rather unpredictable from my experience. My guess is that because the .NET APIs re mostly wrappers to raw C++ APIs, thus, depending how the .NET APIs are implemented, this kind thing sometimes does happen. It can be considered as AutoCAD .NET APIs' bug. But it might be intentional, or by design, from those who implemented the .NET API, who knows. We sometimes have to live this kind of things.