Skip to content

Commit 70e514f

Browse files
committed
Merge pull request tpaviot#5 from tpaviot/tp/pep8-fixes
Misc fixes/refactoring
2 parents b6565cb + ef84b3b commit 70e514f

File tree

16 files changed

+212
-642
lines changed

16 files changed

+212
-642
lines changed

OCCUtils/Adapt.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

OCCUtils/Common.py

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,55 @@
1919

2020
import random
2121

22-
from OCC.Bnd import *
23-
from OCC.BRepBndLib import *
24-
from OCC.TColgp import *
25-
from OCC.TColStd import *
26-
from OCC.BRepAdaptor import *
27-
from OCC.GeomAPI import *
28-
from OCC.gp import *
29-
from OCC.BRepBuilderAPI import *
30-
from OCC.TopoDS import *
31-
from OCC.Quantity import *
22+
from OCC.Bnd import Bnd_Box
23+
from OCC.BRepBndLib import brepbndlib_Add
24+
from OCC.TColgp import (TColgp_HArray1OfPnt,
25+
TColgp_Array1OfPnt,
26+
TColgp_Array1OfPnt2d,
27+
TColgp_Array1OfVec)
28+
from OCC.TColStd import TColStd_HArray1OfBoolean
29+
from OCC.BRepAdaptor import (BRepAdaptor_Curve, BRepAdaptor_HCurve,
30+
BRepAdaptor_CompCurve, BRepAdaptor_HCompCurve)
31+
from OCC.GeomAPI import (GeomAPI_Interpolate, GeomAPI_PointsToBSpline,
32+
GeomAPI_ProjectPointOnCurve)
33+
from OCC.gp import gp_Pnt, gp_Vec, gp_Trsf
34+
from OCC.BRepBuilderAPI import BRepBuilderAPI_Transform
35+
from OCC.TopoDS import TopoDS_Edge, TopoDS_Shape, TopoDS_Wire, TopoDS_Vertex
36+
from OCC.Quantity import Quantity_Color, Quantity_TOC_RGB
3237
from OCC.GProp import GProp_GProps
33-
from OCC.GeomAbs import *
34-
from OCC.BRepGProp import brepgprop_LinearProperties, brepgprop_SurfaceProperties, brepgprop_VolumeProperties
35-
from OCC import Graphic3d
38+
from OCC.GeomAbs import GeomAbs_C1, GeomAbs_C2, GeomAbs_C3
39+
from OCC.BRepGProp import (brepgprop_LinearProperties,
40+
brepgprop_SurfaceProperties,
41+
brepgprop_VolumeProperties)
42+
from OCC.GeomAdaptor import GeomAdaptor_Curve
43+
from OCC.Geom import Geom_Curve
3644

37-
from Context import assert_isdone
45+
from OCC import Graphic3d
3846

3947
#===========================================================================
4048
# No PythonOCC dependencies...
4149
#===========================================================================
4250

51+
52+
class assert_isdone(object):
53+
'''
54+
raises an assertion error when IsDone() returns false, with the error
55+
specified in error_statement
56+
'''
57+
def __init__(self, to_check, error_statement):
58+
self.to_check = to_check
59+
self.error_statement = error_statement
60+
61+
def __enter__(self, ):
62+
if self.to_check.IsDone():
63+
pass
64+
else:
65+
raise AssertionError(self.error_statement)
66+
67+
def __exit__(self, assertion_type, value, traceback):
68+
pass
69+
70+
4371
def roundlist(li, n_decimals=3):
4472
return [round(i, n_decimals) for i in li]
4573

@@ -50,30 +78,26 @@ def roundlist(li, n_decimals=3):
5078
TOLERANCE = 1e-6
5179

5280

53-
def get_boundingbox(shape, tol=TOLERANCE, vec=False):
81+
def get_boundingbox(shape, tol=TOLERANCE):
5482
'''
5583
:param shape: TopoDS_Shape such as TopoDS_Face
5684
:param tol: tolerance
5785
:return: xmin, ymin, zmin, xmax, ymax, zmax
5886
'''
5987
bbox = Bnd_Box()
6088
bbox.SetGap(tol)
61-
#BRepBndLib_AddClose(shape, bbox)
62-
tmp = brepbndlib_Add(shape, bbox)
89+
brepbndlib_Add(shape, bbox)
6390
xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get()
64-
if vec is False:
65-
return xmin, ymin, zmin, xmax, ymax, zmax
66-
else:
67-
return gp_Vec(xmin, ymin, zmin), gp_Vec(xmax, ymax, zmax)
91+
return xmin, ymin, zmin, xmax, ymax, zmax
6892

6993

7094
def smooth_pnts(pnts):
7195
smooth = [pnts[0]]
7296
for i in range(1, len(pnts)-1):
7397
prev = pnts[i-1]
7498
this = pnts[i]
75-
next = pnts[i+1]
76-
pt = (prev+this+next) / 3.0
99+
next_pnt = pnts[i+1]
100+
pt = (prev+this+next_pnt) / 3.0
77101
smooth.append(pt)
78102
smooth.append(pnts[-1])
79103
return smooth
@@ -92,8 +116,8 @@ def to_string(_string):
92116
return TCollection_ExtendedString(_string)
93117

94118

95-
def to_tcol_(_list, type):
96-
array = type(1, len(_list)+1)
119+
def to_tcol_(_list, collection_type):
120+
array = collection_type(1, len(_list)+1)
97121
for n, i in enumerate(_list):
98122
array.SetValue(n+1, i)
99123
return array.GetHandle()
@@ -173,24 +197,24 @@ def fix(li, _type):
173197
print("Failed to interpolate the shown points")
174198

175199

176-
def interpolate_points_vectors_to_spline(list_of_points, list_of_vectors, vector_mask=[], tolerance=TOLERANCE):
200+
def interpolate_points_vectors_to_spline(list_of_points, list_of_vectors, vector_mask=None, tolerance=TOLERANCE):
177201
'''
178202
build a curve from a set of points and vectors
179203
the vectors describe the tangent vector at the corresponding point
180204
'''
181205
# GeomAPI_Interpolate is buggy: need to use `fix` in order to
182206
# get the right points in...
183207
assert len(list_of_points) == len(list_of_vectors), 'vector and point list not of same length'
184-
208+
185209
def fix(li, _type):
186210
'''function factory for 1-dimensional TCol* types'''
187211
pts = _type(1, len(li))
188212
for n, i in enumerate(li):
189-
pts.SetValue(n+1,i)
213+
pts.SetValue(n+1, i)
190214
pts.thisown = False
191215
return pts
192216

193-
if not vector_mask == []:
217+
if vector_mask is not None:
194218
assert len(vector_mask) == len(list_of_points), 'length vector mask is not of length points list nor []'
195219
else:
196220
vector_mask = [True for i in range(len(list_of_points))]
@@ -210,20 +234,22 @@ def fix(li, _type):
210234
raise RuntimeError('FAILED TO INTERPOLATE THE POINTS')
211235

212236

213-
def interpolate_points_to_spline_no_tangency(list_of_points, filter=True, closed=False, tolerance=TOLERANCE):
237+
def interpolate_points_to_spline_no_tangency(list_of_points, filter_pts=True, closed=False, tolerance=TOLERANCE):
214238
'''
215239
GeomAPI_Interpolate is buggy: need to use `fix`
216240
in order to get the right points in...
217241
'''
218242
def fix(li, _type):
219243
'''function factory for 1-dimensional TCol* types'''
220244
pts = _type(1, len(li))
221-
for n, i in enumerate(li):
245+
for n, i in enumerate(li):
222246
pts.SetValue(n+1, i)
223247
pts.thisown = False
224248
return pts
225249

226-
list_of_points = filter_points_by_distance(list_of_points, 0.1)
250+
if filter_pts:
251+
list_of_points = filter_points_by_distance(list_of_points, 0.1)
252+
227253
fixed_points = fix(list_of_points, TColgp_HArray1OfPnt)
228254
try:
229255
interp = GeomAPI_Interpolate(fixed_points.GetHandle(), closed, tolerance)
@@ -240,13 +266,11 @@ def fix(li, _type):
240266
#===========================================================================
241267

242268
def random_vec():
243-
import random
244269
x, y, z = [random.uniform(-1, 1) for i in range(3)]
245270
return gp_Vec(x, y, z)
246271

247272

248273
def random_colored_material_aspect():
249-
import random
250274
clrs = [i for i in dir(Graphic3d) if i.startswith('Graphic3d_NOM_')]
251275
color = random.sample(clrs, 1)[0]
252276
print("color", color)
@@ -303,8 +327,8 @@ def point_in_boundingbox(solid, pnt, tolerance=1e-5):
303327
"""
304328
bbox = Bnd_Box()
305329
bbox.SetGap(tolerance)
306-
tmp = brepbndlib_Add(solid, bbox)
307-
return not(bbox.IsOut(pnt))
330+
brepbndlib_Add(solid, bbox)
331+
return not bbox.IsOut(pnt)
308332

309333

310334
def point_in_solid(solid, pnt, tolerance=1e-5):
@@ -375,33 +399,14 @@ def intersect_shape_by_line(topods_shape, line, low_parameter=0.0, hi_parameter=
375399
shape_inter.VParameter(1),
376400
shape_inter.WParameter(1))
377401

378-
#===========================================================================
379-
# --- TRANSFORM ---
380-
#===========================================================================
381-
382-
383-
def translate_topods_from_vector(brep, vec, copy=False):
384-
'''
385-
translate a brep over a vector
386-
@param brep: the Topo_DS to translate
387-
@param vec: the vector defining the translation
388-
@param copy: copies to brep if True
389-
'''
390-
trns = gp_Trsf()
391-
trns.SetTranslation(vec)
392-
brep_trns = BRepBuilderAPI_Transform(brep, trns, copy)
393-
brep_trns.Build()
394-
return brep_trns.Shape()
395-
396402

397-
def normal_vector_from_plane(plane, vec_length=1):
403+
def normal_vector_from_plane(plane, vec_length=1.):
398404
'''
399405
returns a vector normal to the plane of length vec_length
400406
@param plane:
401407
'''
402408
trns = gp_Vec(plane.Axis().Direction())
403-
trns.Normalized() * vec_length
404-
return trns
409+
return trns.Normalized() * vec_length
405410

406411
#===========================================================================
407412
# FIX
@@ -451,21 +456,21 @@ def volume(self):
451456
'''returns the volume of a solid
452457
'''
453458
prop = GProp_GProps()
454-
error = brepgprop_VolumeProperties(self.shape, prop, self.tolerance)
459+
brepgprop_VolumeProperties(self.shape, prop, self.tolerance)
455460
return prop
456461

457462
def surface(self):
458463
'''returns the area of a surface
459464
'''
460465
prop = GProp_GProps()
461-
error = brepgprop_SurfaceProperties(self.shape, prop, self.tolerance)
466+
brepgprop_SurfaceProperties(self.shape, prop, self.tolerance)
462467
return prop
463468

464469
def linear(self):
465470
'''returns the length of a wire or edge
466471
'''
467472
prop = GProp_GProps()
468-
error = brepgprop_LinearProperties(self.shape, prop)
473+
brepgprop_LinearProperties(self.shape, prop)
469474
return prop
470475

471476

@@ -511,14 +516,25 @@ def vertex2pnt(vertex):
511516
return BRep_Tool.Pnt(vertex)
512517

513518

519+
def adapt_edge_to_curve(edg):
520+
'''
521+
returns a curve adaptor from an edge
522+
@param edg: TopoDS_Edge
523+
'''
524+
return BRepAdaptor_Curve(edg)
525+
526+
527+
def adapt_edge_to_hcurve(edg):
528+
c = BRepAdaptor_HCurve()
529+
c.ChangeCurve().Initialize(edg)
530+
return c
531+
532+
514533
def to_adaptor_3d(curveType):
515534
'''
516535
abstract curve like type into an adaptor3d
517536
@param curveType:
518537
'''
519-
from OCC.BRepAdaptor import BRepAdaptor_CompCurve
520-
from OCC.GeomAdaptor import GeomAdaptor_Curve
521-
from OCC.Geom import Geom_Curve
522538
if isinstance(curveType, TopoDS_Wire):
523539
return BRepAdaptor_CompCurve(curveType)
524540
elif isinstance(curveType, TopoDS_Edge):
@@ -539,7 +555,6 @@ def project_point_on_curve(crv, pnt):
539555
crv = adapt_edge_to_curve(crv).Curve().Curve()
540556
else:
541557
raise NotImplementedError('expected a TopoDS_Edge...')
542-
from OCC.GeomAPI import GeomAPI_ProjectPointOnCurve
543558
rrr = GeomAPI_ProjectPointOnCurve(pnt, crv)
544559
return rrr.LowerDistanceParameter(), rrr.NearestPoint()
545560

0 commit comments

Comments
 (0)