Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #225 from dcowden/tests
Browse files Browse the repository at this point in the history
STEP URL Import Test
  • Loading branch information
jmwright committed Feb 16, 2018
2 parents aa3b360 + c586f04 commit 5deb3c6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
17 changes: 11 additions & 6 deletions cadquery/freecad_impl/importers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import os
import urllib as urlreader
import tempfile

class ImportTypes:
STEP = "STEP"

Expand All @@ -34,10 +34,9 @@ def importStep(fileName):
"""
Accepts a file name and loads the STEP file into a cadquery shape
:param fileName: The path and name of the STEP file to be imported
"""
"""
#Now read and return the shape
try:
#print fileName
rshape = Part.read(fileName)

#Make sure that we extract all the solids
Expand All @@ -50,14 +49,20 @@ def importStep(fileName):
raise ValueError("STEP File Could not be loaded")

#Loads a STEP file from an URL into a CQ.Workplane object
def importStepFromURL(url):
def importStepFromURL(url):
#Now read and return the shape
try:
webFile = urlreader.urlopen(url)
# Account for differences in urllib between Python 2 and 3
if hasattr(urlreader, "urlopen"):
webFile = urlreader.urlopen(url)
else:
import urllib.request
webFile = urllib.request.urlopen(url)

tempFile = tempfile.NamedTemporaryFile(suffix='.step', delete=False)
tempFile.write(webFile.read())
webFile.close()
tempFile.close()
tempFile.close()

rshape = Part.read(tempFile.name)

Expand Down
20 changes: 18 additions & 2 deletions tests/TestImporters.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ def importBox(self, importType, fileName):
# Reimport the shape from the new STEP file
importedShape = importers.importShape(importType,fileName)

#Check to make sure we got a solid back
# Check to make sure we got a solid back
self.assertTrue(importedShape.val().ShapeType() == "Solid")

#Check the number of faces and vertices per face to make sure we have a box shape
# Check the number of faces and vertices per face to make sure we have a box shape
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)
Expand All @@ -51,6 +51,22 @@ def testSTEP(self):
with suppress_stdout_stderr():
self.importBox(importers.ImportTypes.STEP, OUTDIR + "/tempSTEP.step")

def testSTEPFromURL(self):
"""
Tests STEP file import from a URL
"""
stepURL = "https://raw.githubusercontent.com/dcowden/cadquery/master/doc/_static/box_export.step"

importedShape = importers.importStepFromURL(stepURL)

# Check to make sure we got a solid back
self.assertTrue(importedShape.val().ShapeType() == "Solid")

# Check the number of faces and vertices per face to make sure we have a box shape
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)

if __name__ == '__main__':
import unittest
unittest.main()

0 comments on commit 5deb3c6

Please sign in to comment.