Is it safe to pass entity as argument into sub function

Is it safe to pass entity as argument into sub function

trithuongle
Advocate Advocate
354 Views
2 Replies
Message 1 of 3

Is it safe to pass entity as argument into sub function

trithuongle
Advocate
Advocate

Good evening everybody. I have a question that i'm writing a small program to trim/ extend multiples of curve by a boundary curve and a pick point (for trim/ extend side).

In the main function , my code is like this :

 

//Start main transaction :
                using (Transaction tr = doc.TransactionManager.StartTransaction())
                {
                    #region [Get boundary curve]
                    //Prompt option :
                    PromptEntityOptions peo = new PromptEntityOptions("\nSelect the boundary curve :");
                    peo.SetRejectMessage("\nInvalid selection. Please select curve !");
                    peo.AddAllowedClass(typeof(Curve), false);

                    //Prompt result :
                    PromptEntityResult per = ed.GetEntity(peo);
                    if (per.Status != PromptStatus.OK)
                        return;
                    else
                        myUnHighlight.Add(per.ObjectId);

                    //Open curve for write :
                    Curve boCurv = (Curve)tr.GetObject(per.ObjectId, OpenMode.ForWrite);

                    //Highlight curve :
                    try
                    {
                        boCurv.Highlight();
                    }
                    catch (System.Exception ex)
                    {
                        ed.WriteMessage(ex.ToString());
                        return;
                        //throw;
                    }
                    #endregion

                    #region [Get curves to trim /extend]
                    //Create a TypedValue array to define the filter criteria :
                    TypedValue[] acTypValAr = new TypedValue[9];
                    acTypValAr.SetValue(new TypedValue((int)DxfCode.Operator, "<or"), 0);
                    acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "LINE"), 1);
                    acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "ARC"), 2);
                    acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "LWPOLYLINE"), 3);
                    acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "POLYLINE"), 4);
                    acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "XLINE"), 5);
                    acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "RAY"), 6);
                    acTypValAr.SetValue(new TypedValue((int)DxfCode.Start, "SPLINE"), 7);
                    acTypValAr.SetValue(new TypedValue((int)DxfCode.Operator, "or>"), 8);

                    //Create new selection filter from above TypedValue 's array :
                    SelectionFilter acSelFtr = new SelectionFilter(acTypValAr);

                    //Prompt option :
                    PromptSelectionOptions pso = new PromptSelectionOptions();
                    pso.MessageForAdding = "\nSelect curves to trim/ extend :";

                    //Prompt result :
                    PromptSelectionResult acSSPrompt = ed.GetSelection(pso, acSelFtr);

                    //Selection set :
                    SelectionSet ss;
                    //Check prompt return value :
                    if (acSSPrompt.Status != PromptStatus.OK ||
                        acSSPrompt.Value.Count == 0)
                    {
                        ed.WriteMessage("\nYou selected nothing !");
                        return;
                    }
                    else
                        ss = acSSPrompt.Value;

                    //Unhighlight boudnary curve :
                    try
                    {
                        boCurv.Unhighlight();
                    }
                    catch (System.Exception ex)
                    {
                        //throw;
                        ed.WriteMessage(ex.ToString());
                        return;
                    }

                    #endregion

                    //Pick the standard point :
                    Point3d stdPnt = per.PickedPoint;
                    if (KRGetPoint(ed, "\nPick the standard point :", Point3d.Origin, ref stdPnt,
                        false) != PromptStatus.OK)
                        return;

                    //Iterate selection set and trim/ extend members :
                    foreach (SelectedObject sObj in ss)
                    {
                        if (sObj != null && sObj.ObjectId != boCurv.ObjectId)
                        {
                            Point3dCollection intPnts = new Point3dCollection();
                            Curve memCurv = (Curve)tr.GetObject(sObj.ObjectId, OpenMode.ForRead);
                            memCurv.IntersectWith(boCurv, Intersect.OnBothOperands, intPnts, IntPtr.Zero, IntPtr.Zero);
                            if (intPnts.Count > 0)
                            {
                                //Call trim sub SubFunction :
                                //ed.WriteMessage("\nCurve is alreadry intersect .Need to trim !");
                                //memCurv.UpgradeOpen();
                                //memCurv.ColorIndex = 7;
                                CmdTE_TrimCurves(ed, tr, boCurv, memCurv, per.PickedPoint);

                            }
                            else
                            {
                                memCurv.IntersectWith(boCurv, Intersect.ExtendThis, intPnts, IntPtr.Zero, IntPtr.Zero);
                                if (intPnts.Count == 0 )
                                {
                                    //Skip to next entity :
                                    continue;
                                }
                                else
                                {
                                    //Call extend SubFunction :
                                    //ed.WriteMessage("\nNeed to call extend .");
                                    CmdTE_ExtendCurves(ed, tr, boCurv, memCurv, per.PickedPoint);

                                }
                            }
                        }
                    }

                    //Commit changes :
                    tr.Commit();
                }

 

And my sub function is like this :

 

void CmdTE_TrimCurves(Editor baseEd, Transaction baseTr,
                              Curve boCurv, Curve memCurv,
                              Point3d pickPnt)
        {
            //Do something :

        }

        void CmdTE_ExtendCurves(Editor baseEd, Transaction baseTr,
                                Curve boCurv, Curve memCurv,
                                Point3d pickPnt)
        {
           //Do something :

        }

 

So is it safe to pass opened entity as argument into sub function ?

Thanks in advance !

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

_gile
Consultant
Consultant
Accepted solution

Hi,

Yes it is safe while the sub-function is called within the scope of the transaction used to open or create the entity.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 3 of 3

trithuongle
Advocate
Advocate

@_gile 

Hi, thanks for your reply.  I understood it .

0 Likes