Example #1
0
        protected override void OnExecute(Command command, ExecutionContext context, System.Drawing.Rectangle buttonRect)
        {
            base.OnExecute(command, context, buttonRect);

            ICollection <IDesignEdge> designEdges               = Window.ActiveWindow.ActiveContext.GetSelection <IDesignEdge>();
            List <ITrimmedCurve>      trimmedCurves             = new List <ITrimmedCurve>();
            Dictionary <ITrimmedCurve, IDesignEdge> curveToEdge = new Dictionary <ITrimmedCurve, IDesignEdge>();

            foreach (IDesignEdge designEdge in designEdges)
            {
                ITrimmedCurve trimmedCurve = designEdge.Shape;
                trimmedCurves.Add(trimmedCurve);
                curveToEdge[trimmedCurve] = designEdge;
            }

            TrimmedCurveChain curveChain = new TrimmedCurveChain(trimmedCurves);

            Point startPoint = curveChain.Curves[0].StartPoint;
            Point startTail  = startPoint + GetAverageNormal(startPoint, curveToEdge[curveChain.Curves[0].TrimmedCurve]) * edgeOffset;

            for (int i = 0; i < curveChain.Curves.Count; i++)
            {
                Point endPoint = curveChain.Curves[i].EndPoint;
                Point endTail  = endPoint + GetAverageNormal(endPoint, curveToEdge[curveChain.Curves[i].TrimmedCurve]) * edgeOffset;

                if (i != curveChain.Curves.Count - 1)
                {
                    endTail = new Point[] { endTail, endPoint + GetAverageNormal(endPoint, curveToEdge[curveChain.Curves[i + 1].TrimmedCurve]) * edgeOffset }
                }
Example #2
0
        private Body CreateGearBody()
        {
            int numSteps = (int) Math.Max(Math.Ceiling(Math.Abs(64 * HelixRotations / 2 / Math.PI) + 1), 2);

            if (this is BevelGear/*|| this is HypoidGear*/)
                numSteps *= 4;

            int extraSteps = 2;

            if (Accuracy.AngleIsZero(TotalTwistAngle)) {
                numSteps = 2;
                extraSteps = 0;//0
            }

            double accuracy = Accuracy.LinearResolution;
            //	Period.Print();
            var orderedCurveChain = new TrimmedCurveChain(Period);
            orderedCurveChain.Reverse();
            IList<ITrimmedCurve> orderedCurves = orderedCurveChain.SortedCurves;

            var periodBodies = new List<Body>();
            foreach (ITrimmedCurve iTrimmedCurve in orderedCurves) {
                var profiles = new List<ICollection<ITrimmedCurve>>();
                for (int i = -extraSteps; i < numSteps + extraSteps; i++) {
                    ITrimmedCurve transformedCurve = GetTransformedProfileCurve(iTrimmedCurve, (double) i / (numSteps - 1));
                    var profile = new List<ITrimmedCurve>();
                    profile.Add(transformedCurve);
                    profiles.Add(profile);
                }

                try {
                    periodBodies.Add(Body.LoftProfiles(profiles, false, false));
                }
                catch {
                    foreach (ICollection<ITrimmedCurve> curves in profiles) {
                        foreach (ITrimmedCurve curve in curves)
                            DesignCurve.Create(Part, curve);
                    }
                }
            }

            Body periodBody = periodBodies.TryUnionOrFailBodies();

            var profileBodies = new List<Body>();
            var startProfile = new List<ITrimmedCurve>();
            var endProfile = new List<ITrimmedCurve>();
            Matrix trans;
            for (int i = 0; i < GearData.NumberOfTeeth; i++) {
                trans = Matrix.CreateRotation(Line.Create(Point.Origin, Direction.DirZ), GearData.PitchAngle * 2 * i);
                profileBodies.Add(periodBody.CreateTransformedCopy(trans));
            }

            Body gearBody = profileBodies.TryUnionAndStitchOrFailBodies();
            Body cappingBody = GetCappingBody();
            try {
                //	throw new NotImplementedException();
                if (GearData.IsInternal) {
                    //	gearBody.Subtract(new Body[] { cappingBody });
                    cappingBody.Subtract(new Body[] { gearBody });
                    gearBody = cappingBody;
                }
                else
                    gearBody.Intersect(new Body[] { cappingBody });
            }
            catch {
                DesignBody.Create(Part, "capping", cappingBody.Copy());
                DesignBody.Create(Part, "gear", gearBody.Copy());
            }

            return gearBody;
        }
        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
        }
Example #4
0
        private Body CreateGearBody()
        {
            int numSteps = (int)Math.Max(Math.Ceiling(Math.Abs(64 * HelixRotations / 2 / Math.PI) + 1), 2);

            if (this is BevelGear /*|| this is HypoidGear*/)
            {
                numSteps *= 4;
            }

            int extraSteps = 2;

            if (Accuracy.AngleIsZero(TotalTwistAngle))
            {
                numSteps   = 2;
                extraSteps = 0;                //0
            }

            double accuracy = Accuracy.LinearResolution;
            //	Period.Print();
            var orderedCurveChain = new TrimmedCurveChain(Period);

            orderedCurveChain.Reverse();
            IList <ITrimmedCurve> orderedCurves = orderedCurveChain.SortedCurves;

            var periodBodies = new List <Body>();

            foreach (ITrimmedCurve iTrimmedCurve in orderedCurves)
            {
                var profiles = new List <ICollection <ITrimmedCurve> >();
                for (int i = -extraSteps; i < numSteps + extraSteps; i++)
                {
                    ITrimmedCurve transformedCurve = GetTransformedProfileCurve(iTrimmedCurve, (double)i / (numSteps - 1));
                    var           profile          = new List <ITrimmedCurve>();
                    profile.Add(transformedCurve);
                    profiles.Add(profile);
                }

                try {
                    periodBodies.Add(Body.LoftProfiles(profiles, false, false));
                }
                catch {
                    foreach (ICollection <ITrimmedCurve> curves in profiles)
                    {
                        foreach (ITrimmedCurve curve in curves)
                        {
                            DesignCurve.Create(Part, curve);
                        }
                    }
                }
            }

            Body periodBody = periodBodies.TryUnionOrFailBodies();

            var    profileBodies = new List <Body>();
            var    startProfile  = new List <ITrimmedCurve>();
            var    endProfile    = new List <ITrimmedCurve>();
            Matrix trans;

            for (int i = 0; i < GearData.NumberOfTeeth; i++)
            {
                trans = Matrix.CreateRotation(Line.Create(Point.Origin, Direction.DirZ), GearData.PitchAngle * 2 * i);
                profileBodies.Add(periodBody.CreateTransformedCopy(trans));
            }

            Body gearBody    = profileBodies.TryUnionAndStitchOrFailBodies();
            Body cappingBody = GetCappingBody();

            try {
                //	throw new NotImplementedException();
                if (GearData.IsInternal)
                {
                    //	gearBody.Subtract(new Body[] { cappingBody });
                    cappingBody.Subtract(new Body[] { gearBody });
                    gearBody = cappingBody;
                }
                else
                {
                    gearBody.Intersect(new Body[] { cappingBody });
                }
            }
            catch {
                DesignBody.Create(Part, "capping", cappingBody.Copy());
                DesignBody.Create(Part, "gear", gearBody.Copy());
            }

            return(gearBody);
        }
        protected override void OnExecute(Command command, ExecutionContext context, System.Drawing.Rectangle buttonRect)
        {
            base.OnExecute(command, context, buttonRect);

            ICollection<IDesignEdge> designEdges = Window.ActiveWindow.ActiveContext.GetSelection<IDesignEdge>();
            List<ITrimmedCurve> trimmedCurves = new List<ITrimmedCurve>();
            Dictionary<ITrimmedCurve, IDesignEdge> curveToEdge = new Dictionary<ITrimmedCurve, IDesignEdge>();

            foreach (IDesignEdge designEdge in designEdges) {
                ITrimmedCurve trimmedCurve = designEdge.Shape;
                trimmedCurves.Add(trimmedCurve);
                curveToEdge[trimmedCurve] = designEdge;
            }

            TrimmedCurveChain curveChain = new TrimmedCurveChain(trimmedCurves);

            Point startPoint = curveChain.Curves[0].StartPoint;
            Point startTail = startPoint + GetAverageNormal(startPoint, curveToEdge[curveChain.Curves[0].TrimmedCurve]) * edgeOffset;

            for (int i = 0; i < curveChain.Curves.Count; i++) {
                Point endPoint = curveChain.Curves[i].EndPoint;
                Point endTail = endPoint + GetAverageNormal(endPoint, curveToEdge[curveChain.Curves[i].TrimmedCurve]) * edgeOffset;

                if (i != curveChain.Curves.Count - 1)
                    endTail = new Point[] { endTail, endPoint + GetAverageNormal(endPoint, curveToEdge[curveChain.Curves[i + 1].TrimmedCurve]) * edgeOffset }.Average();

                CurveSegment curveSegment = CurveSegment.Create(startTail, endTail);
                DesignCurve curve = DesignCurve.Create(activeWindow.ActiveContext.Context as Part, curveSegment);

                startPoint = endPoint;
                startTail = endTail;
            }
        }
        // hardwired to place on about inch-spaced points
        // Edge-only for now -- See EO comments
        static void MakeTabs_Executing(object sender, EventArgs e)
        {
            Window activeWindow = Window.ActiveWindow;

            Layer tabLayer = NoteHelper.CreateOrGetLayer(activeWindow.ActiveContext.Context.Document, "Tabs", System.Drawing.Color.Fuchsia);

            ICollection<ITrimmedCurve> trimmedCurves = AddInHelper.GetITrimmedCurvesOfSelectedTopology(activeWindow);

            // quantize with lines to points separated by approx targetDistance;
            TrimmedCurveChain curveChain = new TrimmedCurveChain(trimmedCurves);
            Point lastPoint = curveChain.Curves[0].StartPoint;
            double tolerance = 0.2 * inches;
            double targetDistance = 1 * inches;
            trimmedCurves = new List<ITrimmedCurve>();
            Dictionary<ITrimmedCurve, Direction> OriginalNormals = new Dictionary<ITrimmedCurve, Direction>();

            double extraLength = 0;
            foreach (OrientedTrimmedCurve curve in curveChain.Curves) {
                Point point = curve.EndPoint;

                if (Math.Abs((lastPoint - point).Magnitude - targetDistance) > tolerance) {
                    extraLength += (lastPoint - point).Magnitude;
                    continue;
                }

                CurveSegment curveSegment = null;
                //		if (extraLength == 0)
                curveSegment = CurveSegment.Create(lastPoint, point);
                //		else
                //			curveSegment = CurveSegment.Create(point - (lastPoint - point).Direction * ((lastPoint - point).Magnitude + extraLength), point);

                trimmedCurves.Add(curveSegment);

                Edge edge = curve.TrimmedCurve as Edge;
                if (edge != null) {
                    Face face = null;
                    foreach (Face testFace in edge.Faces) {
                        face = testFace;
                        break;
                    }

                    SurfaceEvaluation surfEval = face.ProjectPoint(curve.StartPoint);
                    OriginalNormals[curveSegment] = surfEval.Normal;
                }

                lastPoint = point;
                //		extraLength = 0;
            }

            curveChain = new TrimmedCurveChain(trimmedCurves);
            if (AreTabsFlipped)
                curveChain.Reverse();

            List<Window> curveWindows = new List<Window>(Document.Open(@"C:\Users\bcr.SPACECLAIM\Documents\Models\Pod Tent\TabCurve.scdoc", false));
            bool adjustEnds = true;
            #if false
            List<Window> curveWindows = new List<Window>(Document.Open(@"C:\Users\bcr.SPACECLAIM\Documents\Models\Pod Tent\TabStrapEdgeCurve.scdoc", false));
            bool adjustEnds = true;
            #endif
            NurbsCurve middle = GetFirstNurbsCurveFromPart(curveWindows[0].Scene as Part);
            Debug.Assert(middle != null);

            NurbsCurve endTab = null;
            NurbsCurve endSlot = null;
            foreach (Component component in (curveWindows[0].Scene as Part).Components) {
                if (component.Template.Name == "EndTab")
                    endTab = GetFirstNurbsCurveFromPart(component.Template);

                if (component.Template.Name == "EndSlot")
                    endSlot = GetFirstNurbsCurveFromPart(component.Template);
            }

            Debug.Assert(endTab != null);
            Debug.Assert(endSlot != null);

            Point startPoint = curveChain.Curves[0].StartPoint;
            Point endPoint = curveChain.Curves[0].EndPoint;
            Point startTail = startPoint + (endPoint - startPoint);
            bool mirror = false;

            if (IsTabStartSlot) {
                NurbsCurve tmp = endTab;
                endTab = endSlot;
                endSlot = tmp;

                mirror = !mirror;
            }

            for (int i = 0; i < curveChain.Curves.Count; i++) {
                Point endTail;
                if (i == curveChain.Curves.Count - 1)
                    endTail = curveChain.Curves[i].EndPoint + (curveChain.Curves[i].EndPoint - curveChain.Curves[i].StartPoint);
                else
                    endTail = curveChain.Curves[i + 1].EndPoint;

                Point mid = Point.Origin + (startPoint.Vector + endPoint.Vector) / 2;
                Direction startDirection = (startPoint - startTail).Direction;
                Direction lineDirection = (endPoint - startPoint).Direction;
                Direction endDirection = (endTail - endPoint).Direction;

                Direction upDirection = Direction.DirZ;
                //if (upDirection.IsParallelTo(lineDirection))
                //    upDirection = Direction.DirY;

                if (OriginalNormals.ContainsKey(curveChain.Curves[i].TrimmedCurve))
                    upDirection = OriginalNormals[curveChain.Curves[i].TrimmedCurve];

                Direction normalDirection = Direction.Cross(lineDirection, upDirection);

                Line startMidLine;
                if (startDirection.UnitVector == lineDirection.UnitVector)
                    startMidLine = Line.Create(startPoint, Direction.Cross(lineDirection, upDirection));
                else
                    startMidLine = Line.Create(startPoint, (startDirection.UnitVector - lineDirection.UnitVector).Direction);

                Line endMidLine;
                if (lineDirection.UnitVector == endDirection.UnitVector)
                    endMidLine = Line.Create(endPoint, Direction.Cross(lineDirection, upDirection));
                else
                    endMidLine = Line.Create(endPoint, (lineDirection.UnitVector - endDirection.UnitVector).Direction);

                NurbsCurve template = middle;

                if (mirror) {
                    lineDirection = -lineDirection;
                    Line tmp = startMidLine;
                    startMidLine = endMidLine;
                    endMidLine = tmp;
                }

                if (i == 0)
                    template = endSlot;

                if (i == curveChain.Curves.Count - 1) {
                    if (mirror ^ IsTabStartSlot)
                        template = endSlot;
                    else
                        template = endTab;
                }

                Frame frame = Frame.Create(mid, lineDirection, normalDirection);
                Matrix transform = Matrix.CreateMapping(frame);

                ControlPoint[] controlPoints = new ControlPoint[template.ControlPoints.Length];
                int j = 0;
                foreach (ControlPoint controlPoint in template.ControlPoints) {
                    controlPoints[j] = new ControlPoint(transform * controlPoint.Position, controlPoint.Weight);
                    j++;
                }

                //CurveEvaluation curveEval = null;
                //curveEval = startMidLine.Evaluate(1);
                //DesignCurve.Create(activeWindow.SubjectMatter as Part, CurveSegment.Create(startPoint, curveEval.Point));
                //curveEval = endMidLine.Evaluate(1);
                //DesignCurve.Create(activeWindow.SubjectMatter as Part, CurveSegment.Create(endPoint, curveEval.Point));

                MakeNurbsEndTangent(endMidLine, controlPoints, 0, 1);
                if (adjustEnds)
                    MakeNurbsEndTangent(startMidLine, controlPoints, controlPoints.Length - 1, controlPoints.Length - 2);

                Curve curve = NurbsCurve.Create(template.Data, controlPoints);
                CurveSegment curveSegment = CurveSegment.Create(curve, template.Parameterization.Range.Value);

                DesignCurve tab = DesignCurve.Create(activeWindow.ActiveContext.Context as Part, curveSegment);
                tab.Layer = tabLayer;

                startTail = startPoint;
                startPoint = endPoint;
                endPoint = endTail;
                mirror = !mirror;

            }

            foreach (Window window in curveWindows)
                window.Close();
        }
Example #7
0
        private void SaveFileWork(string path)
        {
            double inch        = 0.0254;
            double sliceHeight = 0.01 * inch;
            double roadWidth   = 0.02 * inch;

            Window activeWindow = Window.ActiveWindow;
            ICollection <IDesignBody> iDesBodies = activeWindow.Document.MainPart.GetDescendants <IDesignBody>();

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

            List <Body> bodies = iDesBodies.Select(b => b.Master.Shape.CreateTransformedCopy(b.TransformToMaster.Inverse)).ToList();
            Box         box    = activeWindow.Document.MainPart.GetBoundingBox(Matrix.Identity);

            double min = box.MinCorner.Z;
            double max = box.MaxCorner.Z;

            var positions = new List <IList <Point> >();
            var count     = box.Size.Z / sliceHeight;

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(path)) {
                file.WriteLine("Slice V0");
                file.WriteLine("FILE(" + Path.GetFileName(path) + ")");
                file.WriteLine("UNITS(INCH)");
                file.WriteLine("SLICES()");
                var rand = new Random();



                for (int i = 0; i < count; i++)
                {
                    var points = new List <Point>();
                    var z      = min + i * sliceHeight;
                    var plane  = Plane.Create(Frame.Create(Point.Create(0, 0, z), Direction.DirZ));
                    file.WriteLine(String.Format("Z {0:F6} {1:F6}", (z + sliceHeight) / inch, sliceHeight / inch));

                    foreach (Body preserveBody in bodies)
                    {
                        var body = preserveBody.Copy();
                        body.Split(plane, null);
                        var halfBodies = body.SeparatePieces().Where(b => Box.Create(b.Vertices.Select(v => v.Position).ToArray()).Center.Z > z);
                        //var halfBodies = body.SeparatePieces().Where(b => b.GetBoundingBox(Matrix.Identity).Center.Z > z);
                        var sectionCurves = halfBodies.SelectMany(b => b.Edges).Where(e => IsPointOnPlane(plane, e.StartPoint) && IsPointOnPlane(plane, e.EndPoint) && IsPointOnPlane(plane, e.Geometry.Evaluate(e.Bounds.Middle()).Point)).ToList <ITrimmedCurve>();

                        if (sectionCurves.Count == 0)
                        {
                            continue;
                        }

                        var loops       = TrimmedCurveChain.GatherLoops(sectionCurves).ToList();
                        var offsetLoops = new List <TrimmedCurveChain>();
                        if (!body.IsClosed)
                        {
                            foreach (TrimmedCurveChain loop in loops)
                            {
                                var curves = loop.SortedCurves.ToList();
                                var first  = curves[0];
                                curves.RemoveAt(0);

                                for (int j = 1; j < 3; j++)
                                {
                                    var offsetCurves = first.OffsetChain(plane, -(roadWidth * j + roadWidth / 2), curves, OffsetCornerType.Round);
                                    offsetLoops.Add(new TrimmedCurveChain(offsetCurves));
                                }
                            }
                        }

                        loops.AddRange(offsetLoops);

                        foreach (TrimmedCurveChain loop in loops)
                        {
                            Point?startPoint = null;
                            Point?lastPoint  = null;
                            bool  isClosed   = false;
                            foreach (OrientedTrimmedCurve orientedCurve in loop.Curves)
                            {
                                List <Point> tessellation = orientedCurve.OriginalTrimmedCurve.GetPolyline().ToList();
                                //if (orientedCurve.IsReversed)
                                //    tessellation.Reverse();

                                if (startPoint == null)
                                {
                                    //        var rnd = rand.Next(1, tessellation.Count);
                                    //        startPoint = tessellation[rnd];
                                    startPoint = tessellation[0];
                                    file.WriteLine(String.Format("{0:F6} {1:F6}", startPoint.Value.X / inch, startPoint.Value.Y / inch));
                                    tessellation = tessellation.Take(1).ToList();
                                }

                                foreach (Point point in tessellation)
                                {
                                    if (lastPoint != null && ArePointsClose(lastPoint.Value, point))
                                    {
                                        continue;
                                    }

                                    //if (lastPoint != null)
                                    //    CurveSegment.Create(lastPoint.Value, point).Print();

                                    if (!ArePointsClose(startPoint.Value, point) || !body.IsClosed)
                                    {
                                        file.WriteLine(String.Format("{0:F6} {1:F6}", point.X / inch, point.Y / inch));
                                    }

                                    lastPoint = point;
                                }
                            }

                            if (startPoint == null)
                            {
                                continue;
                            }

                            file.WriteLine(body.IsClosed ? "C" : "O");

                            //var length = loop.Length;
                            //var steps = length / increment;
                            //Point point;
                            //for (int k = 0; k < steps; k++) {
                            //    if (loop.TryGetPointAlongCurve(increment, out point))
                            //        points.Add(point);
                            //}
                        }


                        //for (int j = 0; j < 1; j++) {
                        //    foreach (var offsetCurve in iTrimmedCurve.Offset(plane, j * roadWidth))
                        //        points.AddRange(iTrimmedCurve.GetPolyline());
                        //}
                    }

                    if (points.Count < 1)
                    {
                        continue;
                    }

                    positions.Add(points);
                }



                file.WriteLine("END");
            }
        }
Example #8
0
        // hardwired to place on about inch-spaced points
        // Edge-only for now -- See EO comments
        static void MakeTabs_Executing(object sender, EventArgs e)
        {
            Window activeWindow = Window.ActiveWindow;

            Layer tabLayer = NoteHelper.CreateOrGetLayer(activeWindow.ActiveContext.Context.Document, "Tabs", System.Drawing.Color.Fuchsia);

            ICollection <ITrimmedCurve> trimmedCurves = AddInHelper.GetITrimmedCurvesOfSelectedTopology(activeWindow);

            // quantize with lines to points separated by approx targetDistance;
            TrimmedCurveChain curveChain     = new TrimmedCurveChain(trimmedCurves);
            Point             lastPoint      = curveChain.Curves[0].StartPoint;
            double            tolerance      = 0.2 * inches;
            double            targetDistance = 1 * inches;

            trimmedCurves = new List <ITrimmedCurve>();
            Dictionary <ITrimmedCurve, Direction> OriginalNormals = new Dictionary <ITrimmedCurve, Direction>();

            double extraLength = 0;

            foreach (OrientedTrimmedCurve curve in curveChain.Curves)
            {
                Point point = curve.EndPoint;

                if (Math.Abs((lastPoint - point).Magnitude - targetDistance) > tolerance)
                {
                    extraLength += (lastPoint - point).Magnitude;
                    continue;
                }

                CurveSegment curveSegment = null;
                //		if (extraLength == 0)
                curveSegment = CurveSegment.Create(lastPoint, point);
                //		else
                //			curveSegment = CurveSegment.Create(point - (lastPoint - point).Direction * ((lastPoint - point).Magnitude + extraLength), point);

                trimmedCurves.Add(curveSegment);

                Edge edge = curve.TrimmedCurve as Edge;
                if (edge != null)
                {
                    Face face = null;
                    foreach (Face testFace in edge.Faces)
                    {
                        face = testFace;
                        break;
                    }

                    SurfaceEvaluation surfEval = face.ProjectPoint(curve.StartPoint);
                    OriginalNormals[curveSegment] = surfEval.Normal;
                }

                lastPoint = point;
                //		extraLength = 0;
            }

            curveChain = new TrimmedCurveChain(trimmedCurves);
            if (AreTabsFlipped)
            {
                curveChain.Reverse();
            }


            List <Window> curveWindows = new List <Window>(Document.Open(@"C:\Users\bcr.SPACECLAIM\Documents\Models\Pod Tent\TabCurve.scdoc", false));
            bool          adjustEnds   = true;

#if false
            List <Window> curveWindows = new List <Window>(Document.Open(@"C:\Users\bcr.SPACECLAIM\Documents\Models\Pod Tent\TabStrapEdgeCurve.scdoc", false));
            bool          adjustEnds   = true;
#endif
            NurbsCurve middle = GetFirstNurbsCurveFromPart(curveWindows[0].Scene as Part);
            Debug.Assert(middle != null);

            NurbsCurve endTab  = null;
            NurbsCurve endSlot = null;
            foreach (Component component in (curveWindows[0].Scene as Part).Components)
            {
                if (component.Template.Name == "EndTab")
                {
                    endTab = GetFirstNurbsCurveFromPart(component.Template);
                }

                if (component.Template.Name == "EndSlot")
                {
                    endSlot = GetFirstNurbsCurveFromPart(component.Template);
                }
            }

            Debug.Assert(endTab != null);
            Debug.Assert(endSlot != null);

            Point startPoint = curveChain.Curves[0].StartPoint;
            Point endPoint   = curveChain.Curves[0].EndPoint;
            Point startTail  = startPoint + (endPoint - startPoint);
            bool  mirror     = false;

            if (IsTabStartSlot)
            {
                NurbsCurve tmp = endTab;
                endTab  = endSlot;
                endSlot = tmp;

                mirror = !mirror;
            }

            for (int i = 0; i < curveChain.Curves.Count; i++)
            {
                Point endTail;
                if (i == curveChain.Curves.Count - 1)
                {
                    endTail = curveChain.Curves[i].EndPoint + (curveChain.Curves[i].EndPoint - curveChain.Curves[i].StartPoint);
                }
                else
                {
                    endTail = curveChain.Curves[i + 1].EndPoint;
                }

                Point     mid            = Point.Origin + (startPoint.Vector + endPoint.Vector) / 2;
                Direction startDirection = (startPoint - startTail).Direction;
                Direction lineDirection  = (endPoint - startPoint).Direction;
                Direction endDirection   = (endTail - endPoint).Direction;

                Direction upDirection = Direction.DirZ;
                //if (upDirection.IsParallelTo(lineDirection))
                //    upDirection = Direction.DirY;

                if (OriginalNormals.ContainsKey(curveChain.Curves[i].TrimmedCurve))
                {
                    upDirection = OriginalNormals[curveChain.Curves[i].TrimmedCurve];
                }

                Direction normalDirection = Direction.Cross(lineDirection, upDirection);

                Line startMidLine;
                if (startDirection.UnitVector == lineDirection.UnitVector)
                {
                    startMidLine = Line.Create(startPoint, Direction.Cross(lineDirection, upDirection));
                }
                else
                {
                    startMidLine = Line.Create(startPoint, (startDirection.UnitVector - lineDirection.UnitVector).Direction);
                }

                Line endMidLine;
                if (lineDirection.UnitVector == endDirection.UnitVector)
                {
                    endMidLine = Line.Create(endPoint, Direction.Cross(lineDirection, upDirection));
                }
                else
                {
                    endMidLine = Line.Create(endPoint, (lineDirection.UnitVector - endDirection.UnitVector).Direction);
                }

                NurbsCurve template = middle;

                if (mirror)
                {
                    lineDirection = -lineDirection;
                    Line tmp = startMidLine;
                    startMidLine = endMidLine;
                    endMidLine   = tmp;
                }

                if (i == 0)
                {
                    template = endSlot;
                }

                if (i == curveChain.Curves.Count - 1)
                {
                    if (mirror ^ IsTabStartSlot)
                    {
                        template = endSlot;
                    }
                    else
                    {
                        template = endTab;
                    }
                }

                Frame  frame     = Frame.Create(mid, lineDirection, normalDirection);
                Matrix transform = Matrix.CreateMapping(frame);

                ControlPoint[] controlPoints = new ControlPoint[template.ControlPoints.Length];
                int            j             = 0;
                foreach (ControlPoint controlPoint in template.ControlPoints)
                {
                    controlPoints[j] = new ControlPoint(transform * controlPoint.Position, controlPoint.Weight);
                    j++;
                }

                //CurveEvaluation curveEval = null;
                //curveEval = startMidLine.Evaluate(1);
                //DesignCurve.Create(activeWindow.SubjectMatter as Part, CurveSegment.Create(startPoint, curveEval.Point));
                //curveEval = endMidLine.Evaluate(1);
                //DesignCurve.Create(activeWindow.SubjectMatter as Part, CurveSegment.Create(endPoint, curveEval.Point));

                MakeNurbsEndTangent(endMidLine, controlPoints, 0, 1);
                if (adjustEnds)
                {
                    MakeNurbsEndTangent(startMidLine, controlPoints, controlPoints.Length - 1, controlPoints.Length - 2);
                }

                Curve        curve        = NurbsCurve.Create(template.Data, controlPoints);
                CurveSegment curveSegment = CurveSegment.Create(curve, template.Parameterization.Range.Value);

                DesignCurve tab = DesignCurve.Create(activeWindow.ActiveContext.Context as Part, curveSegment);
                tab.Layer = tabLayer;

                startTail  = startPoint;
                startPoint = endPoint;
                endPoint   = endTail;
                mirror     = !mirror;
            }

            foreach (Window window in curveWindows)
            {
                window.Close();
            }
        }
Example #9
0
        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
        }