Merge pull request #84 from dcowden/mirrorfix

Mirrorfix
This commit is contained in:
Jeremy Wright
2015-04-23 00:33:58 -04:00
4 changed files with 175 additions and 97 deletions

View File

@ -1269,6 +1269,7 @@ class Workplane(CQ):
#attempt again to consolidate all of the wires
c = consolidated.consolidateWires()
return c
def mirrorY(self):
@ -1288,7 +1289,6 @@ class Workplane(CQ):
Future Enhancements:
mirrorX().mirrorY() should work but doesnt, due to some FreeCAD weirdness
"""
tm = Matrix()
tm.rotateY(math.pi)
@ -1307,7 +1307,6 @@ class Workplane(CQ):
Future Enhancements:
mirrorX().mirrorY() should work but doesnt, due to some FreeCAD weirdness
"""
tm = Matrix()
tm.rotateX(math.pi)
@ -1349,11 +1348,9 @@ class Workplane(CQ):
If possible, a new object with the results are returned.
if not possible, the wires remain separated
FreeCAD has a bug in Part.Wire([]) which does not create wires/edges properly somtimes
Additionally, it has a bug where a profile compose of two wires ( rathre than one )
also does not work properly
together these are a real problem.
FreeCAD has a bug in Part.Wire([]) which does not create wires/edges properly sometimes
Additionally, it has a bug where a profile compose of two wires ( rather than one )
also does not work properly. Together these are a real problem.
"""
wires = self.wires().vals()
if len(wires) < 2:

View File

@ -17,9 +17,10 @@
License along with this library; If not, see <http://www.gnu.org/licenses/>
"""
import math,sys
import math
import cadquery
import FreeCAD
#Turns out we don't need the Part module here.
import Part as FreeCADPart
def sortWiresByBuildOrder(wireList,plane,result=[]):
"""
@ -405,7 +406,7 @@ class Plane:
"""
if isinstance(obj, Vector):
return Vector(self.fG.multiply(obj.wrapped))
elif isinstance(obj,Shape):
elif isinstance(obj, cadquery.Shape):
return obj.transformShape(self.rG)
else:
raise ValueError("Dont know how to convert type %s to local coordinates" % str(type(obj)))
@ -480,24 +481,42 @@ class Plane:
#There might be a better way, but to do this rotation takes 3 steps
#transform geometry to local coordinates
#then rotate about x
#then transform back to global coordiante
#then transform back to global coordinates
resultWires = []
for w in listOfShapes:
mirrored = w.transformGeometry(rotationMatrix.wrapped)
# If the first vertex of the second wire is not coincident with the first or last vertices of the first wire
# we have to fix the wire so that it will mirror correctly
if (mirrored.wrapped.Vertexes[0].X == w.wrapped.Vertexes[0].X and
mirrored.wrapped.Vertexes[0].Y == w.wrapped.Vertexes[0].Y and
mirrored.wrapped.Vertexes[0].Z == w.wrapped.Vertexes[0].Z) or \
(mirrored.wrapped.Vertexes[0].X == w.wrapped.Vertexes[-1].X and
mirrored.wrapped.Vertexes[0].Y == w.wrapped.Vertexes[-1].Y and
mirrored.wrapped.Vertexes[0].Z == w.wrapped.Vertexes[-1].Z):
resultWires.append(mirrored)
else:
# Make sure that our mirrored edges meet up and are ordered properly
aEdges = w.wrapped.Edges
aEdges.extend(mirrored.wrapped.Edges)
comp = FreeCADPart.Compound(aEdges)
mirroredWire = comp.connectEdgesToWires(False).Wires[0]
resultWires.append(cadquery.Shape.cast(mirroredWire))
return resultWires
def _calcTransforms(self):
"""
Computes transformation martrices to convert betwene local and global coordinates
Computes transformation martrices to convert between local and global coordinates
"""
#r is the forward transformation matrix from world to local coordinates
#ok i will be really honest-- i cannot understand exactly why this works
#something bout the order of the transaltion and the rotation.
# the double-inverting is strange, and i dont understand it.
#something bout the order of the translation and the rotation.
# the double-inverting is strange, and I don't understand it.
r = FreeCAD.Base.Matrix()
#forward transform must rotate and adjust for origin

View File

@ -612,6 +612,59 @@ class TestCadQuery(BaseTest):
self.assertEquals(6, s.faces().size())
self.saveModel(s)
def testUnorderedMirror(self):
"""
Tests whether or not a wire can be mirrored if its mirror won't connect to it
"""
r = 20
s = 7
t = 1.5
points = [
(0, t/2),
(r/2-1.5*t, r/2-t),
(s/2, r/2-t),
(s/2, r/2),
(r/2, r/2),
(r/2, s/2),
(r/2-t, s/2),
(r/2-t, r/2-1.5*t),
(t/2, 0)
]
r = Workplane("XY").polyline(points).mirrorX()
self.assertEquals(1, r.wires().size())
self.assertEquals(16, r.edges().size())
# def testChainedMirror(self):
# """
# Tests whether or not calling mirrorX().mirrorY() works correctly
# """
# r = 20
# s = 7
# t = 1.5
#
# points = [
# (0, t/2),
# (r/2-1.5*t, r/2-t),
# (s/2, r/2-t),
# (s/2, r/2),
# (r/2, r/2),
# (r/2, s/2),
# (r/2-t, s/2),
# (r/2-t, r/2-1.5*t),
# (t/2, 0)
# ]
#
# r = Workplane("XY").polyline(points).mirrorX().mirrorY()
#
# self.assertEquals(1, r.wires().size())
# self.assertEquals(32, r.edges().size())
#TODO: Re-work testIbeam test below now that chaining works
#TODO: Add toLocalCoords and toWorldCoords tests
def testIbeam(self):
"""
Make an ibeam. demonstrates fancy mirroring
@ -623,7 +676,7 @@ class TestCadQuery(BaseTest):
t = 1.0
#TODO: for some reason doing 1/4 of the profile and mirroring twice ( .mirrorX().mirrorY() )
#did not work, due to a bug in freecad-- it was losing edges when createing a composite wire.
#did not work, due to a bug in freecad-- it was losing edges when creating a composite wire.
#i just side-stepped it for now
pts = [
@ -641,25 +694,31 @@ class TestCadQuery(BaseTest):
self.saveModel(res)
def testCone(self):
"test that a simple sphere works"
"""
Tests that a simple cone works
"""
s = Solid.makeCone(0, 1.0, 2.0)
t = CQ(s)
self.saveModel(t)
self.assertEqual(2, t.faces().size())
def testFillet(self):
"Tests filleting edges on a solid"
"""
Tests filleting edges on a solid
"""
c = CQ( makeUnitCube()).faces(">Z").workplane().circle(0.25).extrude(0.25,True).edges("|Z").fillet(0.2)
self.saveModel(c)
self.assertEqual(12,c.faces().size() )
def testCounterBores(self):
"""Tests making a set of counterbored holes in a face"""
"""
Tests making a set of counterbored holes in a face
"""
c = CQ(makeCube(3.0))
pnts = [
(-1.0, -1.0), (0.0, 0.0), (1.0, 1.0)
]
c.faces(">Z").workplane().pushPoints(pnts).cboreHole(0.1,0.25,0.25,.75)
c.faces(">Z").workplane().pushPoints(pnts).cboreHole(0.1, 0.25, 0.25, 0.75)
self.assertEquals(18, c.faces().size() )
self.saveModel(c)
@ -673,7 +732,9 @@ class TestCadQuery(BaseTest):
self.saveModel(result)
def testSplitKeepingHalf(self):
"Tests splitting a solid"
"""
Tests splitting a solid
"""
#drill a hole in the side
c = CQ(makeUnitCube()).faces(">Z").workplane().circle(0.25).cutThruAll()
@ -686,7 +747,9 @@ class TestCadQuery(BaseTest):
self.assertEqual(8, c.faces().size())
def testSplitKeepingBoth(self):
"Tests splitting a solid"
"""
Tests splitting a solid
"""
#drill a hole in the side
c = CQ(makeUnitCube()).faces(">Z").workplane().circle(0.25).cutThruAll()
@ -733,13 +796,13 @@ class TestCadQuery(BaseTest):
Tests creating an array of boxes
"""
s = Workplane("XY").rect(4.0, 4.0, forConstruction=True).vertices().box(0.25, 0.25, 0.25, combine=True)
#1 object, 4 solids beause the object is a compound
#1 object, 4 solids because the object is a compound
self.assertEquals(1, s.solids().size())
self.assertEquals(1, s.size())
self.saveModel(s)
s = Workplane("XY").rect(4.0, 4.0, forConstruction=True).vertices().box(0.25, 0.25, 0.25, combine=False)
#4 objects, 4 solids, becaue each is a separate solid
#4 objects, 4 solids, because each is a separate solid
self.assertEquals(4, s.size())
self.assertEquals(4, s.solids().size())

View File

@ -3,23 +3,19 @@ import unittest
import sys
import os
#from cadquery.freecad_impl.verutil import fc_import
#FreeCAD = fc_import("FreeCAD")
#import cadquery.freecad_impl
import FreeCAD
# P = fc_import("FreeCAD.Part")
# V = fc_import("FreeCAD").Base.Vector
import Part as P
from FreeCAD import Vector as V
def readFileAsString(fileName):
f= open(fileName, 'r')
s = f.read()
f.close()
return s
def writeStringToFile(strToWrite, fileName):
f = open(fileName, 'w')
f.write(strToWrite)
@ -29,14 +25,17 @@ def writeStringToFile(strToWrite,fileName):
def makeUnitSquareWire():
return Solid.cast(P.makePolygon([V(0, 0, 0), V(1, 0, 0), V(1, 1, 0), V(0, 1, 0), V(0, 0, 0)]))
def makeUnitCube():
return makeCube(1.0)
def makeCube(size):
return Solid.makeBox(size, size, size)
def toTuple(v):
"convert a vector or a vertex to a 3-tuple: x,y,z"
"""convert a vector or a vertex to a 3-tuple: x,y,z"""
pnt = v
if type(v) == FreeCAD.Base.Vector:
return (v.Point.x, v.Point.y, v.Point.z)