Exemple #1
0
        static void FaceCenter_Executing(object sender, EventArgs e)
        {
            Window activeWindow = Window.ActiveWindow;
            ICollection <IDesignFace> iDesignFaces = activeWindow.GetAllSelectedIDesignFaces();

            if (iDesignFaces.Count == 0)
            {
                return;
            }

            Part parent = activeWindow.ActiveContext.Context as Part;

            if (parent == null)
            {
                return;
            }

            Part      part      = Part.Create(parent.Document, "Face Centers");
            Component component = Component.Create(parent, part);

            foreach (IDesignFace iDesignFace in iDesignFaces)
            {
                List <Point> points = new List <Point>();
                foreach (IDesignEdge iDesignEdge in iDesignFace.Edges)
                {
                    points.Add(iDesignEdge.Shape.StartPoint);
                    points.Add(iDesignEdge.Shape.EndPoint);
                }

                Point             center            = points.Average();
                SurfaceEvaluation surfaceEvaluation = iDesignFace.Shape.ProjectPoint(center);
                center = surfaceEvaluation.Point;

                if (isCreatingSpheres)
                {
                    ShapeHelper.CreateSphere(center, cylinderDiameter, part);
                }

                if (isCreatingCylinders)
                {
                    ShapeHelper.CreateCylinder(center + surfaceEvaluation.Normal * cylinderDiameter, center - surfaceEvaluation.Normal * cylinderDiameter, cylinderDiameter, part);
                }
            }
        }
Exemple #2
0
        static void Skeletize_Executing(object sender, EventArgs e)
        {
            double sphereDiameter = cylinderDiameter * 2;

            Window activeWindow = Window.ActiveWindow;
            ICollection <ITrimmedCurve> iTrimmedCurves = activeWindow.GetAllSelectedITrimmedCurves();

            if (iTrimmedCurves.Count == 0)
            {
                return;
            }

            Part parent = activeWindow.ActiveContext.Context as Part;

            if (parent == null)
            {
                return;
            }

            Part      part      = Part.Create(parent.Document, "Skeleton");
            Component component = Component.Create(parent, part);

            List <Point> allPoints = new List <Point>();

            foreach (ITrimmedCurve iTrimmedCurve in iTrimmedCurves)
            {
                if (isCreatingSpheres && !Accuracy.Equals(iTrimmedCurve.StartPoint, iTrimmedCurve.EndPoint))
                {
                    allPoints.Add(iTrimmedCurve.StartPoint);
                    allPoints.Add(iTrimmedCurve.EndPoint);
                }

                if (iTrimmedCurve.Geometry.GetType().Name == "Line")
                {
                    if (isCreatingCylinders)
                    {
                        ShapeHelper.CreateCylinder(iTrimmedCurve.StartPoint, iTrimmedCurve.EndPoint, cylinderDiameter, part);
                    }
                    if (isCreatingSausages)
                    {
                        ShapeHelper.CreateSausage(iTrimmedCurve.StartPoint, iTrimmedCurve.EndPoint, cylinderDiameter, part);
                    }
                }
                else
                {
                    if (isCreatingCylinders || isCreatingSausages)
                    {
                        ShapeHelper.CreateCable(iTrimmedCurve, cylinderDiameter, part);
                    }
                    if (isCreatingSausages && !Accuracy.Equals(iTrimmedCurve.StartPoint, iTrimmedCurve.EndPoint))
                    {
                        ShapeHelper.CreateSphere(iTrimmedCurve.StartPoint, cylinderDiameter, part);
                        ShapeHelper.CreateSphere(iTrimmedCurve.EndPoint, cylinderDiameter, part);
                        // TBD boolean these with cable
                    }
                }
            }

            if (isCreatingSpheres)
            {
                List <Point> points = new List <Point>();
                while (allPoints.Count > 0)
                {
                    Point testPoint = allPoints[0];
                    allPoints.Remove(allPoints[0]);
                    bool isDuplicate = false;

                    foreach (Point point in allPoints)
                    {
                        if (Accuracy.Equals(testPoint, point))
                        {
                            isDuplicate = true;
                            break;
                        }
                    }

                    if (!isDuplicate)
                    {
                        points.Add(testPoint);
                    }
                }

                foreach (Point point in points)
                {
                    ShapeHelper.CreateSphere(point, sphereDiameter, part);
                }
            }
        }
Exemple #3
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();
        }
        protected override void OnExecute(Command command, ExecutionContext context, System.Drawing.Rectangle buttonRect)
        {
            base.OnExecute(command, context, buttonRect);

            List <ITrimmedCurve> trimmedCurves = new List <ITrimmedCurve>();

            foreach (ITrimmedCurve trimmedCurve in activeWindow.GetAllSelectedITrimmedCurves())
            {
                trimmedCurves.Add(trimmedCurve);
            }

            TrimmedCurveChain curveChain = new TrimmedCurveChain(trimmedCurves);
            double            length     = curveChain.Length;

            int count = (int)Math.Floor(length / dashMinSize / 2) * 2;

            if (curveChain.StartPoint != curveChain.EndPoint)             // odd number when not closed curve
            {
                count++;
            }

            List <DesignCurve> dashes    = new List <DesignCurve>();
            double             lastParam = curveChain.Bounds.Start;
            Point lastPoint;

            Debug.Assert(curveChain.TryGetPointAlongCurve(lastParam, out lastPoint));
            for (int i = 0; i < count; i++)
            {
                Point point;
                if (curveChain.TryGetPointAlongCurve(lastParam -= length / count, out point))
                {
                    if (i % 2 == 1)
                    {
                        DesignCurve dash = DesignCurve.Create(part, CurveSegment.Create(lastPoint, point));
                        dash.Layer = dashLayer;
                        dashes.Add(dash);
                    }
#if false // tori
                    ShapeHelper.CreateTorus(
                        new Point[] { point, lastPoint }.Average(),
                        (point - lastPoint).Direction,
                        0.188 * inches,
                        0.75 * inches,
                        part
                        );
#endif
                    lastPoint = point;
                }
            }
#if false // cylinders
            for (int i = 1; i < count; i++)
            {
                CurveEvaluation eval = dashes[i].Shape.Geometry.Evaluate(dashes[i].Shape.Bounds.Start);
                Direction       dir1 = eval.Tangent;

                eval = dashes[i - 1].Shape.Geometry.Evaluate(dashes[i - 1].Shape.Bounds.End);
                Direction dir2 = eval.Tangent;

                if (dir1 == dir2)
                {
                    DatumPlane.Create(part, "miter parallel", Plane.Create(Frame.Create(eval.Point, eval.Tangent.ArbitraryPerpendicular, Direction.Cross(eval.Tangent.ArbitraryPerpendicular, eval.Tangent))));
                    continue;
                }

                Direction averageDir = (dir1.UnitVector + dir2.UnitVector).Direction;
                Direction xDir       = Direction.Cross(averageDir, dir1);
                //	DatumPlane.Create(part, "miter", Plane.Create(Frame.Create(eval.Point, xDir, Direction.Cross(xDir, averageDir))));
                double offset = 0.0001 / 2;
                ShapeHelper.CreateCylinder(eval.Point + averageDir * offset, eval.Point - averageDir * offset, 7 * inches, part);
            }
#endif
        }
Exemple #5
0
        static void Forstnerize_Executing(object sender, EventArgs e)
        {
            Window activeWindow = Window.ActiveWindow;

            IDesignBody forstnerBody    = null;
            Box         faceBoundingBox = Box.Empty;

            foreach (IDesignFace iDesignFace in activeWindow.ActiveContext.GetSelection <IDesignFace>())
            {
                forstnerBody     = iDesignFace.GetAncestor <IDesignBody>();
                faceBoundingBox |= iDesignFace.Shape.GetBoundingBox(Matrix.Identity);
            }
            if (forstnerBody == null || faceBoundingBox == Box.Empty)
            {
                return;
            }

            Part      part      = Part.Create(activeWindow.Document, "Forstner Bottoms");
            Component component = Component.Create(activeWindow.Scene as Part, part);

            Box    bodyBoundingBox = forstnerBody.Shape.GetBoundingBox(Matrix.Identity);
            Plane  topPlane        = Plane.Create(Frame.Create(bodyBoundingBox.MaxCorner, Direction.DirX, Direction.DirY));
            double xSpacing        = diameter * Math.Sqrt(3) / 2;
            double ySpacing        = diameter * 3 / 4;
            bool   shortRow        = false;

            for (double y = faceBoundingBox.MinCorner.Y; y < faceBoundingBox.MaxCorner.Y; y += ySpacing)
            {
                for (double x = shortRow ? faceBoundingBox.MinCorner.X + xSpacing / 2 : faceBoundingBox.MinCorner.X; x < faceBoundingBox.MaxCorner.X; x += xSpacing)
                {
                    List <IDesignBody> referenceBodies = new List <IDesignBody>();
                    referenceBodies.Add(DesignBody.Create(
                                            Part.Create(activeWindow.Document, "Temp"),
                                            "Target Copy",
                                            forstnerBody.Master.Shape.Copy()
                                            ));

                    Point       lowerPoint = Point.Create(x, y, bodyBoundingBox.MinCorner.Z);
                    Point       upperPoint = Point.Create(x, y, bodyBoundingBox.MaxCorner.Z);
                    IDesignBody drillBody  = ShapeHelper.CreateCylinder(lowerPoint, upperPoint, diameter, activeWindow.Scene as Part);

                    ICollection <IDesignBody> outputBodies = new List <IDesignBody>();
                    try {
                        //XXX					outputBodies = drillBody.Subtract(referenceBodies);
                    }
                    finally {
                        //outputBodies = new List<IDesignBody>();
                    }

                    // Find the top of the faces created by the intersection of the cylinder and the target.  The top of the bounding box of all faces except the top face and the cylinder of the drill are perfect.
                    Box      bottomBox     = Box.Empty;
                    Cylinder drillCylinder = Cylinder.Create(
                        Frame.Create(lowerPoint, Direction.DirX, Direction.DirY),
                        diameter / 2
                        );

                    bool hasTop = false;
                    foreach (IDesignBody iDesignBody in outputBodies)
                    {
                        foreach (IDesignFace iDesignFace in iDesignBody.Faces)
                        {
                            Plane plane = iDesignFace.Shape.Geometry as Plane;
                            if (plane != null)
                            {
                                if (AddInHelper.isCooincident(plane, topPlane))
                                {
                                    hasTop = true;
                                    continue;
                                }
                            }

                            Cylinder cylinder = iDesignFace.Shape.Geometry as Cylinder;
                            if (cylinder != null)
                            {
                                if (AddInHelper.isCooincident(cylinder, drillCylinder))
                                {
                                    continue;
                                }
                            }

                            bottomBox |= iDesignFace.Shape.GetBoundingBox(Matrix.Identity);
                        }
                        iDesignBody.Delete();
                    }

                    if (!bottomBox.IsEmpty && hasTop)
                    {
                        Point      bottomPoint = Point.Create(lowerPoint.X, lowerPoint.Y, bottomBox.MaxCorner.Z);
                        DesignBody bottomBody  = ShapeHelper.CreateCircle(Frame.Create(bottomPoint, Direction.DirX, Direction.DirY), diameter, part);
                        //AddInHelper.CreateCircle(Frame.Create(bottomPoint, Direction.DirX, Direction.DirY), 0.001, part);
                        foreach (DesignFace designFace in bottomBody.Faces)
                        {
                            NoteHelper.AnnotateFace(designFace.GetAncestor <Part>(), designFace, activeWindow.Units.Length.Format(-bottomPoint.Z), diameter / 5, Direction.DirZ);
                        }
                    }
                }
                shortRow = !shortRow;
            }
        }