Skip to content

Commit b5f4773

Browse files
author
Andrea Mola
committed
starting to fill up ffd.py file with cad deformation procedure
1 parent 721858d commit b5f4773

File tree

1 file changed

+303
-11
lines changed

1 file changed

+303
-11
lines changed

pygem/cad/ffd.py

Lines changed: 303 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,24 +103,316 @@ def __call__(self, obj):
103103
else:
104104
raise TypeError
105105

106-
107-
## SURFACES PHASE #####################################################
108-
src_pts = # extract coordinates of surfaces control points
106+
print("Modifying faces")
109107

110-
new_pts = super().__call__(src_pts) # dont touch this line
108+
compound_builder = BRep_Builder()
109+
compound = OCC.TopoDS.TopoDS_Compound()
110+
compound_builder.MakeCompound(compound)
111111

112-
# save here the `new_pts` into the shape
113-
## END SURFACES #######################################################
112+
# cycle on the faces to get the control points
113+
# init some quantities
114+
faceCount = 0
115+
face_list = []
116+
control_point_position = [0]
117+
faces_explorer = TopExp_Explorer(color, TopAbs_FACE)
118+
mesh_points = np.zeros(shape=(0, 3))
119+
120+
while faces_explorer.More():
121+
points_orig = []
122+
points_def = []
123+
# performing some conversions to get the right format (BSplineSurface)
124+
print("Processing face ",faceCount)
125+
face = OCC.TopoDS.topods_Face(faces_explorer.Current())
126+
nurbs_converter = BRepBuilderAPI_NurbsConvert(face)
127+
nurbs_converter.Perform(face)
128+
nurbs_face = nurbs_converter.Shape()
129+
face_aux = OCC.TopoDS.topods_Face(nurbs_face)
130+
int_tools = IntTools_FClass2d(face_aux, 1e-3)
131+
brep_face = BRep_Tool.Surface(OCC.TopoDS.topods_Face(nurbs_face))
132+
old_brep_face = BRep_Tool.Surface(OCC.TopoDS.topods_Face(nurbs_face))
133+
134+
bounds = 0.0
135+
bounds = brep_face.GetObject().Bounds()
136+
#print("Bounds: ", bounds)
137+
bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face)
138+
139+
# we will then add an amount of nodes that will grant us our prescribed resolution both along u and v
140+
uKnotsToAdd = 20;
141+
vKnotsToAdd = 20;
142+
print("Added U knots: ", uKnotsToAdd )
143+
print("Added V knots: ", vKnotsToAdd )
144+
for i in range(uKnotsToAdd):
145+
bspline_face.GetObject().InsertUKnot(bounds[0]+i*(bounds[1]-bounds[0])/uKnotsToAdd,1,1e-7)
146+
for i in range(vKnotsToAdd):
147+
bspline_face.GetObject().InsertVKnot(bounds[2]+i*(bounds[3]-bounds[2])/vKnotsToAdd,1,1e-7)
148+
149+
# openCascade object
150+
occ_face = bspline_face.GetObject()
151+
152+
bounds = 0.0
153+
bounds = occ_face.Bounds()
154+
u_min = bounds[0]
155+
u_max = bounds[1]
156+
v_min = bounds[2]
157+
v_max = bounds[3]
158+
center = occ_face.Value((u_min+u_max)/2.0,(v_min+v_max)/2.0)
159+
print("*Center: ",center.X(),center.Y(),center.Z())
160+
161+
# extract the Control Points of each face
162+
n_poles_u = occ_face.NbUPoles()
163+
n_poles_v = occ_face.NbVPoles()
164+
control_polygon_coordinates = np.zeros(\
165+
shape=(n_poles_u * n_poles_v, 3))
166+
#print("Number of poles: ", n_poles_u * n_poles_v)
167+
# cycle over the poles to get their coordinates
168+
i = 0
169+
for pole_u_direction in range(n_poles_u):
170+
for pole_v_direction in range(n_poles_v):
171+
control_point_coordinates = occ_face.Pole(\
172+
pole_u_direction + 1, pole_v_direction + 1)
173+
control_polygon_coordinates[i, :] = [control_point_coordinates.X(),\
174+
control_point_coordinates.Y(),\
175+
control_point_coordinates.Z()]
176+
177+
#control_point = gp_Pnt(control_point_coordinates.X(),\
178+
#control_point_coordinates.Y(),control_point_coordinates.Z())
179+
#display.DisplayShape(BRepBuilderAPI_MakeVertex(control_point).Vertex(),update=True)
180+
#print("Original: ",control_point.X()," ",control_point.Y()," ",control_point.Z())
181+
i+=1
182+
183+
184+
#orig_control_polygon_coordinates = deepcopy(control_polygon_coordinates)
185+
## SURFACES PHASE #####################################################
186+
src_pts = control_polygon_coordinates
187+
new_pts = super().__call__(src_pts) # dont touch this line
188+
i = 0
189+
for pole_u_direction in range(n_poles_u):
190+
for pole_v_direction in range(n_poles_v):
191+
control_point = gp_Pnt(control_polygon_coordinates[i,0],
192+
control_polygon_coordinates[i,1],
193+
control_polygon_coordinates[i,2])
194+
occ_face.SetPole(pole_u_direction + 1, pole_v_direction + 1, control_point)
195+
i += 1
196+
# through moving the control points, we now changed the SURFACE of the FACE we are processing
197+
# we now need to obtain the curves (actually, the WIRES) that define the bounds of the surface and TRIM the surface
198+
# with them, to obtain the new face
199+
200+
faceFilling = BRepFill_Filling()
201+
# we start creating a face with the modified surface. we will cut this new face with all the wires
202+
# that the original face had
203+
brep = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), tol).Face()
204+
#IGESControl_Controller_Init()
205+
#writer = IGESControl_Writer()
206+
#writer.AddShape(brep)
207+
#nomefile = "untrimmed_face"+str(faceCount)+".iges"
208+
#writer.Write(nomefile)
209+
210+
# we here start looping on the wires of the original face
211+
# in this forst loop we do nothing but count the wires in this face
212+
# few faces have more than one wire: if they have, it is because they have holes, and
213+
# if this is the case one wire is the outer, and the others are the inner ones.
214+
# the loop will also tell us which wire is the outer one
215+
wire_count = 0
216+
wire_explorer = TopExp_Explorer(face_aux, TopAbs_WIRE)
217+
while wire_explorer.More():
218+
wire = OCC.TopoDS.topods_Wire(wire_explorer.Current())
219+
if (wire == breptools_OuterWire(face_aux)):
220+
print("Wire", wire_count+1, "is outer wire")
221+
wire_count += 1
222+
wire_explorer.Next()
223+
print("This face has ",wire_count," wires")
224+
225+
#we now start really looping on the wires
226+
wire_count = 0
227+
outer_wires = []
228+
inner_wires = []
229+
brep_face = BRep_Tool.Surface(brep)
230+
wire_explorer = TopExp_Explorer(face_aux, TopAbs_WIRE)
231+
while wire_explorer.More():
232+
wire = OCC.TopoDS.topods_Wire(wire_explorer.Current())
233+
wire_count += 1
234+
h_bspline_edge = GeomConvert_CompCurveToBSplineCurve()
235+
edge_explorer = TopExp_Explorer(wire, TopAbs_EDGE)
236+
edgesCount=0
237+
while edge_explorer.More():
238+
# performing some conversions to get the right format (BSplineSurface)
239+
#print("Edge in curve: ", edgesCount)
240+
edge = OCC.TopoDS.topods_Edge(edge_explorer.Current())
241+
if (BRep_Tool.Degenerated(edge) == False):
242+
bspline_converter = BRepBuilderAPI_NurbsConvert(edge)
243+
bspline_converter.Perform(edge)
244+
bspline_tshape_edge = bspline_converter.Shape()
245+
h_geom_edge, a, b = BRep_Tool_Curve(OCC.TopoDS.topods_Edge(bspline_tshape_edge))
246+
this_bspline_edge = geomconvert_CurveToBSplineCurve(h_geom_edge)
247+
bspline_geom_edge = this_bspline_edge.GetObject()
248+
h_bspline_edge.Add(this_bspline_edge,1e-4)
249+
edgesCount += 1
250+
#print("Curve ", curve_count, "added edge ", edgesCount)
251+
edge_explorer.Next()
252+
253+
bspline_geom_hedge = h_bspline_edge.BSplineCurve()
254+
bspline_geom_edge = bspline_geom_hedge.GetObject()
255+
unified_edge = BRepBuilderAPI_MakeEdge(bspline_geom_hedge).Edge()
256+
#aa = bspline_geom_edge.FirstParameter()
257+
#bb = bspline_geom_edge.LastParameter()
258+
#print("Unif. Edge First Point:", bspline_geom_edge.Value(aa).X(),
259+
#bspline_geom_edge.Value(aa).Y(),
260+
#bspline_geom_edge.Value(aa).Z())
261+
#print("Unif. Edge Last Point:", bspline_geom_edge.Value(bb).X(),
262+
#bspline_geom_edge.Value(bb).Y(),
263+
#bspline_geom_edge.Value(bb).Z())
264+
#unified_edge = BRepBuilderAPI_MakeEdge(bspline_geom_hedge).Edge()
265+
#print("SAVING UNIFIED CURVE: ", curve_count)
266+
#IGESControl_Controller_Init()
267+
#writer = IGESControl_Writer()
268+
#writer.AddShape(unified_edge)
269+
#filename = nomedir+"unfied_curve_"+str(curve_count)+".iges"
270+
##print(filename)
271+
#writer.Write(filename)
272+
273+
274+
firstParam = bspline_geom_edge.FirstParameter()
275+
lastParam = bspline_geom_edge.LastParameter()
276+
#print("First Parameter: ", firstParam)
277+
#print("Last Parameter: ", lastParam)
278+
knotsToAdd = 200
279+
for i in range(knotsToAdd):
280+
bspline_geom_edge.InsertKnot(firstParam+i*(lastParam-firstParam)/knotsToAdd,1,1e-7)
281+
shapesList = TopTools_ListOfShape()
282+
# openCascade object
283+
occ_edge = bspline_geom_edge
284+
285+
# extract the Control Points of each face
286+
n_poles = occ_edge.NbPoles()
287+
control_polygon_coordinates = np.zeros(\
288+
shape=(n_poles , 3))
289+
#print("Number of poles: ", n_poles)
290+
# cycle over the poles to get their coordinates. The idea here is to move poles
291+
# coordinates to deform the curves
292+
i = 0
293+
for pole in range(n_poles):
294+
control_point_coordinates = occ_edge.Pole(pole + 1)
295+
control_polygon_coordinates[i, :] = [control_point_coordinates.X(),\
296+
control_point_coordinates.Y(),\
297+
control_point_coordinates.Z()]
298+
#control_point = gp_Pnt(control_point_coordinates.X(),\
299+
#control_point_coordinates.Y(),control_point_coordinates.Z())
300+
#orig_control_point = control_point
301+
#display.DisplayShape(BRepBuilderAPI_MakeVertex(control_point).Vertex(),update=True)
302+
#print("Original: ",control_point.X()," ",control_point.Y()," ",control_point.Z())
303+
304+
## CURVES PHASE #######################################################
305+
src_pts = control_point_coordinates
306+
307+
new_pts = super().__call__(src_pts) # dont touch this line
114308

309+
# save here the `new_pts` into the shape
310+
## END CURVES #########################################################
115311

312+
i = 0
313+
for pole in range(n_poles):
314+
#print("Function test:", mod_test_control_point.X(),mod_test_control_point.Y(),mod_test_control_point.Z())
315+
#print("**")
316+
control_point = gp_Pnt(control_polygon_coordinates[i,0],
317+
control_polygon_coordinates[i,1],
318+
control_polygon_coordinates[i,2])
319+
occ_edge.SetPole(pole + 1, control_point)
320+
i += 1
321+
322+
#occ_new_hedge = geomprojlib.Project(occ_edge.GetHandle(),occ_face.GetHandle())
323+
#new_curve = occ_new_hedge.GetObject()
324+
#print("??", new_curve.IsClosed())
325+
modified_edge= BRepBuilderAPI_MakeEdge(occ_edge.GetHandle()).Edge()
326+
shapesList.Append(modified_edge)
327+
#if new_curve.IsClosed()==False:
328+
#fixer_edge = BRepBuilderAPI_MakeEdge(new_curve.Value(new_curve.LastParameter()),new_curve.Value(new_curve.FirstParameter())).Edge()
329+
#shapesList.Append(fixer_edge)
330+
#display.DisplayShape(modified_edge,update=True,color="BLUE1")
331+
332+
wire_maker = BRepBuilderAPI_MakeWire()
333+
wire_maker.Add(shapesList)
334+
result_wire = wire_maker.Wire()
335+
336+
337+
# now, the wire can be oute or inner. we store the outer and (possible) inner ones in different lists
338+
# this is because we first need to trim the surface using the outer wire, and then we can trim it
339+
# with the wires corresponding to al the holse. if this is not done, the procedure will not work
340+
if (wire == breptools_OuterWire(face_aux)):
341+
outer_wires.append(result_wire)
342+
else:
343+
inner_wires.append(result_wire)
344+
wire_count += 1
345+
wire_explorer.Next()
346+
347+
#faceFilling.LoadInitSurface(brep)
348+
#faceFilling.Build()
349+
# checking if things worked out
350+
#print("Is face Filling Done? ",faceFilling.IsDone())
351+
352+
# so once we finished looping on all the wires to modify them, we use the only outer one to trim the surface
353+
face_maker = BRepBuilderAPI_MakeFace(occ_face.GetHandle(),outer_wires[0])
354+
355+
# and then add all other inner wires for the holes
356+
for inner_wire in inner_wires:
357+
face_maker.Add(inner_wire)
358+
359+
# finally, we get our trimmed face with all its holes
360+
brep_surf = face_maker.Face()
361+
#brep_surf = faceFilling.Face()
362+
IGESControl_Controller_Init()
363+
writer = IGESControl_Writer()
364+
writer.AddShape(brep_surf)
365+
nomefile = nomedir+"trimmed_face"+str(faceCount)+".iges"
366+
writer.Write(nomefile)
367+
368+
# we add the face to the comound of the color we are working on
369+
compound_builder.Add(compound, brep_surf)
370+
face_list.append(brep_surf)
371+
372+
# and move to the next face
373+
faceCount += 1
374+
faces_explorer.Next()
375+
376+
#sew = BRepBuilderAPI_Sewing(100.0)
377+
#for sh in face_list:
378+
#sew.Add(sh)
379+
#print("??")
380+
#sew.SetNonManifoldMode(True)
381+
#sew.Perform()
382+
#shell = sew.SewedShape()
383+
#if shell.ShapeType() == TopAbs_SHELL:
384+
#print("It's a shell")
385+
#else:
386+
#print("No luck here")
387+
##shell = sew_shapes( face_list, 0.1 )
388+
389+
390+
# after looping on all the faces of a color to modifiy them, we save the color compound in an iges file
391+
IGESControl_Controller_Init()
392+
writer = IGESControl_Writer()
393+
writer.AddShape(compound)
394+
nomefile = nomedir+"Color_"+str(color_count)+".iges"
395+
writer.Write(nomefile)
396+
#display.DisplayShape(compound,update=True,color="RED")
397+
print(faceCount)
398+
colors_mod.append(compound)
399+
else:
400+
# if the color was not to be modified with this procedure (like the top and transom), we just skip them
401+
print("Non modified color")
402+
#IGESControl_Controller_Init()
403+
#writer = IGESControl_Writer()
404+
#writer.AddShape(color)
405+
#nomefile = nomedir+"Color_"+str(color_count)+".iges"
406+
#writer.Write(nomefile)
407+
##display.DisplayShape(compound,update=True,color="RED")
408+
print(color_count)
409+
colors_mod.append(color)
410+
411+
412+
## END SURFACES #######################################################
116413

117-
## CURVES PHASE #######################################################
118-
src_pts = # extract coordinates of curves control points
119414

120-
new_pts = super().__call__(src_pts) # dont touch this line
121415

122-
# save here the `new_pts` into the shape
123-
## END CURVES #########################################################
124416

125417

126418

0 commit comments

Comments
 (0)