def are_same_faces(occface1, occface2): """ This function checks if the two OCCfaces are the same. Parameters ---------- occface1 : OCCface The first OCCface to be checked. occface2 : OCCface The second OCCface to be checked. Returns ------- True or False : bool If True the faces are the same, if False the faces are not the same. """ pyptlist1 = fetch.points_frm_occface(occface1) pyptlist2 = fetch.points_frm_occface(occface2) pyptlist1.sort() pyptlist2.sort() if pyptlist1 == pyptlist2: return True else: return False
def flatten_face_z_value(occface, z=0): """ This function flatten the OCCface to the Z-value specified. Parameters ---------- occface : OCCface The OCCface to be flatten. z : float, optional The Z-value to flatten to. Default = 0. Returns ------- flatten face : OCCface The flatten OCCface. """ pyptlist = fetch.points_frm_occface(occface) pyptlist_2d = [] for pypt in pyptlist: pypt2d = (pypt[0], pypt[1], z) pyptlist_2d.append(pypt2d) flatten_face = construct.make_polygon(pyptlist_2d) return flatten_face
def flatten_face_z_value(occface, z=0): """ This function flatten the OCCface to the Z-value specified. Parameters ---------- occface : OCCface The OCCface to be flatten. z : float, optional The Z-value to flatten to. Default = 0. Returns ------- flatten face : OCCface The flatten OCCface. """ pyptlist = fetch.points_frm_occface(occface) pyptlist_2d = [] for pypt in pyptlist: pypt2d = (pypt[0],pypt[1],z) pyptlist_2d.append(pypt2d) flatten_face = construct.make_polygon(pyptlist_2d) return flatten_face
def write_2_stl2(occtopology, stl_filepath, is_meshed = True, linear_deflection = 0.8, angle_deflection = 0.5): """ This function writes a 3D model into STL format. This is different from write2stl as it uses the numpy-stl library. Parameters ---------- occtopology : OCCtopology Geometries to be written into STL. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex stl_filepath : str The file path of the STL file. mesh_incremental_float : float, optional Default = 0.8. Returns ------- None : None The geometries are written to a STL file. """ import numpy as np from stl import mesh if is_meshed == False: tri_faces = construct.simple_mesh(occtopology, linear_deflection = linear_deflection, angle_deflection = angle_deflection) occtopology = construct.make_compound(tri_faces) face_list = fetch.topo_explorer(occtopology, "face") vlist = fetch.topo_explorer(occtopology, "vertex") occptlist = modify.occvertex_list_2_occpt_list(vlist) pyptlist = modify.occpt_list_2_pyptlist(occptlist) pyptlist = modify.rmv_duplicated_pts(pyptlist) vertices = np.array(pyptlist) face_index_2dlsit = [] for face in face_list: f_pyptlist = fetch.points_frm_occface(face) f_pyptlist.reverse() if len(f_pyptlist) == 3: index_list = [] for fp in f_pyptlist: p_index = pyptlist.index(fp) index_list.append(p_index) face_index_2dlsit.append(index_list) elif len(f_pyptlist) > 3: print "THE FACE HAS THE WRONG NUMBER OF VERTICES, IT HAS:", len(f_pyptlist), "VERTICES" tri_faces = construct.simple_mesh(face) for tri_face in tri_faces: tps = fetch.points_frm_occface(tri_face) index_list = [] for tp in tps: p_index = pyptlist.index(tp) index_list.append(p_index) face_index_2dlsit.append(index_list) # else: # print "THE FACE HAS THE WRONG NUMBER OF VERTICES, IT HAS:", len(f_pyptlist), "VERTICES" faces = np.array(face_index_2dlsit) shape_mesh = mesh.Mesh(np.zeros(faces.shape[0], dtype = mesh.Mesh.dtype)) for i, f in enumerate(faces): for j in range(3): shape_mesh.vectors[i][j] = vertices[f[j],:] shape_mesh.save(stl_filepath)
def write_2_stl2(occtopology, stl_filepath, is_meshed=True, linear_deflection=0.8, angle_deflection=0.5): """ This function writes a 3D model into STL format. This is different from write2stl as it uses the numpy-stl library. Parameters ---------- occtopology : OCCtopology Geometries to be written into STL. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface, OCCwire, OCCedge, OCCvertex stl_filepath : str The file path of the STL file. mesh_incremental_float : float, optional Default = 0.8. Returns ------- None : None The geometries are written to a STL file. """ import numpy as np from stl import mesh if is_meshed == False: tri_faces = construct.simple_mesh(occtopology, linear_deflection=linear_deflection, angle_deflection=angle_deflection) occtopology = construct.make_compound(tri_faces) face_list = fetch.topo_explorer(occtopology, "face") vlist = fetch.topo_explorer(occtopology, "vertex") occptlist = modify.occvertex_list_2_occpt_list(vlist) pyptlist = modify.occpt_list_2_pyptlist(occptlist) pyptlist = modify.rmv_duplicated_pts(pyptlist) vertices = np.array(pyptlist) face_index_2dlsit = [] for face in face_list: f_pyptlist = fetch.points_frm_occface(face) f_pyptlist.reverse() if len(f_pyptlist) == 3: index_list = [] for fp in f_pyptlist: p_index = pyptlist.index(fp) index_list.append(p_index) face_index_2dlsit.append(index_list) elif len(f_pyptlist) > 3: print "THE FACE HAS THE WRONG NUMBER OF VERTICES, IT HAS:", len( f_pyptlist), "VERTICES" tri_faces = construct.simple_mesh(face) for tri_face in tri_faces: tps = fetch.points_frm_occface(tri_face) index_list = [] for tp in tps: p_index = pyptlist.index(tp) index_list.append(p_index) face_index_2dlsit.append(index_list) # else: # print "THE FACE HAS THE WRONG NUMBER OF VERTICES, IT HAS:", len(f_pyptlist), "VERTICES" faces = np.array(face_index_2dlsit) shape_mesh = mesh.Mesh(np.zeros(faces.shape[0], dtype=mesh.Mesh.dtype)) for i, f in enumerate(faces): for j in range(3): shape_mesh.vectors[i][j] = vertices[f[j], :] shape_mesh.save(stl_filepath)
def occtopo_2_collada(dae_filepath, occface_list=None, face_rgb_colour_list=None, occedge_list=None): """ This function converts OCCtopologies into a pycollada Collada class. The units are in meter. Parameters ---------- occface_list : list of OCCfaces The geometries to be visualised with the results. The list of geometries must correspond to the list of results. Other OCCtopologies are also accepted, but the OCCtopology must contain OCCfaces. OCCtopology includes: OCCshape, OCCcompound, OCCcompsolid, OCCsolid, OCCshell, OCCface. dae_filepath : str The file path of the DAE (Collada) file. face_rgb_colour_list : list of tuple of floats, optional Each tuple is a r,g,b that is specifying the colour of the face. The number of colours must correspond to the number of OCCfaces. occedge_list : list of OCCedges, optional OCCedges to be visualised together, Default = None. Returns ------- mesh : pycollada Collada class object The collada object from pycollada library. """ import collada from collada import asset, material, source, geometry, scene import numpy mesh = collada.Collada() mesh.assetInfo.upaxis = asset.UP_AXIS.Z_UP mesh.assetInfo.unitmeter = 1.0 mesh.assetInfo.unitname = "meter" if face_rgb_colour_list != None: mat_list = [] colour_cnt = 0 for rgb_colour in face_rgb_colour_list: effect = material.Effect("effect" + str(colour_cnt), [], "phong", diffuse=rgb_colour, specular=rgb_colour, double_sided=True) mat = material.Material("material" + str(colour_cnt), "mymaterial" + str(colour_cnt), effect) mesh.effects.append(effect) mesh.materials.append(mat) mat_list.append(mat) colour_cnt += 1 else: effect = material.Effect("effect0", [], "phong", diffuse=(1, 1, 1), specular=(1, 1, 1)) mat = material.Material("material0", "mymaterial", effect) mesh.effects.append(effect) mesh.materials.append(mat) edgeeffect = material.Effect("edgeeffect0", [], "phong", diffuse=(1, 1, 1), specular=(1, 1, 1), double_sided=True) edgemat = material.Material("edgematerial0", "myedgematerial", effect) mesh.effects.append(edgeeffect) mesh.materials.append(edgemat) geomnode_list = [] shell_cnt = 0 if occface_list: for occshell in occface_list: vert_floats = [] normal_floats = [] vcnt = [] indices = [] face_list = fetch.topo_explorer(occshell, "face") vert_cnt = 0 for face in face_list: wire_list = fetch.topo_explorer(face, "wire") nwire = len(wire_list) if nwire == 1: pyptlist = fetch.points_frm_occface(face) vcnt.append(len(pyptlist)) face_nrml = calculate.face_normal(face) pyptlist.reverse() for pypt in pyptlist: vert_floats.append(pypt[0]) vert_floats.append(pypt[1]) vert_floats.append(pypt[2]) normal_floats.append(face_nrml[0]) normal_floats.append(face_nrml[1]) normal_floats.append(face_nrml[2]) indices.append(vert_cnt) vert_cnt += 1 if nwire > 1: tri_face_list = construct.simple_mesh(face) for tface in tri_face_list: pyptlist = fetch.points_frm_occface(tface) vcnt.append(len(pyptlist)) face_nrml = calculate.face_normal(tface) pyptlist.reverse() for pypt in pyptlist: vert_floats.append(pypt[0]) vert_floats.append(pypt[1]) vert_floats.append(pypt[2]) normal_floats.append(face_nrml[0]) normal_floats.append(face_nrml[1]) normal_floats.append(face_nrml[2]) indices.append(vert_cnt) vert_cnt += 1 vert_id = "ID" + str(shell_cnt) + "1" vert_src = source.FloatSource(vert_id, numpy.array(vert_floats), ('X', 'Y', 'Z')) normal_id = "ID" + str(shell_cnt) + "2" normal_src = source.FloatSource(normal_id, numpy.array(normal_floats), ('X', 'Y', 'Z')) geom = geometry.Geometry(mesh, "geometry" + str(shell_cnt), "geometry" + str(shell_cnt), [vert_src, normal_src]) input_list = source.InputList() input_list.addInput(0, 'VERTEX', "#" + vert_id) #input_list.addInput(1, 'NORMAL', "#"+normal_id) vcnt = numpy.array(vcnt) indices = numpy.array(indices) if face_rgb_colour_list != None: mat_name = "materialref" + str(shell_cnt) polylist = geom.createPolylist(indices, vcnt, input_list, mat_name) geom.primitives.append(polylist) mesh.geometries.append(geom) matnode = scene.MaterialNode(mat_name, mat_list[shell_cnt], inputs=[]) geomnode = scene.GeometryNode(geom, [matnode]) geomnode_list.append(geomnode) else: mat_name = "materialref" polylist = geom.createPolylist(indices, vcnt, input_list, mat_name) geom.primitives.append(polylist) mesh.geometries.append(geom) matnode = scene.MaterialNode(mat_name, mat, inputs=[]) geomnode = scene.GeometryNode(geom, [matnode]) geomnode_list.append(geomnode) shell_cnt += 1 if occedge_list: edge_cnt = 0 for occedge in occedge_list: vert_floats = [] indices = [] pypt_list = fetch.points_frm_edge(occedge) if len(pypt_list) == 2: vert_cnt = 0 for pypt in pypt_list: vert_floats.append(pypt[0]) vert_floats.append(pypt[1]) vert_floats.append(pypt[2]) indices.append(vert_cnt) vert_cnt += 1 vert_id = "ID" + str(edge_cnt + shell_cnt) + "1" vert_src = source.FloatSource(vert_id, numpy.array(vert_floats), ('X', 'Y', 'Z')) geom = geometry.Geometry( mesh, "geometry" + str(edge_cnt + shell_cnt), "geometry" + str(edge_cnt + shell_cnt), [vert_src]) input_list = source.InputList() input_list.addInput(0, 'VERTEX', "#" + vert_id) indices = numpy.array(indices) mat_name = "edgematerialref" linelist = geom.createLineSet(indices, input_list, mat_name) geom.primitives.append(linelist) mesh.geometries.append(geom) matnode = scene.MaterialNode(mat_name, edgemat, inputs=[]) geomnode = scene.GeometryNode(geom, [matnode]) geomnode_list.append(geomnode) edge_cnt += 1 vis_node = scene.Node("node0", children=geomnode_list) myscene = scene.Scene("myscene", [vis_node]) mesh.scenes.append(myscene) mesh.scene = myscene return mesh