Example #1
0
        static void notesImagePlanar_Executing(object sender, EventArgs e)
        {
            Bitmap bitmap = OpenBitmap();

            if (bitmap == null)
            {
                return;
            }

            Part part = CreateImagePart();

            Point[] points = new Point[4];
            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    points[0] = Point.Create((i + 0) * stepSize, (j + 0) * stepSize, 0);
                    points[1] = Point.Create((i + 1) * stepSize, (j + 0) * stepSize, 0);
                    points[2] = Point.Create((i + 1) * stepSize, (j + 1) * stepSize, 0);
                    points[3] = Point.Create((i + 0) * stepSize, (j + 1) * stepSize, 0);

                    DesignBody designBody = ShapeHelper.CreatePolygon(points, Plane.PlaneXY, 0, part);
                    designBody.SetColor(null, GetOpaquePixel(bitmap, i, j));
                }
            }
        }
Example #2
0
        protected override void CreateAlignmentBodies(out Body addendumBody, out Body dedendumBody)
        {
            addendumBody = ShapeHelper.CreatePolygon(new Point[] {
                StartCone.WrapPoint(GearData.PitchAngle, GearData.AddendumRadius - GearData.PitchRadius),
                StartCone.WrapPoint(GearData.PitchAngle, 0),
                EndCone.WrapPoint(GearData.PitchAngle, 0),
                EndCone.WrapPoint(GearData.PitchAngle, EndScale * (GearData.AddendumRadius - GearData.PitchRadius))
            }, 0);

            dedendumBody = ShapeHelper.CreatePolygon(new Point[] {
                StartCone.WrapPoint(0, GearData.BaseRadius - GearData.PitchRadius),
                StartCone.WrapPoint(0, 0),
                EndCone.WrapPoint(0, 0),
                EndCone.WrapPoint(0, EndScale * (GearData.BaseRadius - GearData.PitchRadius))
            }, 0);
        }
Example #3
0
        protected override void CreateAlignmentBodies(out Body addendumBody, out Body dedendumBody)
        {
            addendumBody = ShapeHelper.CreatePolygon(new Point[] {
                Point.Create(GearData.AddendumRadius, 0, 0),
                Point.Create(GearData.PitchRadius, 0, 0),
                Point.Create(GearData.PitchRadius, 0, -Depth),
                Point.Create(GearData.AddendumRadius, 0, -Depth)
            }, 0);

            addendumBody.Transform(GearData.HalfToothTrans);

            dedendumBody = ShapeHelper.CreatePolygon(new Point[] {
                Point.Create(GearData.BaseRadius, 0, 0),
                Point.Create(GearData.PitchRadius, 0, 0),
                Point.Create(GearData.PitchRadius, 0, -Depth),
                Point.Create(GearData.BaseRadius, 0, -Depth)
            }, 0);
        }
Example #4
0
        static void notesImageCylindrical_Executing(object sender, EventArgs e)
        {
            Bitmap bitmap = OpenBitmap();

            if (bitmap == null)
            {
                return;
            }

            Part part = CreateImagePart();

            int    width  = bitmap.Width;
            double radius = 0.5 * stepSize / Math.Sin(Math.PI / width) * Math.PI;

            Point[] points = new Point[4];
            for (int i = 0; i < width; i++)
            {
                double angle1 = (double)i / width * 2 * Math.PI;
                double angle2 = (double)(i + 1) / width * 2 * Math.PI;
                for (int j = 0; j < bitmap.Height; j++)
                {
                    double x1 = Math.Sin(angle1) * radius;
                    double y1 = Math.Cos(angle1) * radius;
                    double z1 = j * stepSize;

                    double x2 = Math.Sin(angle2) * radius;
                    double y2 = Math.Cos(angle2) * radius;
                    double z2 = (j + 1) * stepSize;

                    points[0] = Point.Create(x1, y1, z1);
                    points[1] = Point.Create(x1, y1, z2);
                    points[2] = Point.Create(x2, y2, z2);
                    points[3] = Point.Create(x2, y2, z1);

                    DesignBody designBody = ShapeHelper.CreatePolygon(points, null, 0, part);
                    designBody.SetColor(null, GetOpaquePixel(bitmap, i, j));
                }
            }
        }
Example #5
0
        public ICollection <DesignBody> CreateSolid(Part mainPart)
        {
            var    bands        = new List <ICollection <Body> >();
            var    cutters      = new List <Body[]>();
            double newScale     = 0.094;
            double cutterHeight = 0.01 / newScale;
            double cutterWidth  = 0.0005 / newScale;

            bool swap = false;

            for (int i = 0; i < iSteps; i++)
            {
                var band = new List <Body>();


                //if (i == 4) {
                //        DesignCurve.Create(Window.ActiveWindow.Scene as Part, CurveSegment.Create(points[i][0], points[i][1]));
                //        DesignCurve.Create(Window.ActiveWindow.Scene as Part, CurveSegment.Create(points[i + iSteps / 2][jSteps / 2], points[i + iSteps / 2][jSteps / 2 + 1]));
                //}


                for (int j = 0; j < jSteps; j++)
                {
                    // Main ring
                    Point p00 = points[i][j];
                    Point p01 = points[i][j + 1];
                    Point p10 = points[i + 1][j];
                    Point p11 = points[i + 1][j + 1];

                    Body b0, b1;
                    if ((p00 - p11).Magnitude < (p10 - p01).Magnitude)
                    {
                        b0 = ShapeHelper.CreatePolygon(new Point[] { p00, p01, p11 }, 0);
                        b1 = ShapeHelper.CreatePolygon(new Point[] { p00, p11, p10 }, 0);
                    }
                    else
                    {
                        b0 = ShapeHelper.CreatePolygon(new Point[] { p01, p10, p00 }, 0);
                        b1 = ShapeHelper.CreatePolygon(new Point[] { p01, p11, p10 }, 0);
                    }

                    // Tabs

                    /*            Male      Female      Male
                     * ---p00last-------p00--------p01-------p10next--- v+
                     *       |           |          |           |
                     *       |    pn0    |          |    pn1    |
                     *       |           |          |           |
                     * ---p10last-------p10--------p11-------p11next---
                     *
                     */
                    Point pn0 = (new Point[] { points[i - 1][j], points[i - 1][j + 1] }).Average();
                    Point pn1 = (new Point[] { points[i + 2][j], points[i + 2][j + 1] }).Average();

                    Direction normal0 = Vector.Cross(p01 - pn0, p00 - pn0).Direction;
                    Direction normal1 = Vector.Cross(p10 - pn1, p11 - pn1).Direction;

                    Body tab0 = Tabs.CreateCircularTab(p01, p00, -normal0, tabAngles[i][j], swap);
                    Body tab1 = Tabs.CreateCircularTab(p10, p11, -normal1, tabAngles[i + 1][j], !swap);

                    //DesignBody annotateMe = DesignBody.Create(mainPart, "annotatme", (swap ? tab0 : tab1).Copy());
                    //NoteHelper.AnnotateFace(mainPart, annotateMe.Faces.First(), string.Format("{0},{1}", i, j), 0.02, null);
                    //annotateMe.Delete();

                    //DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", b0.Copy());
                    //DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", b1.Copy());
                    //DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", tab0.Copy());
                    //DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", tab1.Copy());

                    try {
                        b0.Unite(new Body[] { b1, tab0, tab1 });
                    }
                    catch {
                        DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", b0.Copy());
                        DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", b1.Copy());
                        DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", tab0.Copy());
                        DesignBody.Create(Window.ActiveWindow.Scene as Part, "test", tab1.Copy());
                        return(null);
                    }

                    //	Debug.Assert(b0.Shells.Count == 1);
                    band.Add(b0);

                    swap = !swap;
                }

                bands.Add(band.TryUnionBodies());

                // Cutters
                Point p0ThisSide0  = points[i][0];
                Point p0ThisSide1  = points[i][1];
                Point p0OtherSide0 = points[i + iSteps / 2][jSteps / 2];
                Point p0OtherSide1 = points[i + iSteps / 2][1 + jSteps / 2];

                Point p1ThisSide0  = points[i + 1][0];
                Point p1ThisSide1  = points[i + 1][1];
                Point p1OtherSide0 = points[i + 1 + iSteps / 2][jSteps / 2];
                Point p1OtherSide1 = points[i + 1 + iSteps / 2][1 + jSteps / 2];

                Point p0 = CurveSegment.Create(p0ThisSide0, p0ThisSide1).GetInBetweenPoint(
                    CurveSegment.Create(p0OtherSide0, p0OtherSide1
                                        ));

                Point p1 = CurveSegment.Create(p1ThisSide0, p1ThisSide1).GetInBetweenPoint(
                    CurveSegment.Create(p1OtherSide0, p1OtherSide1
                                        ));

                //Point p0 = CurveSegment.Create(p0ThisSide0, p0ThisSide1).IntersectCurve(
                //    CurveSegment.Create(p0OtherSide0, p0OtherSide1
                //)).First().Point;

                //Point p1 = CurveSegment.Create(p1ThisSide0, p1ThisSide1).IntersectCurve(
                //    CurveSegment.Create(p1OtherSide0, p1OtherSide1
                //)).First().Point;

                Direction n0 = (p0OtherSide1 - p0OtherSide0).Direction;
                Direction n1 = (p1OtherSide1 - p1OtherSide0).Direction;

                Direction d0 = (p0ThisSide1 - p0ThisSide0).Direction;
                Direction d1 = (p1ThisSide1 - p1ThisSide0).Direction;

                var profiles = new List <ICollection <ITrimmedCurve> >();
                profiles.Add(p0.GetRectanglePointsAround(d0 * cutterHeight, n0 * cutterWidth).GetProfile());
                profiles.Add(p1.GetRectanglePointsAround(d1 * cutterHeight, n1 * cutterWidth).GetProfile());
                Body cutterA = Body.LoftProfiles(profiles, false, true);

                profiles = new List <ICollection <ITrimmedCurve> >();
                profiles.Add(p0.GetRectanglePointsAround(n0 * cutterHeight, d0 * cutterWidth).GetProfile());
                profiles.Add(p1.GetRectanglePointsAround(n1 * cutterHeight, d1 * cutterWidth).GetProfile());
                Body cutterB = Body.LoftProfiles(profiles, false, true);

                cutters.Add(new Body[] { cutterA, cutterB });
            }

            var    designBands = new List <DesignBody>();
            Layer  cutterLayer = NoteHelper.CreateOrGetLayer(mainPart.Document, "Cutters", System.Drawing.Color.DarkViolet);
            Matrix scaleMatrix = Matrix.CreateScale(scale, Point.Origin);

            for (int i = 0; i < bands.Count; i++)
            {
                int whichCutter = i % 2;

                Part part = Part.Create(mainPart.Document, string.Format("Band {0:00}", i));
                Component.Create(mainPart, part);

                int ii = i;
                if (whichCutter == 0)
                {
                    ii = i + iSteps / 2;
                }

                List <Body> mergedCutters = new Body[] {
                    cutters[(ii + iSteps - 1) % iSteps][whichCutter].Copy(),
                    cutters[ii % iSteps][whichCutter].Copy(),
                    cutters[(ii + 1) % iSteps][whichCutter].Copy()
                }.TryUnionBodies().ToList();

                Debug.Assert(mergedCutters.Count == 1, "Couldn't merge cutters");

                double nominalRadius = 0.02;
                double innerRadius   = (nominalRadius - cutterWidth / 2) / newScale;
                double outerRadius   = (nominalRadius + cutterWidth / 2) / newScale;

                var edgeRounds = new List <KeyValuePair <Edge, EdgeRound> >();
                foreach (Edge edge in mergedCutters[0].Edges)
                {
                    if (edge.Length > cutterHeight * 1.1 || edge.Length < cutterHeight * 0.9)
                    {
                        continue;
                    }

                    double angle = edge.GetAngle();
                    if (Math.Abs(angle) > Math.PI / 4 || angle == 0)
                    {
                        continue;
                    }

                    edgeRounds.Add(new KeyValuePair <Edge, EdgeRound>(edge, new FixedRadiusRound(angle > 0 ? outerRadius : innerRadius)));
                }

                mergedCutters[0].RoundEdges(edgeRounds);

                mergedCutters.Add(cutters[(ii - 1 + iSteps / 2) % iSteps][1 - whichCutter].Copy());
                mergedCutters.Add(cutters[(ii + 1 + iSteps / 2) % iSteps][1 - whichCutter].Copy());

                HSBColor hsbColor = new HSBColor(0, 100, 200);
                hsbColor.H = (float)((double)i / bands.Count * 360);

                var cutBand = new List <Body>();
                foreach (Body body in bands[i])
                {
                    foreach (Body cutterBody in mergedCutters)
                    {
                        body.Imprint(cutterBody);
                        foreach (Face face in body.Faces)
                        {
                            if (!IsSpanningBody(face, cutterBody))
                            {
                                continue;
                            }

                            body.DeleteFaces(new Face[] { face }, RepairAction.None);
                            //	DesignBody designBody = DesignBody.Create(part, "Cutter", cutterBody.Copy());
                            //	designBody.SetColor(null, hsbColor.Color);
                        }
                    }

                    cutBand.AddRange(body.SeparatePieces());
                }

                cutBand = cutBand.TryUnionBodies().ToList();

                //foreach (Body body in bands[i]) {
                foreach (Body body in cutBand)
                {
                    body.Transform(scaleMatrix);
                    DesignBody designBody = DesignBody.Create(part, "Band", body);
                    designBody.SetColor(null, hsbColor.Color);
                    designBands.Add(designBody);
                }

                //foreach (Body body in mergedCutters) {
                //    DesignBody designBody = DesignBody.Create(part, "Cutter", body);
                //    designBody.Layer = cutterLayer;
                //    hsbColor.H += 180 * whichCutter;
                //    designBody.SetColor(null, hsbColor.Color);
                ////	designBands[i].Shape.Imprint(designBody.Shape);
                //}
            }

            Trace.WriteLine("vParameters");
            for (int j = 0; j < jSteps; j++)
            {
                for (int i = 0; i < iSteps; i++)
                {
                    Trace.Write(vParameters[i][j] + " ");
                }

                Trace.WriteLine("");
            }

            Trace.WriteLine("tabAngles");
            for (int j = 0; j < jSteps; j++)
            {
                for (int i = 0; i < iSteps; i++)
                {
                    Trace.Write(tabAngles[i][j] + " ");
                }

                Trace.WriteLine("");
            }


            return(designBands);
        }
        protected override void OnExecute(Command command, ExecutionContext context, System.Drawing.Rectangle buttonRect)
        {
            double surfaceDeviation = Values[Resources.TesselateSurfaceDeviationText].Value / ActiveWindow.Units.Length.ConversionFactor;
            double angleDeviation   = Values[Resources.TesselateAngleDeviationText].Value / ActiveWindow.Units.Angle.ConversionFactor;

            ICollection <Face> faces       = null;
            IDesignBody        iDesignBody = Window.ActiveWindow.ActiveContext.SingleSelection as IDesignBody;

            if (iDesignBody != null)
            {
                faces = (iDesignBody.Master.Shape as Body).Faces;
            }
            else
            {
                ICollection <IDesignFace> iDesignFaces = Window.ActiveWindow.ActiveContext.GetSelection <IDesignFace>();
                foreach (IDesignFace iDesignFace in iDesignFaces)
                {
                    iDesignBody = iDesignFace.GetAncestor <DesignBody>();
                    continue; // TBD: handle multiple selection of faces across different DesignBodies
                }

                if (iDesignBody != null)
                {
                    List <Face> modelerFaces = new List <Face>();
                    foreach (DesignFace designFace in iDesignFaces)
                    {
                        if (designFace.GetAncestor <DesignBody>() == iDesignBody)
                        {
                            modelerFaces.Add(designFace.Shape as Face);
                        }
                    }
                    if (modelerFaces.Count > 0)
                    {
                        faces = modelerFaces;
                    }
                }
                else
                {
                    return;
                }
            }

            iDesignBody.SetVisibility(null, false);
            Body body = iDesignBody.Shape as Body;

            IDictionary <Face, FaceTessellation> tessellationMap = body.GetTessellation(faces, FacetSense.RightHanded, new TessellationOptions(surfaceDeviation, angleDeviation));

            Body        testfacetBody = null;
            List <Body> facetBodies   = new List <Body>();

            Point[] points = new Point[3];

            foreach (FaceTessellation faceTessellation in tessellationMap.Values)
            {
                IList <FacetVertex> vertices = faceTessellation.Vertices;
                foreach (FacetStrip facetStrip in faceTessellation.FacetStrips)
                {
                    foreach (Facet facet in facetStrip.Facets)
                    {
                        points[0] = vertices[facet.Vertex0].Position;
                        points[1] = vertices[facet.Vertex1].Position;
                        points[2] = vertices[facet.Vertex2].Position;

                        testfacetBody = ShapeHelper.CreatePolygon(points, null, 0);
                        if (testfacetBody != null)
                        {
                            facetBodies.Add(testfacetBody);
                        }
                    }
                }
            }

            // when the topology has holes (e.g. an annulus made with one surface), non-manifold vertices can occur when merging triangles around the hole.  The workaround is to merge the fragment lumps.
            facetBodies = new List <Body>(facetBodies.TryUnionBodies());
            facetBodies = new List <Body>(facetBodies.TryUnionBodies());
            foreach (Body facetBody in facetBodies)
            {
                DesignBody.Create(iDesignBody.GetAncestor <Part>(), "Tesselation", facetBody);
            }
        }
        protected override void OnExecute(Command command, ExecutionContext context, System.Drawing.Rectangle buttonRect)
        {
            Part part = Window.ActiveWindow.Scene as Part;

            Debug.Assert(part != null);

            Double stepSize = 0.001;
            string tessellateLoftResolutionPropertyName = "Loft Tessellation Resolution";

            if (!part.Document.CustomProperties.ContainsKey(tessellateLoftResolutionPropertyName))
            {
                CustomProperty.Create(part.Document, tessellateLoftResolutionPropertyName, stepSize);
            }

            CustomProperty property;

            if (part.Document.CustomProperties.TryGetValue(tessellateLoftResolutionPropertyName, out property))
            {
                stepSize = (double)property.Value;
            }

            List <ITrimmedCurve> curves = new List <ITrimmedCurve>(Window.ActiveWindow.GetAllSelectedITrimmedCurves());

            if (curves.Count < 3)
            {
                return;
            }

            Point startPoint = curves[0].StartPoint;

            if (curves[1].StartPoint != startPoint)
            {
                if (curves[1].StartPoint != startPoint)
                {
                    curves[0] = CurveSegment.Create(curves[0].GetGeometry <Curve>(), Interval.Create(curves[0].Bounds.End, curves[0].Bounds.Start)); // TBD Figure out why we can't call ReverseCurves and debug
                }
                else
                {
                    curves[1] = CurveSegment.Create(curves[1].GetGeometry <Curve>(), Interval.Create(curves[1].Bounds.End, curves[1].Bounds.Start));
                }
            }

            for (int i = 2; i < curves.Count; i++)
            {
                if (curves[i].StartPoint != startPoint)
                {
                    curves[i] = CurveSegment.Create(curves[i].GetGeometry <Curve>(), Interval.Create(curves[i].Bounds.End, curves[i].Bounds.Start));
                }

                if (curves[i].StartPoint != startPoint)
                {
                    return;
                }
            }

            double endZ = double.NegativeInfinity;

            foreach (ITrimmedCurve curve in curves)
            {
                if (curve.EndPoint.Z > endZ)
                {
                    endZ = curve.EndPoint.Z;
                }
            }

            Plane  startPlane = Plane.Create(Frame.Create(startPoint, Direction.DirX, Direction.DirY));
            double cuttingZ   = startPoint.Z;
            List <List <Point> > curveSteps = new List <List <Point> >();
            List <List <Point> > insetSteps = new List <List <Point> >();

            while (true)
            {
                cuttingZ -= stepSize;
                if (cuttingZ < endZ)
                {
                    break;
                }

                Plane        cuttingPlane = Plane.Create(Frame.Create(Point.Create(startPoint.X, startPoint.Y, cuttingZ), Direction.DirX, Direction.DirY));
                List <Point> curvePoints  = new List <Point>();
                List <Point> planarPoints = new List <Point>();
                foreach (ITrimmedCurve curve in curves)
                {
                    ICollection <IntPoint <SurfaceEvaluation, CurveEvaluation> > surfaceIntersections = cuttingPlane.IntersectCurve(curve.GetGeometry <Curve>());
                    foreach (IntPoint <SurfaceEvaluation, CurveEvaluation> surfaceIntersection in surfaceIntersections)
                    {
                        Point point = surfaceIntersection.Point;
                        curvePoints.Add(point);

                        Point     projectedPoint = startPlane.ProjectPoint(point).Point;
                        Direction direction      = (projectedPoint - startPoint).Direction;
                        double    length         = CurveSegment.Create(curve.GetGeometry <Curve>(), Interval.Create(curve.Bounds.Start, surfaceIntersection.EvaluationB.Param)).Length;
                        planarPoints.Add(startPoint + direction * length);

                        break; // assume one intersection
                    }
                }

                List <Point> insetPoints = new List <Point>();
                for (int i = 0; i < planarPoints.Count; i++)
                {
                    int ii = i == planarPoints.Count - 1 ? 0 : i + 1;

                    ICollection <Point> pointCandidates = AddInHelper.IntersectSpheres(new Sphere[] {
                        Sphere.Create(Frame.Create(startPoint, Direction.DirX, Direction.DirY), ((startPoint - curvePoints[i]).Magnitude + (startPoint - curvePoints[ii]).Magnitude) / 2),
                        Sphere.Create(Frame.Create(curvePoints[i], Direction.DirX, Direction.DirY), (startPoint - curvePoints[i]).Magnitude),
                        Sphere.Create(Frame.Create(curvePoints[ii], Direction.DirX, Direction.DirY), (startPoint - curvePoints[ii]).Magnitude)
                    });

                    Point planarMidPoint = Point.Origin + (planarPoints[i] + planarPoints[ii].Vector).Vector / 2;
                    Point insetPoint;
                    foreach (Point point in pointCandidates)
                    {
                        Point testPoint = startPlane.ProjectPoint(point).Point;

                        if ((testPoint - planarMidPoint).Magnitude < (insetPoint - planarMidPoint).Magnitude)
                        {
                            insetPoint = point;
                        }
                    }

                    insetPoints.Add(insetPoint);
                }

                curveSteps.Add(curvePoints);
                insetSteps.Add(insetPoints);
            }

            for (int i = 0; i < curveSteps.Count - 1; i++)
            {
                for (int j = 0; j < curveSteps[i].Count; j++)
                {
                    int jj = j == curveSteps[i].Count - 1 ? 0 : j + 1;

                    ShapeHelper.CreatePolygon(new Point[] { curveSteps[i][j], curveSteps[i + 1][j], insetSteps[i][j] }, 0, null);
                    ShapeHelper.CreatePolygon(new Point[] { curveSteps[i + 1][j], insetSteps[i][j], insetSteps[i + 1][j] }, 0, null);
                    ShapeHelper.CreatePolygon(new Point[] { insetSteps[i][j], insetSteps[i + 1][j], curveSteps[i][jj] }, 0, null);
                    ShapeHelper.CreatePolygon(new Point[] { insetSteps[i + 1][j], curveSteps[i][jj], curveSteps[i + 1][jj] }, 0, null);
                }
            }

            return;
        }
        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]);
        }
Example #9
0
        static void notesImageSpherical_Executing(object sender, EventArgs e)
        {
            Bitmap bitmap = OpenBitmap();

            if (bitmap == null)
            {
                return;
            }

            Part part = CreateImagePart();

            int    width  = bitmap.Width;
            int    height = bitmap.Height;
            double radius = 0.5 * stepSize / Math.Sin(Math.PI / width) * Math.PI;

            Point startPoint = Point.Create(radius, 0, 0);
            Line  yAxis      = Line.Create(Point.Origin, Direction.DirY);
            Line  zAxis      = Line.Create(Point.Origin, Direction.DirZ);

            List <Point> points = new List <Point>(4);

            for (int i = 0; i < 4; i++)
            {
                points.Add(Point.Origin);
            }

            for (int i = 0; i < width; i++)
            {
                double angle1 = (double)i / width * 2 * Math.PI;
                double angle2 = (double)(i + 1) / width * 2 * Math.PI;
                for (int j = 0; j < height; j++)
                {
                    double azimuth1 = ((double)j) / height * Math.PI - Math.PI / 2;
                    double azimuth2 = ((double)j + 1) / height * Math.PI - Math.PI / 2;

                    points[0] = Matrix.CreateRotation(zAxis, angle1) * Matrix.CreateRotation(yAxis, azimuth1) * startPoint;
                    points[1] = Matrix.CreateRotation(zAxis, angle1) * Matrix.CreateRotation(yAxis, azimuth2) * startPoint;
                    points[2] = Matrix.CreateRotation(zAxis, angle2) * Matrix.CreateRotation(yAxis, azimuth2) * startPoint;
                    points[3] = Matrix.CreateRotation(zAxis, angle2) * Matrix.CreateRotation(yAxis, azimuth1) * startPoint;

                    Point?extraPoint = null;
                    if (Accuracy.Equals(points[3], points[0]))
                    {
                        extraPoint = points[0];
                        points.Remove(points[0]);
                    }
                    else
                    {
                        for (int k = 0; k < 3; k++)
                        {
                            if (Accuracy.Equals(points[k], points[k + 1]))
                            {
                                extraPoint = points[k];
                                points.Remove(points[k]);
                                break;
                            }
                        }
                    }

                    DesignBody designBody = ShapeHelper.CreatePolygon(points, null, 0, part);
                    designBody.SetColor(null, GetOpaquePixel(bitmap, i, j));

                    if (extraPoint != null)
                    {
                        points.Add(extraPoint.Value);
                    }
                }
            }
        }
Example #10
0
        protected override void OnExecute(Command command, ExecutionContext context, System.Drawing.Rectangle buttonRect)
        {
            Window activeWindow = Window.ActiveWindow;
            Part   part         = activeWindow.Scene as Part;

            int bandCount = 2;
            int iSteps    = bandCount * 24;
            int jSteps    = 24;

            //double uStep = 2 * Math.PI / iSteps;
            double vStep = 2 * Math.PI / jSteps;
            bool   swap  = false;

            for (int j = 0; j < jSteps; j++)
            {
                double v = 2 * Math.PI * j / jSteps;
                List <List <Body> > bands = new List <List <Body> >();

                for (int i = 0; i < iSteps; i++)
                {
                    double u00 = Figure8ParamCurve(v, ((double)i - 0.5) / iSteps);
                    double u10 = Figure8ParamCurve(v, ((double)i + 0.5) / iSteps);
                    double u01 = Figure8ParamCurve(v + vStep, ((double)i - 0.5) / iSteps);
                    double u11 = Figure8ParamCurve(v + vStep, ((double)i + 0.5) / iSteps);

                    Point p00 = Figure8(PointUV.Create(u00, v));
                    Point p10 = Figure8(PointUV.Create(u10, v));
                    Point p11 = Figure8(PointUV.Create(u11, v + vStep));
                    Point p01 = Figure8(PointUV.Create(u01, v + vStep));

                    Body b0, b1;
                    if ((p00 - p11).Magnitude < (p01 - p10).Magnitude)
                    {
                        b0 = ShapeHelper.CreatePolygon(new Point[] { p00, p10, p11 }, 0);
                        b1 = ShapeHelper.CreatePolygon(new Point[] { p00, p11, p01 }, 0);
                    }
                    else
                    {
                        b0 = ShapeHelper.CreatePolygon(new Point[] { p10, p01, p00 }, 0);
                        b1 = ShapeHelper.CreatePolygon(new Point[] { p10, p11, p01 }, 0);
                    }

                    Point pn0 = (new Point[] {
                        Figure8(PointUV.Create(u00, v - vStep)),
                        Figure8(PointUV.Create(u10, v - vStep))
                    }).Average();

                    Point pn1 = (new Point[] {
                        Figure8(PointUV.Create(u11, v + 2 * vStep)),
                        Figure8(PointUV.Create(u01, v + 2 * vStep))
                    }).Average();

                    Direction normal0 = Vector.Cross(p10 - pn0, p00 - pn0).Direction;
                    Direction normal1 = Vector.Cross(p01 - pn1, p11 - pn1).Direction;
                    //Direction normal0 = new List<Face>(b0.Faces)[0].ProjectPoint(p00).Normal;
                    //Direction normal1 = new List<Face>(b1.Faces)[0].ProjectPoint(p00).Normal;
                    double tabAngle = Math.PI / 2;
                    Body   tab0     = Tabs.CreateCircularTab(p00, p10, normal0, tabAngle, swap);
                    Body   tab1     = Tabs.CreateCircularTab(p11, p01, normal1, tabAngle, !swap);

                    swap = !swap;

                    b0.Unite(new Body[] { b1, tab0, tab1 });
                    int thisBand = bandCount * i / iSteps;
                    if (thisBand == bands.Count)
                    {
                        bands.Add(new List <Body>());
                    }

                    bands[thisBand].Add(b0);
                }

                Point c0 = Figure8(PointUV.Create(Math.PI / 2, v));
                Point c1 = Figure8(PointUV.Create(Math.PI / 2, v + vStep));
                ShapeHelper.CreateCylinder(c0 - (c1 - c0.Vector).Vector * 3, c1 - (c0 - c1.Vector).Vector * 3, 0.05, part);

                foreach (List <Body> band in bands)
                {
                    //foreach (Body body in band) {
                    //    try {
                    //        body.Subtract(new Body[] { gapCylinder.Copy() });
                    //    }
                    //    catch { }
                    //}

                    ICollection <Body> bodies = band.TryUnionBodies();

                    foreach (Body body in bodies)
                    {
                        DesignBody.Create(part, "Band", body);
                    }
                }
                //break;
            }

            activeWindow.ZoomExtents();
        }