how avoid getting exception when prompt point result is null?

how avoid getting exception when prompt point result is null?

a.kouchakzadeh
Advocate Advocate
337 Views
2 Replies
Message 1 of 3

how avoid getting exception when prompt point result is null?

a.kouchakzadeh
Advocate
Advocate

Hello to all
Im facing a problem, my application asks the user to pick a point but when the user presses escape,  the application throws this exception:

akouchakzadeh_0-1653921324663.png

then non of the other application work and after a few seconds, autocad crashes.

I noticed when the transaction is not aborted, this happens.

so what Im doing is manually aborting the transaction from the caller method.

this is the code where I abort the transaction:

I have to validate if pointList is null in the last piece of my code in the first method.

public static void TestBlockIntersection2()
        {

            var doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            var ed = doc.Editor;
            var db = doc.Database;
            var tr = db.TransactionManager.StartTransaction();

            var layerTable = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForWrite);
            var layerName = "SPRKVIEW";
            
            if (!layerTable.Has(layerName))
            {
                ed.WriteMessage("\nSPRKVIEW layer does not exist!");
                CreateLayers(layerName as String, layerTable as LayerTable);
            }

            List<Point3d> pointList = CornerPoints(ed as Editor);
            if (pointList==null)
            {
                tr.Abort();
                return;
            }
}


private static List<Point3d> CornerPoints(Editor ed)
        {
            var ppo = new PromptPointOptions("\nPick first corner");
            ppo.AllowNone=true;
            PromptPointResult ppr = ed.GetPoint(ppo);
            if (ppr.Status!=PromptStatus.OK) return null;

            Point3d firstCorner = ppr.Value;
            var pco = new PromptCornerOptions("\npick second corner", firstCorner);
            //pco.BasePoint=firstCorner;
            var pcr = ed.GetCorner(pco);

            if (pcr.Status!=PromptStatus.OK) return null;
                
            Point3d secondCorner = pcr.Value;

            var pointLists = new List<Point3d>();
            pointLists.Add(firstCorner);
            pointLists.Add(secondCorner);
            return pointLists;
        }

do I have to abort the transaction every single time Im getting a value from another method?

lets say I have 50 other methods that the user might press escape. do I have to validate the value every single time?

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

norman.yuan
Mentor
Mentor
Accepted solution

Pressing Esc does not stop code with exception, if you always test PromptResult.Status after Editor.Getxxxx() call. It is your code that raises exception, if the code does not deal the PromptResult correctly (I do not see the code you showed here would raise exception).

 

As for wanting avoid calling Transaction.Abort() if user cancels input, it is mostly the code workflow issue. If a process (say, a command) expects user input or inputs, whenever possible, you should collect user input(s) first BEFORE starting a Transaction, so you do not have to abort it because of inputting is cancelled.  Takes your code as example, you would do this (pseudo code):

 

void TestBlockIntersection()

{

  var dwg=...

  var ed=dwg.Editor;

  // collect point first. If user input is cancelled, return

  var cornerPoints=GetCornerPoints(ed);

  if (cornerPoints == null) return;

  // Open transaction

  using (var tran=dwg.TransactionManager.StartTrnsaction())

  {

    // Create layer if necessary

    ... ...

 

    // Do something with the collected corner points

    ... ... 

    tran.Commit()

  }

}

 

Of course, some times you may have a loop including user input to be executed. In this case, often, you may not want to abort entire transaction for the changes made by each loop, right? So, you would need to decide, when user cancels the input to get out the loop, to commit the change that have done by completed loops, or abort all the changes.

 

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 3 of 3

a.kouchakzadeh
Advocate
Advocate

thanks for the explanation Sir.

 

0 Likes