def edge_to_bezier(topods_edge): """take an edge and returns: * a bool is_bezier * the bezier curve * degrees * poles """ ad = BRepAdaptor_Curve(topods_edge) if ad.IsRational(): return True, ad.Bezier(), ad.Degree() return False, None, None
def FindGeometry(self, object: AIS_InteractiveObject): if object.Type() == AIS_KOI_Shape: shape = self.myContext.SelectedShape() if shape.ShapeType() == TopAbs_VERTEX: pass elif shape.ShapeType() == TopAbs_EDGE: curve = BRepAdaptor_Curve(shape) curve_type = curve.GetType() if curve_type == GeomAbs_BezierCurve: return curve.Bezier() elif curve_type == GeomAbs_BSplineCurve: return curve.BSpline() elif curve_type == GeomAbs_Circle: return Geom_Circle(curve.Circle()) elif curve_type == GeomAbs_Line: return Geom_Line(curve.Line()) elif shape.ShapeType() == TopAbs_FACE: pass
def recognize_edge(self, a_edge): """ Takes a TopoDS shape and tries to identify its nature whether it is a plane a cylinder a torus etc. if a plane, returns the normal if a cylinder, returns the radius """ curve = BRepAdaptor_Curve(a_edge) curve_type = curve.GetType() if curve_type == GeomAbs_BezierCurve: print("--> Bezier") self._selectedShape = curve.Bezier() elif curve_type == GeomAbs_BSplineCurve: print("--> BSpline") self._selectedShape = curve.BSpline() elif curve_type == GeomAbs_Circle: print("--> Circle") self._selectedShape = Geom_Circle(curve.Circle()) else: # TODO there are plenty other type that can be checked # see documentation for the BRepAdaptor class # https://www.opencascade.com/doc/occt-6.9.1/refman/html/class_b_rep_adaptor___surface.html print("not implemented")
def on_select(shapes): if len(shapes) < 1: return s = shapes[0] if s.ShapeType() == TopAbs_EDGE: if s.Orientation() == TopAbs_FORWARD: print('FORWARD') else: print('REVERSED') tmp = BRepAdaptor_Curve(s) # display start and endpoints of curve start_point = tmp.Value(tmp.FirstParameter()) end_point = tmp.Value(tmp.LastParameter()) if s.Orientation() == TopAbs_FORWARD: display.DisplayColoredShape( BRepBuilderAPI_MakeVertex(start_point).Vertex(), "BLUE") display.DisplayColoredShape( BRepBuilderAPI_MakeVertex(end_point).Vertex(), "RED") else: display.DisplayColoredShape( BRepBuilderAPI_MakeVertex(start_point).Vertex(), "RED") display.DisplayColoredShape( BRepBuilderAPI_MakeVertex(end_point).Vertex(), "BLUE") if tmp.GetType() == GeomAbs_Line: t = tmp.Line() gcode = f'G01 X{end_point.X():.6f} Y{end_point.Y():.6f}' print( f'line: ({start_point.X():.6f}, {start_point.Y():.6f}) → ({end_point.X():.6f}, {end_point.Y():.6f})' ) # print(gcode) print( f'line parameters: {tmp.FirstParameter():.6f}, {tmp.LastParameter():.6f}' ) elif tmp.GetType() == GeomAbs_Circle: t = tmp.Circle() center_point = t.Location() # make two line segments, both from the centerpoint, and one to each of the endpoints # the cross product of the lines determines the direction. positive = CCW, negative = CW # assume for now the arc is in the XY plane, so only check the sign of z # assume clockwise for now # center format arc v1 = gp_Vec(center_point, start_point) v2 = gp_Vec(center_point, end_point) # angle > 0 if acute, < 0 if obtuse angle = v1.AngleWithRef(v2, gp_Vec(0, 0, 1)) # use cross product to determine direction v1.Cross(v2) v1 *= angle # TODO: verify CCW = True if v1.Z() > 0 else False gcode = "G0{0} X{1:.6f} Y{2:.6f} I{3:.6f} J{4:.6f}".format( 2 if CCW else 3, end_point.X(), end_point.Y(), center_point.X() - start_point.X(), center_point.Y() - start_point.Y()) print( "circle: start (%.6f, %.6f), end (%.6f, %.6f), center (%.6f, %.6f), radius %.6f" % (start_point.X(), start_point.Y(), end_point.X(), end_point.Y(), center_point.X(), center_point.Y(), t.Radius())) print("circle parameters: %.6f, %.6f" % (tmp.FirstParameter() / math.pi, tmp.LastParameter() / math.pi)) # print(gcode) elif tmp.GetType() == GeomAbs_Ellipse: t = tmp.Ellipse() x = t.XAxis() y = t.YAxis() print("ellipse") elif tmp.GetType() == GeomAbs_Hyperbola: t = tmp.Hyperbola() print("hyperbola") elif tmp.GetType() == GeomAbs_Parabola: t = tmp.Parabola() print("parabola") elif tmp.GetType() == GeomAbs_BezierCurve: t = tmp.Bezier() print("bezier") elif tmp.GetType() == GeomAbs_BSplineCurve: t = tmp.BSpline() print("bspline") elif tmp.GetType() == GeomAbs_OffsetCurve: t = tmp.OffsetCurve() print("offset") elif tmp.GetType() == GeomAbs_OtherCurve: print("other")