4444 import configparser as configparser
4545except ImportError :
4646 import ConfigParser as configparser
47- import os
4847import numpy as np
49- from scipy import special
50- from OCC .TopoDS import TopoDS_Shape , topods_Compound , TopoDS_Compound , topods_Face , TopoDS_Face , topods_Wire , TopoDS_Wire , topods_Edge , TopoDS_Edge
51- from OCC .BRep import (BRep_Builder )
52- from OCC .TopExp import TopExp_Explorer
53- from OCC .TopAbs import TopAbs_VERTEX , TopAbs_EDGE , TopAbs_FACE , TopAbs_WIRE
54- from OCC .TopTools import TopTools_ListOfShape
55- from OCC .BRepBuilderAPI import BRepBuilderAPI_MakeFace , BRepBuilderAPI_MakeWire , BRepBuilderAPI_MakeEdge , BRepBuilderAPI_MakeVertex , BRepBuilderAPI_NurbsConvert
56- from OCC .BRep import BRep_Tool , BRep_Tool_Curve
57- from OCC .GeomConvert import geomconvert_SurfaceToBSplineSurface , geomconvert_CurveToBSplineCurve , GeomConvert_CompCurveToBSplineCurve
58- from OCC .gp import gp_Pnt
59- from OCC .BRepTools import breptools_OuterWire
60-
61-
62- from OCC .IGESControl import IGESControl_Reader , IGESControl_Writer
63-
64-
48+ from OCC .Core .TopoDS import (TopoDS_Shape , topods_Compound , \
49+ TopoDS_Compound , topods_Face , \
50+ TopoDS_Face , topods_Wire , \
51+ TopoDS_Wire , topods_Edge )
52+ from OCC .Core .BRep import BRep_Builder
53+ from OCC .Core .TopExp import TopExp_Explorer
54+ from OCC .Core .TopAbs import (TopAbs_EDGE , TopAbs_FACE , TopAbs_WIRE )
55+ from OCC .Core .TopTools import TopTools_ListOfShape
56+ from OCC .Core .BRepBuilderAPI import (BRepBuilderAPI_MakeFace , \
57+ BRepBuilderAPI_MakeWire , \
58+ BRepBuilderAPI_MakeEdge , \
59+ BRepBuilderAPI_NurbsConvert )
60+ from OCC .Core .BRep import BRep_Tool , BRep_Tool_Curve
61+ from OCC .Core .GeomConvert import (geomconvert_SurfaceToBSplineSurface , \
62+ geomconvert_CurveToBSplineCurve , \
63+ GeomConvert_CompCurveToBSplineCurve )
64+ from OCC .Core .gp import gp_Pnt
65+ from OCC .Core .BRepTools import breptools_OuterWire
66+
6567from pygem import FFD as OriginalFFD
6668from pygem .cad .igeshandler import IgesHandler
6769
@@ -99,11 +101,12 @@ class FFD(OriginalFFD):
99101 >>> import numpy as np
100102 >>> ffd = FFD()
101103 >>> ffd.read_parameters('tests/test_datasets/parameters_test_ffd_sphere.prm')
102- # TODO
103- >>> new = free_form.modified_mesh_points
104+ >>> input_cad_file_name = "input.iges"
105+ >>> modified_cad_file_name = "output.iges"
106+ >>> ffd(input_cad_file_name,modified_cad_file_name)
104107 """
105108
106- def __call__ (self , obj ,dst = None ):
109+ def __call__ (self , obj , dst = None ):
107110 """
108111 This method performs the deformation on the CAD file.
109112 """
@@ -121,7 +124,7 @@ def __call__(self, obj,dst=None):
121124 print ("Modifying faces" )
122125
123126
124- tol = 1e-5
127+
125128
126129 #create compound to store modified faces
127130 compound_builder = BRep_Builder ()
@@ -133,40 +136,32 @@ def __call__(self, obj,dst=None):
133136 # init some quantities
134137 faceCount = 0
135138 face_list = []
136- control_point_position = [0 ]
137139 faces_explorer = TopExp_Explorer (shape , TopAbs_FACE )
138- mesh_points = np .zeros (shape = (0 , 3 ))
139140
140141 while faces_explorer .More ():
141- points_orig = []
142- points_def = []
143142 # performing some conversions to get the right format (BSplineSurface)
144- print ("Processing face " ,faceCount )
143+ print ("Processing face " , faceCount )
145144 face = topods_Face (faces_explorer .Current ())
146145 nurbs_converter = BRepBuilderAPI_NurbsConvert (face )
147- nurbs_converter .Perform (face )
148146 nurbs_face = nurbs_converter .Shape ()
149147 face_aux = topods_Face (nurbs_face )
150148 brep_face = BRep_Tool .Surface (topods_Face (nurbs_face ))
151- old_brep_face = BRep_Tool .Surface (topods_Face (nurbs_face ))
152-
153149 bounds = 0.0
154- bounds = brep_face .GetObject ().Bounds ()
155- #print("Bounds: ", bounds)
156- bspline_face = geomconvert_SurfaceToBSplineSurface (brep_face )
150+ bounds = brep_face .Bounds ()
157151
152+ bspline_face = geomconvert_SurfaceToBSplineSurface (brep_face )
158153 # we will then add an amount of nodes that will grant us our prescribed resolution both along u and v
159- uKnotsToAdd = 20 ;
160- vKnotsToAdd = 20 ;
161- print ("Added U knots: " , uKnotsToAdd )
162- print ("Added V knots: " , vKnotsToAdd )
154+ uKnotsToAdd = 30 ;
155+ vKnotsToAdd = 30 ;
156+ print ("Added U knots: " , uKnotsToAdd )
157+ print ("Added V knots: " , vKnotsToAdd )
163158 for i in range (uKnotsToAdd ):
164- bspline_face .GetObject (). InsertUKnot (bounds [0 ]+ i * (bounds [1 ]- bounds [0 ])/ uKnotsToAdd ,1 , 1e-7 )
159+ bspline_face .InsertUKnot (bounds [0 ]+ i * (bounds [1 ]- bounds [0 ])/ uKnotsToAdd , 1 , 1e-7 )
165160 for i in range (vKnotsToAdd ):
166- bspline_face .GetObject (). InsertVKnot (bounds [2 ]+ i * (bounds [3 ]- bounds [2 ])/ vKnotsToAdd ,1 , 1e-7 )
161+ bspline_face .InsertVKnot (bounds [2 ]+ i * (bounds [3 ]- bounds [2 ])/ vKnotsToAdd , 1 , 1e-7 )
167162
168163 # openCascade object
169- occ_face = bspline_face . GetObject ()
164+ occ_face = bspline_face
170165
171166 bounds = 0.0
172167 bounds = occ_face .Bounds ()
@@ -175,7 +170,7 @@ def __call__(self, obj,dst=None):
175170 v_min = bounds [2 ]
176171 v_max = bounds [3 ]
177172 center = occ_face .Value ((u_min + u_max )/ 2.0 ,(v_min + v_max )/ 2.0 )
178- print ("Face Center: " ,center .X (),center .Y (),center .Z ())
173+ print ("Face Center: " , center .X (), center .Y (), center .Z ())
179174
180175 # extract the Control Points of each face
181176 n_poles_u = occ_face .NbUPoles ()
@@ -192,7 +187,7 @@ def __call__(self, obj,dst=None):
192187 control_point_coordinates .Y (),\
193188 control_point_coordinates .Z ()]
194189 i += 1
195-
190+
196191 ## SURFACES PHASE #####################################################
197192 src_pts = control_polygon_coordinates
198193 new_pts = super ().__call__ (src_pts ) # dont touch this line
@@ -209,9 +204,11 @@ def __call__(self, obj,dst=None):
209204 # we now need to obtain the curves (actually, the WIRES) that define the bounds of the surface and TRIM the surface
210205 # with them, to obtain the new face
211206
212- # we start creating a face with the modified surface. we will cut this new face with all the wires
207+ # we start creating a face with the modified surface. we will later cut this new face with all the wires
213208 # that the original face had
214- brep = BRepBuilderAPI_MakeFace (occ_face .GetHandle (), tol ).Face ()
209+ # this tolerance can be moved among the function parameters
210+ tolerance = 1e-2
211+ brep = BRepBuilderAPI_MakeFace (occ_face , tolerance ).Face ()
215212
216213
217214 # we here start looping on the wires of the original face
@@ -239,32 +236,30 @@ def __call__(self, obj,dst=None):
239236 wire_explorer = TopExp_Explorer (face_aux , TopAbs_WIRE )
240237 while wire_explorer .More ():
241238 wire = topods_Wire (wire_explorer .Current ())
242- wire_count += 1
243239 h_bspline_edge = GeomConvert_CompCurveToBSplineCurve ()
244240 edge_explorer = TopExp_Explorer (wire , TopAbs_EDGE )
245241 edgesCount = 0
246242 while edge_explorer .More ():
247243 # performing some conversions to get the right format (BSplineSurface)
248- #print("Edge in curve: ", edgesCount)
249244 edge = topods_Edge (edge_explorer .Current ())
250245 if (BRep_Tool .Degenerated (edge ) == False ):
251246 bspline_converter = BRepBuilderAPI_NurbsConvert (edge )
252247 bspline_converter .Perform (edge )
253248 bspline_tshape_edge = bspline_converter .Shape ()
254249 h_geom_edge , a , b = BRep_Tool_Curve (topods_Edge (bspline_tshape_edge ))
255250 this_bspline_edge = geomconvert_CurveToBSplineCurve (h_geom_edge )
256- bspline_geom_edge = this_bspline_edge . GetObject ()
257- h_bspline_edge .Add (this_bspline_edge ,1e-4 )
251+ bspline_geom_edge = this_bspline_edge
252+ h_bspline_edge .Add (this_bspline_edge ,tolerance )
258253 edgesCount += 1
259- #print("Curve ", curve_count, "added edge ", edgesCount)
254+
260255 edge_explorer .Next ()
261256
262257 bspline_geom_hedge = h_bspline_edge .BSplineCurve ()
263- bspline_geom_edge = bspline_geom_hedge . GetObject ()
258+ bspline_geom_edge = bspline_geom_hedge
264259 unified_edge = BRepBuilderAPI_MakeEdge (bspline_geom_hedge ).Edge ()
265260
266261 # number of knots is enriched here: this can become a user prescribed parameter for the class
267- knotsToAdd = 200
262+ knotsToAdd = 30
268263 firstParam = bspline_geom_edge .FirstParameter ()
269264 lastParam = bspline_geom_edge .LastParameter ()
270265 for i in range (knotsToAdd ):
@@ -301,17 +296,18 @@ def __call__(self, obj,dst=None):
301296 occ_edge .SetPole (pole + 1 , control_point )
302297 i += 1
303298
304- modified_edge = BRepBuilderAPI_MakeEdge (occ_edge . GetHandle () ).Edge ()
299+ modified_edge = BRepBuilderAPI_MakeEdge (occ_edge ).Edge ()
305300 shapesList .Append (modified_edge )
306301
307302 wire_maker = BRepBuilderAPI_MakeWire ()
308303 wire_maker .Add (shapesList )
309304 result_wire = wire_maker .Wire ()
310-
305+ iges_handler = IgesHandler ()
306+ iges_handler .write_shape_to_file (result_wire , "face_" + str (faceCount )+ "_wire_" + str (wire_count )+ ".iges" )
311307
312- # now, the wire can be oute or inner. we store the outer and (possible) inner ones in different lists
308+ # now, the wire can be outer or inner. we store the outer and (possible) inner ones in different lists
313309 # this is because we first need to trim the surface using the outer wire, and then we can trim it
314- # with the wires corresponding to al the holse . if this is not done, the procedure will not work
310+ # with the wires corresponding to all the holes . if this is not done, the procedure will not work
315311 if (wire == breptools_OuterWire (face_aux )):
316312 outer_wires .append (result_wire )
317313 else :
@@ -321,11 +317,12 @@ def __call__(self, obj,dst=None):
321317
322318
323319 # so once we finished looping on all the wires to modify them, we use the only outer one to trim the surface
324- face_maker = BRepBuilderAPI_MakeFace (occ_face . GetHandle () ,outer_wires [0 ])
320+ face_maker = BRepBuilderAPI_MakeFace (occ_face ,outer_wires [0 ])
325321
326322 # and then add all other inner wires for the holes
327323 for inner_wire in inner_wires :
328324 face_maker .Add (inner_wire )
325+
329326
330327 # finally, we get our trimmed face with all its holes
331328 brep_surf = face_maker .Face ()
0 commit comments