Not selecting Mtext / pressing escape button breaks autocad.

Not selecting Mtext / pressing escape button breaks autocad.

stefanveurink68AXD
Advocate Advocate
363 Views
2 Replies
Message 1 of 3

Not selecting Mtext / pressing escape button breaks autocad.

stefanveurink68AXD
Advocate
Advocate

I'm asking user for selecting objects, then selecting a line, then selecting an text, then giving a certain point. 

of course it's possible objects, a line, or a text, is not available. so user is pressing escape button. 

 

Now when pressing escape during selecting objects, or selecting a line, the code just ends.  

However, when pressing escape during selecting an mtext, it gives me a bug like: 

" An unhandled exception of type 'System.AccessViolationException' occurred in AcdbMgd.dll

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

Problem is, it's a little unclear where the bug really happens, when stepping through te code it happens at the line AFTER selecting the text, doesn't matter what this next line is, or even if it has to do with the selected line. 

 

Tried many try/catches around different parts but doesn't solve it. 

 

Here's the code of selecting the Mtext: 

 

 

private MText selecteerlegendatekst()
        {
            //selectieinstellingen
            var Popties = new PromptEntityOptions("Selecteer bijbehorende text");
            
            Popties.SetRejectMessage("Dit is geen Mtext");  

            Popties.AddAllowedClass(typeof(MText), true);

            // vars instellen 
            var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            var dat = doc.Database;
            var edi = doc.Editor;

            //get polyline
            var selectie = edi.GetEntity(Popties);

            try
            {



                using (DocumentLock docLock = doc.LockDocument())
                {
                    var tra = dat.TransactionManager.StartTransaction();
                    //converten text 
                    Entity obj = (Entity)tra.GetObject(selectie.ObjectId, OpenMode.ForWrite);

                    MText Ltext = new MText();



                    if (obj.GetType() == typeof(DBText))
                    {
                        DBText dbtext = (DBText)tra.GetObject(selectie.ObjectId, OpenMode.ForWrite);
                        Ltext = naarMText(dbtext);
                    }
                    else
                    {
                        Ltext = (MText)tra.GetObject(selectie.ObjectId, OpenMode.ForWrite);
                    }


                    tra.Dispose();

                    return Ltext;
                }
            }
        }

 

by the way, when Mtext is selected everything works fine. 

 

So, anyone know what could be the problem, or more important, how to solve this?

0 Likes
Accepted solutions (1)
364 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor
Accepted solution

It looks like you miss a bit of very basic knowledge of using Editor.GetXXXX() methods: these methods return a PromptXxxxxResults, derived from PrompResult class. Your code SHOULD ALWAYS test the PrompXxxxResult's Status property to decide what user did: PromptStatus.Ok/Keyword/Cancel/None/Error/Other...Typically the code would be like:

 

var selectie = edi.GetEntity(Popties);
if (selectie.Status == PromptStatus.OK)
{
   // You go ahead with the selected entity...
}
else
{
  // user did not select an entity, you either
  // exist the current method, or redirect the code
  // to do something else
}

 

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3

stefanveurink68AXD
Advocate
Advocate

Thanks 👍

0 Likes