1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
from libc.math cimport sqrt
from cadmesh cimport *
class AdmeshError(Exception):
pass
cdef class Stl(object):
cdef stl_file _c_stl_file
cdef bint _opened
cdef int _iterindex
BINARY = 0
ASCII = 1
INMEMORY = 2
def __cinit__(self, path=''):
if path:
self._open(path)
else:
stl_initialize(&self._c_stl_file)
if stl_get_error(&self._c_stl_file):
stl_clear_error(&self._c_stl_file)
raise AdmeshError('stl_initialize')
self._c_stl_file.stats.type = Stl.INMEMORY
@property
def stats(self):
"""The statistics about the STL model"""
return self._c_stl_file.stats
def __str__(self):
header = str(self._c_stl_file.stats.header.decode('ascii'))
if self._c_stl_file.stats.type == Stl.ASCII and header.startswith('solid '):
header = header[len('solid '):].strip()
return 'Stl(\'{header}\')'.format(header=header)
def __iter__(self):
self._iterindex = 0
return self
def __next__(self):
if self._iterindex >= len(self):
raise StopIteration
self._iterindex += 1
return self._c_stl_file.facet_start[self._iterindex - 1]
def __len__(self):
return self._c_stl_file.stats.number_of_facets
def _open(self, path):
by_path = path.encode('UTF-8')
stl_open(&self._c_stl_file, by_path)
if stl_get_error(&self._c_stl_file):
stl_clear_error(&self._c_stl_file)
raise AdmeshError('stl_open')
def add_facets(self, facets):
"""
Add one or more facets
Every facet is a tuple of two elements:
* vertices: tuple of three points (each a tuple of three floats)
* normal: tuple of three floats
Or a similar dict.
Example usage:
stl_object.add_facets([
(((0, 0, 0), (1, 0, 0), (0, 1, 0)), (1, 0, 0)),
(((0, 0, 0), (0, 1, 0), (1, 0, 0)), (0, 1, 0)),
])
Or:
stl_object.add_facets([
{'vertex': [{'x': 0, 'y': 0, 'z': 0},
{'x': 1, 'y': 0, 'z': 0},
{'x': 0, 'y': 1, 'z': 0}],
'normal': {'x': 1, 'y': 0, 'z': 0}},
])
"""
cdef stl_facet facet_struct
cdef size_t i
cdef bint first = False
cdef int current_facet_index = self._c_stl_file.stats.number_of_facets
if current_facet_index == 0:
first = True
self._c_stl_file.stats.number_of_facets += len(facets)
stl_reallocate(&self._c_stl_file)
if stl_get_error(&self._c_stl_file):
# reset the facet count back to the original size
self._c_stl_file.stats.number_of_facets = current_facet_index
stl_clear_error(&self._c_stl_file)
raise AdmeshError('stl_reallocate')
for facet in facets:
if isinstance(facet, dict):
facet_struct.vertex = facet['vertex']
facet_struct.normal = facet['normal']
# we will not copy extra, as it is mostly garbage
else:
for i in range(3):
facet_struct.vertex[i] = stl_vertex(facet[0][i][0],
facet[0][i][1],
facet[0][i][2])
facet_struct.normal = stl_normal(facet[1][0],
facet[1][1],
facet[1][2])
self._c_stl_file.facet_start[current_facet_index] = facet_struct
stl_facet_stats(&self._c_stl_file, facet_struct, first)
first = False
current_facet_index += 1
self._c_stl_file.stats.size.x = self._c_stl_file.stats.max.x - self._c_stl_file.stats.min.x
self._c_stl_file.stats.size.y = self._c_stl_file.stats.max.y - self._c_stl_file.stats.min.y
self._c_stl_file.stats.size.z = self._c_stl_file.stats.max.z - self._c_stl_file.stats.min.z
self._c_stl_file.stats.bounding_diameter = sqrt(
self._c_stl_file.stats.size.x * self._c_stl_file.stats.size.x +
self._c_stl_file.stats.size.y * self._c_stl_file.stats.size.y +
self._c_stl_file.stats.size.z * self._c_stl_file.stats.size.z
)
def repair(self,
fixall_flag=True,
exact_flag=False,
tolerance_flag=False,
tolerance=0,
increment_flag=False,
increment=0,
nearby_flag=False,
iterations=2,
remove_unconnected_flag=False,
fill_holes_flag=False,
normal_directions_flag=False,
normal_values_flag=False,
reverse_all_flag=False,
verbose_flag=True):
"""stl_repair"""
stl_repair(&self._c_stl_file,
fixall_flag,
exact_flag,
tolerance_flag,
tolerance,
increment_flag,
increment,
nearby_flag,
iterations,
remove_unconnected_flag,
fill_holes_flag,
normal_directions_flag,
normal_values_flag,
reverse_all_flag,
verbose_flag)
if stl_get_error(&self._c_stl_file):
stl_clear_error(&self._c_stl_file)
raise AdmeshError('stl_repair')
def __dealloc__(self):
stl_close(&self._c_stl_file)
|