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 #115 from galou/more_planes
Browse files Browse the repository at this point in the history
Add the named planes ZX YX ZY
  • Loading branch information
jmwright committed Oct 6, 2015
2 parents 5d9217c + 2ebae04 commit 9dd8db7
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 12 deletions.
52 changes: 43 additions & 9 deletions cadquery/freecad_impl/geom.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ def __init__(self,*args):
if len(args) == 3:
fV = FreeCAD.Base.Vector(args[0],args[1],args[2])
elif len(args) == 1:
if type(args[0]) is tuple:
if isinstance(args[0], Vector):
fV = args[0].wrapped
elif isinstance(args[0], tuple):
fV = FreeCAD.Base.Vector(args[0][0],args[0][1],args[0][2])
elif type(args[0] is FreeCAD.Base.Vector):
elif isinstance(args[0], FreeCAD.Base.Vector):
fV = args[0]
elif type(args[0] is Vector):
fV = args[0].wrapped
else:
fV = args[0]
else:
Expand Down Expand Up @@ -221,7 +221,7 @@ def named(cls,stdName,origin=(0,0,0)):
"""
Create a predefined Plane based on the conventional names.
:param stdName: one of (XY|YZ|XZ|front|back|left|right|top|bottom
:param stdName: one of (XY|YZ|ZX|XZ|YX|ZY|front|back|left|right|top|bottom
:type stdName: string
:param origin: the desired origin, specified in global coordinates
:type origin: 3-tuple of the origin of the new plane, in global coorindates.
Expand All @@ -234,7 +234,10 @@ def named(cls,stdName,origin=(0,0,0)):
=========== ======= ======= ======
XY +x +y +z
YZ +y +z +x
ZX +z +x +y
XZ +x +z -y
YX +y +x -z
ZY +z +y -x
front +x +y +z
back -x +y -z
left +z +y -x
Expand All @@ -248,7 +251,10 @@ def named(cls,stdName,origin=(0,0,0)):
#origin, xDir, normal
'XY' : Plane(Vector(origin),Vector((1,0,0)),Vector((0,0,1))),
'YZ' : Plane(Vector(origin),Vector((0,1,0)),Vector((1,0,0))),
'ZX': Plane(origin, (0, 0, 1), (0, 1, 0)),
'XZ' : Plane(Vector(origin),Vector((1,0,0)),Vector((0,-1,0))),
'YX': Plane(origin, (0, 1, 0), (0, 0, -1)),
'ZY': Plane(origin, (0, 0, 1), (-1, 0, 0)),
'front': 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))),
Expand All @@ -270,10 +276,28 @@ def XY(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)

@classmethod
def ZX(cls, origin=(0, 0, 0), xDir=Vector(0, 0, 1)):
plane = Plane.named('ZX', origin)
plane._setPlaneDir(xDir)
return plane

@classmethod
def XZ(cls,origin=(0,0,0),xDir=Vector(1,0,0)):
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
def front(cls,origin=(0,0,0),xDir=Vector(1,0,0)):
return Plane.named('front',origin)
Expand Down Expand Up @@ -313,9 +337,14 @@ def __init__(self, origin, xDir, normal ):
:return: a plane in the global space, with the xDirection of the plane in the specified direction.
"""
self.xDir = xDir.normalize()
self.yDir = normal.cross(self.xDir).normalize()
normal = Vector(normal)
if (normal.Length == 0.0):
raise ValueError('normal should be non null')
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!
self.invZDir = self.zDir.multiply(-1.0)
Expand All @@ -331,7 +360,7 @@ def setOrigin3d(self,originVector):
:return: void
"""
self.origin = originVector
self.origin = Vector(originVector)
self._calcTransforms()

def setOrigin2d(self,x,y):
Expand All @@ -353,7 +382,6 @@ def setOrigin2d(self,x,y):
"""
self.setOrigin3d(self.toWorldCoords((x,y)))


def isWireInside(self,baseWire,testWire):
"""
Determine if testWire is inside baseWire, after both wires are projected into the current plane
Expand Down Expand Up @@ -508,6 +536,12 @@ def rotateShapes(self, listOfShapes, rotationMatrix):

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):
"""
Expand Down
47 changes: 44 additions & 3 deletions tests/TestWorkplanes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@
from cadquery import *
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):

def testYZPlaneOrigins(self):
Expand Down Expand Up @@ -40,8 +47,6 @@ def testXZPlaneOrigins(self):
#origin is always (0,0,0) in local coordinates
self.assertTupleAlmostEquals((0,0,0), p.toLocalCoords(p.origin).toTuple() ,2 )



def testPlaneBasics(self):
p = Plane.XY()
#local to world
Expand Down Expand Up @@ -81,4 +86,40 @@ def testOffsetPlanes(self):
p = Plane.XZ(origin=(2,0,2))
r = p.toWorldCoords((1.0,1.0)).toTuple()
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)

0 comments on commit 9dd8db7

Please sign in to comment.