C2. Cutting sequence generation

a. Define tool

#============================================================================
#parameters units in mm
#============================================================================

blade_thickness = 0.2
blade_diameter = 5
table = 15
height = 0.50

#============================================================================
#Visualize tabel
#============================================================================

points = [[0,0,0], [table,0,0], [table,table,0], [0,table,0]]

polygon = Polygon (points)
artist = PolygonArtist(polygon, layer="TOOL::table", color=(255, 255, 255))
artist.clear_layer()
artist.draw(show_edges=True, show_face=False)

#============================================================================
#Visualize blade
#============================================================================

origin_point = [(table/2), (table/2), (height+blade_diameter/2)]
plane = Plane(origin_point, [1,0,0])
circle = Circle(plane, blade_diameter/2)

axis_line_p1 = add_vectors (origin_point, [1,0,0])
axis_line_p2 = add_vectors (origin_point, [-1,0,0])
line = Line(axis_line_p1, axis_line_p2)

artist = CircleArtist(circle, layer="TOOL::circle", color=(255, 0, 0))
artist.clear_layer()
artist.draw()

artist = LineArtist(line, layer="TOOL::circle_axis", color=(255, 0, 0))
artist.clear_layer()
artist.draw()

b. Translate blocks to bed

#=====================================================================================
#translate block to cutting bed
#=====================================================================================
selected_block = None
for i,block in enumerate (blocks):
    if i == 20:
        selected_block = block

point = selected_block.face_centroid(1)
normal = block.face_normal(1)
vertices = block.face_vertices(1)
xaxis = subtract_vectors(block.vertex_attributes(vertices[0], 'xyz'), point)
yaxis = cross_vectors(xaxis, normal)
block_frame = Frame(point, xaxis, yaxis)

x_axis = Vector.Xaxis()
y_axis = Vector.Yaxis()
to_frame = Frame(origin_point,x_axis,y_axis)

X = Transformation.from_frame_to_frame(block_frame, to_frame)
selected_block.transform(X)

#============================================================================
#Output
#============================================================================

block_vertices_coordinates = []
for vkey in selected_block.vertices():
    coordinates = selected_block.vertex_coordinates(vkey,axes='xyz')
    block_vertices_coordinates.append(coordinates)

with open(FILE_O, 'w+') as f:
    data = block_vertices_coordinates
    json.dump(data, f)

with open(FILE_OO, 'w+') as f:
    data = selected_block.to_data()
    json.dump(data, f)

c. oriented_bbox

#=====================================================================
#Compute bbox
#=====================================================================
bbox = oriented_bounding_box_numpy(block_points)
orient_bbox = np.array(bbox).tolist()

d. load and check data

#=====================================================================
#Transformation
#=====================================================================
bbox = Box.from_bounding_box(block_points)
bbox_frame = bbox.frame

to_frame = Frame (origin_point, Vector.Xaxis(), Vector.Yaxis())
X = Transformation.from_frame_to_frame(bbox_frame, to_frame)
bbox.transform(X)
selected_block.transform(X)

02_Identify block faces

e.Cutting sequence

#Cutting sequence of blocks
import os
import json
import copy

from compas.geometry import add_vectors,Circle,  Box,intersection_polyline_plane, project_points_plane, is_intersection_plane_plane,is_intersection_line_plane, bounding_box,Point, Plane,Polyline, Line, Point, Vector, Frame, add_vectors, dot_vectors, intersection_line_plane, intersection_plane_plane
from compas_rhino.objects import MeshObject #Deprecated
from compas_rhino.geometry import RhinoCurve
from compas.datastructures import Mesh
from compas_rhino.utilities import select_curves
import compas_rhino
from compas.utilities import pairwise, linspace
from compas_rhino.artists import CircleArtist, PolylineArtist, LineArtist, BoxArtist, MeshArtist, PointArtist, LineArtist, FrameArtist
#==========================================================================
#load data of block, bbox (get it into a dict maybe?)
#==========================================================================
HERE = os.path.dirname(__file__)
FILE_I = os.path.join(HERE, 'data', 'aligned_block.json')
FILE_II = os.path.join(HERE, 'data', 'aligned_bbox.json')
block = Mesh.from_json(FILE_I)
bbox = Box.from_json(FILE_II)

#==========================================================================
#Parameters (units in mm)
#==========================================================================
groove_spacing = 0.1
guids = select_curves("select start and end curve")
start_line = (RhinoCurve.from_guid(guids[0])).to_compas()
end_line = (RhinoCurve.from_guid(guids[1])).to_compas()

#==========================================================================
#CUTTING
#==========================================================================
#step1: generate the vertical planes of intersection
line = Line(start_line.midpoint, end_line.midpoint)
number_of_div = int(line.length/groove_spacing)
intersection_planes = []
increment = groove_spacing
for i in range(number_of_div):
    point = line.point(groove_spacing/2)
    normal = [1,0,0]
    plane = Plane(point, normal)
    intersection_planes.append(plane)
    groove_spacing += increment

#step2: perpendicular start plans
start_plane_normal = [0,0,1]
start_plane_point = add_vectors(block.centroid(), (0,0,1))
start_plane = Plane(start_plane_point, start_plane_normal)

#step3: sort faces of block
top = sorted(block.faces(), key=lambda face: dot_vectors(block.face_normal(face), [0, 0, +1]))[-1]
top_vertices = block.face_vertices(top)
top_points = block.vertices_attributes('xyz', keys=top_vertices)
top_points = [top_points[-1]] + top_points[:-1]
top_points = [top_points[-1]] + top_points[:-1]#reordering the points to start from the second as first
top_points.append(top_points[0])

intersection_polyline = Polyline(top_points)

intersection_lines = []
for a, b in pairwise(top_points):
    intersection_lines.append(Line(a,b))
    intersection_lines.append(Line(top_points[-1], top_points[0]))
    
#==========================================================================
# Intersecion points
#==========================================================================
points_sequence = []
for i, plane in enumerate(intersection_planes[:-2]):
    points_bc = intersection_polyline_plane(intersection_polyline, plane)
    point_a = project_points_plane(points_bc, start_plane)[0]
    point_d = project_points_plane(points_bc, start_plane)[1]
    if (i%2)==0:
        points_sequence.append(point_a)
        points_sequence.extend(points_bc)
        points_sequence.append(point_d)
    else:
        points_cb = copy.deepcopy(points_bc)
        points_cb.reverse()
        points_sequence.append(point_d)
        points_sequence.extend(points_cb)
        points_sequence.append(point_a)

# interpolate points
interpolated_points = Polyline(points_sequence)

#==========================================================================
#Visualize
#==========================================================================
artist = PolylineArtist(interpolated_points, layer="CUTTING::interpolation")
artist.clear_layer()
artist.draw()

artist = LineArtist(None, layer="Blocks::line")
artist.clear_layer()
for line in intersection_lines:
    artist = LineArtist(line, layer="Blocks::line")
    artist.draw()

e.Cutting sequence: Conduit visualization

#Cutting sequence of blocks
import os
import json
import copy

from compas.geometry import add_vectors,Circle,  Box,intersection_polyline_plane, project_points_plane, is_intersection_plane_plane,is_intersection_line_plane, bounding_box,Point, Plane,Polyline, Line, Point, Vector, Frame, add_vectors, dot_vectors, intersection_line_plane, intersection_plane_plane
from compas_rhino.objects import MeshObject #Deprecated
from compas_rhino.geometry import RhinoCurve
from compas.datastructures import Mesh
from compas_rhino.utilities import select_curves
import compas_rhino
from compas.utilities import pairwise, linspace
from compas_rhino.artists import CircleArtist, PolylineArtist, LineArtist, BoxArtist, MeshArtist, PointArtist, LineArtist, FrameArtist
#==========================================================================
#load data of block, bbox (get it into a dict maybe?)
#==========================================================================
HERE = os.path.dirname(__file__)
FILE_I = os.path.join(HERE, 'data', 'aligned_block.json')
FILE_II = os.path.join(HERE, 'data', 'aligned_bbox.json')

block = Mesh.from_json(FILE_I)

bbox = Box.from_json(FILE_II)

#==========================================================================
#Parameters (units in mm)
#==========================================================================
groove_spacing = 0.1

guids = select_curves("select start and end curve")
start_line = (RhinoCurve.from_guid(guids[0])).to_compas()
end_line = (RhinoCurve.from_guid(guids[1])).to_compas()

#==========================================================================
#CUTTING
#==========================================================================
#step1: generate the vertical planes of intersection
line = Line(start_line.midpoint, end_line.midpoint)
number_of_div = int(line.length/groove_spacing)
intersection_planes = []
increment = groove_spacing
for i in range(number_of_div):
    point = line.point(groove_spacing/2)
    normal = [1,0,0]
    plane = Plane(point, normal)
    intersection_planes.append(plane)
    groove_spacing += increment

#step2: perpendicular start plans
start_plane_normal = [0,0,1]
start_plane_point = add_vectors(block.centroid(), (0,0,1))
start_plane = Plane(start_plane_point, start_plane_normal)

#step3: sort faces of block
top = sorted(block.faces(), key=lambda face: dot_vectors(block.face_normal(face), [0, 0, +1]))[-1]
top_vertices = block.face_vertices(top)
top_points = block.vertices_attributes('xyz', keys=top_vertices)
top_points = [top_points[-1]] + top_points[:-1]
top_points = [top_points[-1]] + top_points[:-1]#reordering the points to start from the second as first
top_points.append(top_points[0])

intersection_polyline = Polyline(top_points)
intersection_lines = []
for a, b in pairwise(top_points):
    intersection_lines.append(Line(a,b))
    intersection_lines.append(Line(top_points[-1], top_points[0]))
    
#==========================================================================
# Intersecion points
#==========================================================================
points_sequence = []
for i, plane in enumerate(intersection_planes[:-2]):
    points_bc = intersection_polyline_plane(intersection_polyline, plane)
    point_a = project_points_plane(points_bc, start_plane)[0]
    point_d = project_points_plane(points_bc, start_plane)[1]
    if (i%2)==0:
        points_sequence.append(point_a)
        points_sequence.extend(points_bc)
        points_sequence.append(point_d)
    else:
        points_cb = copy.deepcopy(points_bc)
        points_cb.reverse()
        points_sequence.append(point_d)
        points_sequence.extend(points_cb)
        points_sequence.append(point_a)

# interpolate points
interpolated_points = Polyline(points_sequence)

#==========================================================================
#Conduit
#==========================================================================
compas_rhino.rs.Redraw()

for i,j in pairwise(range(len(interpolated_points.points))):
    start = interpolated_points.points[i]
    stop = interpolated_points.points[j]
    vector = stop - start

    for i in linspace(0, 1, 50):
        point = start + vector * i

        point_2 = add_vectors(point, [0, 0, 2])
        normal_2 = [1,0,0]
        plane_2 =Plane(point_2, normal_2)
        circle = Circle(plane_2, 2)


        artist = CircleArtist(circle, layer="CUTTING::moving_blade")
        artist.clear_layer()
        artist.draw()
        compas_rhino.rs.Redraw()
        compas_rhino.wait()

#==========================================================================
#Visualize
#==========================================================================
artist = PolylineArtist(interpolated_points, layer="CUTTING::interpolation")
artist.clear_layer()
artist.draw()

artist = LineArtist(None, layer="Blocks::line")
artist.clear_layer()

Last updated