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 #235 from dcowden/tests
Browse files Browse the repository at this point in the history
Tests and minor changes to freecad_impl/shapes.py that the tests highlighted.
  • Loading branch information
jmwright committed Mar 5, 2018
2 parents bb2a7b1 + b06bdf2 commit a229e1d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
18 changes: 12 additions & 6 deletions cadquery/freecad_impl/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ def cast(cls, obj, forConstruction=False):
# TODO: all these should move into the exporters folder.
# we dont need a bunch of exporting code stored in here!
#
def exportStl(self, fileName):
self.wrapped.exportStl(fileName)
def exportStl(self, fileName, tolerance=0.1):
self.wrapped.exportStl(fileName, tolerance)

def exportStep(self, fileName):
self.wrapped.exportStep(fileName)

def exportShape(self, fileName, fileFormat):
def exportShape(self, fileName, fileFormat, tolerance=0.1):
if fileFormat == ExportFormats.STL:
self.wrapped.exportStl(fileName)
self.wrapped.exportStl(fileName, tolerance)
elif fileFormat == ExportFormats.BREP:
self.wrapped.exportBrep(fileName)
elif fileFormat == ExportFormats.STEP:
Expand Down Expand Up @@ -306,7 +306,13 @@ def Solids(self):
return [Solid(i) for i in self.wrapped.Solids]

def Area(self):
return self.wrapped.Area
"""
Returns the area of a shape, but only if it is a face
"""
if self.wrapped.ShapeType == 'Face':
return self.wrapped.Area
else:
raise ValueError("shape type must be 'Face' to calculate the area")

def Length(self):
return self.wrapped.Length
Expand Down Expand Up @@ -650,7 +656,7 @@ def makeRuledSurface(cls, edgeOrWire1, edgeOrWire2, dist=None):
Create a ruled surface out of two edges or wires. If wires are used then
these must have the same
"""
return Shape.cast(FreeCADPart.makeRuledSurface(edgeOrWire1.obj, edgeOrWire2.obj, dist))
return Shape.cast(FreeCADPart.makeRuledSurface(edgeOrWire1.wrapped, edgeOrWire2.wrapped))

def cut(self, faceToCut):
"Remove a face from another one"
Expand Down
49 changes: 46 additions & 3 deletions tests/TestCadObjects.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def testFace(self):
mplanec = Face.cast(face1)
mplane = Face(face1)

#self.assertTupleAlmostEquals((0.0, 0.0, 1.0), mplane.normalAt().toTuple(), 3)
self.assertTupleAlmostEquals((5.0, 5.0, 0.0), mplane.Center().toTuple(), 3)

def testShapeProps(self):
"""
Expand All @@ -109,6 +109,7 @@ def testShapeProps(self):

# Dynamic type checking
self.assertTrue(e.isType(e, 'Edge'))
self.assertFalse(e.isType(None, 'Edge'))

# Checking null objects
self.assertFalse(e.isNull())
Expand All @@ -125,8 +126,17 @@ def testShapeProps(self):
# Testing whether shape is closed
self.assertTrue(e.Closed())

# Getting the area of the circle
# self.assertAlmostEqual(12.566, e.Area(), 3)
# Trying to get the area of the circular edge
with self.assertRaises(ValueError):
e.Area()

# Getting the area of the square face
mplane = Face.makePlane(10.0, 10.0)
self.assertAlmostEqual(100.0, mplane.Area(), 3)

# Getting the center of a solid
s = Solid.makeCylinder(10.0, 10.0)
self.assertTupleAlmostEquals((0.0, 0.0, 5.0), s.Center().toTuple(), 3)

def testBasicBoundingBox(self):
v = Vertex(Part.Vertex(1, 1, 1))
Expand Down Expand Up @@ -359,6 +369,39 @@ def testTranslate(self):

self.assertTupleAlmostEquals((1.0, 2.0, 4.0), e2.Center().toTuple(), 3)

def testScale(self):
"""
Tests scaling a shape and whether the dimensions are correct afterwards
"""
e = Shape.cast(Part.makeCircle(2.0, FreeCAD.Base.Vector(1, 2, 3)))
e2 = e.scale(0.5)

self.assertAlmostEquals(2.0, e2.BoundingBox(tolerance=0.0001).xlen, 3)
self.assertAlmostEquals(2.0, e2.BoundingBox(tolerance=0.0001).ylen, 3)

def testCopy(self):
"""
Tests making a copy of a shape object and whether the new one has the
same properties as the original.
"""
e = Shape.cast(Part.makeCircle(2.0, FreeCAD.Base.Vector(1, 2, 3)))
e2 = e.copy()

self.assertEquals(e.BoundingBox().xlen, e2.BoundingBox().xlen)
self.assertEquals(e.BoundingBox().ylen, e2.BoundingBox().ylen)

def testRuledSurface(self):
"""
Tests making a ruled surface from two edges/wires.
"""
edge1 = Shape(Part.makeLine((0, 0, 5), (0, 10, 5)))
edge2 = Shape(Part.makeLine((5, 5, 0), (10, 10, 0)))

surf1 = Face.makeRuledSurface(edge1, edge2)

self.assertEquals(surf1.ShapeType(), 'Face')
self.assertTrue(surf1.isValid())

def testVertices(self):
e = Shape.cast(Part.makeLine((0, 0, 0), (1, 1, 0)))
self.assertEqual(2, len(e.Vertices()))
Expand Down
3 changes: 2 additions & 1 deletion tests/TestCadQuery.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ def tearDown(self):
def saveModel(self, shape):
"""
shape must be a CQ object
Save models in SVG and STEP format
Save models in SVG, STEP and STL format
"""

with suppress_stdout_stderr():
shape.exportSvg(os.path.join(OUTDIR,self._testMethodName + ".svg"))
shape.val().exportStep(os.path.join(OUTDIR,self._testMethodName + ".step"))
shape.val().exportStl(os.path.join(OUTDIR,self._testMethodName + ".stl"))

def testToFreeCAD(self):
"""
Expand Down

0 comments on commit a229e1d

Please sign in to comment.