Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new file: lecture_09/assignment_04/huang-su/2_your_own_function.gh #65

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
13,726 changes: 13,726 additions & 0 deletions lecture_09/assignment_04/huang-su/data/simple_vase_open_low_res.obj

Large diffs are not rendered by default.

130 changes: 130 additions & 0 deletions lecture_09/assignment_04/huang-su/own_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import time
import os
import logging

import compas_slicer.utilities as utils
from compas_slicer.pre_processing import move_mesh_to_point
from compas_slicer.slicers import PlanarSlicer
from compas_slicer.post_processing import generate_brim
from compas_slicer.post_processing import generate_raft
from compas_slicer.post_processing import simplify_paths_rdp
from compas_slicer.post_processing import seams_smooth
from compas_slicer.print_organization import PlanarPrintOrganizer
from compas_slicer.print_organization import set_extruder_toggle
from compas_slicer.print_organization import add_safety_printpoints
from compas_slicer.print_organization import set_linear_velocity_constant
from compas_slicer.print_organization import set_blend_radius
from compas_slicer.utilities import save_to_json

from compas.datastructures import Mesh
from compas.geometry import Point

# own function
from slicing_texture import create_overhang_texture

# ==============================================================================
# Logging
# ==============================================================================
logger = logging.getLogger('logger')
logging.basicConfig(format='%(levelname)s-%(message)s', level=logging.INFO)

# ==============================================================================
# Select location of data folder and specify model to slice
# ==============================================================================
DATA = os.path.join(os.path.dirname(__file__), 'data')
OUTPUT_DIR = utils.get_output_directory(DATA) # creates 'output' folder if it doesn't already exist
MODEL = 'simple_vase_open_low_res.obj'


def main():
start_time = time.time()

# ==========================================================================
# Load mesh
# ==========================================================================
compas_mesh = Mesh.from_obj(os.path.join(DATA, MODEL))

# ==========================================================================
# Move to origin
# ==========================================================================
move_mesh_to_point(compas_mesh, Point(0, 0, 0))

# ==========================================================================
# Slicing
# options: 'default': Both for open and closed paths. But slow
# 'cgal': Very fast. Only for closed paths.
# Requires additional installation (compas_cgal).
# ==========================================================================
slicer = PlanarSlicer(compas_mesh, slicer_type="cgal", layer_height=5)
slicer.slice_model()

# ==========================================================================
# Generate brim / raft
# ==========================================================================
# NOTE: Typically you would want to use either a brim OR a raft,
# however, in this example both are used to explain the functionality
# generate_brim(slicer, layer_width=3.0, number_of_brim_offsets=4)
# generate_raft(slicer,
# raft_offset=20,
# distance_between_paths=5,
# direction="xy_diagonal",
# raft_layers=1)

# ==========================================================================
# Simplify the paths by removing points with a certain threshold
# change the threshold value to remove more or less points
# ==========================================================================
simplify_paths_rdp(slicer, threshold=0.7)

############################################################################
# INSERT OWN TEXTURE HERE
############################################################################
create_overhang_texture(slicer, overhang_distance=5)

# ==========================================================================
# Smooth the seams between layers
# change the smooth_distance value to achieve smoother, or more abrupt seams
# ==========================================================================
# seams_smooth(slicer, smooth_distance=10)

# ==========================================================================
# Prints out the info of the slicer
# ==========================================================================
slicer.printout_info()

# ==========================================================================
# Save slicer data to JSON
# ==========================================================================
save_to_json(slicer.to_data(), OUTPUT_DIR, 'slicer_data.json')

# ==========================================================================
# Initializes the PlanarPrintOrganizer and creates PrintPoints
# ==========================================================================
print_organizer = PlanarPrintOrganizer(slicer)
print_organizer.create_printpoints()

# ==========================================================================
# Set fabrication-related parameters
# ==========================================================================
# set_extruder_toggle(print_organizer, slicer)
# add_safety_printpoints(print_organizer, z_hop=10.0)
# set_linear_velocity_constant(print_organizer, v=100.0)
# set_blend_radius(print_organizer, d_fillet=10.0)

# ==========================================================================
# Prints out the info of the PrintOrganizer
# ==========================================================================
print_organizer.printout_info()

# ==========================================================================
# Converts the PrintPoints to data and saves to JSON
# =========================================================================
printpoints_data = print_organizer.output_printpoints_dict()
utils.save_to_json(printpoints_data, OUTPUT_DIR, 'out_printpoints.json')

end_time = time.time()
print("Total elapsed time", round(end_time - start_time, 2), "seconds")


if __name__ == "__main__":
main()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions lecture_09/assignment_04/huang-su/slicing_texture.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from compas_slicer.geometry import Path
from compas_slicer.utilities.utils import get_normal_of_path_on_xy_plane
from compas.geometry import Point
from compas.geometry import scale_vector, add_vectors

def create_overhang_texture(slicer, overhang_distance):
"""Creates a cool overhang texture"""

print("Creating cool texture")

for i, layer in enumerate(slicer.layers):
#if i % 10 == 0 and i > 0:
print('layer: ', layer)
for j, path in enumerate(layer.paths):
print('path: ', path)
# create an empty layer in which we can store our modified points
new_path = []
for k, pt in enumerate(path.points):
# for every second point (only even points)
if (k+i) % 15 == 0:
# get the normal of the point in relation to the mesh
normal = get_normal_of_path_on_xy_plane(k, pt, path, mesh=None)
# scale the vector by a number to move the point
normal_scaled = scale_vector(normal, -overhang_distance*((k+i) / 5))
# create a new point by adding the point and the normal vector
new_pt = add_vectors(pt, normal_scaled)
# recreate the new_pt values as compas_points
pt = Point(new_pt[0], new_pt[1], new_pt[2])

# append the points to the new path
new_path.append(pt)

# replace the current path with the new path that we just created
layer.paths[j] = Path(new_path, is_closed=path.is_closed)
print('ok')