Use the xDir parameter in Plane.XY() and similar.

Add simple tests for named planes
Use isinstance instead of type in Vector because type caused the
problem that 'type(Vector) is FreeCAD.Base.Vector'.
This commit is contained in:
Gaël Ecorchard 2015-10-04 22:49:34 +02:00
parent b42375b3d6
commit 2ebae0461b
2 changed files with 84 additions and 15 deletions

View File

@ -73,12 +73,12 @@ class Vector(object):
if len(args) == 3: if len(args) == 3:
fV = FreeCAD.Base.Vector(args[0],args[1],args[2]) fV = FreeCAD.Base.Vector(args[0],args[1],args[2])
elif len(args) == 1: elif len(args) == 1:
if type(args[0]) is tuple: if isinstance(args[0], Vector):
fV = FreeCAD.Base.Vector(args[0][0],args[0][1],args[0][2])
elif type(args[0] is FreeCAD.Base.Vector):
fV = args[0]
elif type(args[0] is Vector):
fV = args[0].wrapped fV = args[0].wrapped
elif isinstance(args[0], tuple):
fV = FreeCAD.Base.Vector(args[0][0],args[0][1],args[0][2])
elif isinstance(args[0], FreeCAD.Base.Vector):
fV = args[0]
else: else:
fV = args[0] fV = args[0]
else: else:
@ -251,10 +251,10 @@ class Plane:
#origin, xDir, normal #origin, xDir, normal
'XY' : Plane(Vector(origin),Vector((1,0,0)),Vector((0,0,1))), 'XY' : Plane(Vector(origin),Vector((1,0,0)),Vector((0,0,1))),
'YZ' : Plane(Vector(origin),Vector((0,1,0)),Vector((1,0,0))), 'YZ' : Plane(Vector(origin),Vector((0,1,0)),Vector((1,0,0))),
'ZX': Plane(Vector(origin), Vector((0, 0, 1)), Vector((0, 1, 0))), 'ZX': Plane(origin, (0, 0, 1), (0, 1, 0)),
'XZ' : Plane(Vector(origin),Vector((1,0,0)),Vector((0,-1,0))), 'XZ' : Plane(Vector(origin),Vector((1,0,0)),Vector((0,-1,0))),
'YX': Plane(Vector(origin), Vector((0, 1, 0)), Vector((0, 0, -1))), 'YX': Plane(origin, (0, 1, 0), (0, 0, -1)),
'ZY': Plane(Vector(origin), Vector((0, 0, 1)), Vector((-1, 0, 0))), 'ZY': Plane(origin, (0, 0, 1), (-1, 0, 0)),
'front': Plane(Vector(origin),Vector((1,0,0)),Vector((0,0,1))), 'front': Plane(Vector(origin),Vector((1,0,0)),Vector((0,0,1))),
'back': Plane(Vector(origin),Vector((-1,0,0)),Vector((0,0,-1))), 'back': Plane(Vector(origin),Vector((-1,0,0)),Vector((0,0,-1))),
'left': Plane(Vector(origin),Vector((0,0,1)),Vector((-1,0,0))), 'left': Plane(Vector(origin),Vector((0,0,1)),Vector((-1,0,0))),
@ -276,10 +276,28 @@ class Plane:
def YZ(cls,origin=(0,0,0),xDir=Vector(1,0,0)): def YZ(cls,origin=(0,0,0),xDir=Vector(1,0,0)):
return Plane.named('YZ',origin) return Plane.named('YZ',origin)
@classmethod
def ZX(cls, origin=(0, 0, 0), xDir=Vector(0, 0, 1)):
plane = Plane.named('ZX', origin)
plane._setPlaneDir(xDir)
return plane
@classmethod @classmethod
def XZ(cls,origin=(0,0,0),xDir=Vector(1,0,0)): def XZ(cls,origin=(0,0,0),xDir=Vector(1,0,0)):
return Plane.named('XZ',origin) return Plane.named('XZ',origin)
@classmethod
def YX(cls, origin=(0, 0, 0), xDir=Vector(0, 1, 0)):
plane = Plane.named('YX', origin)
plane._setPlaneDir(xDir)
return plane
@classmethod
def ZY(cls, origin=(0, 0, 0), xDir=Vector(0, 0, 1)):
plane = Plane.named('ZY', origin)
plane._setPlaneDir(xDir)
return plane
@classmethod @classmethod
def front(cls,origin=(0,0,0),xDir=Vector(1,0,0)): def front(cls,origin=(0,0,0),xDir=Vector(1,0,0)):
return Plane.named('front',origin) return Plane.named('front',origin)
@ -319,9 +337,14 @@ class Plane:
:return: a plane in the global space, with the xDirection of the plane in the specified direction. :return: a plane in the global space, with the xDirection of the plane in the specified direction.
""" """
self.xDir = xDir.normalize() normal = Vector(normal)
self.yDir = normal.cross(self.xDir).normalize() if (normal.Length == 0.0):
raise ValueError('normal should be non null')
self.zDir = normal.normalize() self.zDir = normal.normalize()
xDir = Vector(xDir)
if (xDir.Length == 0.0):
raise ValueError('xDir should be non null')
self._setPlaneDir(xDir)
#stupid freeCAD!!!!! multiply has a bug that changes the original also! #stupid freeCAD!!!!! multiply has a bug that changes the original also!
self.invZDir = self.zDir.multiply(-1.0) self.invZDir = self.zDir.multiply(-1.0)
@ -337,7 +360,7 @@ class Plane:
:return: void :return: void
""" """
self.origin = originVector self.origin = Vector(originVector)
self._calcTransforms() self._calcTransforms()
def setOrigin2d(self,x,y): def setOrigin2d(self,x,y):
@ -359,7 +382,6 @@ class Plane:
""" """
self.setOrigin3d(self.toWorldCoords((x,y))) self.setOrigin3d(self.toWorldCoords((x,y)))
def isWireInside(self,baseWire,testWire): def isWireInside(self,baseWire,testWire):
""" """
Determine if testWire is inside baseWire, after both wires are projected into the current plane Determine if testWire is inside baseWire, after both wires are projected into the current plane
@ -514,6 +536,12 @@ class Plane:
return resultWires return resultWires
def _setPlaneDir(self, xDir):
"""Set the vectors parallel to the plane, i.e. xDir and yDir"""
if (self.zDir.dot(xDir) > 1e-5):
raise ValueError('xDir must be parralel to the plane')
self.xDir = xDir.normalize()
self.yDir = self.zDir.cross(self.xDir).normalize()
def _calcTransforms(self): def _calcTransforms(self):
""" """

View File

@ -7,6 +7,13 @@
from cadquery import * from cadquery import *
from tests import BaseTest,toTuple from tests import BaseTest,toTuple
xAxis_ = Vector(1, 0, 0)
yAxis_ = Vector(0, 1, 0)
zAxis_ = Vector(0, 0, 1)
xInvAxis_ = Vector(-1, 0, 0)
yInvAxis_ = Vector(0, -1, 0)
zInvAxis_ = Vector(0, 0, -1)
class TestWorkplanes(BaseTest): class TestWorkplanes(BaseTest):
def testYZPlaneOrigins(self): def testYZPlaneOrigins(self):
@ -40,8 +47,6 @@ class TestWorkplanes(BaseTest):
#origin is always (0,0,0) in local coordinates #origin is always (0,0,0) in local coordinates
self.assertTupleAlmostEquals((0,0,0), p.toLocalCoords(p.origin).toTuple() ,2 ) self.assertTupleAlmostEquals((0,0,0), p.toLocalCoords(p.origin).toTuple() ,2 )
def testPlaneBasics(self): def testPlaneBasics(self):
p = Plane.XY() p = Plane.XY()
#local to world #local to world
@ -82,3 +87,39 @@ class TestWorkplanes(BaseTest):
r = p.toWorldCoords((1.0,1.0)).toTuple() r = p.toWorldCoords((1.0,1.0)).toTuple()
self.assertTupleAlmostEquals((3.0,0.0,3.0),r ,2 ) self.assertTupleAlmostEquals((3.0,0.0,3.0),r ,2 )
self.assertTupleAlmostEquals((10.0,10.0), p.toLocalCoords(Vector(12.0,0.0,12.0)).toTuple() ,2 ) self.assertTupleAlmostEquals((10.0,10.0), p.toLocalCoords(Vector(12.0,0.0,12.0)).toTuple() ,2 )
def testXYPlaneBasics(self):
p = Plane.named('XY')
self.assertTupleAlmostEquals(p.zDir.toTuple(), zAxis_.toTuple(), 4)
self.assertTupleAlmostEquals(p.xDir.toTuple(), xAxis_.toTuple(), 4)
self.assertTupleAlmostEquals(p.yDir.toTuple(), yAxis_.toTuple(), 4)
def testYZPlaneBasics(self):
p = Plane.named('YZ')
self.assertTupleAlmostEquals(p.zDir.toTuple(), xAxis_.toTuple(), 4)
self.assertTupleAlmostEquals(p.xDir.toTuple(), yAxis_.toTuple(), 4)
self.assertTupleAlmostEquals(p.yDir.toTuple(), zAxis_.toTuple(), 4)
def testZXPlaneBasics(self):
p = Plane.named('ZX')
self.assertTupleAlmostEquals(p.zDir.toTuple(), yAxis_.toTuple(), 4)
self.assertTupleAlmostEquals(p.xDir.toTuple(), zAxis_.toTuple(), 4)
self.assertTupleAlmostEquals(p.yDir.toTuple(), xAxis_.toTuple(), 4)
def testXZPlaneBasics(self):
p = Plane.named('XZ')
self.assertTupleAlmostEquals(p.zDir.toTuple(), yInvAxis_.toTuple(), 4)
self.assertTupleAlmostEquals(p.xDir.toTuple(), xAxis_.toTuple(), 4)
self.assertTupleAlmostEquals(p.yDir.toTuple(), zAxis_.toTuple(), 4)
def testYXPlaneBasics(self):
p = Plane.named('YX')
self.assertTupleAlmostEquals(p.zDir.toTuple(), zInvAxis_.toTuple(), 4)
self.assertTupleAlmostEquals(p.xDir.toTuple(), yAxis_.toTuple(), 4)
self.assertTupleAlmostEquals(p.yDir.toTuple(), xAxis_.toTuple(), 4)
def testZYPlaneBasics(self):
p = Plane.named('ZY')
self.assertTupleAlmostEquals(p.zDir.toTuple(), xInvAxis_.toTuple(), 4)
self.assertTupleAlmostEquals(p.xDir.toTuple(), zAxis_.toTuple(), 4)
self.assertTupleAlmostEquals(p.yDir.toTuple(), yAxis_.toTuple(), 4)