From a94cbca11312574b3bde3008acbd66dedc51d977 Mon Sep 17 00:00:00 2001 From: Justin Buchanan Date: Sun, 18 Nov 2018 21:04:11 -0800 Subject: [PATCH 1/2] add test case that loads an invalid STEP file --- tests/TestImporters.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/TestImporters.py b/tests/TestImporters.py index c455506e..e2b2b643 100644 --- a/tests/TestImporters.py +++ b/tests/TestImporters.py @@ -48,6 +48,16 @@ class TestImporters(BaseTest): """ self.importBox(importers.ImportTypes.STEP, OUTDIR + "/tempSTEP.step") + def testInvalidSTEP(self): + """ + Attempting to load an invalid STEP file should throw an exception, but + not segfault. + """ + tmpfile = OUTDIR + "/badSTEP.step" + with open(tmpfile, 'w') as f: f.write("invalid STEP file") + with self.assertRaises(ValueError): + importers.importShape(importers.ImportTypes.STEP, tmpfile) + if __name__ == '__main__': import unittest From 8fb1e7ad2d103a6bac094f55881ceb0dfa4b30ce Mon Sep 17 00:00:00 2001 From: Justin Buchanan Date: Mon, 19 Nov 2018 00:08:24 -0800 Subject: [PATCH 2/2] check status code from STEPControl_Reader.ReadFile() Otherwise subsequent calls may cause a segfault. See #4 --- cadquery/occ_impl/importers.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/cadquery/occ_impl/importers.py b/cadquery/occ_impl/importers.py index ec1e2a16..fa3bdb4c 100644 --- a/cadquery/occ_impl/importers.py +++ b/cadquery/occ_impl/importers.py @@ -8,6 +8,7 @@ import urllib as urlreader import tempfile from OCC.STEPControl import STEPControl_Reader +import OCC.IFSelect class ImportTypes: @@ -38,23 +39,22 @@ def importStep(fileName): :param fileName: The path and name of the STEP file to be imported """ # Now read and return the shape - try: - reader = STEPControl_Reader() - reader.ReadFile(fileName) - reader.TransferRoot() + reader = STEPControl_Reader() + readStatus = reader.ReadFile(fileName) + if readStatus != OCC.IFSelect.IFSelect_RetDone: + raise ValueError("STEP File could not be loaded") + reader.TransferRoot() - occ_shapes = [] - for i in range(reader.NbShapes()): - occ_shapes.append(reader.Shape(i + 1)) + occ_shapes = [] + for i in range(reader.NbShapes()): + occ_shapes.append(reader.Shape(i + 1)) - # Make sure that we extract all the solids - solids = [] - for shape in occ_shapes: - solids.append(Shape.cast(shape)) + # Make sure that we extract all the solids + solids = [] + for shape in occ_shapes: + solids.append(Shape.cast(shape)) - return cadquery.Workplane("XY").newObject(solids) - except: - raise ValueError("STEP File Could not be loaded") + return cadquery.Workplane("XY").newObject(solids) # Loads a STEP file from an URL into a CQ.Workplane object