Iteratively label and export parts (3-matic 14)

Next script allows you to create a set of boxes, label them and export each box as an stl file. It shows the possibility to handle large data sets which each need a separate label number.

# This script consists of 2 functions:
# - create_boxes: creates a number of boxes of a certain size and with a predefined target edge length
# - label_boxes: Puts a number-label with predefined color on each box. Next, each box is exported separately in an stl file.
# input create_boxes function:
# - number and size of the created boxes
# - target edge length of created boxes
# input label_boxes function:
# - size of boxes
# - label color in tuple, e.g. black = (0,0,0)
# - output directory where the output stl files are saved

# Author: Sofie Desiron (Materialise) 
# Version: 1.0 (May 2019) 

import trimatic
import os

def create_boxes(number_of_boxes, box_size, target_edge_length):
	distance_corner_points = box_size + 3
	total_distance = distance_corner_points*number_of_boxes
	for k in range(0,total_distance,distance_corner_points):
		box = trimatic.create_box_part((k,0,0),box_size,box_size,box_size)
		trimatic.uniform_remesh(entities=box,target_triangle_edge_length=target_edge_length,preserve_sharp_edge_angle=True)

	
def label_boxes(box_size, label_color, output_dir):	
	list_parts = trimatic.get_parts()
	l=0
	distance_corner_points = box_size + 3
	for part in list_parts:
		part.name= "part_" + str(l)
		surf_set_label = trimatic.quick_label(entity=part,text=str(l),point=(l*distance_corner_points+0.5,0,0.5), direction=(1,0,0),font_height=0.5,label_height=0.01)
		list_surfs_label = surf_set_label.get_surfaces()
		for surf in list_surfs_label:
			surf.color = label_color
		trimatic.export_stl_binary(entities=part, output_directory=output_dir)
		l=l+1
		
#Example:
#create_boxes(number_of_boxes=15, box_size=10, target_edge_length=0.8)
#label_boxes(box_size=10, label_color=(0,0,0), output_dir=r"C:\Example folder\\")
#NOTE: Make sure the assigned output_directory exists