SOFAgh / CADability

CADability is a pure .net class library for modeling and analyzing 3d data, both with your code and interactively. It comes with an optional Windows.Forms user interface, which can be adopted to other environments. It does not refer to other 3d modeling libraries. For data exchange you can use STEP, STL or DXF files.

Home Page:https://sofagh.github.io/CADability/CADabilityDoc/articles/general.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SplitCyclical IndexOutOfRangeException

dsn27 opened this issue · comments

commented

Customer reports this error that can occur sometimes.
No repro available yet.

Exception Source: CADability
Exception Type: System.IndexOutOfRangeException
Exception Message: Der Index war außerhalb des Arraybereichs.
Exception Target Site: SplitCyclical

---- Stack Trace ----
   CADability.Shapes.Border.SplitCyclical(positions As Double[])
       Border.cs: line 2081, col 21
   CADability.Shapes.Border.ConvexHull()
       Border.cs: line 4354, col 29
   CADability.Shapes.Border.GetSmallestEnclosingRectangle()
       Border.cs: line 4410, col 13
   LogiCal.CADModule.Calculation.CadHole.ProcessBorder(border As Border)
       CadHole.cs: line 0073, col 13
   LogiCal.CADModule.Calculation.CadItem.ProcessHoles(simple As SimpleShape)
       CadItem.cs: line 0543, col 17
   LogiCal.CADModule.Calculation.CadItem.ProcessSimpleShape(simple As SimpleShape)
       CadItem.cs: line 0403, col 13
   LogiCal.CADModule.Calculation.CadItemCollection.ProcessSingleMode(cshape As CompoundShape)
       CadItemCollection.cs: line 0342, col 17
   LogiCal.CADModule.Calculation.CadItemCollection.ProcessCompoundShape(totalCalcMode As Boolean, cshape As CompoundShape)
       CadItemCollection.cs: line 0143, col 13
   LogiCal.CADModule.Calculation.Calculator.ProcessGeoObjects(totalCalcMode As Boolean, geos As GeoObjectList, data As DefaultData, progress As IProgress`1, ct As CancellationToken)
       Calculator.cs: line 0325, col 13
   LogiCal.CADModule.Calculation.Calculator.ProcessFrame(totalCalcMode As Boolean, geoObjects As GeoObjectList, data As DefaultData, progress As IProgress`1, ct As CancellationToken)
       Calculator.cs: line 0026, col 13
   LogiCal.CADModule.<>c__DisplayClass430_0.<StartCalculation>b__0()
       frmCAD.cs: line 0965, col 54
   System.Threading.Tasks.Task`1.InnerInvoke()
       : N 00078
   System.Threading.Tasks.Task.Execute()
       : N 00071
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       : N 00032
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(task As Task)
       : N 00062
   LogiCal.CADModule.<StartCalculation>d__430.MoveNext()
       frmCAD.cs: line 0965, col 13
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       : N 00032
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(task As Task)
       : N 00062
   LogiCal.CADModule.<bbiStartCalc_ItemClick>d__375.MoveNext()
       frmCAD.cs: line 0501, col 13
   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       : N 00032
commented

If the positions array contains:
double[] positions = { 1.7797981169118655, -1.9057885098560976E-15)

The second itaration of the for loop will fail with: IndexOutOfRange Exception.
Because the index equals -1

The attached DXF file was used to reproduce the problem in our software. But I haven't been able to create code for CADability to make it fail.
IOOR.zip

grafik

commented

Load DXF file from above and run code in button1_Click(...)

Code for reproduction:

        private void button1_Click(object sender, EventArgs e)
        {
            var allObjects = cadControl.CadFrame.ActiveView.Model.AllObjects;
            allObjects.DecomposeAll();
            GeoObjectList geoList2D = Create2DCurves(allObjects);
            var curve2DData = GeoListToCurve2DArray(geoList2D);


            var cShape = CompoundShape.CreateFromList(curve2DData, 0.01d, true, out GeoObjectList deadObjects);
            var sShape = cShape.SimpleShapes[0];
            SmallestEnclosingRect ser = sShape.Outline.GetSmallestEnclosingRectangle();
            Debug.WriteLine(ser.Height + "x" + ser.Width);
        }
        public static GeoObjectList Create2DCurves(GeoObjectList list)
        {
            ICurve ICGo;
            ICurve2D IC2DGo;
            IGeoObject GoNew;

            GeoObjectList golNew = new GeoObjectList(list.Count);

            foreach (IGeoObject gi in list)
            {
                //Projektion in 2D und wiederherstellung des GeoObjects
                ICGo = gi as ICurve;
                if (ICGo != null)
                {
                    IC2DGo = ICGo.GetProjectedCurve(Plane.XYPlane);
                    GoNew = IC2DGo.MakeGeoObject(Plane.XYPlane);
                    GoNew.UserData.Add(gi.UserData);
                    GoNew.CopyAttributes(gi);
                    golNew.Add(GoNew);
                }
                else
                {
                    golNew.Add(gi);
                }
            }

            return golNew;
        }
        public static ICurve2D[] GeoListToCurve2DArray(GeoObjectList list)
        {
            //Convert ICurve to ICurve2D
            List<ICurve2D> curves2d = new List<ICurve2D>(list.Count);

            foreach (IGeoObject g in list)
            {
                if (g is ICurve c)
                {
                    //Project curve to right plane
                    ICurve2D curve2D = c.GetProjectedCurve(Plane.XYPlane);
                    curves2d.Add(curve2D);
                }
            }

            return curves2d.ToArray();
        }
commented

Works like a charm. Thanks!