def AddCurveToScene(self, shp, edge_color, deflection): """ shp is either a TopoDS_Wire or a TopodS_Edge. """ if is_edge(shp): pnts = discretize_edge(shp, deflection) elif is_wire(shp): pnts = discretize_wire(shp, deflection) np_edge_vertices = np.array(pnts, dtype=np.float32) np_edge_indices = np.arange(np_edge_vertices.shape[0], dtype=np.uint32) edge_geometry = BufferGeometry( attributes={ 'position': BufferAttribute(np_edge_vertices), 'index': BufferAttribute(np_edge_indices) }) edge_material = LineBasicMaterial(color=edge_color, linewidth=1) # and to the dict of shapes, to have a mapping between meshes and shapes edge_id = "%s" % uuid.uuid4().hex self._shapes[edge_id] = shp edge_line = Line(geometry=edge_geometry, material=edge_material, name=edge_id) # and to the dict of shapes, to have a mapping between meshes and shapes edge_id = "%s" % uuid.uuid4().hex self._shapes[edge_id] = shp return edge_line
def DisplayShape(self, shape, vertex_shader=None, fragment_shader=None, export_edges=False, color=(0.65, 0.65, 0.7), specular_color=(0.2, 0.2, 0.2), shininess=0.9, transparency=0., line_color=(0, 0., 0.), line_width=2., mesh_quality=1.): """ Adds a shape to the rendering buffer. This class computes the x3d file """ # if the shape is an edge or a wire, use the related functions if is_edge(shape): print("X3D exporter, discretize an edge") pnts = discretize_edge(shape) edge_hash = "edg%s" % uuid.uuid4().hex line_set = ExportEdgeToILS(pnts) x3dfile_content = indexed_lineset_to_x3d_string([line_set], ils_id=edge_hash) edge_full_path = os.path.join(self._path, edge_hash + '.x3d') with open(edge_full_path, "w") as edge_file: edge_file.write(x3dfile_content) # store this edge hash self._x3d_edges[edge_hash] = [color, line_width] return self._x3d_shapes, self._x3d_edges elif is_wire(shape): print("X3D exporter, discretize a wire") pnts = discretize_wire(shape) wire_hash = "wir%s" % uuid.uuid4().hex line_set = ExportEdgeToILS(pnts) x3dfile_content = indexed_lineset_to_x3d_string([line_set], ils_id=wire_hash) wire_full_path = os.path.join(self._path, wire_hash + '.x3d') with open(wire_full_path, "w") as wire_file: wire_file.write(x3dfile_content) # store this edge hash self._x3d_edges[wire_hash] = [color, line_width] return self._x3d_shapes, self._x3d_edges shape_uuid = uuid.uuid4().hex shape_hash = "shp%s" % shape_uuid x3d_exporter = X3DExporter(shape, vertex_shader, fragment_shader, export_edges, color, specular_color, shininess, transparency, line_color, line_width, mesh_quality) x3d_exporter.compute() x3d_filename = os.path.join(self._path, "%s.x3d" % shape_hash) # the x3d filename is computed from the shape hash shape_id = len(self._x3d_shapes) x3d_exporter.write_to_file(x3d_filename, shape_id) ## TODO : orientation and translation # get shape translation and orientation # trans = shape.Location().Transformation().TranslationPart().Coord() # vector # v = gp_Vec() # angle = shape.Location().Transformation().GetRotation().GetVectorAndAngle(v) # ori = (v.X(), v.Y(), v.Z(), angle) # angles # fill the shape dictionnary with shape hash, translation and orientation # self._x3d_shapes[shape_hash] = [trans, ori] self._x3d_shapes[shape_hash] = [export_edges, color, specular_color, shininess, transparency, line_color, line_width] return self._x3d_shapes, self._x3d_edges
def DisplayShape(self, shape, vertex_shader=None, fragment_shader=None, export_edges=False, color=(0.65, 0.65, 0.7), specular_color=(0.2, 0.2, 0.2), shininess=0.9, transparency=0., line_color=(0, 0., 0.), line_width=2., mesh_quality=1.): """ Adds a shape to the rendering buffer. This class computes the x3d file """ # if the shape is an edge or a wire, use the related functions if is_edge(shape): print("X3D exporter, discretize an edge") pnts = discretize_edge(shape) edge_hash = "edg%s" % uuid.uuid4().hex line_set = export_edge_to_indexed_lineset(pnts) x3dfile_content = indexed_lineset_to_x3d_string([line_set], ils_id=edge_hash) edge_full_path = os.path.join(self._path, edge_hash + '.x3d') with open(edge_full_path, "w") as edge_file: edge_file.write(x3dfile_content) # store this edge hash self._x3d_edges[edge_hash] = [color, line_width] return self._x3d_shapes, self._x3d_edges if is_wire(shape): print("X3D exporter, discretize a wire") pnts = discretize_wire(shape) wire_hash = "wir%s" % uuid.uuid4().hex line_set = export_edge_to_indexed_lineset(pnts) x3dfile_content = indexed_lineset_to_x3d_string([line_set], ils_id=wire_hash) wire_full_path = os.path.join(self._path, wire_hash + '.x3d') with open(wire_full_path, "w") as wire_file: wire_file.write(x3dfile_content) # store this edge hash self._x3d_edges[wire_hash] = [color, line_width] return self._x3d_shapes, self._x3d_edges shape_uuid = uuid.uuid4().hex shape_hash = "shp%s" % shape_uuid x3d_exporter = X3DExporter(shape, vertex_shader, fragment_shader, export_edges, color, specular_color, shininess, transparency, line_color, line_width, mesh_quality) x3d_exporter.compute() x3d_filename = os.path.join(self._path, "%s.x3d" % shape_hash) # the x3d filename is computed from the shape hash shape_id = len(self._x3d_shapes) x3d_exporter.write_to_file(x3d_filename, shape_id) self._x3d_shapes[shape_hash] = [ export_edges, color, specular_color, shininess, transparency, line_color, line_width ] return self._x3d_shapes, self._x3d_edges
def drawShape(self, shape, color=(0.65, 0.65, 0.7), transparency=0., line_width=1., ): # if the shape is an edge or a wire, use the related functions shininess=0.9 specular_color=(0.2, 0.2, 0.2) shape_precision, wire_precision = self.precision if is_edge(shape): print("discretize an edge") pnts = discretize_edge(shape, wire_precision) edge_hash = "exp_%s_edge" % str(self.shapeNum).zfill(3) self.shapeNum += 1 str_to_write = export_edgedata_to_json(edge_hash, pnts) edge_full_path = os.path.join(self._path, edge_hash + '.json') with open(edge_full_path, "w") as edge_file: edge_file.write(str_to_write) # store this edge hash self.stringList.append("\tzdeskXCurve(slidePath+'%s.json', %s, %g);\n" % (edge_hash, color_to_hex(color), line_width)) print("%s, %i segments" % (edge_hash ,len(pnts)-1)) elif is_wire(shape): print("discretize a wire") pnts = discretize_wire(shape, wire_precision) wire_hash = "exp_%s_wire" % str(self.shapeNum).zfill(3) self.shapeNum += 1 str_to_write = export_edgedata_to_json(wire_hash, pnts) wire_full_path = os.path.join(self._path, wire_hash + '.json') with open(wire_full_path, "w") as wire_file: wire_file.write(str_to_write) # store this edge hash self.stringList.append("\tzdeskXCurve(slidePath+'%s.json', %s, %g);\n" % (wire_hash, color_to_hex(color), line_width)) print("%s, %i segments" % (wire_hash ,len(pnts)-1)) else: #solid or shell print("tesselate a shape") shape_uuid = uuid.uuid4().hex shape_hash = "exp_%s_shape" % str(self.shapeNum).zfill(3) self.shapeNum += 1 # tesselate tess = ShapeTesselator(shape) tess.Compute(compute_edges=False, mesh_quality=shape_precision, parallel=True) # export to 3JS shape_full_path = os.path.join(self._path, shape_hash + '.json') with open(shape_full_path, 'w') as json_file: json_file.write(tess.ExportShapeToThreejsJSONString(shape_uuid)) self.stringList.append("\tzdeskXShape(slidePath+'%s.json', %s, %s, %g, %g);\n" % (shape_hash, color_to_hex(color), color_to_hex(specular_color), shininess, 1 - transparency)) print("%s, %i triangles\n" % (shape_hash, tess.ObjGetTriangleCount()))
def AddCurveToScene(self, shp, color): """ shp is either a TopoDS_Wire or a TopodS_Edge. """ if is_edge(shp): pnts = discretize_edge(shp) elif is_wire(shp): pnts = discretize_wire(shp) np_edge_vertices = np.array(pnts, dtype=np.float32) np_edge_indices = np.arange(np_edge_vertices.shape[0], dtype=np.uint32) edge_geometry = BufferGeometry(attributes={ 'position': BufferAttribute(np_edge_vertices), 'index' : BufferAttribute(np_edge_indices) }) edge_material = LineBasicMaterial(color=color, linewidth=1) edge_lines = Line(geometry=edge_geometry, material=edge_material) # Add geometries to pickable or non pickable objects self._displayed_pickable_objects.add(edge_lines)
def AddCurveToScene(self, shp, color): """ shp is either a TopoDS_Wire or a TopodS_Edge. """ if is_edge(shp): pnts = discretize_edge(shp) elif is_wire(shp): pnts = discretize_wire(shp) np_edge_vertices = np.array(pnts, dtype=np.float32) np_edge_indices = np.arange(np_edge_vertices.shape[0], dtype=np.uint32) edge_geometry = BufferGeometry(attributes={ 'position': BufferAttribute(np_edge_vertices), 'index' : BufferAttribute(np_edge_indices) }) edge_material = LineBasicMaterial(color=color, linewidth=2, fog=True) edge_lines = LineSegments(geometry=edge_geometry, material=edge_material) # Add geometries to pickable or non pickable objects self._displayed_pickable_objects.add(edge_lines)
def DisplayShape(self, shape, export_edges=False, color=(0.65, 0.65, 0.7), specular_color=(0.2, 0.2, 0.2), shininess=0.9, transparency=0., line_color=(0, 0., 0.), line_width=2., mesh_quality=1.): # if the shape is an edge or a wire, use the related functions if is_edge(shape): print("discretize an edge") pnts = discretize_edge(shape) edge_hash = "edg%s" % uuid.uuid4().hex str_to_write = export_edgedata_to_json(edge_hash, pnts) edge_full_path = os.path.join(self._path, edge_hash + '.json') with open(edge_full_path, "w") as edge_file: edge_file.write(str_to_write) # store this edge hash self._3js_edges[edge_hash] = [color, line_width] return self._3js_shapes, self._3js_edges elif is_wire(shape): print("discretize a wire") pnts = discretize_wire(shape) wire_hash = "wir%s" % uuid.uuid4().hex str_to_write = export_edgedata_to_json(wire_hash, pnts) wire_full_path = os.path.join(self._path, wire_hash + '.json') with open(wire_full_path, "w") as wire_file: wire_file.write(str_to_write) # store this edge hash self._3js_edges[wire_hash] = [color, line_width] return self._3js_shapes, self._3js_edges shape_uuid = uuid.uuid4().hex shape_hash = "shp%s" % shape_uuid # tesselate tess = Tesselator(shape) tess.Compute(compute_edges=export_edges, mesh_quality=mesh_quality, uv_coords=False, parallel=True) # update spinning cursor sys.stdout.write("\r%s mesh shape %s, %i triangles " % (next(self.spinning_cursor), shape_hash, tess.ObjGetTriangleCount())) sys.stdout.flush() # export to 3JS shape_full_path = os.path.join(self._path, shape_hash + '.json') # add this shape to the shape dict, sotres everything related to it self._3js_shapes[shape_hash] = [export_edges, color, specular_color, shininess, transparency, line_color, line_width] # generate the mesh #tess.ExportShapeToThreejs(shape_hash, shape_full_path) # and also to JSON with open(shape_full_path, 'w') as json_file: json_file.write(tess.ExportShapeToThreejsJSONString(shape_uuid)) # draw edges if necessary if export_edges: # export each edge to a single json # get number of edges nbr_edges = tess.ObjGetEdgeCount() for i_edge in range(nbr_edges): # after that, the file can be appended str_to_write = '' edge_point_set = [] nbr_vertices = tess.ObjEdgeGetVertexCount(i_edge) for i_vert in range(nbr_vertices): edge_point_set.append(tess.GetEdgeVertex(i_edge, i_vert)) # write to file edge_hash = "edg%s" % uuid.uuid4().hex str_to_write += export_edgedata_to_json(edge_hash, edge_point_set) # create the file edge_full_path = os.path.join(self._path, edge_hash + '.json') with open(edge_full_path, "w") as edge_file: edge_file.write(str_to_write) # store this edge hash, with black color self._3js_edges[hash] = [(0, 0, 0), line_width] return self._3js_shapes, self._3js_edges
def test_discretize_wire(self): tor = BRepPrimAPI_MakeTorus(50, 20).Shape() topo = TopologyExplorer(tor) for wire in topo.wires(): pnts = discretize_wire(wire) self.assertTrue(pnts)
def ConvertShape( self, shape, export_edges=False, color=(0.65, 0.65, 0.7), specular_color=(0.2, 0.2, 0.2), shininess=0.9, transparency=0.0, line_color=(0, 0.0, 0.0), line_width=1.0, point_size=1.0, mesh_quality=1.0, ): # if the shape is an edge or a wire, use the related functions color = color_to_hex(color) specular_color = color_to_hex(specular_color) if is_edge(shape): print("discretize an edge") pnts = discretize_edge(shape) edge_hash = "edg%s" % uuid.uuid4().hex shape_content = export_edgedata_to_json(edge_hash, pnts) # store this edge hash self._3js_edges[edge_hash] = [color, line_width, shape_content] return self._3js_shapes, self._3js_edges, self._3js_vertex elif is_wire(shape): print("discretize a wire") pnts = discretize_wire(shape) wire_hash = "wir%s" % uuid.uuid4().hex shape_content = export_edgedata_to_json(wire_hash, pnts) # store this edge hash self._3js_edges[wire_hash] = [color, line_width, shape_content] return self._3js_shapes, self._3js_edges, self._3js_vertex # if shape is array of gp_Pnt elif isinstance(shape, list) and isinstance(shape[0], gp_Pnt): print("storage points") vertices_list = [] # will be passed to javascript BB = BRep_Builder() compound = TopoDS_Compound() BB.MakeCompound(compound) for vertex in shape: vertext_to_add = BRepBuilderAPI_MakeVertex(vertex).Shape() BB.Add(compound, vertext_to_add) vertices_list.append([vertex.X(), vertex.Y(), vertex.Z()]) points_hash = "pnt%s" % uuid.uuid4().hex # store this vertex hash. Note: TopoDS_Compound did not save now self._3js_vertex[points_hash] = [color, point_size, vertices_list] return self._3js_shapes, self._3js_edges, self._3js_vertex # convert as TopoDS_Shape shape_uuid = uuid.uuid4().hex shape_hash = "shp%s" % shape_uuid # tesselate tess = ShapeTesselator(shape) tess.Compute(compute_edges=export_edges, mesh_quality=mesh_quality, parallel=True) # update spinning cursor sys.stdout.write("\r%s mesh shape %s, %i triangles " % (next( self.spinning_cursor), shape_hash, tess.ObjGetTriangleCount())) sys.stdout.flush() # export to 3JS # generate the mesh shape_content = tess.ExportShapeToThreejsJSONString(shape_uuid) # add this shape to the shape dict, sotres everything related to it self._3js_shapes[shape_hash] = [ export_edges, color, specular_color, shininess, transparency, line_color, line_width, shape_content, ] # draw edges if necessary if export_edges: # export each edge to a single json # get number of edges nbr_edges = tess.ObjGetEdgeCount() for i_edge in range(nbr_edges): # after that, the file can be appended edge_content = "" edge_point_set = [] nbr_vertices = tess.ObjEdgeGetVertexCount(i_edge) for i_vert in range(nbr_vertices): edge_point_set.append(tess.GetEdgeVertex(i_edge, i_vert)) # write to file edge_hash = "edg%s" % uuid.uuid4().hex edge_content += export_edgedata_to_json( edge_hash, edge_point_set) # store this edge hash, with black color self._3js_edges[edge_hash] = [ color_to_hex((0, 0, 0)), line_width, edge_content, ] return self._3js_shapes, self._3js_edges, self._3js_vertex