C. Mesh in Rhino

1. Visualize Mesh in Rhino

1.a: load mesh in Rhino

compas_rhino.artists.MeshArtist can visualize a Mesh object in Rhino.

# ==============================================================================
# Import
# ==============================================================================
import os
from compas.datastructures import Mesh
from compas_rhino.artists import MeshArtist

# ==============================================================================
# Unserialization
# ==============================================================================
HERE = os.path.dirname(__file__)
FILE = os.path.join(HERE, 'data', 'cablenet.json')
cablenet_mesh = Mesh.from_json(FILE)

# ==============================================================================
# Visualization
# ==============================================================================
artist = MeshArtist(cablenet_mesh, layer="CSD2::Mesh")
artist.draw()

1.b: Modify mesh vertices

Select vertices whose x coordinate is 10 and are not on the boundary. Move them up along the Z-axis 10. And visualize them again.

for vkey in cablenet_mesh.vertices_where({'x': 10}):
    if cablenet_mesh.is_vertex_on_boundary(vkey) is False:
        cablenet_mesh.vertex_attribute(vkey, 'z', 10)

1.c: Draw mesh normals

MeshArtist contains methods to visualize the vertex and face keys as text-dots in Rhino. Vertex and face normals can also be visualized, which follows the right-hand rule.

artist.draw_vertexlabels()
artist.draw_vertexnormals(scale=5)

artist.draw_facelabels()
artist.draw_facenormals(scale=5)

Vertex labels show the vertex key. Vertex normal is a normal vector at the vertex as the weighted average of the normals of the neighboring faces.

Face labels show the face key. Face normal by default is the unit normal vector of the face.

2. Ex: Create a box mesh in COMPAS

Question:

Create a closed mesh box, whose length is 10, width is 7 and height is 5.

Answer:

2.a: build mesh by adding vertex and face

You could build the mesh from scratch by adding vertices and faces one by one, and use MeshArtist to visualize every step.

# ==============================================================================
# Import
# ==============================================================================
from compas.datastructures import Mesh
from compas_rhino.artists import MeshArtist

# ==============================================================================
# Build a Mesh
# ==============================================================================
box = Mesh()
v0 = box.add_vertex(x=0, y=0, z=0)
v1 = box.add_vertex(x=10, y=0, z=0)
v2 = box.add_vertex(x=10, y=7, z=0)
v3 = box.add_vertex(x=0, y=7, z=0)

box.add_face([v0, v1, v2, v3])

# ==============================================================================
# Visualization
# ==============================================================================
artist = MeshArtist(box, layer="CSD2::Box")
artist.clear_layer()
artist.draw()
artist.draw_vertexlabels()
artist.draw_facenormals(scale=2)

2. b: construct mesh from vertices and faces

Building a mesh vertex per vertex and face per face is fine for very simple meshes, but quickly becomes tedious for meshes of relevant size. Alternative constructors can be used to simplify this process.

# ==============================================================================
# Mesh from vertices and faces
# ==============================================================================
# vertices
v0 = [0, 0, 0]
v1 = [10, 0, 0]
v2 = [10, 7, 0]
v3 = [0, 7, 0]
v4 = [0, 0, 5]
v5 = [10, 0, 5]
v6 = [10, 7, 5]
v7 = [0, 7, 5]
vertices = [v0, v1, v2, v3, v4, v5, v6, v7]

# faces
f0 = [3, 2, 1, 0]
f1 = [4, 7, 3, 0]
f2 = [7, 6, 2, 3]
f3 = [6, 5, 1, 2]
f4 = [5, 4, 0, 1]
f5 = [5, 6, 7, 4]
faces = [f0, f1, f2, f3, f4, f5]

box = Mesh.from_vertices_and_faces(vertices, faces)

2.c: construct mesh from box geometry

Another alternative way is to firstly create a compas.geometry.Box object then convert the object to a Mesh object. By default, the box's center is in the Frame.wordXY(). Thus, the box mesh is not in the same location as the former mesh. To solve this, either create the box in the correct center point or translate the box mesh afterward.

from compas.geometry import Box, Frame
length = 10
width = 7
height = 5
box = Mesh.from_shape(Box(Frame.worldXY(), length, width, height))
from compas.geometry import Translation
T = Translation.from_vector([length / 2, width / 2, height / 2])
box.transform(T)

Last updated