SpaceClaim.AddIn.Unfold.TessellateLoftButtonCapsule.OnExecute C# (CSharp) Method

OnExecute() protected method

protected OnExecute ( System.Windows.Forms.Command command, ExecutionContext context, System buttonRect ) : void
command System.Windows.Forms.Command
context ExecutionContext
buttonRect System
return void
        protected override void OnExecute(Command command, ExecutionContext context, System.Drawing.Rectangle buttonRect)
        {
            if (ScenePart == null)
                return;

            double stepSize = Values[Resources.TessellateLoftStepSize].Value / ActiveWindow.Units.Length.ConversionFactor;

            ITrimmedCurve[] curves = Window.ActiveWindow.GetAllSelectedITrimmedCurves().ToArray();

            if (curves.Length != 2)
                Debug.Fail("Need exactly two curves");

            ITrimmedCurve curve0 = curves[0], curve1 = curves[1];

            //List<Point> points0 = curve0.Shape.GetPolyline() as List<Point>;
            //List<Point> points1 = curve1.Shape.GetPolyline() as List<Point>;
            //Debug.Assert (points0 != null && points1 != null);
            //if ((points1[0] - points0[0]).Magnitude > (points1[points1.Count - 1] - points0[0]).Magnitude)
            //    points1.Reverse();

            if ((curve1.StartPoint - curve0.StartPoint).Magnitude > (curve1.EndPoint - curve0.StartPoint).Magnitude)
                curve1 = CurveSegment.Create(curve1.GetGeometry<Curve>(), Interval.Create(curve1.Bounds.End, curve1.Bounds.Start));

            List<Point> points0 = curve0.TessellateCurve(stepSize) as List<Point>;
            List<Point> points1 = curve1.TessellateCurve(stepSize) as List<Point>;

            if ((points1[0] - points0[0]).Magnitude > (points1[points1.Count - 1] - points0[0]).Magnitude)
                points1.Reverse();

            //int steps = Math.Min(points0.Count, points1.Count);
            //for (int i = 0; i < steps; i++) {
            //    AddInHelper.CreateLines(new List<Point> { points0[i], points1[i] }, part);

            int basePoint0 = points0.Count - 1;
            int basePoint1 = points1.Count - 1;

            List<Body> bodies = new List<Body>();
            while (basePoint0 > 0 || basePoint1 > 0) {
                double base0Diagonal = double.MaxValue;
                double base1Diagonal = double.MaxValue;

                if (basePoint1 > 0)
                    base0Diagonal = (points1[basePoint1 - 1] - points0[basePoint0]).Magnitude;

                if (basePoint0 > 0)
                    base1Diagonal = (points0[basePoint0 - 1] - points1[basePoint1]).Magnitude;

                List<Point> facetPoints = new List<Point>();
                facetPoints.Add(points0[basePoint0]);
                facetPoints.Add(points1[basePoint1]);

                if (base0Diagonal < base1Diagonal)
                    facetPoints.Add(points1[--basePoint1]);
                else
                    facetPoints.Add(points0[--basePoint0]);

                Plane plane = null;
                if (AddInHelper.TryCreatePlaneFromPoints(facetPoints, out plane))
                    bodies.Add(ShapeHelper.CreatePolygon(facetPoints, plane, 0));
            }

            bodies = bodies.TryUnionBodies().ToList();
            Debug.Assert(bodies.Count == 1);

            DesignBody.Create(ScenePart, Resources.TessellateLoftBodyName, bodies[0]);
        }