Exemple #1
0
        //--------------------------------------------------------------------------------------------------

        public static bool RenderEdge(IDrawingRenderer renderer, TopoDS_Edge edge, bool reverse, TopoDS_Face face)
        {
            var res = true;

            reverse ^= edge.Orientation() == TopAbs_Orientation.TopAbs_REVERSED;

            double first = 0, last = 0;

            if (face != null)
            {
                var curve = BRep_Tool.CurveOnSurface(edge, new Geom_Plane(Ax3.XOY), new TopLoc_Location(), ref first, ref last);
                if (curve == null)
                {
                    return(false);
                }

                res &= RenderCurve(renderer, curve, first, last, reverse);
            }
            else
            {
                if (!(edge.TShape() is BRep_TEdge tedge))
                {
                    return(res);
                }

                var curves = tedge.CurvesList();
                if (reverse)
                {
                    curves.Reverse();
                }

                foreach (var curveOnSurface in curves.OfType <BRep_CurveOnSurface>())
                {
                    var curve = curveOnSurface.PCurve();
                    first = curveOnSurface.First();
                    last  = curveOnSurface.Last();
                    res  &= RenderCurve(renderer, curve, first, last, reverse);
                }
            }

            return(res);
        }
Exemple #2
0
        //--------------------------------------------------------------------------------------------------

        void _ExportEdge(TopoDS_Edge edge, bool reverse, TopoDS_Face face)
        {
            reverse ^= edge.Orientation() == TopAbs_Orientation.TopAbs_REVERSED;

            double first = 0, last = 0;

            if (face != null)
            {
                var curve = BRep_Tool.CurveOnSurface(edge, new Geom_Plane(Ax3.XOY), new TopLoc_Location(), ref first, ref last);
                if (curve == null)
                {
                    return;
                }

                AddCurve(curve, reverse, first, last);
            }
            else
            {
                var tedge = edge.TShape() as BRep_TEdge;
                if (tedge == null)
                {
                    return;
                }

                var curves = tedge.CurvesList();
                if (reverse)
                {
                    curves.Reverse();
                }

                foreach (var curveOnSurface in curves.Cast <BRep_CurveOnSurface>())
                {
                    var curve = curveOnSurface.PCurve();
                    first = curveOnSurface.First();
                    last  = curveOnSurface.Last();
                    AddCurve(curve, reverse, first, last);
                }
            }
        }
Exemple #3
0
        //--------------------------------------------------------------------------------------------------

        public static bool ComputeAxisFromEdge(TopoDS_Face face, TopoDS_Edge edge, double parameter, out Ax1 axis)
        {
            axis = new Ax1();

            // Get edge point and tangent
            var adaptorEdge = edge.Adaptor();
            var edgeFirst   = adaptorEdge.FirstParameter();
            var edgeLast    = adaptorEdge.LastParameter();
            var centerParam = parameter.Clamp(edgeFirst, edgeLast);
            Pnt centerPoint = Pnt.Origin;
            Vec edgeTangent = Vec.Zero;

            adaptorEdge.D1(centerParam, ref centerPoint, ref edgeTangent);

            if (edge.Orientation() == TopAbs_Orientation.TopAbs_REVERSED)
            {
                edgeTangent.Reverse();
            }

            // Get normal at point of edge
            double first = 0, last = 0;
            var    curveOnSurface = BRep_Tool.CurveOnSurface(edge, face, ref first, ref last);

            if (curveOnSurface == null)
            {
                return(false);
            }

            var paramNormalized = (centerParam - edgeFirst) / (edgeLast - edgeFirst);
            var centerUV        = curveOnSurface.Value(first + (last - first) * paramNormalized);
            Vec faceNormal      = Vec.Zero;

            new BRepGProp_Face(face).Normal(centerUV.X, centerUV.Y, ref centerPoint, ref faceNormal);

            axis = new Ax1(centerPoint, faceNormal.ToDir().Crossed(edgeTangent.ToDir()));
            return(true);
        }
        /// <summary>
        /// Searches for the longest edge of a shape, and find the longest parallel edge
        /// </summary>
        public static (TopoDS_Edge edge, Ax1?axis, TopoDS_Edge opEdge, Ax1?opAxis) FindLongestEdge(TopoDS_Shape shape)
        {
            TopoDS_Edge foundEdge = null;
            Ax1?        foundAxis = null;
            TopoDS_Edge opEdge    = null;
            Ax1?        opAxis    = null;

            // Find the longest edge to revolve around
            double edgeLen   = -1;
            var    edgeInfos = new List <(TopoDS_Edge edge, double len, Ax1 axis)>();
            var    edges     = shape.Edges();

            foreach (var edge in edges)
            {
                var brepAdaptor = new BRepAdaptor_Curve(edge);
                if (brepAdaptor.GetGeomType() != GeomAbs_CurveType.GeomAbs_Line)
                {
                    break;
                }

                var v1   = brepAdaptor.Value(brepAdaptor.FirstParameter());
                var v2   = brepAdaptor.Value(brepAdaptor.LastParameter());
                var len  = v1.Distance(v2);
                var axis = new Ax1(v1, new Vec(v1, v2).ToDir());
                if (edge.Orientation() == TopAbs_Orientation.TopAbs_REVERSED)
                {
                    axis.Reverse();
                }
                edgeInfos.Add((edge, len, axis));

                if (foundEdge != null && foundEdge.IsSame(edge))
                {
                    // Same edge with another orientation
                    if (foundEdge.Orientation() == TopAbs_Orientation.TopAbs_REVERSED &&
                        edge.Orientation() == TopAbs_Orientation.TopAbs_FORWARD)
                    {
                        // Prefer forward edges
                        foundEdge = edge;
                    }
                    continue;
                }

                if (len > edgeLen)
                {
                    foundEdge = edge;
                    edgeLen   = len;
                    foundAxis = axis;
                }
            }

            if (foundAxis != null)
            {
                // Find opposite edge
                edgeLen = -1;
                foreach (var ei in edgeInfos)
                {
                    if (ei.edge == foundEdge)
                    {
                        continue;
                    }
                    if (!ei.axis.IsParallel(foundAxis.Value, 0.00001))
                    {
                        continue;
                    }

                    if (ei.len > edgeLen)
                    {
                        opEdge  = ei.edge;
                        edgeLen = ei.len;
                        opAxis  = ei.axis;
                    }
                }
            }
            return(foundEdge, foundAxis, opEdge, opAxis);
        }