コード例 #1
0
        /// <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);
        }