This example uses Metafold API/SDK to sweep over beam lattice and evaluate the shape metrics and export multiple STL files.
Steps
- Import all necessary libraries
- Insert project and token information
- Define the Scene parameters
- Set the beam lattice parameters to sweep through
- Create a for loop and evaluate the Metafold graph
- Evaluate metrics and export the part as STL
1- Import all necessary libraries
Start by importing the required libraries
import pandas as pd
import numpy as np
import os
import csv
from numpy.typing import ArrayLike
from pprint import pprint
from scipy.spatial.transform import Rotation as R
from metafold import MetafoldClient
from metafold.func_types import JSONEvaluator, FuncType, Vec3f, Mat4f
from metafold.func import *
2- Insert project and token information
In the following code snippet, we create a MetafoldClient
instance and assign it to the variable client
. This step is crucial for interacting with the Metafold API. The access_token
parameter should contain your authentication token, which grants you access to the Metafold service. Similarly, the project_id
parameter specifies the specific project or workspace you want to work with. Make sure to replace access_token
and project_id
with your own credentials and identifiers for this to work properly.
project_id = 5701
access_token = "..."
client=MetafoldClient(access_token,project_id)
To create a grid of sample points, GenerateSamplePoints
is used. The generated grid includes sample points at the boundary minimum and maximum as defined by the offset and size. The grid is also commonly referred to as a patch.
3- Define the Scene parameters
source = GenerateSamplePoints({
"offset": [ -15, -15, 0],
"size": [30, 30, 30],
"resolution": [128, 128, 128]
}
4- Set the beam lattice parameters to sweep through
In the following example, the lattice_data contains the edges and nodes of the lattice, providing the structural definition of the BCC arrangement. The section_radius
and the scale
values that are to be studied are given as a list with radius of the lattice sections that are interesting.
section_radius = [0.1, 0.125, 0.15]
scale = [3, 6, 9]
bcc_params= {
"lattice_data" : {
"edges": [
[0, 7],
[4, 7],
[1, 7],
[2, 7],
[5, 7],
[6, 7],
[3, 7],
[7, 8]
],
"nodes": [
[0, 0, 0],
[0, 1, 0],
[1, 0, 0],
[1, 1, 0],
[0, 0, 1],
[0, 1, 1],
[1, 0, 1],
[0.5, 0.5, 0.5],
[1, 1, 1]
]
},
}
5- Create a for loop and evaluate the Metafold graph
An empty list densities is created to store the different metrics of the created list of beam lattice shapes.
a loop is created to sweep over section_radius
and scale
The SampleLattice
samples a lattice at the given sample source
using the parameters specified earlier.
Next, with the Redistance
function, a grid of samples can be transformed into signed distances from the zero level set. The output of this function can provide valuable distance information for further processing.
The Threshold
operation clamps a set of samples within a specified range that's centered on zero and maps them to the snorm [-1, 1] range, then provides the resulting values as bytes. The expression { "width": 0.03
} inside the Threshold function should be used to define the width of the threshold applied.
densities=[]
for section_radius_value in section_radius:
for scale_value in scale:
bcc_params["section_radius"]= section_radius_value
bcc_params["scale"]= [scale_value, scale_value, scale_value]
shape_func= Threshold(
Redistance(
SampleLattice(source, parameters=bcc_params)
),
{
"width": 0.03,
},
)
The JSONEvaluator
evaluator = JSONEvaluator(source)
shape_func(evaluator)
graph_json=evaluator.json()
6- Evaluate metrics and export the part as STL
job= client.jobs.run("evaluate_metrics", {"graph": graph_json, "point_source": 0})
pprint((scale_value, section_radius_value, job.meta["relative_density"]))
densities.append((scale_value, section_radius_value, job.meta["relative_density"]))
export_job = client.jobs.run("export_triangle_mesh", {
"graph": graph_json,
"point_source": 0,
})
export_asset = export_job.assets[0].id
client.assets.download_file(export_asset, f"bcc_{scale_value}_{section_radius_value * 1000}.stl")
df = pd.DataFrame(densities, columns=["section_radius_value", "scale_value", "relative_density"])
df.to_csv('out.csv', index=False)