def tessellate(self, tolerance): tess = Tesselator(self.wrapped) tess.Compute(compute_edges=True, mesh_quality=tolerance) vertices = [] indexes = [] # add vertices for i_vert in range(tess.ObjGetVertexCount()): xyz = tess.GetVertex(i_vert) vertices.append(Vector(*xyz)) # add triangles for i_tr in range(tess.ObjGetTriangleCount()): indexes.append(tess.GetTriangleIndex(i_tr)) return vertices, indexes
def draw_shape_mpl(shape): """ Draw a TopoDS_Shape with matplotlib """ tess = Tesselator(shape) triangles = [] edges = [] # get the triangles triangle_count = tess.ObjGetTriangleCount() for i_triangle in range(0, triangle_count): i1, i2, i3 = tess.GetTriangleIndex(i_triangle) triangles.append( [tess.GetVertex(i1), tess.GetVertex(i2), tess.GetVertex(i3)]) # get the edges edge_count = tess.ObjGetEdgeCount() for i_edge in range(0, edge_count): vertex_count = tess.ObjEdgeGetVertexCount(i_edge) edge = [] for i_vertex in range(0, vertex_count): vertex = tess.GetEdgeVertex(i_edge, i_vertex) edge.append(vertex) edges.append(edge) # plot it fig = plt.figure() ax = Axes3D(fig) ax.add_collection3d(Poly3DCollection(triangles, linewidths=0.2, alpha=0.5)) ax.add_collection3d(Line3DCollection(edges, colors='w', linewidths=1.0)) ax.get_xaxis().set_visible(True) ax.get_yaxis().set_visible(True) ax.set_autoscale_on(True) plt.show()
def generate_png(shape): """ Draw a TopoDS_Shape with matplotlib """ from OCC.Visualization import Tesselator from mpl_toolkits.mplot3d import Axes3D from matplotlib import pyplot as plt from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection tess = Tesselator(shape) triangles = [] edges = [] # get the triangles triangle_count = tess.ObjGetTriangleCount() for i_triangle in range(0, triangle_count): i1, i2, i3 = tess.GetTriangleIndex(i_triangle) triangles.append([tess.GetVertex(i1), tess.GetVertex(i2), tess.GetVertex(i3)]) # get the edges edge_count = tess.ObjGetEdgeCount() for i_edge in range(0, edge_count): vertex_count = tess.ObjEdgeGetVertexCount(i_edge) edge = [] for i_vertex in range(0, vertex_count): vertex = tess.GetEdgeVertex(i_edge, i_vertex) edge.append(vertex) edges.append(edge) # plot it fig_side = plt.figure(figsize=(10, 4)) ax = Axes3D(fig_side) ax.add_collection3d(Poly3DCollection(triangles, linewidths=0.15, alpha=0.5)) ax.add_collection3d(Line3DCollection(edges, colors='w', linewidths=1.0)) ax.set_axis_off() ax.set_xlim(-1800, 1800) ax.set_ylim(-800, 800) ax.set_zlim(-800, 800) ax.view_init(elev=-1., azim=90) fig_side.savefig("views/side.png") fig_top = plt.figure(figsize=(5, 4)) ax_top = Axes3D(fig_top) ax_top.add_collection3d(Poly3DCollection(triangles, linewidths=0.15, alpha=0.5)) ax_top.add_collection3d(Line3DCollection(edges, colors='w', linewidths=1.0)) ax_top.set_axis_off() ax_top.set_xlim(-2500, 2500) ax_top.set_ylim(-50, 450) ax_top.set_zlim(-250, 250) ax_top.view_init(elev=-1., azim=-2) fig_top.savefig("views/back.png") ax_top.set_xlim(-2500, 2500) ax_top.set_ylim(-50, 450) ax_top.set_zlim(-200, 300) ax_top.view_init(elev=-1., azim=182) fig_top.savefig("views/front.png")