1- """
2- Module for generic deformation for CAD file.
3- """
1+ """Module for generic deformation for CAD file."""
42
53import os
6- import numpy as np
74from itertools import product
8- from OCC .Core .TopoDS import (
9- TopoDS_Shape ,
10- TopoDS_Compound ,
11- TopoDS_Face ,
12- TopoDS_Edge ,
13- TopoDS_Wire ,
14- topods
15- )
16- from OCC .Core .BRep import BRep_Builder
17- from OCC .Core .TopExp import TopExp_Explorer
18- from OCC .Core .TopAbs import TopAbs_EDGE , TopAbs_FACE , TopAbs_WIRE
19- from OCC .Core .TopTools import TopTools_ListOfShape
5+
6+ import numpy as np
7+ from OCC .Core .BRep import BRep_Builder , BRep_Tool , BRep_Tool_Curve
208from OCC .Core .BRepBuilderAPI import (
9+ BRepBuilderAPI_MakeEdge ,
2110 BRepBuilderAPI_MakeFace ,
2211 BRepBuilderAPI_MakeWire ,
23- BRepBuilderAPI_MakeEdge ,
2412 BRepBuilderAPI_NurbsConvert ,
2513)
26- from OCC .Core .BRep import BRep_Tool , BRep_Tool_Curve
14+ from OCC .Core .BRepTools import breptools_OuterWire
2715from OCC .Core .Geom import Geom_BSplineCurve , Geom_BSplineSurface
2816from OCC .Core .GeomConvert import (
29- geomconvert_SurfaceToBSplineSurface ,
30- geomconvert_CurveToBSplineCurve ,
3117 GeomConvert_CompCurveToBSplineCurve ,
18+ geomconvert_CurveToBSplineCurve ,
19+ geomconvert_SurfaceToBSplineSurface ,
3220)
3321from OCC .Core .gp import gp_Pnt
34- from OCC .Core .BRepTools import breptools_OuterWire
3522from OCC .Core .IFSelect import IFSelect_RetDone
23+ from OCC .Core .IGESControl import (
24+ IGESControl_Controller_Init ,
25+ IGESControl_Reader ,
26+ IGESControl_Writer ,
27+ )
3628from OCC .Core .Interface import Interface_Static_SetCVal
3729from OCC .Core .STEPControl import (
38- STEPControl_Writer ,
39- STEPControl_Reader ,
4030 STEPControl_AsIs ,
31+ STEPControl_Reader ,
32+ STEPControl_Writer ,
4133)
42- from OCC .Core .IGESControl import (
43- IGESControl_Writer ,
44- IGESControl_Reader ,
45- IGESControl_Controller_Init ,
34+ from OCC .Core .TopAbs import TopAbs_EDGE , TopAbs_FACE , TopAbs_WIRE
35+ from OCC .Core .TopExp import TopExp_Explorer
36+ from OCC .Core .TopoDS import (
37+ TopoDS_Compound ,
38+ TopoDS_Face ,
39+ TopoDS_Shape ,
40+ TopoDS_Wire ,
41+ topods ,
4642)
43+ from OCC .Core .TopTools import TopTools_ListOfShape
4744
4845
4946class CADDeformation :
50- """
51- Base class for applting deformation to CAD geometries.
47+ """Base class for applting deformation to CAD geometries.
5248
5349 :param int u_knots_to_add: the number of knots to add to the NURBS surfaces
5450 along `u` direction before the deformation. This parameter is useful
@@ -93,7 +89,11 @@ class CADDeformation:
9389 """
9490
9591 def __init__ (
96- self , u_knots_to_add = 0 , v_knots_to_add = 0 , t_knots_to_add = 0 , tolerance = 1e-4
92+ self ,
93+ u_knots_to_add = 0 ,
94+ v_knots_to_add = 0 ,
95+ t_knots_to_add = 0 ,
96+ tolerance = 1e-4 ,
9797 ):
9898 self .u_knots_to_add = u_knots_to_add
9999 self .v_knots_to_add = v_knots_to_add
@@ -154,14 +154,14 @@ def write_shape(filename, shape):
154154 """
155155
156156 def write_iges (filename , shape ):
157- """IGES writer"""
157+ """IGES writer. """
158158 IGESControl_Controller_Init ()
159159 writer = IGESControl_Writer ()
160160 writer .AddShape (shape )
161161 writer .Write (filename )
162162
163163 def write_step (filename , shape ):
164- """STEP writer"""
164+ """STEP writer. """
165165 step_writer = STEPControl_Writer ()
166166 # Changes write schema to STEP standard AP203
167167 # It is considered the most secure standard for STEP.
@@ -184,8 +184,7 @@ def write_step(filename, shape):
184184 writer (filename , shape )
185185
186186 def _bspline_surface_from_face (self , face ):
187- """
188- Private method that takes a TopoDS_Face and transforms it into a
187+ """Private method that takes a TopoDS_Face and transforms it into a
189188 Bspline_Surface.
190189
191190 :param TopoDS_Face face: the TopoDS_Face to be converted
@@ -202,8 +201,7 @@ def _bspline_surface_from_face(self, face):
202201 return bspline_surface
203202
204203 def _bspline_curve_from_wire (self , wire ):
205- """
206- Private method that takes a TopoDS_Wire and transforms it into a
204+ """Private method that takes a TopoDS_Wire and transforms it into a
207205 Bspline_Curve.
208206
209207 :param TopoDS_Wire wire: the TopoDS_Face to be converted
@@ -247,8 +245,7 @@ def _bspline_curve_from_wire(self, wire):
247245 return comp_curve
248246
249247 def _enrich_curve_knots (self , bsp_curve ):
250- """
251- Private method that adds `self.t_knots_to_add` poles to the passed
248+ """Private method that adds `self.t_knots_to_add` poles to the passed
252249 curve.
253250
254251 :param Geom_BSplineCurve bsp_curve: the NURBS curve to enrich
@@ -263,17 +260,19 @@ def _enrich_curve_knots(self, bsp_curve):
263260 last_param = bsp_curve .LastParameter ()
264261 for i in range (self .t_knots_to_add ):
265262 bsp_curve .InsertKnot (
266- first_param + i * (last_param - first_param ) / self .t_knots_to_add ,
263+ first_param
264+ + i * (last_param - first_param ) / self .t_knots_to_add ,
267265 1 ,
268266 self .tolerance ,
269267 )
270268
271269 def _enrich_surface_knots (self , bsp_surface ):
272- """
273- Private method that adds `self.u_knots_to_add` and `self.v_knots_to_add`
274- knots to the input surface `bsp_surface`, in u and v direction respectively.
270+ """Private method that adds `self.u_knots_to_add` and
271+ `self.v_knots_to_add` knots to the input surface `bsp_surface`, in u
272+ and v direction respectively.
275273
276- :param Geom_BSplineCurve bsp_surface: the NURBS surface to enrich
274+ :param Geom_BSplineCurve bsp_surface: the NURBS surface to
275+ enrich
277276 """
278277 if not isinstance (bsp_surface , Geom_BSplineSurface ):
279278 raise TypeError ("bsp_surface must be a Geom_BSplineSurface" )
@@ -297,18 +296,20 @@ def _enrich_surface_knots(self, bsp_surface):
297296 )
298297
299298 def _pole_get_components (self , pole ):
300- """Extract component from gp_Pnt"""
299+ """Extract component from gp_Pnt. """
301300 return pole .X (), pole .Y (), pole .Z ()
302301
303302 def _pole_set_components (self , components ):
304- """Return a gp_Pnt with the passed components"""
305- assert len (components ) == 3
303+ """Return a gp_Pnt with the passed components."""
304+ if len (components ) != 3 :
305+ raise ValueError (
306+ f"Expected 3 components for gp_Pnt, got { len (components )} "
307+ )
306308 return gp_Pnt (* components )
307309
308310 def _deform_bspline_curve (self , bsp_curve ):
309- """
310- Private method that deforms the control points of `bsp_curve` using the
311- inherited method. All the changes are performed in place.
311+ """Private method that deforms the control points of `bsp_curve` using
312+ the inherited method. All the changes are performed in place.
312313
313314 :param Geom_Bspline_Curve bsp_curve: the curve to deform
314315 """
@@ -336,9 +337,8 @@ def _deform_bspline_curve(self, bsp_curve):
336337 bsp_curve .SetPole (pole_id + 1 , new_pole )
337338
338339 def _deform_bspline_surface (self , bsp_surface ):
339- """
340- Private method that deforms the control points of `surface` using the
341- inherited method. All the changes are performed in place.
340+ """Private method that deforms the control points of `surface` using
341+ the inherited method. All the changes are performed in place.
342342
343343 :param Geom_Bspline_Surface bsp_surface: the surface to deform
344344 """
@@ -368,12 +368,11 @@ def _deform_bspline_surface(self, bsp_surface):
368368 bsp_surface .SetPole (u + 1 , v + 1 , new_pole )
369369
370370 def __call__ (self , obj , dst = None ):
371- """
372- This method performs the deformation on the CAD geometry. If `obj` is a
373- TopoDS_Shape, the method returns a TopoDS_Shape containing the deformed
374- geometry. If `obj` is a filename, the method deforms the geometry
375- contained in the file and writes the deformed shape to `dst` (which has
376- to be set).
371+ """This method performs the deformation on the CAD geometry. If `obj`
372+ is a TopoDS_Shape, the method returns a TopoDS_Shape containing the
373+ deformed geometry. If `obj` is a filename, the method deforms the
374+ geometry contained in the file and writes the deformed shape to `dst`
375+ (which has to be set).
377376
378377 :param obj: the input geometry.
379378 :type obj: str or TopoDS_Shape
@@ -484,7 +483,9 @@ def __call__(self, obj, dst=None):
484483 # so once we finished looping on all the wires to modify them,
485484 # we first use the only outer one to trim the surface
486485 # face builder object
487- face_maker = BRepBuilderAPI_MakeFace (bspline_surface , outer_wires [0 ])
486+ face_maker = BRepBuilderAPI_MakeFace (
487+ bspline_surface , outer_wires [0 ]
488+ )
488489
489490 # and then add all other inner wires for the holes
490491 for inner_wire in inner_wires :
0 commit comments