2014-08-18 14:45:02 -04:00
|
|
|
"""
|
|
|
|
Tests file importers such as STEP
|
|
|
|
"""
|
2017-09-17 00:57:12 +02:00
|
|
|
# core modules
|
2018-07-15 15:04:06 +02:00
|
|
|
import tempfile
|
2018-11-30 18:08:03 -08:00
|
|
|
import os
|
2014-08-18 14:45:02 -04:00
|
|
|
|
2020-06-02 18:24:36 +02:00
|
|
|
from cadquery import importers, Workplane
|
2014-08-18 14:45:02 -04:00
|
|
|
from tests import BaseTest
|
|
|
|
|
2017-09-17 00:57:12 +02:00
|
|
|
# where unit test output will be saved
|
2018-07-15 15:04:06 +02:00
|
|
|
OUTDIR = tempfile.gettempdir()
|
2014-08-18 14:45:02 -04:00
|
|
|
|
2020-06-01 14:13:52 +02:00
|
|
|
# test data directory
|
|
|
|
testdataDir = os.path.join(os.path.dirname(__file__), "testdata")
|
2014-08-18 14:45:02 -04:00
|
|
|
|
2020-06-01 16:21:20 +02:00
|
|
|
|
2014-12-11 20:42:41 -05:00
|
|
|
class TestImporters(BaseTest):
|
|
|
|
def importBox(self, importType, fileName):
|
|
|
|
"""
|
|
|
|
Exports a simple box to a STEP file and then imports it again
|
|
|
|
:param importType: The type of file we're importing (STEP, STL, etc)
|
|
|
|
:param fileName: The path and name of the file to write to
|
|
|
|
"""
|
2017-09-17 00:57:12 +02:00
|
|
|
# We're importing a STEP file
|
2014-12-11 20:42:41 -05:00
|
|
|
if importType == importers.ImportTypes.STEP:
|
2017-09-17 00:57:12 +02:00
|
|
|
# We first need to build a simple shape to export
|
2014-12-11 20:42:41 -05:00
|
|
|
shape = Workplane("XY").box(1, 2, 3).val()
|
|
|
|
|
2017-09-17 00:57:12 +02:00
|
|
|
# Export the shape to a temporary file
|
2014-12-11 20:42:41 -05:00
|
|
|
shape.exportStep(fileName)
|
|
|
|
|
|
|
|
# Reimport the shape from the new STEP file
|
2017-09-17 00:57:12 +02:00
|
|
|
importedShape = importers.importShape(importType, fileName)
|
2014-12-11 20:42:41 -05:00
|
|
|
|
2017-09-17 00:57:12 +02:00
|
|
|
# Check to make sure we got a solid back
|
2014-12-11 20:42:41 -05:00
|
|
|
self.assertTrue(importedShape.val().ShapeType() == "Solid")
|
|
|
|
|
2017-09-17 00:57:12 +02:00
|
|
|
# Check the number of faces and vertices per face to make sure we have a box shape
|
2020-01-20 20:52:12 +01:00
|
|
|
self.assertTrue(
|
|
|
|
importedShape.faces("+X").size() == 1
|
|
|
|
and importedShape.faces("+X").vertices().size() == 4
|
|
|
|
)
|
|
|
|
self.assertTrue(
|
|
|
|
importedShape.faces("+Y").size() == 1
|
|
|
|
and importedShape.faces("+Y").vertices().size() == 4
|
|
|
|
)
|
|
|
|
self.assertTrue(
|
|
|
|
importedShape.faces("+Z").size() == 1
|
|
|
|
and importedShape.faces("+Z").vertices().size() == 4
|
|
|
|
)
|
2014-12-11 20:42:41 -05:00
|
|
|
|
|
|
|
def testSTEP(self):
|
|
|
|
"""
|
|
|
|
Tests STEP file import
|
|
|
|
"""
|
|
|
|
self.importBox(importers.ImportTypes.STEP, OUTDIR + "/tempSTEP.step")
|
2014-08-18 14:45:02 -04:00
|
|
|
|
2018-11-18 21:04:11 -08:00
|
|
|
def testInvalidSTEP(self):
|
|
|
|
"""
|
|
|
|
Attempting to load an invalid STEP file should throw an exception, but
|
|
|
|
not segfault.
|
|
|
|
"""
|
|
|
|
tmpfile = OUTDIR + "/badSTEP.step"
|
2020-01-20 20:52:12 +01:00
|
|
|
with open(tmpfile, "w") as f:
|
|
|
|
f.write("invalid STEP file")
|
2018-11-18 21:04:11 -08:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
importers.importShape(importers.ImportTypes.STEP, tmpfile)
|
|
|
|
|
2018-11-30 18:08:03 -08:00
|
|
|
def testImportMultipartSTEP(self):
|
|
|
|
"""
|
|
|
|
Import a STEP file that contains two objects and ensure that both are
|
|
|
|
loaded.
|
|
|
|
"""
|
2020-06-01 14:13:52 +02:00
|
|
|
|
2018-11-30 18:08:03 -08:00
|
|
|
filename = os.path.join(testdataDir, "red_cube_blue_cylinder.step")
|
|
|
|
objs = importers.importShape(importers.ImportTypes.STEP, filename)
|
|
|
|
self.assertEqual(2, len(objs.all()))
|
2020-06-01 16:21:20 +02:00
|
|
|
|
2020-06-01 14:13:52 +02:00
|
|
|
def testImportDXF(self):
|
|
|
|
"""
|
|
|
|
Test DXF import with various tolerances.
|
|
|
|
"""
|
|
|
|
|
|
|
|
filename = os.path.join(testdataDir, "gear.dxf")
|
2020-06-01 16:21:20 +02:00
|
|
|
|
2020-06-01 14:13:52 +02:00
|
|
|
obj = importers.importDXF(filename)
|
2020-06-01 16:21:20 +02:00
|
|
|
self.assertFalse(obj.val().isValid())
|
|
|
|
|
2020-06-01 14:13:52 +02:00
|
|
|
obj = importers.importDXF(filename, tol=1e-3)
|
2020-06-01 16:21:20 +02:00
|
|
|
self.assertTrue(obj.val().isValid())
|
|
|
|
self.assertEqual(obj.faces().size(), 1)
|
|
|
|
self.assertEqual(obj.wires().size(), 2)
|
|
|
|
|
2020-06-01 16:19:02 +02:00
|
|
|
obj = obj.wires().toPending().extrude(1)
|
2020-06-01 16:21:20 +02:00
|
|
|
self.assertTrue(obj.val().isValid())
|
|
|
|
self.assertEqual(obj.solids().size(), 1)
|
2017-09-17 00:57:12 +02:00
|
|
|
|
2020-06-01 14:13:52 +02:00
|
|
|
obj = importers.importShape(importers.ImportTypes.DXF, filename, tol=1e-3)
|
2020-06-02 17:52:25 +02:00
|
|
|
self.assertTrue(obj.val().isValid())
|
2020-06-02 17:53:30 +02:00
|
|
|
|
2020-06-02 17:52:25 +02:00
|
|
|
# additional files to test more DXF entities
|
2020-06-02 17:53:30 +02:00
|
|
|
|
2020-06-02 17:52:25 +02:00
|
|
|
filename = os.path.join(testdataDir, "MC 12x31.dxf")
|
|
|
|
obj = importers.importDXF(filename)
|
|
|
|
self.assertTrue(obj.val().isValid())
|
2020-06-02 17:53:30 +02:00
|
|
|
|
2020-06-02 17:52:25 +02:00
|
|
|
filename = os.path.join(testdataDir, "1001.dxf")
|
|
|
|
obj = importers.importDXF(filename)
|
|
|
|
self.assertTrue(obj.val().isValid())
|
2020-06-01 16:21:20 +02:00
|
|
|
|
2020-07-16 20:04:05 +02:00
|
|
|
# test spline import
|
|
|
|
|
|
|
|
filename = os.path.join(testdataDir, "spline.dxf")
|
|
|
|
obj = importers.importDXF(filename, tol=1)
|
|
|
|
self.assertTrue(obj.val().isValid())
|
|
|
|
self.assertEqual(obj.faces().size(), 1)
|
|
|
|
self.assertEqual(obj.wires().size(), 2)
|
|
|
|
|
2020-07-17 16:59:21 +02:00
|
|
|
# test rational spline import
|
|
|
|
filename = os.path.join(testdataDir, "rational_spline.dxf")
|
|
|
|
obj = importers.importDXF(filename)
|
|
|
|
self.assertTrue(obj.val().isValid())
|
|
|
|
self.assertEqual(obj.faces().size(), 1)
|
|
|
|
self.assertEqual(obj.edges().size(), 1)
|
|
|
|
|
|
|
|
# importing of a complex shape exported from Inkscape
|
|
|
|
filename = os.path.join(testdataDir, "genshi.dxf")
|
|
|
|
obj = importers.importDXF(filename)
|
|
|
|
self.assertTrue(obj.val().isValid())
|
|
|
|
self.assertEqual(obj.faces().size(), 1)
|
|
|
|
|
2020-01-20 20:52:12 +01:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2014-12-11 20:42:41 -05:00
|
|
|
import unittest
|
2020-01-20 20:52:12 +01:00
|
|
|
|
2014-12-11 20:42:41 -05:00
|
|
|
unittest.main()
|