Skip to content

Commit c356bed

Browse files
author
Andrea Mola
committed
fixed more errors on cad FFD interface and its tests
1 parent ea75e6f commit c356bed

File tree

7 files changed

+85
-88
lines changed

7 files changed

+85
-88
lines changed

pygem/cad/ffd.py

Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,26 @@
4444
import configparser as configparser
4545
except ImportError:
4646
import ConfigParser as configparser
47-
import os
4847
import 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+
6567
from pygem import FFD as OriginalFFD
6668
from 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()

pygem/cad/igeshandler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"""
22
Derived module from filehandler.py to handle iges and igs files.
33
"""
4-
from OCC.IGESControl import (IGESControl_Reader, IGESControl_Writer,
4+
from OCC.Core.IGESControl import (IGESControl_Reader, IGESControl_Writer,
55
IGESControl_Controller_Init)
6-
from OCC.IFSelect import IFSelect_RetDone
6+
from OCC.Core.IFSelect import IFSelect_RetDone
77
from pygem.cad import NurbsHandler
88

99

pygem/cad/nurbshandler.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@
66
"""
77
import os
88
import numpy as np
9-
from OCC.BRep import BRep_Tool, BRep_Builder, BRep_Tool_Curve
10-
from OCC.BRepMesh import BRepMesh_IncrementalMesh
11-
from OCC.BRepAlgo import brepalgo_IsValid
12-
from OCC.BRepBuilderAPI import (
9+
from OCC.Core.BRep import BRep_Tool, BRep_Builder, BRep_Tool_Curve
10+
from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh
11+
from OCC.Core.BRepAlgo import brepalgo_IsValid
12+
from OCC.Core.BRepBuilderAPI import (
1313
BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeFace,
1414
BRepBuilderAPI_NurbsConvert, BRepBuilderAPI_MakeWire, BRepBuilderAPI_Sewing)
15-
from OCC.BRepOffsetAPI import BRepOffsetAPI_FindContigousEdges
15+
from OCC.Core.BRepOffsetAPI import BRepOffsetAPI_FindContigousEdges
1616
from OCC.Display.SimpleGui import init_display
17-
from OCC.GeomConvert import (geomconvert_SurfaceToBSplineSurface,
17+
from OCC.Core.GeomConvert import (geomconvert_SurfaceToBSplineSurface,
1818
geomconvert_CurveToBSplineCurve)
19-
from OCC.gp import gp_Pnt, gp_XYZ
20-
from OCC.Precision import precision_Confusion
21-
from OCC.ShapeAnalysis import ShapeAnalysis_WireOrder
22-
from OCC.ShapeFix import ShapeFix_ShapeTolerance, ShapeFix_Shell
23-
from OCC.StlAPI import StlAPI_Writer
24-
from OCC.TColgp import TColgp_Array1OfPnt, TColgp_Array2OfPnt
25-
from OCC.TopAbs import (TopAbs_FACE, TopAbs_EDGE, TopAbs_WIRE, TopAbs_FORWARD,
19+
from OCC.Core.gp import gp_Pnt, gp_XYZ
20+
from OCC.Core.Precision import precision_Confusion
21+
from OCC.Core.ShapeAnalysis import ShapeAnalysis_WireOrder
22+
from OCC.Core.ShapeFix import ShapeFix_ShapeTolerance, ShapeFix_Shell
23+
from OCC.Core.StlAPI import StlAPI_Writer
24+
from OCC.Core.TColgp import TColgp_Array1OfPnt, TColgp_Array2OfPnt
25+
from OCC.Core.TopAbs import (TopAbs_FACE, TopAbs_EDGE, TopAbs_WIRE, TopAbs_FORWARD,
2626
TopAbs_SHELL)
27-
from OCC.TopExp import TopExp_Explorer, topexp
28-
from OCC.TopoDS import (topods_Face, TopoDS_Compound, topods_Shell, topods_Edge,
27+
from OCC.Core.TopExp import TopExp_Explorer, topexp
28+
from OCC.Core.TopoDS import (topods_Face, TopoDS_Compound, topods_Shell, topods_Edge,
2929
topods_Wire, topods, TopoDS_Shape)
3030
from matplotlib import pyplot
3131
from mpl_toolkits import mplot3d
@@ -113,7 +113,7 @@ def parse(self, filename):
113113
bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face)
114114

115115
# openCascade object
116-
occ_face = bspline_face.GetObject()
116+
occ_face = bspline_face
117117

118118
# extract the Control Points of each face
119119
n_poles_u = occ_face.NbUPoles()
@@ -186,7 +186,7 @@ def write(self, mesh_points, filename, tolerance=None):
186186
face_aux = topods_Face(nurbs_face)
187187
brep_face = BRep_Tool.Surface(topods_Face(nurbs_face))
188188
bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face)
189-
occ_face = bspline_face.GetObject()
189+
occ_face = bspline_face
190190

191191
n_poles_u = occ_face.NbUPoles()
192192
n_poles_v = occ_face.NbVPoles()
@@ -206,7 +206,7 @@ def write(self, mesh_points, filename, tolerance=None):
206206
# construct the deformed wire for the trimmed surfaces
207207
wire_maker = BRepBuilderAPI_MakeWire()
208208
tol = ShapeFix_ShapeTolerance()
209-
brep = BRepBuilderAPI_MakeFace(occ_face.GetHandle(),
209+
brep = BRepBuilderAPI_MakeFace(occ_face,
210210
self.tolerance).Face()
211211
brep_face = BRep_Tool.Surface(brep)
212212

@@ -229,7 +229,7 @@ def write(self, mesh_points, filename, tolerance=None):
229229
wire = wire_maker.Wire()
230230

231231
# trimming the surfaces
232-
brep_surf = BRepBuilderAPI_MakeFace(occ_face.GetHandle(),
232+
brep_surf = BRepBuilderAPI_MakeFace(occ_face,
233233
wire).Shape()
234234
compound_builder.Add(compound, brep_surf)
235235
n_faces += 1
@@ -450,7 +450,7 @@ def write_edge(points_edge, topo_edge):
450450
cpt = points_edge[i - 1]
451451
bspline_edge_curve.SetPole(i, gp_Pnt(cpt[0], cpt[1], cpt[2]))
452452

453-
new_edge = BRepBuilderAPI_MakeEdge(bspline_edge_curve.GetHandle())
453+
new_edge = BRepBuilderAPI_MakeEdge(bspline_edge_curve)
454454

455455
return new_edge.Edge()
456456

@@ -476,7 +476,7 @@ def write_face(self, points_face, list_points_edge, topo_face, toledge):
476476
topo_nurbsface = topods.Face(nurbs_face)
477477
h_geomsurface = BRep_Tool.Surface(topo_nurbsface)
478478
h_bsurface = geomconvert_SurfaceToBSplineSurface(h_geomsurface)
479-
bsurface = h_bsurface.GetObject()
479+
bsurface = h_bsurface
480480

481481
nb_u = bsurface.NbUPoles()
482482
nb_v = bsurface.NbVPoles()
@@ -496,7 +496,7 @@ def write_face(self, points_face, list_points_edge, topo_face, toledge):
496496
# create modified new face
497497
new_bspline_tface = BRepBuilderAPI_MakeFace()
498498
toler = precision_Confusion()
499-
new_bspline_tface.Init(bsurface.GetHandle(), False, toler)
499+
new_bspline_tface.Init(bsurface, False, toler)
500500

501501
# cycle on the wires
502502
face_wires_explorer = TopExp_Explorer(

pygem/cad/stephandler.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
22
Derived module from nurbshandler.py to handle step and stp files.
33
"""
4-
from OCC.IFSelect import IFSelect_RetDone
5-
from OCC.Interface import Interface_Static_SetCVal
6-
from OCC.STEPControl import STEPControl_Writer, STEPControl_Reader
7-
from OCC.STEPControl import STEPControl_AsIs
4+
from OCC.Core.IFSelect import IFSelect_RetDone
5+
from OCC.Core.Interface import Interface_Static_SetCVal
6+
from OCC.Core.STEPControl import STEPControl_Writer, STEPControl_Reader
7+
from OCC.Core.STEPControl import STEPControl_AsIs
88
from pygem.cad import NurbsHandler
99

1010

tests/test_ffd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def test_ffd_iges_pipe_mod_through_files(self):
398398
def test_ffd_iges_pipe_mod_through_topods_shape(self):
399399
from pygem.cad.igeshandler import IgesHandler
400400
from pygem.cad import FFD
401-
from OCC.TopoDS import TopoDS_Shape
401+
from OCC.Core.TopoDS import TopoDS_Shape
402402
iges_handler = IgesHandler()
403403
orig_shape = iges_handler.load_shape_from_file('tests/test_datasets/test_pipe.iges')
404404
ffd = FFD()

tests/test_igeshandler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
import numpy as np
55

6-
from OCC.TopoDS import TopoDS_Shape, TopoDS_Compound, topods_Compound
7-
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox
6+
from OCC.Core.TopoDS import TopoDS_Shape, TopoDS_Compound, topods_Compound
7+
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
88

99
from pygem.cad.igeshandler import IgesHandler
1010

tests/test_stephandler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
from unittest import TestCase
33

44
import numpy as np
5-
from OCC.TopoDS import TopoDS_Shape, TopoDS_Compound, topods_Compound
6-
from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox
5+
from OCC.Core.TopoDS import TopoDS_Shape, TopoDS_Compound, topods_Compound
6+
from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox
77

88
from pygem.cad import StepHandler
99

0 commit comments

Comments
 (0)