def import_as_one_shape(file): shp = read_step_file(file) tess = Tesselator(shp) tess.Compute() threejsString = tess.ExportShapeToThreejsJSONString('someid') # with open("outputs/threejs/shape.json", "w") as f: # f.write(threejsString) return threejsString
def DisplayShape(self, shape, vertex_shader=None, fragment_shader=None, map_faces_to_mesh=False, export_edges=False, color=(0.65, 0.65, 0.65), specular_color=(1, 1, 1), shininess=0.9, transparency=0., line_color=(0, 0., 0.), line_width=2., mesh_quality=1.): # create the shape hash 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) # sys.stdout.write( "\r%s mesh shape, %i triangles" % (next(self.spinning_cursor), 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 edges = [] 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 += ExportEdgeToJSON(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 self._edges_hash.append(edge_hash)
def test_export_to_3js_JSON(self): a_box = BRepPrimAPI_MakeBox(10, 20, 30).Shape() tess = Tesselator(a_box) tess.Compute() # get the JSON string JSON_str = tess.ExportShapeToThreejsJSONString("myshapeid") # check the python JSON parser can decode the string # i.e. the JSON string is well formed dico = json.loads(JSON_str) # after that, check that the number of vertices is ok assert len(dico["data"]["attributes"]["position"]["array"]) == 36 * 3
def _exportThreeJS(filePath, _shape): _tess = Tesselator(_shape) _tess.Compute(uv_coords=False, compute_edges=False, mesh_quality=50) json_shape = _tess.ExportShapeToThreejsJSONString(filePath) json_shape = json_shape.replace("data\\", "data/") json_shape = json_shape.replace("\\step_postprocessed\\", "/step_postprocessed/") return json_shape
def _write_threejs_json(html_dir, shape): tess = Tesselator(shape) tess.Compute(compute_edges=False, mesh_quality=1.) with open(os.path.join(html_dir, "table.json"), 'w') as f: f.write(tess.ExportShapeToThreejsJSONString('table'))