Esempio n. 1
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();
            }
        }