|
| 1 | +""" |
| 2 | +An example of a simple way of preparing and analyzing a model |
| 3 | +for properties of interest to 3D printing, implemented using |
| 4 | +dataclasses which can easily be used for API routes. |
| 5 | +
|
| 6 | +- Volume |
| 7 | +- Oriented Bounding Box |
| 8 | +- thin regions |
| 9 | +
|
| 10 | +The nominal operations flow we're using here is defined by |
| 11 | +easily serialized `dataclass` objects and goes: |
| 12 | +
|
| 13 | +RawFile -> # a raw loadable file |
| 14 | + PrintCandidate -> # usable properties about the model |
| 15 | + PrintJob -> # after quantities and options are picked |
| 16 | + PrintRelease # i.e. what you would send to the machine |
| 17 | +""" |
| 18 | + |
| 19 | +import os |
| 20 | +from dataclasses import dataclass |
| 21 | +from typing import Optional |
| 22 | + |
| 23 | +import trimesh |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class RawFile: |
| 28 | + # the data for the loadable file |
| 29 | + file_data: bytes |
| 30 | + |
| 31 | + # the original name of the file |
| 32 | + # the extension will be used to determine type |
| 33 | + file_name: str |
| 34 | + |
| 35 | + # the units of the file if it is in a format |
| 36 | + # that does not include units (i.e. STL files) |
| 37 | + units: Optional[str] = None |
| 38 | + |
| 39 | + |
| 40 | +@dataclass |
| 41 | +class Error: |
| 42 | + # the user-facing message |
| 43 | + message: str |
| 44 | + |
| 45 | + # a code that can be used to group problems |
| 46 | + code: str |
| 47 | + |
| 48 | + # if faces were colored to indicate the problem |
| 49 | + color: str |
| 50 | + |
| 51 | + |
| 52 | +@dataclass |
| 53 | +class PrintCandidate: |
| 54 | + # the repaired model repaired into a GLB file |
| 55 | + glb: bytes |
| 56 | + |
| 57 | + # any problems that were encountered |
| 58 | + errors: list[Error] |
| 59 | + |
| 60 | + # the volume of the mesh |
| 61 | + volume: float |
| 62 | + |
| 63 | + # the size of the axis-aligned bounding box |
| 64 | + extents: list[float] |
| 65 | + |
| 66 | + |
| 67 | +def load(raw: RawFile) -> trimesh.Trimesh: |
| 68 | + """ |
| 69 | + Load a raw file for 3D printing, collapsing meshes into a single body. |
| 70 | + """ |
| 71 | + # coerce scenes and formats with normals into a mesh |
| 72 | + mesh = trimesh.load( |
| 73 | + file_obj=trimesh.util.wrap_as_stream(raw.file_data), |
| 74 | + file_type=raw.file_name, |
| 75 | + process=True, |
| 76 | + merge_tex=True, # ignore UV coordinates |
| 77 | + merge_norm=True, # ignore vertex normals |
| 78 | + force="mesh", # concatenate into single mesh |
| 79 | + ) |
| 80 | + mesh.process(merge_tex=True, merge_norm=True) |
| 81 | + |
| 82 | + # if passed explicitly |
| 83 | + if mesh.units is None and raw.units is not None: |
| 84 | + mesh.units = raw.units |
| 85 | + |
| 86 | + # convert mesh to meters and guess as a fallback |
| 87 | + mesh.convert_units("meters", guess=True) |
| 88 | + |
| 89 | + # make sure it is using face colors |
| 90 | + mesh.visual = trimesh.visual.ColorVisuals(mesh=mesh) |
| 91 | + |
| 92 | + return mesh |
| 93 | + |
| 94 | + |
| 95 | +def prepare( |
| 96 | + mesh: trimesh.Trimesh, |
| 97 | + minimum_thickness: float = 0.0005, |
| 98 | +) -> PrintCandidate: |
| 99 | + """ |
| 100 | + Take a raw 3D model and process it for 3D printing. |
| 101 | + """ |
| 102 | + |
| 103 | + # collect issues to report |
| 104 | + problems = [] |
| 105 | + |
| 106 | + if not mesh.fill_holes(): |
| 107 | + problems.append( |
| 108 | + Error(message="Mesh is not watertight!", code="watertight", color="#FF0000") |
| 109 | + ) |
| 110 | + # will apply "red" to broken faces |
| 111 | + trimesh.repair.broken_faces(mesh=mesh, color=[255, 0, 0, 255]) |
| 112 | + |
| 113 | + thick_bad = ( |
| 114 | + trimesh.proximity.thickness( |
| 115 | + mesh, mesh.triangles_center - mesh.face_normals * 0.001 |
| 116 | + ) |
| 117 | + < minimum_thickness |
| 118 | + ) |
| 119 | + |
| 120 | + if thick_bad.any(): |
| 121 | + mesh.visual.face_colors[thick_bad] = [0, 0, 255, 255] |
| 122 | + problems.append( |
| 123 | + Error( |
| 124 | + message="Mesh has regions below minimum thickness!", |
| 125 | + code="watertight", |
| 126 | + color="#0000FF", |
| 127 | + ) |
| 128 | + ) |
| 129 | + |
| 130 | + mesh.show() |
| 131 | + |
| 132 | + return PrintCandidate( |
| 133 | + glb=mesh.export(file_type="glb"), |
| 134 | + errors=problems, |
| 135 | + volume=mesh.volume, |
| 136 | + extents=mesh.extents, |
| 137 | + ) |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + # current working directory |
| 142 | + cwd = os.path.abspath(os.path.expanduser(os.path.dirname(__file__))) |
| 143 | + |
| 144 | + # create a raw "request" |
| 145 | + file_name = "machinist.3DXML" |
| 146 | + # file_name = 'ADIS16480.STL' |
| 147 | + file_path = os.path.join(cwd, "..", "models", file_name) |
| 148 | + with open(file_path, "rb") as f: |
| 149 | + loaded = load(raw=RawFile(file_data=f.read(), file_name=file_name, units=None)) |
| 150 | + |
| 151 | + from pyinstrument import Profiler |
| 152 | + |
| 153 | + with Profiler() as P: |
| 154 | + report = prepare(loaded) |
| 155 | + |
| 156 | + P.print() |
0 commit comments