Skip to content

Commit 26cd0a4

Browse files
committed
the test cases pass now at least
1 parent 3f7d38c commit 26cd0a4

17 files changed

Lines changed: 1579 additions & 136 deletions

cadquery/CQ.py

Lines changed: 1415 additions & 2 deletions
Large diffs are not rendered by default.

cadquery/README.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
***
2+
Core CadQuery implementation.
3+
4+
No files should depend on or import FreeCAD , pythonOCC, or other CAD Kernel libraries!!!
5+
Dependencies should be on the classes provided by implementation packages, which in turn
6+
can depend on CAD libraries.
7+
8+
***

cadquery/__init__.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,20 @@
1919

2020

2121
#these items point to the freecad implementation
22-
from .freecad_impl.geom import Plane,BoundBox,Vector
23-
from .freecad_impl.shapes import Shape,Vertex,Edge,Wire,Solid,Shell,Compound
24-
from .freecad_impl.exporters import SvgExporter, AmfExporter, JsonExporter
22+
from .freecad_impl.geom import Plane,BoundBox,Vector,Matrix,sortWiresByBuildOrder
23+
from .freecad_impl.shapes import Shape,Vertex,Edge,Face,Wire,Solid,Shell,Compound
24+
from .freecad_impl import exporters
2525

2626
#these items are the common implementation
27-
from .CQ import CQ
28-
from .workplane import Workplane
29-
from . import plugins
27+
28+
#the order of these matter
3029
from . import selectors
30+
from .CQ import CQ,CQContext,Workplane
31+
3132

3233
__all__ = [
33-
'CQ','Workplane','plugins','selectors','Plane','BoundBox',
34-
'Shape','Vertex','Edge','Wire','Solid','Shell','Compound',
35-
'SvgExporter','AmfExporter','JsonExporter',
34+
'CQ','Workplane','plugins','selectors','Plane','BoundBox','Matrix','Vector','sortWiresByBuildOrder',
35+
'Shape','Vertex','Edge','Wire','Solid','Shell','Compound','exporters',
3636
'plugins'
3737
]
3838

cadquery/freecad_impl/README.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
It is ok for files in this directory to import FreeCAD, FreeCAD.Base, and FreeCAD.Part.
2+
3+
Other modules should _not_ depend on FreeCAD

cadquery/freecad_impl/exporters.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
License along with this library; If not, see <http://www.gnu.org/licenses/>
1818
"""
1919

20+
import FreeCAD
21+
from FreeCAD import Drawing
22+
2023
try:
2124
import xml.etree.cElementTree as ET
2225
except ImportError:
@@ -54,15 +57,8 @@ def guessUnitOfMeasure(shape):
5457

5558
return UNITS.MM
5659

57-
class Exporter(object):
58-
59-
def export(self):
60-
"""
61-
return a string representing the model exported in the specified format
62-
"""
63-
raise NotImplementedError()
6460

65-
class AmfExporter(Exporter):
61+
class AmfExporter(object):
6662
def __init__(self,tessellation):
6763

6864
self.units = "mm"
@@ -105,7 +101,7 @@ def writeAmf(self,outFileName):
105101
three.js JSON object notation
106102
https://github.com/mrdoob/three.js/wiki/JSON-Model-format-3.0
107103
"""
108-
class JsonExporter(Exporter):
104+
class JsonExporter(object):
109105
def __init__(self):
110106

111107
self.vertices = [];
@@ -135,10 +131,6 @@ def toJson(self):
135131
'nFaces' : self.nFaces
136132
};
137133

138-
class SvgExporter(Exporter):
139-
140-
def export(self):
141-
pass
142134

143135
def getPaths(freeCadSVG):
144136
"""
@@ -240,11 +232,15 @@ def getSVG(shape,opts=None):
240232
#)
241233
return svg
242234

235+
243236
def exportSVG(shape, fileName):
244237
"""
238+
accept a cadquery shape, and export it to the provided file
239+
TODO: should use file-like objects, not a fileName, and/or be able to return a string instead
245240
export a view of a part to svg
246241
"""
247-
svg = getSVG(shape)
242+
243+
svg = getSVG(shape.val().wrapped)
248244
f = open(fileName,'w')
249245
f.write(svg)
250246
f.close()

cadquery/freecad_impl/geom.py

Lines changed: 48 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
"""
2-
Copyright (C) 2011-2013 Parametric Products Intellectual Holdings, LLC
3-
4-
This file is part of CadQuery.
5-
6-
CadQuery is free software; you can redistribute it and/or
7-
modify it under the terms of the GNU Lesser General Public
8-
License as published by the Free Software Foundation; either
9-
version 2.1 of the License, or (at your option) any later version.
10-
11-
CadQuery is distributed in the hope that it will be useful,
12-
but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14-
Lesser General Public License for more details.
15-
16-
You should have received a copy of the GNU Lesser General Public
17-
License along with this library; If not, see <http://www.gnu.org/licenses/>
2+
Copyright (C) 2011-2013 Parametric Products Intellectual Holdings, LLC
3+
4+
This file is part of CadQuery.
5+
6+
CadQuery is free software; you can redistribute it and/or
7+
modify it under the terms of the GNU Lesser General Public
8+
License as published by the Free Software Foundation; either
9+
version 2.1 of the License, or (at your option) any later version.
10+
11+
CadQuery is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
Lesser General Public License for more details.
15+
16+
You should have received a copy of the GNU Lesser General Public
17+
License along with this library; If not, see <http://www.gnu.org/licenses/>
1818
"""
1919

2020
import math,sys
@@ -48,33 +48,6 @@ def sortWiresByBuildOrder(wireList,plane,result=[]):
4848

4949
return result
5050

51-
def sortWiresByBuildOrderOld(wireList,plane,result=[]):
52-
"""
53-
Tries to determine how wires should be combined into faces.
54-
Assume:
55-
The wires make up one or more faces, which could have 'holes'
56-
Outer wires are listed ahead of inner wires
57-
there are no wires inside wires inside wires ( IE, islands -- we can deal with that later on )
58-
none of the wires are construction wires
59-
Compute:
60-
one or more sets of wires, with the outer wire listed first, and inner ones
61-
Returns, list of lists.
62-
"""
63-
outerWire = wireList.pop(0)
64-
65-
remainingWires = list(wireList)
66-
childWires = []
67-
for w in wireList:
68-
if plane.isWireInside(outerWire,w):
69-
childWires.append(remainingWires.pop(0))
70-
else:
71-
#doesnt match, assume this wire is a new outer
72-
result.append([outerWire] + childWires)
73-
return sortWiresByBuildOrder(remainingWires,plane,result)
74-
75-
result.append([outerWire] + childWires)
76-
return result
77-
7851
class Vector(object):
7952
"""
8053
Create a 3-dimensional vector
@@ -212,6 +185,25 @@ def __ge__(self,other):
212185
def __eq__(self,other):
213186
return self.wrapped.__eq__(other)
214187

188+
class Matrix:
189+
"""
190+
A 3d , 4x4 transformation matrix.
191+
192+
Used to move geometry in space.
193+
"""
194+
def __init__(self,matrix=None):
195+
if matrix == None:
196+
self.wrapped = FreeCAD.Base.Matrix()
197+
else:
198+
self.wrapped = matrix
199+
200+
def rotateX(self,angle):
201+
self.wrapped.rotateX(angle)
202+
203+
def rotateY(self,angle):
204+
self.wrapped.rotateY(angle)
205+
206+
215207
class Plane:
216208
"""
217209
A 2d coordinate system in space, with the x-y axes on the a plane, and a particular point as the origin.
@@ -329,6 +321,7 @@ def __init__(self, origin, xDir, normal ):
329321

330322
self.setOrigin3d(origin)
331323

324+
332325
def setOrigin3d(self,originVector):
333326
"""
334327
Move the origin of the plane, leaving its orientation and xDirection unchanged.
@@ -417,6 +410,7 @@ def toLocalCoords(self,obj):
417410
else:
418411
raise ValueError("Dont know how to convert type %s to local coordinates" % str(type(obj)))
419412

413+
420414
def toWorldCoords(self, tuplePoint):
421415
"""
422416
Convert a point in local coordinates to global coordinates.
@@ -433,6 +427,7 @@ def toWorldCoords(self, tuplePoint):
433427
v = Vector(tuplePoint[0],tuplePoint[1],tuplePoint[2])
434428
return Vector(self.rG.multiply(v.wrapped))
435429

430+
436431
def rotated(self,rotate=Vector(0,0,0)):
437432
"""
438433
returns a copy of this plane, rotated about the specified axes, as measured from horizontal
@@ -453,7 +448,7 @@ def rotated(self,rotate=Vector(0,0,0)):
453448
rotate = rotate.multiply(math.pi / 180.0 )
454449

455450
#compute rotation matrix
456-
m = Base.Matrix()
451+
m = FreeCAD.Base.Matrix()
457452
m.rotateX(rotate.x)
458453
m.rotateY(rotate.y)
459454
m.rotateZ(rotate.z)
@@ -473,7 +468,7 @@ def _calcTransforms(self):
473468
#ok i will be really honest-- i cannot understand exactly why this works
474469
#something bout the order of the transaltion and the rotation.
475470
# the double-inverting is strange, and i dont understand it.
476-
r = Base.Matrix()
471+
r = FreeCAD.Base.Matrix()
477472

478473
#forward transform must rotate and adjust for origin
479474
(r.A11, r.A12, r.A13 ) = (self.xDir.x, self.xDir.y, self.xDir.z )
@@ -484,7 +479,14 @@ def _calcTransforms(self):
484479
(invR.A14,invR.A24,invR.A34) = (self.origin.x,self.origin.y,self.origin.z)
485480

486481
( self.rG,self.fG ) = ( invR,invR.inverse() )
487-
482+
483+
def computeTransform(self,tMatrix):
484+
"""
485+
Computes the 2-d projection of the supplied matrix
486+
"""
487+
488+
rm = self.fG.multiply(tMatrix.wrapped).multiply(self.rG)
489+
return Matrix(rm)
488490

489491
class BoundBox(object):
490492
"A BoundingBox for an object or set of objects. Wraps the FreeCAD one"

0 commit comments

Comments
 (0)