Skip to content

Commit eb92585

Browse files
committed
Add method write_face in NurbsHandler
1 parent 3245b0c commit eb92585

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

pygem/nurbshandler.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,120 @@ def write_edge(points_edge, topo_edge):
435435

436436
return new_edge.Edge()
437437

438+
def write_face(self, points_face, list_points_edge, topo_face, toledge):
439+
"""
440+
Method to recreate a Face associated to a geometric surface
441+
after the modification of Face points. It returns a TopoDS_Face.
442+
443+
:param points_face: the new face points array.
444+
:param list_points_edge: new edge points
445+
:param topo_face: the face to be modified
446+
:param toledge: tolerance on the surface creation after modification
447+
:return: TopoDS_Face (Shape)
448+
449+
:rtype: TopoDS_Shape
450+
451+
"""
452+
453+
# convert Face to Geom B-spline Surface
454+
nurbs_converter = BRepBuilderAPI_NurbsConvert(topo_face)
455+
nurbs_converter.Perform(topo_face)
456+
nurbs_face = nurbs_converter.Shape()
457+
topo_nurbsface = OCC.TopoDS.topods.Face(nurbs_face)
458+
h_geomsurface = BRep_Tool.Surface(topo_nurbsface)
459+
h_bsurface = geomconvert_SurfaceToBSplineSurface(h_geomsurface)
460+
bsurface = h_bsurface.GetObject()
461+
462+
nb_u = bsurface.NbUPoles()
463+
nb_v = bsurface.NbVPoles()
464+
# check consistency
465+
if points_face.shape[0] != nb_u * nb_v:
466+
raise ValueError("Input control points do not have not have the "
467+
"same number as the geometric face!")
468+
469+
# cycle on the face points
470+
indice_cpt = 0
471+
for iu in range(1, nb_u + 1):
472+
for iv in range(1, nb_v + 1):
473+
cpt = points_face[indice_cpt]
474+
bsurface.SetPole(iu, iv, gp_Pnt(cpt[0], cpt[1], cpt[2]))
475+
indice_cpt += 1
476+
477+
# create modified new face
478+
new_bspline_tface = BRepBuilderAPI_MakeFace()
479+
toler = OCC.Precision.precision_Confusion()
480+
new_bspline_tface.Init(bsurface.GetHandle(), False, toler)
481+
482+
# cycle on the wires
483+
face_wires_explorer = TopExp_Explorer(topo_nurbsface
484+
.Oriented(TopAbs_FORWARD),
485+
TopAbs_WIRE)
486+
ind_edge_total = 0
487+
488+
while face_wires_explorer.More():
489+
# get old wire
490+
twire = OCC.TopoDS.topods_Wire(face_wires_explorer.Current())
491+
492+
# cycle on the edges
493+
ind_edge = 0
494+
wire_explorer_edge = TopExp_Explorer(twire.Oriented(TopAbs_FORWARD),
495+
TopAbs_EDGE)
496+
# check edges order on the wire
497+
mode3d = True
498+
tolerance_edges = toledge
499+
500+
wire_order = ShapeAnalysis_WireOrder(mode3d, tolerance_edges)
501+
# an edge list
502+
deformed_edges = []
503+
# cycle on the edges
504+
while wire_explorer_edge.More():
505+
tedge = OCC.TopoDS.topods_Edge(wire_explorer_edge.Current())
506+
new_bspline_tedge = self.write_edge(
507+
list_points_edge[ind_edge_total], tedge)
508+
509+
deformed_edges.append(new_bspline_tedge)
510+
analyzer = topexp()
511+
vfirst = analyzer.FirstVertex(new_bspline_tedge)
512+
vlast = analyzer.LastVertex(new_bspline_tedge)
513+
pt1 = BRep_Tool.Pnt(vfirst)
514+
pt2 = BRep_Tool.Pnt(vlast)
515+
516+
wire_order.Add(pt1.XYZ(), pt2.XYZ())
517+
518+
ind_edge += 1
519+
ind_edge_total += 1
520+
wire_explorer_edge.Next()
521+
522+
# grouping the edges in a wire, then in the face
523+
# check edges order and connectivity within the wire
524+
wire_order.Perform()
525+
# new wire to be created
526+
stol = ShapeFix_ShapeTolerance()
527+
new_bspline_twire = BRepBuilderAPI_MakeWire()
528+
for order_i in range(1, wire_order.NbEdges() + 1):
529+
deformed_edge_i = wire_order.Ordered(order_i)
530+
if deformed_edge_i > 0:
531+
# insert the deformed edge to the new wire
532+
new_edge_toadd = deformed_edges[deformed_edge_i - 1]
533+
stol.SetTolerance(new_edge_toadd, toledge)
534+
new_bspline_twire.Add(new_edge_toadd)
535+
if new_bspline_twire.Error() != 0:
536+
stol.SetTolerance(new_edge_toadd, toledge * 10.0)
537+
new_bspline_twire.Add(new_edge_toadd)
538+
else:
539+
deformed_edge_revers = deformed_edges[
540+
np.abs(deformed_edge_i) - 1]
541+
stol.SetTolerance(deformed_edge_revers, toledge)
542+
new_bspline_twire.Add(deformed_edge_revers)
543+
if new_bspline_twire.Error() != 0:
544+
stol.SetTolerance(deformed_edge_revers, toledge * 10.0)
545+
new_bspline_twire.Add(deformed_edge_revers)
546+
# add new wire to the Face
547+
new_bspline_tface.Add(new_bspline_twire.Wire())
548+
face_wires_explorer.Next()
549+
550+
return OCC.TopoDS.topods.Face(new_bspline_tface.Face())
551+
438552
def write_shape_to_file(self, shape, filename):
439553
"""
440554
Abstract method to write the 'shape' to the `filename`.

0 commit comments

Comments
 (0)