def crackpolygon(meshes, count): tempMeshes = meshes newMeshes = [] if count == 0: return 1 else: for mesh in tempMeshes: if rs.MeshVertexCount(mesh) != 3 and rs.MeshVertexCount(mesh) != 4: countV = rs.MeshVertexCount(mesh) print "mesh has too many vertices" else: #print "Cool" centroid = rs.MeshAreaCentroid(mesh) normals = rs.MeshFaceNormals(mesh) centroid = rs.PointAdd(centroid, normals[0] * (count / 5)) vertices = rs.MeshVertices(mesh) for i in range(1, len(vertices)): newVertices = [] newVertices.append(vertices[i]) newVertices.append(centroid) newVertices.append(vertices[i - 1]) newFaces = [[0, 1, 2]] newMesh = rs.AddMesh(newVertices, newFaces) newMeshes.append(newMesh) newVertices = [] newVertices.append(vertices[0]) newVertices.append(centroid) newVertices.append(vertices[len(vertices) - 1]) newFaces = [[0, 1, 2]] newMesh = rs.AddMesh(newVertices, newFaces) newMeshes.append(newMesh) return crackpolygon(newMeshes, count - 1)
def construct_mesh_center(points): meshes = [] ### calc center point bbox = rs.BoundingBox(points) line = rs.AddLine(bbox[0], bbox[6]) c = rs.CurveMidPoint(line) ### set new point list points.append(c) ### c_index = len(points) - 1 # print(c_index) for j in xrange(len(points) - 1): if j < (len(points) - 2): vertex_ = [(c_index, int(j), int(j) + 1, int(j) + 1)] # print(vertex_) m = rs.AddMesh(points, vertex_) meshes.append(m) else: vertex_ = [(c_index, int(j), 0, 0)] # print(vertex_) m = rs.AddMesh(points, vertex_) meshes.append(m) mesh_joined = rs.JoinMeshes(meshes) return mesh_joined
def xdraw_mesh(vertices, faces, name=None, color=None, **kwargs): guid = rs.AddMesh(vertices, faces) if color: rs.ObjectColor(guid, color) if name: rs.ObjectName(guid, name) return guid
def flow(mesh_id=None, step=1): """Performs one step of the face flow of the given mesh, replacing that mesh with a new one. """ # If mesh_id is None, then get the mesh from the user. # Check that all its faces are correctly set up. mesh_id = mu.get_and_check_mesh(mesh_id) # Various precomputations (including motion vectors for each face) normals = get_motion_vectors(mesh_id, step) adj_faces = adjacent_faces(mesh_id) n = len(rs.MeshVertices(mesh_id)) face_planes = mu.get_face_planes(mesh_id) # Shift all the planes by their normal vectors. # NOTE(mikhaildubov): This computation relies on the fact that normals are # listed in the same order as the corresponding faces. face_planes_translated = [ translate_plane(face_planes[i], normals[i]) for i in xrange(len(face_planes)) ] # Calculate the intersections of the shifted planes. # Those are going to be the vertices of the updated mesh. new_vertices = [] for i in xrange(n): adj_planes = [face_planes_translated[j] for j in adj_faces[i]] adj_planes_eq = [rs.PlaneEquation(plane) for plane in adj_planes] intersection_point = planes_intersection(adj_planes_eq) new_vertices.append(intersection_point) # Update the mesh new_mesh_id = rs.AddMesh(new_vertices, rs.MeshFaceVertices(mesh_id)) rs.DeleteObject(mesh_id) return new_mesh_id
def draw_harmonic_disp(path, amp, name): nodes, elements = get_nodes_elements_from_result_files(path) har_disp, freqs = get_harmonic_data_from_result_files(path) vkeys = sorted(nodes.keys(), key=int) vert = [[nodes[k]['x'], nodes[k]['y'], nodes[k]['z']] for k in vkeys] fkeys = sorted(elements.keys(), key=int) faces = [elements[k]['topology'] for k in fkeys] dkeys = sorted(har_disp.keys(), key=int) for freq in freqs: print freq lname = 'freq ' + str(round(freq, 2)) + 'Hz' rs.AddLayer(lname) rs.CurrentLayer(lname) disp = [har_disp[k][freq]['real'] for k in dkeys] disp = [[disp[k]['x'] * amp, disp[k]['y'] * amp, disp[k]['z'] * amp] for k in dkeys] dvert = [] dlens = [] for i in range(len(vert)): v = vert[i] d = disp[i] dlens.append(sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2])) dvert.append([v[0] + d[0], v[1] + d[1], v[2] + d[2]]) colors = [] maxd = max(dlens) mind = min(dlens) if mind == maxd: colors = [0, 0, 0] * len(vert) else: for dlen in dlens: value = (dlen - mind) / (maxd - mind) colors.append(i_to_rgb(value)) rs.AddMesh(dvert, faces, vertex_colors=colors)
def meshfunction_xy(): zfunc, domain, resolution = loadfunctiondata() zfunc = rs.StringBox( zfunc, "Specify a function f(x,y[,D,A])", "Mesh function") if not zfunc: return while True: prompt = "Function domain x{%f,%f} y{%f,%f} @%d" % (domain[0], domain[1], domain[2], domain[3], resolution) result = rs.GetString(prompt, "Insert", ("xMin","xMax","yMin","yMax","Resolution","Insert")) if not result: return result = result.upper() if result=="XMIN": f = rs.GetReal("X-Domain start", domain[0]) if f is not None: domain[0]=f elif result=="XMAX": f = rs.GetReal("X-Domain end", domain[1]) if f is not None: domain[1]=f elif result=="YMIN": f = rs.GetReal("Y-Domain start", domain[2]) if f is not None: domain[2]=f elif result=="YMAX": f = rs.GetReal("Y-Domain end", domain[3]) if f is not None: domain[3]=f elif result=="RESOLUTION": f = rs.GetInteger("Resolution of the graph", resolution) if f is not None: resolution=f elif result=="INSERT": break verts = createmeshvertices(zfunc, domain, resolution) faces = createmeshfaces(resolution) rs.AddMesh(verts, faces)
def flow(mesh_id=None, step=1): """Performs one step of the harmonic flow of the given mesh, replacing that mesh with a new one. """ # TODO(mikhaildubov): This flow results in a degenerate case at the poles of sphere134.3dm. # Fix this by making it a true MCF (i.e. by using the angles). # If mesh_id is None, then get the mesh from the user. # Check that all its faces are correctly set up. mesh_id = mu.get_and_check_mesh(mesh_id) # Various precomputations (including motion vectors for each vertex) v = rs.MeshVertices(mesh_id) n = len(v) harmonic_vectors = get_motion_vectors(mesh_id, step) # Move each vertex by its motion vector new_vertices = [] for i in xrange(n): new_vertices.append(rs.PointAdd(v[i], harmonic_vectors[i])) # Update the mesh new_mesh_id = rs.AddMesh(new_vertices, rs.MeshFaceVertices(mesh_id)) rs.DeleteObject(mesh_id) return new_mesh_id
def xdraw_mesh(vertices, faces, color, name): guid = rs.AddMesh(vertices, faces) if color: rs.ObjectColor(guid, color) if name: rs.ObjectName(guid, name) return guid
def plot_mesh(self, s_mesh, data_tag, mode='max'): # modes: max, min, avg, angle_avg data = s_mesh.get_data_on_nodes(data_tag, mode) rs_points = [] for v in s_mesh.c_mesh.vertices(): rs_points.append(s_mesh.c_mesh.vertex_coordinates(v)) faces = [] for idx, f in enumerate(s_mesh.c_mesh.faces()): f_vts = s_mesh.c_mesh.face_vertices(f) if idx == 0: print('initial indices are: {}'.format(f_vts)) if len(f_vts) < 4: f_vts.append(f_vts[-1]) faces.append(f_vts) n_data = ut.normalise_data(data) color_data = ut.colorbar(n_data[0]) new_gh_mesh = rs.AddMesh(rs_points, faces, vertex_normals=None, texture_coordinates=None, vertex_colors=None) return new_gh_mesh, data, color_data
def display_faces(faces): vertices = [] vertexColors = [] facesIndices = [] for f in faces: faceIndices = [] # add vertices for v in f.vertices: faceIndices.append(len(vertices)) vertices.append((v.x,v.y,v.z)) vertexColors.append((f.color[0]*255,f.color[1]*255,f.color[2]*255)) p = len(f.vertices) if p <= 4: # add one face if it is tri or quad facesIndices.append(faceIndices) else: # add multiple faces if it is ngon points = [(v.x, v.y, v.z) for v in f.vertices] center_pt = centroid_points(points) c_index = len(vertices) vertices.append(center_pt) vertexColors.append((f.color[0]*255,f.color[1]*255,f.color[2]*255)) faces = [[a, b, c_index] for a, b in pairwise(faceIndices + faceIndices[0:1])] facesIndices.extend(faces) return rs.AddMesh(vertices,facesIndices,None,None,vertexColors)
def add_cube(side=1): """Adds a cube with the given side to Rhinoceros.""" vertices = [(0, 0, 0), (side, 0, 0), (side, side, 0), (0, side, 0), (0, 0, side), (side, 0, side), (side, side, side), (0, side, side)] face_vertices = [(0, 3, 2, 1), (4, 5, 6, 7), (0, 1, 5, 4), (1, 2, 6, 5), (2, 3, 7, 6), (3, 0, 4, 7)] return rs.AddMesh(vertices, face_vertices)
def LocationMesh(origin, basis): points = UnitBasis() # Convert directions to positions relative to origin for b in range(3): points[b] = rs.VectorAdd(basis[b], origin) # Construct basis tetrahedron mesh = rs.AddMesh([origin, points[0], points[1], points[2]], [[0, 2, 1], [0, 3, 2], [0, 1, 3], [1, 2, 3]]) return mesh
def add_mesh(self, verts3): ### Convert verts(Number-List) to Point points = self.vert3_to_points(verts3) # print(points) ### Create Mesh m = rs.AddMesh(points, [[0, 1, 2]]) return m
def plot_volmesh(volmesh, layer=None, draw_cells=True): """ Plot a volmesh datastructure. Parameters ---------- volmesh : obj volmesh datastructure object. layer : str Layer name to draw on. draw_cells : bool Draw cells. Returns ------- None """ if layer: rs.CurrentLayer(layer) vkeys = sorted(list(volmesh.vertices()), key=int) vertices = [volmesh.vertex_coordinates(vkey) for vkey in vkeys] if draw_cells: meshes = [] for ckey in volmesh.cell: faces = [ volmesh.halfface_vertices(fk, ordered=True) for fk in volmesh.cell_halffaces(ckey) ] meshes.append(rs.AddMesh(vertices, faces)) return meshes else: faces = [] for fk in volmesh.halfface: face = volmesh.halfface_vertices(fk, ordered=True) faces.append(face) mesh = rs.AddMesh(vertices, faces) return mesh
def randommeshcolors(): mesh_id = rs.GetObject("Mesh to randomize", 32, True, True) if not mesh_id: return verts = rs.MeshVertices(mesh_id) faces = rs.MeshFaceVertices(mesh_id) colors = [] for vert in verts: rgb = random() * 255, random() * 255, random() * 255 colors.append(rgb) rs.AddMesh(verts, faces, vertex_colors=colors) rs.DeleteObject(mesh_id)
def display_faces(faces): vertices=[] facesIndices=[] vertexColors=[] for f in faces: faceIndices=[] for v in f.vertices: faceIndices.append(len(vertices)) vertices.append((v.x,v.y,v.z)) vertexColors.append((f.color[0]*255,f.color[1]*255,f.color[2]*255)) facesIndices.append(faceIndices) rs.AddMesh(vertices,facesIndices,None,None,vertexColors)
def construct_mesh_step(points): meshes = [] ### step for i in xrange(1, len(points) - 1): vertex_ = [(0, int(i), int(i) + 1, int(i) + 1)] m = rs.AddMesh(points, vertex_) meshes.append(m) mesh_joined = rs.JoinMeshes(meshes) return mesh_joined
def discretise_mesh(mesh, layer, target, min_angle=15, factor=1): """ Discretise a mesh from an input triangulated coarse mesh into small denser meshes. Parameters ---------- mesh : guid The guid of the Rhino input mesh. layer : str Layer name to draw results. target : float Target length of each triangle. min_angle : float Minimum internal angle of triangles. factor : float Factor on the maximum area of each triangle. Returns ------- None """ rhinomesh = RhinoMesh(mesh) vertices = rhinomesh.get_vertex_coordinates() faces = [face[:3] for face in rhinomesh.get_face_vertices()] try: points, tris = meshing.discretise_faces(vertices=vertices, faces=faces, target=target, min_angle=min_angle, factor=factor) rs.CurrentLayer(rs.AddLayer(layer)) rs.DeleteObjects(rs.ObjectsByLayer(layer)) rs.EnableRedraw(False) for pts, tri in zip(points, tris): mesh_faces = [] for i in tri: face_ = i + [i[-1]] mesh_faces.append(face_) rs.AddMesh(pts, mesh_faces) rs.EnableRedraw(True) except: print('***** Error using MeshPy (Triangle) or drawing faces *****')
def draw_light(mesh,temp = True): key_index = dict((key, index) for index, key in mesh.vertices_enum()) xyz = mesh.xyz faces = [] for fkey in mesh.faces_iter(): face = mesh.face_vertices(fkey,True) face.append(face[-1]) faces.append([key_index[k] for k in face]) guid = rs.AddMesh(xyz, faces) if temp: rs.EnableRedraw(True) rs.EnableRedraw(False) rs.DeleteObject(guid) return guid
def createMesh(): xstep = (domain[1] - domain[0]) / nx ystep = (domain[3] - domain[2]) / ny verts = [] for i in range(nx + 1): x = domain[0] + i * xstep for j in range(ny + 1): y = domain[2] + j * ystep z = solveEquation(function, x, y) verts.append((x, y, z)) faces = [] for m in range(nx): for n in range(ny): e = m * (ny + 1) + n faces.append((e, e + 1, e + ny + 2, e + ny + 1)) rs.AddMesh(verts, faces)
def ProximityAnalysis(): mesh_id = rs.GetObject("Mesh for proximity analysis", 32, True, True) if not mesh_id: return brep_id = rs.GetObject("Surface for proximity test", 8+16, False, True) if not brep_id: return vertices = rs.MeshVertices(mesh_id) faces = rs.MeshFaceVertices(mesh_id) arrD = VertexValueArray(vertices, brep_id) minD = min(arrD) maxD = max(arrD) colors = [] for i in range(len(vertices)): proxFactor = (arrD[i]-minD)/(maxD-minD) colors.append( (255, 255*proxFactor, 255*proxFactor) ) rs.AddMesh(vertices, faces, vertex_colors=colors) rs.DeleteObject(mesh_id)
def get_mesh_faces(mesh): """ Takes a mesh and convert its faces into individual meshes. :param mesh: mesh :return: list of "face" meshes """ mesh_faces = rs.MeshFaces(mesh) faces = [] for i in range(0, len(mesh_faces), 4): vertices = [mesh_faces[i], mesh_faces[i + 1], mesh_faces[i + 2], mesh_faces[i + 3]] added_mesh = rs.AddMesh(vertices, [[0, 1, 2, 3]]) faces.append(added_mesh) return faces
def color_mesh(mesh,dis_ub,dis_lb): max_distances = [] mesh_faces = [] for fkey in mesh.faces_iter(): keys = mesh.face_vertices(fkey,ordered=True) points = [mesh.vertex_coordinates(key) for key in keys] plane = rs.PlaneFitFromPoints(points) points_planar = [rs.PlaneClosestPoint(plane, pt) for pt in points] distances = [distance(pt1,pt2) for pt1,pt2 in zip(points,points_planar)] max_distances.append(max(distances)) mesh_faces.append(rs.AddMesh(points,[(0,1,2,3)])) values = normalize_values(max_distances,dis_ub,dis_lb,1,0) for i,face in enumerate(mesh_faces): rs.ObjectColor(face,i2rgb(values[i])) return mesh_faces
def mesh_to_point(mesh_dict, point_list, mesh_list, len_mesh_list): ''' Randomly selects a mesh from mesh_list, generates it and moves it to a point in point_list. Then checks for intersections with other meshes. ''' mlist = [] n = 0 for point in point_list: added = rs.AddMesh(mesh_dict[mesh_list[n]][0], mesh_dict[mesh_list[n]][1]) center = rs.MeshAreaCentroid(added) inset = rs.MeshClosestPoint(added, center) translation = [point[i] - inset[0][i] for i in range(0, 3)] moved = rs.MoveObject(added, translation) mesh = rotatexyz.rotate(moved) mlist.append(mesh) n += 1 if n == len_mesh_list: n = 0 return (mlist)
def weld_meshes_from_layer(layer_input, layer_output): """ Grab meshes on an input layer and weld them onto an output layer. Parameters ---------- layer_input : str Layer containing the Rhino meshes to weld. layer_output : str Layer to plot single welded mesh. Returns ------- None """ print('Welding meshes on layer:{0}'.format(layer_input)) mdl = Structure(path=' ') add_nodes_elements_from_layers(mdl, mesh_type='ShellElement', layers=layer_input) faces = [] for element in mdl.elements.values(): enodes = element.nodes if len(enodes) == 3: enodes.append(enodes[-1]) if len(enodes) == 4: faces.append(enodes) rs.DeleteObjects(rs.ObjectsByLayer(layer_output)) rs.CurrentLayer(layer_output) rs.AddMesh(mdl.nodes_xyz(), faces)
def draw_light(mesh, temp=True): pts = [] faces = [] count = 0 for u, v in mesh.edges(): pts.append(mesh.vertex_coordinates(u)) pts.append(mesh.vertex_coordinates(v)) pts.append( (mesh.vertex[u]['x2'], mesh.vertex[u]['y2'], mesh.vertex[u]['z2'])) pts.append( (mesh.vertex[v]['x2'], mesh.vertex[v]['y2'], mesh.vertex[v]['z2'])) faces.append([count, count + 1, count + 3, count + 2]) count += 4 guid = rs.AddMesh(pts, faces) if temp: Rhino.RhinoApp.Wait() rs.Redraw() # rs.EnableRedraw(True) # rs.EnableRedraw(False) rs.DeleteObject(guid) return guid
def meshLoftSections(sections, closed=True): faces = [] v = [] for i in range(len(sections)): v.extend(sections[i]) for i in range(len(sections) - 1): for j in range(len(sections[0]) - 1): index0 = i * len(sections[0]) + j index1 = i * len(sections[0]) + (j + 1) % len(sections) index2 = (i + 1) * len(sections[0]) + (j + 1) % len(sections) index3 = (i + 1) * len(sections[0]) + j faces.append([index0, index1, index2, index3]) if closed: st_cnt, en_cnt = [0, 0, 0], [0, 0, 0] for i in range(len(sections[0])): st_cnt = rs.PointAdd(st_cnt, sections[0][i]) en_cnt = rs.PointAdd(en_cnt, sections[-1][i]) st_cnt = rs.VectorScale(st_cnt, 1 / len(sections[0])) en_cnt = rs.VectorScale(en_cnt, 1 / len(sections[0])) init = 0 v.append(st_cnt) for j in range(len(sections[0]) - 1): index0 = init + j index1 = len(v) - 1 index2 = init + (j + 1) % (len(sections[0]) - 1) index3 = init + j faces.append([index0, index1, index2]) #init = len(sections)*(len(sections[0])-1) + 1 init = len(v) - 1 - len(sections[0]) v.append(en_cnt) for j in range(len(sections[0]) - 1): index0 = init + j index1 = len(v) - 1 index2 = init + j + 1 index3 = init + j faces.append([index0, index1, index2]) mesh = rs.AddMesh(v, faces)
def constructMeshBox(INPUTPNTS, SIZE): vertices = [] vertices.append((0.0, 0.0, 0.0)) vertices.append((SIZE, 0.0, 0.0)) vertices.append((SIZE, SIZE, 0.0)) vertices.append((0.0, SIZE, 0.0)) vertices.append((0.0, 0.0, SIZE)) vertices.append((SIZE, 0.0, SIZE)) vertices.append((SIZE, SIZE, SIZE)) vertices.append((0.0, SIZE, SIZE)) faceVertices = [] faceVertices.append((0, 1, 2, 3)) faceVertices.append((0, 1, 5, 4)) faceVertices.append((1, 2, 6, 5)) faceVertices.append((2, 3, 7, 6)) faceVertices.append((3, 0, 4, 7)) faceVertices.append((4, 5, 6, 7)) tempMesh = rs.AddMesh(vertices, faceVertices) for pt in INPUTPNTS: tempVec = rs.VectorCreate(pt, [0.5, 0.5, 0.5]) rs.CopyObject(tempMesh, tempVec) rs.DeleteObject(tempMesh)
verts = [] mesh = [] points = [] line = [] surface = [] with open(path) as data: content = data.read() content = content.splitlines() for i in range(len(content)): content[i] = content[i].split(',') temp = [] for pos in content[i]: temp.append(float(pos) * 10) dataRead.append(tuple(temp)) for section in dataRead: temp = [] for j in range(0, len(section), 3): point = (section[j], section[j + 2], section[j + 1]) temp.append(point) verts.append(temp) for i in range(len(verts) - 1): for j in range(len(verts[0])): quadList = (verts[i][j - 1], verts[i][j], verts[i + 1][j], verts[i + 1][j - 1]) surface.append(rs.AddSrfPt((quadList[0], quadList[1], quadList[2]))) surface.append(rs.AddSrfPt((quadList[0], quadList[2], quadList[3]))) faceList = [(0, 1, 2, 2), (0, 2, 3, 3)] mesh.append(rs.AddMesh(quadList, faceList))
def addMeshQuad(verts): faces = [(0, 1, 2, 3)] mesh = rs.AddMesh(verts, faces) return mesh