From 2363e2c41eb797b154d40a4058771eea299ad1e3 Mon Sep 17 00:00:00 2001 From: Nicola Demo Date: Fri, 20 Mar 2020 19:33:37 +0100 Subject: [PATCH 1/3] porting to python-occ==7.4 --- .travis.yml | 25 +++--------- pygem/igeshandler.py | 4 +- pygem/nurbshandler.py | 42 ++++++++++---------- pygem/params/ffdparams.py | 80 ++++++++++++++++++++------------------- pygem/stephandler.py | 8 ++-- tests/test_ffdparams.py | 44 ++++++++++----------- tests/test_igeshandler.py | 8 ++-- tests/test_stephandler.py | 8 ++-- 8 files changed, 102 insertions(+), 117 deletions(-) diff --git a/.travis.yml b/.travis.yml index e6c08d4..6910868 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,17 +8,11 @@ service: docker matrix: include: - os: linux - python: 2.7 - env: TOXENV=py27 - - os: linux - python: 3.5 - env: TOXENV=py35 - - os: osx - language: generic - env: TOXENV=py27 + python: 3.7 + env: TOXENV=py37 - os: osx language: generic - env: TOXENV=py35 + env: TOXENV=py37 before_script: - "export DISPLAY=:99.0" @@ -51,22 +45,13 @@ install: - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then conda create --yes -n test python=$TRAVIS_PYTHON_VERSION; else - if [[ "$TOXENV" == "py27" ]]; then - conda create --yes -n test python="2.7"; - else - conda create --yes -n test python="3.5"; - fi + conda create --yes -n test python="3.7"; fi - source activate test - echo $LD_LIBRARY_PATH - echo $DYLD_LIBRARY_PATH - echo $PATH - - conda install "numpy>=1.12" scipy matplotlib vtk nose setuptools coveralls - - if [[ "$TOXENV" == "py27" ]]; then - conda install --yes -c conda-forge -c dlr-sc -c pythonocc -c oce pythonocc-core=0.17 python=2.7; - else - conda install --yes -c conda-forge -c dlr-sc -c pythonocc -c oce pythonocc-core=0.17 python=3.5; - fi + - conda install --yes -c conda-forge pythonocc-core=7.4.0 numpy scipy matplotlib vtk nose setuptools coveralls python=3.7 - python setup.py install script: diff --git a/pygem/igeshandler.py b/pygem/igeshandler.py index e187a3d..8904aef 100644 --- a/pygem/igeshandler.py +++ b/pygem/igeshandler.py @@ -1,9 +1,9 @@ """ Derived module from filehandler.py to handle iges and igs files. """ -from OCC.IGESControl import (IGESControl_Reader, IGESControl_Writer, +from OCC.Core.IGESControl import (IGESControl_Reader, IGESControl_Writer, IGESControl_Controller_Init) -from OCC.IFSelect import IFSelect_RetDone +from OCC.Core.IFSelect import IFSelect_RetDone from pygem.nurbshandler import NurbsHandler diff --git a/pygem/nurbshandler.py b/pygem/nurbshandler.py index 7076dad..620f61c 100644 --- a/pygem/nurbshandler.py +++ b/pygem/nurbshandler.py @@ -6,25 +6,25 @@ """ import os import numpy as np -from OCC.BRep import BRep_Tool, BRep_Builder, BRep_Tool_Curve -from OCC.BRepAlgo import brepalgo_IsValid -from OCC.BRepBuilderAPI import ( +from OCC.Core.BRep import BRep_Tool, BRep_Builder, BRep_Tool_Curve +from OCC.Core.BRepAlgo import brepalgo_IsValid +from OCC.Core.BRepBuilderAPI import ( BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeFace, BRepBuilderAPI_NurbsConvert, BRepBuilderAPI_MakeWire, BRepBuilderAPI_Sewing) -from OCC.BRepOffsetAPI import BRepOffsetAPI_FindContigousEdges +from OCC.Core.BRepOffsetAPI import BRepOffsetAPI_FindContigousEdges from OCC.Display.SimpleGui import init_display -from OCC.GeomConvert import (geomconvert_SurfaceToBSplineSurface, +from OCC.Core.GeomConvert import (geomconvert_SurfaceToBSplineSurface, geomconvert_CurveToBSplineCurve) -from OCC.gp import gp_Pnt, gp_XYZ -from OCC.Precision import precision_Confusion -from OCC.ShapeAnalysis import ShapeAnalysis_WireOrder -from OCC.ShapeFix import ShapeFix_ShapeTolerance, ShapeFix_Shell -from OCC.StlAPI import StlAPI_Writer -from OCC.TColgp import TColgp_Array1OfPnt, TColgp_Array2OfPnt -from OCC.TopAbs import (TopAbs_FACE, TopAbs_EDGE, TopAbs_WIRE, TopAbs_FORWARD, +from OCC.Core.gp import gp_Pnt, gp_XYZ +from OCC.Core.Precision import precision_Confusion +from OCC.Core.ShapeAnalysis import ShapeAnalysis_WireOrder +from OCC.Core.ShapeFix import ShapeFix_ShapeTolerance, ShapeFix_Shell +from OCC.Core.StlAPI import StlAPI_Writer +from OCC.Core.TColgp import TColgp_Array1OfPnt, TColgp_Array2OfPnt +from OCC.Core.TopAbs import (TopAbs_FACE, TopAbs_EDGE, TopAbs_WIRE, TopAbs_FORWARD, TopAbs_SHELL) -from OCC.TopExp import TopExp_Explorer, topexp -from OCC.TopoDS import (topods_Face, TopoDS_Compound, topods_Shell, topods_Edge, +from OCC.Core.TopExp import TopExp_Explorer, topexp +from OCC.Core.TopoDS import (topods_Face, TopoDS_Compound, topods_Shell, topods_Edge, topods_Wire, topods, TopoDS_Shape) from matplotlib import pyplot from mpl_toolkits import mplot3d @@ -112,7 +112,7 @@ def parse(self, filename): bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face) # openCascade object - occ_face = bspline_face.GetObject() + occ_face = bspline_face # extract the Control Points of each face n_poles_u = occ_face.NbUPoles() @@ -185,7 +185,7 @@ def write(self, mesh_points, filename, tolerance=None): face_aux = topods_Face(nurbs_face) brep_face = BRep_Tool.Surface(topods_Face(nurbs_face)) bspline_face = geomconvert_SurfaceToBSplineSurface(brep_face) - occ_face = bspline_face.GetObject() + occ_face = bspline_face n_poles_u = occ_face.NbUPoles() n_poles_v = occ_face.NbVPoles() @@ -205,7 +205,7 @@ def write(self, mesh_points, filename, tolerance=None): # construct the deformed wire for the trimmed surfaces wire_maker = BRepBuilderAPI_MakeWire() tol = ShapeFix_ShapeTolerance() - brep = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), + brep = BRepBuilderAPI_MakeFace(occ_face, self.tolerance).Face() brep_face = BRep_Tool.Surface(brep) @@ -228,7 +228,7 @@ def write(self, mesh_points, filename, tolerance=None): wire = wire_maker.Wire() # trimming the surfaces - brep_surf = BRepBuilderAPI_MakeFace(occ_face.GetHandle(), + brep_surf = BRepBuilderAPI_MakeFace(occ_face, wire).Shape() compound_builder.Add(compound, brep_surf) n_faces += 1 @@ -303,7 +303,7 @@ def parse_face(topo_face): h_geom_edge = BRep_Tool_Curve( topods_Edge(bspline_tshape_edge))[0] h_bspline_edge = geomconvert_CurveToBSplineCurve(h_geom_edge) - bspline_geom_edge = h_bspline_edge.GetObject() + bspline_geom_edge = h_bspline_edge nb_poles = bspline_geom_edge.NbPoles() @@ -334,7 +334,7 @@ def parse_face(topo_face): nurbs_face = nurbs_converter.Shape() h_geomsurface = BRep_Tool.Surface(topods.Face(nurbs_face)) h_bsurface = geomconvert_SurfaceToBSplineSurface(h_geomsurface) - bsurface = h_bsurface.GetObject() + bsurface = h_bsurface # get access to control points (poles) nb_u = bsurface.NbUPoles() @@ -435,7 +435,7 @@ def write_edge(points_edge, topo_edge): topo_curve = topods_Edge(nurbs_curve) h_geomcurve = BRep_Tool.Curve(topo_curve)[0] h_bcurve = geomconvert_CurveToBSplineCurve(h_geomcurve) - bspline_edge_curve = h_bcurve.GetObject() + bspline_edge_curve = h_bcurve # Edge geometric properties nb_cpt = bspline_edge_curve.NbPoles() diff --git a/pygem/params/ffdparams.py b/pygem/params/ffdparams.py index 67abba8..c962ea5 100644 --- a/pygem/params/ffdparams.py +++ b/pygem/params/ffdparams.py @@ -8,9 +8,9 @@ import ConfigParser as configparser import os import numpy as np -from OCC.Bnd import Bnd_Box -from OCC.BRepBndLib import brepbndlib_Add -from OCC.BRepMesh import BRepMesh_IncrementalMesh +#from OCC.Bnd import Bnd_Box +#from OCC.BRepBndLib import brepbndlib_Add +#from OCC.BRepMesh import BRepMesh_IncrementalMesh import vtk import pygem.affine as at @@ -482,41 +482,45 @@ def save_points(self, filename, write_deformed=True): writer.SetInputData(data) writer.Write() - def build_bounding_box(self, - shape, - tol=1e-6, - triangulate=False, - triangulate_tol=1e-1): - """ - Builds a bounding box around the given shape. All parameters are set to - match the computed box, the deformed FFD points are reset. - - :param shape: the shape to compute the bounding box. - :type shape: TopoDS_Shape or its subclass - :param float tol: tolerance of the computed bounding box. - :param bool triangulate: if True, shape is triangulated before the - bouning box creation. - :param float triangulate_tol: tolerance of triangulation (size of - created triangles). - - .. note:: - - Every UV-Surface has to be rectangular. When a solid is created - surfaces are trimmed. The trimmed part, however, is still saved - inside a file. It is just *invisible* when drawn in a program. - """ - bbox = Bnd_Box() - bbox.SetGap(tol) - if triangulate: - BRepMesh_IncrementalMesh(shape, triangulate_tol) - brepbndlib_Add(shape, bbox, triangulate) - xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() - min_xyz = np.array([xmin, ymin, zmin]) - max_xyz = np.array([xmax, ymax, zmax]) - - self.box_origin = min_xyz - self.box_length = max_xyz - min_xyz - self.reset_deformation() + +# TODO +# to reimplement avoiding OCC +# +# def build_bounding_box(self, +# shape, +# tol=1e-6, +# triangulate=False, +# triangulate_tol=1e-1): +# """ +# Builds a bounding box around the given shape. All parameters are set to +# match the computed box, the deformed FFD points are reset. +# +# :param shape: the shape to compute the bounding box. +# :type shape: TopoDS_Shape or its subclass +# :param float tol: tolerance of the computed bounding box. +# :param bool triangulate: if True, shape is triangulated before the +# bouning box creation. +# :param float triangulate_tol: tolerance of triangulation (size of +# created triangles). +# +# .. note:: +# +# Every UV-Surface has to be rectangular. When a solid is created +# surfaces are trimmed. The trimmed part, however, is still saved +# inside a file. It is just *invisible* when drawn in a program. +# """ +# bbox = Bnd_Box() +# bbox.SetGap(tol) +# if triangulate: +# BRepMesh_IncrementalMesh(shape, triangulate_tol) +# brepbndlib_Add(shape, bbox, triangulate) +# xmin, ymin, zmin, xmax, ymax, zmax = bbox.Get() +# min_xyz = np.array([xmin, ymin, zmin]) +# max_xyz = np.array([xmax, ymax, zmax]) +# +# self.box_origin = min_xyz +# self.box_length = max_xyz - min_xyz +# self.reset_deformation() def reset_deformation(self): """ diff --git a/pygem/stephandler.py b/pygem/stephandler.py index 2f9f89e..e9b7f80 100644 --- a/pygem/stephandler.py +++ b/pygem/stephandler.py @@ -1,10 +1,10 @@ """ Derived module from nurbshandler.py to handle step and stp files. """ -from OCC.IFSelect import IFSelect_RetDone -from OCC.Interface import Interface_Static_SetCVal -from OCC.STEPControl import STEPControl_Writer, STEPControl_Reader -from OCC.STEPControl import STEPControl_AsIs +from OCC.Core.IFSelect import IFSelect_RetDone +from OCC.Core.Interface import Interface_Static_SetCVal +from OCC.Core.STEPControl import STEPControl_Writer, STEPControl_Reader +from OCC.Core.STEPControl import STEPControl_AsIs from pygem.nurbshandler import NurbsHandler diff --git a/tests/test_ffdparams.py b/tests/test_ffdparams.py index daa31ab..b9c9c8d 100644 --- a/tests/test_ffdparams.py +++ b/tests/test_ffdparams.py @@ -3,10 +3,6 @@ from unittest import TestCase import numpy as np -from OCC.BRepAlgoAPI import BRepAlgoAPI_Cut -from OCC.BRepPrimAPI import BRepPrimAPI_MakeSphere, BRepPrimAPI_MakeBox -from OCC.gp import gp_Pnt - from pygem.params import FFDParameters @@ -357,26 +353,26 @@ def test_print(self): params = FFDParameters(n_control_points=[3, 2, 2]) print(params) - def test_build_bounding_box_1(self): - origin = np.array([0., 0., 0.]) - tops = np.array([1., 1., 1.]) - cube = BRepPrimAPI_MakeBox(*tops).Shape() - params = FFDParameters() - params.build_bounding_box(cube) - - np.testing.assert_array_almost_equal(params.box_length, tops, decimal=5) - - def test_build_bounding_box_2(self): - origin = np.array([0., 0., 0.]) - tops = np.array([1., 1., 1.]) - cube = BRepPrimAPI_MakeBox(*tops).Shape() - params = FFDParameters() - params.build_bounding_box(cube) - - expected_matrix = np.array([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.], - [0., 0., 1.]]) - np.testing.assert_almost_equal( - params.position_vertices, expected_matrix, decimal=5) +# def test_build_bounding_box_1(self): +# origin = np.array([0., 0., 0.]) +# tops = np.array([1., 1., 1.]) +# cube = BRepPrimAPI_MakeBox(*tops).Shape() +# params = FFDParameters() +# params.build_bounding_box(cube) +# +# np.testing.assert_array_almost_equal(params.box_length, tops, decimal=5) +# +# def test_build_bounding_box_2(self): +# origin = np.array([0., 0., 0.]) +# tops = np.array([1., 1., 1.]) +# cube = BRepPrimAPI_MakeBox(*tops).Shape() +# params = FFDParameters() +# params.build_bounding_box(cube) +# +# expected_matrix = np.array([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.], +# [0., 0., 1.]]) +# np.testing.assert_almost_equal( +# params.position_vertices, expected_matrix, decimal=5) def test_set_position_of_vertices(self): expected_matrix = np.array([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.], diff --git a/tests/test_igeshandler.py b/tests/test_igeshandler.py index 39fe9a0..b651b30 100644 --- a/tests/test_igeshandler.py +++ b/tests/test_igeshandler.py @@ -2,8 +2,8 @@ from unittest import TestCase import numpy as np -from OCC.TopoDS import TopoDS_Shape -from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox +from OCC.Core.TopoDS import TopoDS_Shape, TopoDS_Compound +from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox import pygem.igeshandler as ih @@ -195,13 +195,13 @@ def test_iges_load_shape_correct_iges(self): iges_handler = ih.IgesHandler() shape = iges_handler.load_shape_from_file( 'tests/test_datasets/test_pipe.iges') - self.assertEqual(type(shape), TopoDS_Shape) + self.assertEqual(type(shape), TopoDS_Compound) def test_iges_load_shape_correct_igs(self): iges_handler = ih.IgesHandler() shape = iges_handler.load_shape_from_file( 'tests/test_datasets/test_pipe.igs') - self.assertEqual(type(shape), TopoDS_Shape) + self.assertEqual(type(shape), TopoDS_Compound) def test_iges_write_shape_to_file_raises_wrong_type(self): iges_handler = ih.IgesHandler() diff --git a/tests/test_stephandler.py b/tests/test_stephandler.py index b195668..5b9355e 100644 --- a/tests/test_stephandler.py +++ b/tests/test_stephandler.py @@ -2,8 +2,8 @@ from unittest import TestCase import numpy as np -from OCC.TopoDS import TopoDS_Shape -from OCC.BRepPrimAPI import BRepPrimAPI_MakeBox +from OCC.Core.TopoDS import TopoDS_Shape, TopoDS_Compound +from OCC.Core.BRepPrimAPI import BRepPrimAPI_MakeBox import pygem.stephandler as sh @@ -195,13 +195,13 @@ def test_step_load_shape_correct_step(self): step_handler = sh.StepHandler() shape = step_handler.load_shape_from_file( 'tests/test_datasets/test_pipe.step') - self.assertEqual(type(shape), TopoDS_Shape) + self.assertEqual(type(shape), TopoDS_Compound) def test_step_load_shape_correct_stp(self): step_handler = sh.StepHandler() shape = step_handler.load_shape_from_file( 'tests/test_datasets/test_pipe.stp') - self.assertEqual(type(shape), TopoDS_Shape) + self.assertEqual(type(shape), TopoDS_Compound) def test_step_write_shape_to_file_raises_wrong_type(self): step_handler = sh.StepHandler() From 383e78688600811a87e2dd3bfee6fd56725831ae Mon Sep 17 00:00:00 2001 From: Nicola Demo Date: Sat, 21 Mar 2020 16:44:12 +0100 Subject: [PATCH 2/3] Fix bug on STL writer --- pygem/nurbshandler.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pygem/nurbshandler.py b/pygem/nurbshandler.py index 620f61c..c19e26b 100644 --- a/pygem/nurbshandler.py +++ b/pygem/nurbshandler.py @@ -7,6 +7,7 @@ import os import numpy as np from OCC.Core.BRep import BRep_Tool, BRep_Builder, BRep_Tool_Curve +from OCC.Core.BRepMesh import BRepMesh_IncrementalMesh from OCC.Core.BRepAlgo import brepalgo_IsValid from OCC.Core.BRepBuilderAPI import ( BRepBuilderAPI_MakeEdge, BRepBuilderAPI_MakeFace, @@ -734,7 +735,12 @@ def plot(self, plot_file=None, save_fig=False): stl_writer = StlAPI_Writer() # Do not switch SetASCIIMode() from False to True. stl_writer.SetASCIIMode(False) - stl_writer.Write(shape, 'aux_figure.stl') + + # Necessary to write to STL [to check] + stl_mesh = BRepMesh_IncrementalMesh(shape, 0.01) + stl_mesh.Perform() + + f = stl_writer.Write(shape, 'aux_figure.stl') # Create a new plot figure = pyplot.figure() From 6ee89b7973565b1a5c516e54fd9bbb8a5347457e Mon Sep 17 00:00:00 2001 From: Nicola Demo Date: Mon, 23 Mar 2020 12:12:38 +0100 Subject: [PATCH 3/3] Test new dist for travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 6910868..c02ef81 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ sudo: true -dist: trusty +dist: xenial language: python