Skip to content

Commit 7ebabb3

Browse files
authored
Merge pull request #103 from mtezzele/writer
documentation files and Controller_Init in igeshandler
2 parents ad8626c + c29618f commit 7ebabb3

File tree

5 files changed

+66
-80
lines changed

5 files changed

+66
-80
lines changed

docs/source/code.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Code Documentation
1515
stlhandler
1616
vtkhandler
1717
unvhandler
18+
nurbshandler
1819
igeshandler
20+
stephandler
1921
utils
2022
gui
2123

docs/source/nurbshandler.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Nurbshandler
2+
=================
3+
4+
.. automodule:: pygem.nurbshandler
5+
6+
.. autoclass:: NurbsHandler
7+
:members:
8+
:private-members:
9+
:special-members:

docs/source/stephandler.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Stephandler
2+
=================
3+
4+
.. automodule:: pygem.stephandler
5+
6+
.. autoclass:: StepHandler
7+
:members:
8+
:private-members:
9+
:special-members:

pygem/igeshandler.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
Derived module from filehandler.py to handle iges and igs files.
33
"""
44

5-
from OCC.IGESControl import (IGESControl_Reader, IGESControl_Writer)
6-
5+
from OCC.IGESControl import (IGESControl_Reader, IGESControl_Writer, IGESControl_Controller_Init)
76
from pygem.nurbshandler import NurbsHandler
87

98

@@ -17,15 +16,15 @@ class IgesHandler(NurbsHandler):
1716
It is equal to ['.iges', '.igs'].
1817
:cvar list control_point_position: index of the first NURBS control point (or pole)
1918
of each face of the iges file.
20-
:cvar float tolerance: tolerance for the construction of the faces and wires
19+
:cvar float tolerance: tolerance for the construction of the faces and wires
2120
in the write function. Default value is 1e-6.
2221
:cvar TopoDS_Shape shape: shape meant for modification.
23-
22+
2423
.. warning::
2524
2625
- For non trivial geometries it could be necessary to increase the tolerance.
2726
Linking edges into a single wire and then trimming the surface with the wire
28-
can be hard for the software, especially when the starting CAD has not been
27+
can be hard for the software, especially when the starting CAD has not been
2928
made for analysis but for design purposes.
3029
"""
3130

@@ -61,6 +60,7 @@ def write_shape_to_file(self, shape, filename):
6160
"""
6261
self._check_filename_type(filename)
6362
self._check_extension(filename)
63+
IGESControl_Controller_Init()
6464
writer = IGESControl_Writer()
6565
writer.AddShape(shape)
6666
writer.Write(filename)

pygem/nurbshandler.py

Lines changed: 41 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@
44
File handling operations (reading/writing) must be implemented in derived classes.
55
"""
66
import os
7-
8-
import OCC.TopoDS
97
import numpy as np
8+
import OCC.TopoDS
109
from OCC.BRep import (BRep_Tool, BRep_Builder)
11-
from OCC.BRepBuilderAPI import (
12-
BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeFace
13-
)
14-
from OCC.BRepBuilderAPI import (
15-
BRepBuilderAPI_NurbsConvert, BRepBuilderAPI_MakeWire
16-
)
10+
from OCC.BRepBuilderAPI import (BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeFace, \
11+
BRepBuilderAPI_NurbsConvert, BRepBuilderAPI_MakeWire)
1712
from OCC.Display.SimpleGui import init_display
1813
from OCC.GeomConvert import geomconvert_SurfaceToBSplineSurface
1914
from OCC.ShapeFix import ShapeFix_ShapeTolerance
@@ -24,7 +19,6 @@
2419
from matplotlib import pyplot
2520
from mpl_toolkits import mplot3d
2621
from stl import mesh
27-
2822
import pygem.filehandler as fh
2923

3024

@@ -62,20 +56,16 @@ def _check_infile_instantiation(self):
6256
6357
"""
6458
if not self.shape or not self.infile:
65-
raise RuntimeError(
66-
"You can not write a file without having parsed one."
67-
)
59+
raise RuntimeError("You can not write a file without having parsed one.")
6860

6961
def load_shape_from_file(self, filename):
7062
"""
7163
Abstract method to load a specific file as a shape.
7264
7365
Not implemented, it has to be implemented in subclasses.
7466
"""
75-
raise NotImplementedError(
76-
"Subclass must implement abstract method " +\
77-
self.__class__.__name__ + ".load_shape_from_file"
78-
)
67+
raise NotImplementedError("Subclass must implement abstract method " +\
68+
self.__class__.__name__ + ".load_shape_from_file")
7969

8070
def parse(self, filename):
8171
"""
@@ -114,30 +104,22 @@ def parse(self, filename):
114104
# extract the Control Points of each face
115105
n_poles_u = occ_face.NbUPoles()
116106
n_poles_v = occ_face.NbVPoles()
117-
control_polygon_coordinates = np.zeros(
118-
shape=(n_poles_u * n_poles_v, 3)
119-
)
107+
control_polygon_coordinates = np.zeros(\
108+
shape=(n_poles_u * n_poles_v, 3))
120109

121110
# cycle over the poles to get their coordinates
122111
i = 0
123112
for pole_u_direction in xrange(n_poles_u):
124113
for pole_v_direction in xrange(n_poles_v):
125-
control_point_coordinates = occ_face.Pole(
126-
pole_u_direction + 1, pole_v_direction + 1
127-
)
128-
control_polygon_coordinates[i, :] = [
129-
control_point_coordinates.X(),
130-
control_point_coordinates.Y(),
131-
control_point_coordinates.Z()
132-
]
114+
control_point_coordinates = occ_face.Pole(\
115+
pole_u_direction + 1, pole_v_direction + 1)
116+
control_polygon_coordinates[i, :] = [control_point_coordinates.X(),\
117+
control_point_coordinates.Y(),\
118+
control_point_coordinates.Z()]
133119
i += 1
134120
# pushing the control points coordinates to the mesh_points array (used for FFD)
135-
mesh_points = np.append(
136-
mesh_points, control_polygon_coordinates, axis=0
137-
)
138-
control_point_position.append(
139-
control_point_position[-1] + n_poles_u * n_poles_v
140-
)
121+
mesh_points = np.append(mesh_points, control_polygon_coordinates, axis=0)
122+
control_point_position.append(control_point_position[-1] + n_poles_u * n_poles_v)
141123

142124
n_faces += 1
143125
faces_explorer.Next()
@@ -192,23 +174,17 @@ def write(self, mesh_points, filename, tolerance=None):
192174
i = 0
193175
for pole_u_direction in xrange(n_poles_u):
194176
for pole_v_direction in xrange(n_poles_v):
195-
control_point_coordinates = mesh_points[
196-
i + control_point_position[n_faces], :
197-
]
177+
control_point_coordinates = mesh_points[i + control_point_position[n_faces], :]
198178
point_xyz = gp_XYZ(*control_point_coordinates)
199179

200180
gp_point = gp_Pnt(point_xyz)
201-
occ_face.SetPole(
202-
pole_u_direction + 1, pole_v_direction + 1, gp_point
203-
)
181+
occ_face.SetPole(pole_u_direction + 1, pole_v_direction + 1, gp_point)
204182
i += 1
205183

206184
# construct the deformed wire for the trimmed surfaces
207185
wire_maker = BRepBuilderAPI_MakeWire()
208186
tol = ShapeFix_ShapeTolerance()
209-
brep = BRepBuilderAPI_MakeFace(
210-
occ_face.GetHandle(), self.tolerance
211-
).Face()
187+
brep = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), self.tolerance).Face()
212188
brep_face = BRep_Tool.Surface(brep)
213189

214190
# cycle on the edges
@@ -218,9 +194,8 @@ def write(self, mesh_points, filename, tolerance=None):
218194
# edge in the (u,v) coordinates
219195
edge_uv_coordinates = BRep_Tool.CurveOnSurface(edge, face_aux)
220196
# evaluating the new edge: same (u,v) coordinates, but different (x,y,x) ones
221-
edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge(
222-
edge_uv_coordinates[0], brep_face
223-
)
197+
edge_phis_coordinates_aux = BRepBuilderAPI_MakeEdge(\
198+
edge_uv_coordinates[0], brep_face)
224199
edge_phis_coordinates = edge_phis_coordinates_aux.Edge()
225200
tol.SetTolerance(edge_phis_coordinates, self.tolerance)
226201
wire_maker.Add(edge_phis_coordinates)
@@ -230,8 +205,7 @@ def write(self, mesh_points, filename, tolerance=None):
230205
wire = wire_maker.Wire()
231206

232207
# trimming the surfaces
233-
brep_surf = BRepBuilderAPI_MakeFace(
234-
occ_face.GetHandle(), wire).Shape()
208+
brep_surf = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), wire).Shape()
235209
compound_builder.Add(compound, brep_surf)
236210
n_faces += 1
237211
faces_explorer.Next()
@@ -243,10 +217,9 @@ def write_shape_to_file(self, shape, filename):
243217
244218
Not implemented, it has to be implemented in subclasses.
245219
"""
246-
raise NotImplementedError(
220+
raise NotImplementedError(\
247221
"Subclass must implement abstract method " +\
248-
self.__class__.__name__ + ".write_shape_to_file"
249-
)
222+
self.__class__.__name__ + ".write_shape_to_file")
250223

251224
def plot(self, plot_file=None, save_fig=False):
252225
"""
@@ -277,35 +250,28 @@ def plot(self, plot_file=None, save_fig=False):
277250
# Load the STL files and add the vectors to the plot
278251
stl_mesh = mesh.Mesh.from_file('aux_figure.stl')
279252
os.remove('aux_figure.stl')
280-
axes.add_collection3d(
281-
mplot3d.art3d.Poly3DCollection(stl_mesh.vectors / 1000)
282-
)
253+
axes.add_collection3d(mplot3d.art3d.Poly3DCollection(stl_mesh.vectors / 1000))
283254

284255
# Get the limits of the axis and center the geometry
285-
max_dim = np.array([
286-
np.max(stl_mesh.vectors[:, :, 0]) / 1000,
287-
np.max(stl_mesh.vectors[:, :, 1]) / 1000,
288-
np.max(stl_mesh.vectors[:, :, 2]) / 1000
289-
])
290-
min_dim = np.array([
291-
np.min(stl_mesh.vectors[:, :, 0]) / 1000,
292-
np.min(stl_mesh.vectors[:, :, 1]) / 1000,
293-
np.min(stl_mesh.vectors[:, :, 2]) / 1000
294-
])
256+
max_dim = np.array([\
257+
np.max(stl_mesh.vectors[:, :, 0]) / 1000,\
258+
np.max(stl_mesh.vectors[:, :, 1]) / 1000,\
259+
np.max(stl_mesh.vectors[:, :, 2]) / 1000])
260+
min_dim = np.array([\
261+
np.min(stl_mesh.vectors[:, :, 0]) / 1000,\
262+
np.min(stl_mesh.vectors[:, :, 1]) / 1000,\
263+
np.min(stl_mesh.vectors[:, :, 2]) / 1000])
295264

296265
max_lenght = np.max(max_dim - min_dim)
297-
axes.set_xlim(
298-
-.6 * max_lenght + (max_dim[0] + min_dim[0]) / 2,
299-
.6 * max_lenght + (max_dim[0] + min_dim[0]) / 2
300-
)
301-
axes.set_ylim(
302-
-.6 * max_lenght + (max_dim[1] + min_dim[1]) / 2,
303-
.6 * max_lenght + (max_dim[1] + min_dim[1]) / 2
304-
)
305-
axes.set_zlim(
306-
-.6 * max_lenght + (max_dim[2] + min_dim[2]) / 2,
307-
.6 * max_lenght + (max_dim[2] + min_dim[2]) / 2
308-
)
266+
axes.set_xlim(\
267+
-.6 * max_lenght + (max_dim[0] + min_dim[0]) / 2,\
268+
.6 * max_lenght + (max_dim[0] + min_dim[0]) / 2)
269+
axes.set_ylim(\
270+
-.6 * max_lenght + (max_dim[1] + min_dim[1]) / 2,\
271+
.6 * max_lenght + (max_dim[1] + min_dim[1]) / 2)
272+
axes.set_zlim(\
273+
-.6 * max_lenght + (max_dim[2] + min_dim[2]) / 2,\
274+
.6 * max_lenght + (max_dim[2] + min_dim[2]) / 2)
309275

310276
# Show the plot to the screen
311277
if not save_fig:

0 commit comments

Comments
 (0)