33import pathlib as pl
44import OCP
55from .assemblymesher_base import assemblymesher
6-
7- single_thread_override = False
8- try :
9- import multiprocessing as mp
10-
11- manager = mp .Manager ()
12- lock = manager .Lock ()
13- except :
14- single_thread_override = True
6+ import os
157
168from .stl_utils import *
179from . import meshutils
@@ -26,6 +18,10 @@ class MesherCQSTL2(assemblymesher):
2618
2719 cq_mesher_faceHash = {}
2820
21+ #writer object from OCP
22+ wr = OCP .StlAPI .StlAPI_Writer ()
23+ wr .ASCIIMode = True
24+
2925 def __init__ (
3026 self ,
3127 tolerance ,
@@ -88,106 +84,70 @@ def _mesh_surfaces(self):
8884 # loop over all surfaces in all entities
8985 # and generate meshes (refined or otherwise)
9086 mpargs = []
91- if single_thread_override or self .threads == 1 :
92- face_hash_table = {}
93- else :
94- # manager=mp.Manager()
95- face_hash_table = manager .dict ()
87+ face_hash_table = {}
88+
9689 k = 0
9790 for i , e in enumerate (self .cq_mesher_entities ):
9891 if self .verbosity_level :
9992 print (f"INFO: triangulating solid { i } " )
10093 e .solid = self ._triangulate_solid (e .solid ,self .cq_mesher_tolerance ,self .cq_mesher_ang_tolerance )
10194 mpargs .extend (
10295 [
103- ( k + j , j , i , self .refine , self .surface_hash (f ), face_hash_table )
96+ [ k + j , j , i , self .refine , self .surface_hash (f ), face_hash_table ]
10497 for j , f in enumerate (e .solid .Faces ())
10598 ]
10699 )
107100
108101 # we have a set of mesh jobs - scatter those
109- if single_thread_override or self .threads == 1 :
110- output = []
111- for args in mpargs :
112- output .append (self ._mesh_single_nothread (* args ))
113- else :
114- pool = mp .Pool (processes = self .threads )
115- output = pool .starmap (self ._mesh_single , mpargs )
102+ for args in mpargs :
103+ self ._mesh_single (* args )
116104
117105 # process the list of meshed faces.
118106 stls = []
119107 for i , e in enumerate (self .cq_mesher_entities ):
120108 face_stls = []
121109 for k , v in face_hash_table .items ():
122- vids = v [1 ] # the volumes that this face belongs to
110+ vids = v [1 : ] # the volumes that this face belongs to
123111 if i in vids :
124112 # this face is part of this volume
125- face_stls .append (v )
113+ face_stls .append ([ v [ 0 ], v [ 1 :]] )
126114 stls .append (face_stls )
127115 return stls
128116
129117 def _triangulate_solid (self , solid , tol : float = 1e-3 , atol : float = 1e-1 ):
130118 """ create a mesh by means of the underlying OCCT IncrementalMesh
131119 on a single solid. This will later be split into surfaces.
132120 This has to be done since otherwise a single solid can get leaky
133- when surfaces do not connect
121+ when its surfaces do not connect properly
134122 """
135123 solid .mesh (tol ,atol )
136124 return solid
137125
138126 @classmethod
139- def _mesh_single_nothread (cls , global_fid , fid , vid , refine , hh , faceHash ):
127+ def _mesh_single (cls , global_fid , fid , vid , refine , hh , faceHash ):
140128 f = cls .cq_mesher_entities [vid ].solid .Faces ()[fid ]
141129 if hh in faceHash .keys ():
142130 # surface is in table - simply add the vid to the hash-table
143- faceHash [ hh ][ 1 ]. append ( vid )
144- if cls . verbosity_level :
145- print ( f"INFO: mesher reusing { hh } ( { faceHash [hh ][0 ] } , { faceHash [ hh ][ 1 ] } )" )
146- return ( hh , faceHash [hh ])
131+ done = True
132+ ffn = faceHash [ hh ][ 0 ]
133+ previd = faceHash [hh ][1 ]
134+ faceHash [hh ]= [ ffn , previd , vid ]
147135 else :
148- facefilename = f"vol_{ vid + 1 } _face{ global_fid :04} .stl"
149- wr = OCP .StlAPI .StlAPI_Writer ()
150- wr .ASCIIMode = True
151- status = False
152- status = wr .Write (f .wrapped ,facefilename )
153- k = 0
154- while (not status ):
155- print (f'WARNING: failed to write file { facefilename } , retrying (iter{ k } )' )
156- status = wr .Write (f .wrapped ,facefilename )
157- k = k + 1
158- if (k > 8 ):
159- print (f'ERROR: could not write file { facefilename } , volume { vid + 1 } will likely be leaking' )
160- return None
161- faceHash [hh ] = [facefilename , manager .list ([vid ])]
162- if cls .verbosity_level > 1 :
163- print (f"INFO: cq export to file { facefilename } " )
164- if refine :
165- cls ._refine_stls (facefilename , refine )
166- return (hh , faceHash [hh ])
136+ done = False
167137
168- @classmethod
169- def _mesh_single (cls , global_fid , fid , vid , refine , hh , faceHash ):
170- f = cls .cq_mesher_entities [vid ].solid .Faces ()[fid ]
171- if hh in faceHash .keys ():
172- # surface is in table - simply add the vid to the hash-table
173- with lock :
174- faceHash [hh ][1 ].append (vid )
138+ if done :
175139 if cls .verbosity_level :
176- print (f"INFO: mesher reusing { hh } { faceHash [hh ][1 ] } " )
177- return ( hh , faceHash [ hh ])
140+ print (f"INFO: mesher reusing { hh } ( { faceHash [hh ][0 ] } , { faceHash [ hh ][ 1 :] } ) " )
141+ return
178142 else :
179143 facefilename = f"vol_{ vid + 1 } _face{ global_fid :04} .stl"
180- with lock :
181- faceHash [hh ] = [facefilename , manager .list ([vid ])]
182- wr = OCP .StlAPI .StlAPI_Writer ()
183- wr .ASCIIMode = True
184- wr .Write (f .wrapped ,facefilename )
144+ faceHash [hh ] = [facefilename , vid , - 1 ]
145+ status = cls .wr .Write (f .wrapped ,facefilename )
185146 if cls .verbosity_level > 1 :
186147 print (f"INFO: cq export to file { facefilename } " )
187148 if refine :
188149 cls ._refine_stls (facefilename , refine )
189- return (hh , faceHash [hh ])
190-
150+ return
191151
192152class MesherCQSTL2Builder :
193153 def __init__ (self ):
0 commit comments