<s> """<STR_LIT>""" <EOL> import math <EOL> import cadquery <EOL> import FreeCAD <EOL> import Part as FreeCADPart <EOL> def sortWiresByBuildOrder ( wireList , plane , result = [ ] ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> remainingWires = list ( wireList ) <EOL> while remainingWires : <EOL> outerWire = remainingWires . pop ( <NUM_LIT:0> ) <EOL> group = [ outerWire ] <EOL> otherWires = list ( remainingWires ) <EOL> for w in otherWires : <EOL> if plane . isWireInside ( outerWire , w ) : <EOL> group . append ( w ) <EOL> remainingWires . remove ( w ) <EOL> result . append ( group ) <EOL> return result <EOL> class Vector ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> if len ( args ) == <NUM_LIT:3> : <EOL> fV = FreeCAD . Base . Vector ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] , args [ <NUM_LIT:2> ] ) <EOL> elif len ( args ) == <NUM_LIT:1> : <EOL> if isinstance ( args [ <NUM_LIT:0> ] , Vector ) : <EOL> fV = args [ <NUM_LIT:0> ] . wrapped <EOL> elif isinstance ( args [ <NUM_LIT:0> ] , tuple ) : <EOL> fV = FreeCAD . Base . Vector ( args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , args [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , args [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] ) <EOL> elif isinstance ( args [ <NUM_LIT:0> ] , FreeCAD . Base . Vector ) : <EOL> fV = args [ <NUM_LIT:0> ] <EOL> else : <EOL> fV = args [ <NUM_LIT:0> ] <EOL> elif len ( args ) == <NUM_LIT:0> : <EOL> fV = FreeCAD . Base . Vector ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> self . _wrapped = fV <EOL> @ property <EOL> def x ( self ) : <EOL> return self . wrapped . x <EOL> @ property <EOL> def y ( self ) : <EOL> return self . wrapped . y <EOL> @ property <EOL> def z ( self ) : <EOL> return self . wrapped . z <EOL> @ property <EOL> def Length ( self ) : <EOL> return self . wrapped . Length <EOL> @ property <EOL> def wrapped ( self ) : <EOL> return self . _wrapped <EOL> def toTuple ( self ) : <EOL> return ( self . x , self . y , self . z ) <EOL> def cross ( self , v ) : <EOL> return Vector ( self . wrapped . cross ( v . wrapped ) ) <EOL> def dot ( self , v ) : <EOL> return self . wrapped . dot ( v . wrapped ) <EOL> def sub ( self , v ) : <EOL> return Vector ( self . wrapped . sub ( v . wrapped ) ) <EOL> def add ( self , v ) : <EOL> return Vector ( self . wrapped . add ( v . wrapped ) ) <EOL> def multiply ( self , scale ) : <EOL> """<STR_LIT>""" <EOL> tmp_fc_vector = FreeCAD . Base . Vector ( self . wrapped ) <EOL> return Vector ( tmp_fc_vector . multiply ( scale ) ) <EOL> def normalized ( self ) : <EOL> """<STR_LIT>""" <EOL> tmp_fc_vector = FreeCAD . Base . Vector ( self . wrapped ) <EOL> tmp_fc_vector . normalize ( ) <EOL> return Vector ( tmp_fc_vector ) <EOL> def Center ( self ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> def getAngle ( self , v ) : <EOL> return self . wrapped . getAngle ( v . wrapped ) <EOL> def distanceToLine ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def projectToLine ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def distanceToPlane ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def projectToPlane ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def __add__ ( self , v ) : <EOL> return self . add ( v ) <EOL> def __repr__ ( self ) : <EOL> return self . wrapped . __repr__ ( ) <EOL> def __str__ ( self ) : <EOL> return self . wrapped . __str__ ( ) <EOL> def __ne__ ( self , other ) : <EOL> return self . wrapped . __ne__ ( other ) <EOL> def __eq__ ( self , other ) : <EOL> return self . wrapped . __eq__ ( other ) <EOL> class Matrix : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , matrix = None ) : <EOL> if matrix is None : <EOL> self . wrapped = FreeCAD . Base . Matrix ( ) <EOL> else : <EOL> self . wrapped = matrix <EOL> def rotateX ( self , angle ) : <EOL> self . wrapped . rotateX ( angle ) <EOL> def rotateY ( self , angle ) : <EOL> self . wrapped . rotateY ( angle ) <EOL> class Plane ( object ) : <EOL> """<STR_LIT>""" <EOL> @ classmethod <EOL> def named ( cls , stdName , origin = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> """<STR_LIT>""" <EOL> namedPlanes = { <EOL> '<STR_LIT>' : Plane ( origin , ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) ) , <EOL> '<STR_LIT>' : Plane ( origin , ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) , <EOL> '<STR_LIT>' : Plane ( origin , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) , ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) ) , <EOL> '<STR_LIT>' : Plane ( origin , ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , - <NUM_LIT:1> , <NUM_LIT:0> ) ) , <EOL> '<STR_LIT>' : Plane ( origin , ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:1> ) ) , <EOL> '<STR_LIT>' : Plane ( origin , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) , ( - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) , <EOL> '<STR_LIT>' : Plane ( origin , ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) ) , <EOL> '<STR_LIT>' : Plane ( origin , ( - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:1> ) ) , <EOL> '<STR_LIT:left>' : Plane ( origin , ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) , ( - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) , <EOL> '<STR_LIT:right>' : Plane ( origin , ( <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:1> ) , ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) , <EOL> '<STR_LIT>' : Plane ( origin , ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) ) , <EOL> '<STR_LIT>' : Plane ( origin , ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) , ( <NUM_LIT:0> , - <NUM_LIT:1> , <NUM_LIT:0> ) ) <EOL> } <EOL> try : <EOL> return namedPlanes [ stdName ] <EOL> except KeyError : <EOL> raise ValueError ( '<STR_LIT>' . format ( <EOL> namedPlanes . keys ( ) ) ) <EOL> @ classmethod <EOL> def XY ( cls , origin = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , xDir = Vector ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> plane = Plane . named ( '<STR_LIT>' , origin ) <EOL> plane . _setPlaneDir ( xDir ) <EOL> return plane <EOL> @ classmethod <EOL> def YZ ( cls , origin = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , xDir = Vector ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) ) : <EOL> plane = Plane . named ( '<STR_LIT>' , origin ) <EOL> plane . _setPlaneDir ( xDir ) <EOL> return plane <EOL> @ classmethod <EOL> def ZX ( cls , origin = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , xDir = Vector ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) ) : <EOL> plane = Plane . named ( '<STR_LIT>' , origin ) <EOL> plane . _setPlaneDir ( xDir ) <EOL> return plane <EOL> @ classmethod <EOL> def XZ ( cls , origin = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , xDir = Vector ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> plane = Plane . named ( '<STR_LIT>' , origin ) <EOL> plane . _setPlaneDir ( xDir ) <EOL> return plane <EOL> @ classmethod <EOL> def YX ( cls , origin = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , xDir = Vector ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ) ) : <EOL> plane = Plane . named ( '<STR_LIT>' , origin ) <EOL> plane . _setPlaneDir ( xDir ) <EOL> return plane <EOL> @ classmethod <EOL> def ZY ( cls , origin = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , xDir = Vector ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) ) : <EOL> plane = Plane . named ( '<STR_LIT>' , origin ) <EOL> plane . _setPlaneDir ( xDir ) <EOL> return plane <EOL> @ classmethod <EOL> def front ( cls , origin = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , xDir = Vector ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> plane = Plane . named ( '<STR_LIT>' , origin ) <EOL> plane . _setPlaneDir ( xDir ) <EOL> return plane <EOL> @ classmethod <EOL> def back ( cls , origin = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , xDir = Vector ( - <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> plane = Plane . named ( '<STR_LIT>' , origin ) <EOL> plane . _setPlaneDir ( xDir ) <EOL> return plane <EOL> @ classmethod <EOL> def left ( cls , origin = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , xDir = Vector ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ) ) : <EOL> plane = Plane . named ( '<STR_LIT:left>' , origin ) <EOL> plane . _setPlaneDir ( xDir ) <EOL> return plane <EOL> @ classmethod <EOL> def right ( cls , origin = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , xDir = Vector ( <NUM_LIT:0> , <NUM_LIT:0> , - <NUM_LIT:1> ) ) : <EOL> plane = Plane . named ( '<STR_LIT:right>' , origin ) <EOL> plane . _setPlaneDir ( xDir ) <EOL> return plane <EOL> @ classmethod <EOL> def top ( cls , origin = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , xDir = Vector ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> plane = Plane . named ( '<STR_LIT>' , origin ) <EOL> plane . _setPlaneDir ( xDir ) <EOL> return plane <EOL> @ classmethod <EOL> def bottom ( cls , origin = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) , xDir = Vector ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> plane = Plane . named ( '<STR_LIT>' , origin ) <EOL> plane . _setPlaneDir ( xDir ) <EOL> return plane <EOL> def __init__ ( self , origin , xDir , normal ) : <EOL> """<STR_LIT>""" <EOL> normal = Vector ( normal ) <EOL> if ( normal . Length == <NUM_LIT:0.0> ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . zDir = normal . normalized ( ) <EOL> xDir = Vector ( xDir ) <EOL> if ( xDir . Length == <NUM_LIT:0.0> ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . _setPlaneDir ( xDir ) <EOL> self . invZDir = self . zDir . multiply ( - <NUM_LIT:1.0> ) <EOL> self . origin = origin <EOL> @ property <EOL> def origin ( self ) : <EOL> return self . _origin <EOL> @ origin . setter <EOL> def origin ( self , value ) : <EOL> self . _origin = Vector ( value ) <EOL> self . _calcTransforms ( ) <EOL> def setOrigin2d ( self , x , y ) : <EOL> """<STR_LIT>""" <EOL> self . origin = self . toWorldCoords ( ( x , y ) ) <EOL> def isWireInside ( self , baseWire , testWire ) : <EOL> """<STR_LIT>""" <EOL> tBaseWire = baseWire . transformGeometry ( self . fG ) <EOL> tTestWire = testWire . transformGeometry ( self . fG ) <EOL> bb = tBaseWire . BoundingBox ( ) <EOL> tb = tTestWire . BoundingBox ( ) <EOL> return bb == BoundBox . findOutsideBox2D ( bb , tb ) <EOL> def toLocalCoords ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( obj , Vector ) : <EOL> return Vector ( self . fG . multiply ( obj . wrapped ) ) <EOL> elif isinstance ( obj , cadquery . Shape ) : <EOL> return obj . transformShape ( self . rG ) <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" . format ( <EOL> type ( obj ) ) ) <EOL> def toWorldCoords ( self , tuplePoint ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( tuplePoint , Vector ) : <EOL> v = tuplePoint <EOL> elif len ( tuplePoint ) == <NUM_LIT:2> : <EOL> v = Vector ( tuplePoint [ <NUM_LIT:0> ] , tuplePoint [ <NUM_LIT:1> ] , <NUM_LIT:0> ) <EOL> else : <EOL> v = Vector ( tuplePoint ) <EOL> return Vector ( self . rG . multiply ( v . wrapped ) ) <EOL> def rotated ( self , rotate = ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) ) : <EOL> """<STR_LIT>""" <EOL> rotate = Vector ( rotate ) <EOL> rotate = rotate . multiply ( math . pi / <NUM_LIT> ) <EOL> m = FreeCAD . Base . Matrix ( ) <EOL> m . rotateX ( rotate . x ) <EOL> m . rotateY ( rotate . y ) <EOL> m . rotateZ ( rotate . z ) <EOL> newXdir = Vector ( m . multiply ( self . xDir . wrapped ) ) <EOL> newZdir = Vector ( m . multiply ( self . zDir . wrapped ) ) <EOL> return Plane ( self . origin , newXdir , newZdir ) <EOL> def rotateShapes ( self , listOfShapes , rotationMatrix ) : <EOL> """<STR_LIT>""" <EOL> resultWires = [ ] <EOL> for w in listOfShapes : <EOL> mirrored = w . transformGeometry ( rotationMatrix . wrapped ) <EOL> if ( ( mirrored . wrapped . Vertexes [ <NUM_LIT:0> ] . X == w . wrapped . Vertexes [ <NUM_LIT:0> ] . X and <EOL> mirrored . wrapped . Vertexes [ <NUM_LIT:0> ] . Y == w . wrapped . Vertexes [ <NUM_LIT:0> ] . Y and <EOL> mirrored . wrapped . Vertexes [ <NUM_LIT:0> ] . Z == w . wrapped . Vertexes [ <NUM_LIT:0> ] . Z ) or <EOL> ( mirrored . wrapped . Vertexes [ <NUM_LIT:0> ] . X == w . wrapped . Vertexes [ - <NUM_LIT:1> ] . X and <EOL> mirrored . wrapped . Vertexes [ <NUM_LIT:0> ] . Y == w . wrapped . Vertexes [ - <NUM_LIT:1> ] . Y and <EOL> mirrored . wrapped . Vertexes [ <NUM_LIT:0> ] . Z == w . wrapped . Vertexes [ - <NUM_LIT:1> ] . Z ) ) : <EOL> resultWires . append ( mirrored ) <EOL> else : <EOL> aEdges = w . wrapped . Edges <EOL> aEdges . extend ( mirrored . wrapped . Edges ) <EOL> comp = FreeCADPart . Compound ( aEdges ) <EOL> mirroredWire = comp . connectEdgesToWires ( False ) . Wires [ <NUM_LIT:0> ] <EOL> resultWires . append ( cadquery . Shape . cast ( mirroredWire ) ) <EOL> return resultWires <EOL> def _setPlaneDir ( self , xDir ) : <EOL> """<STR_LIT>""" <EOL> if ( self . zDir . dot ( xDir ) > <NUM_LIT> ) : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> xDir = Vector ( xDir ) <EOL> self . xDir = xDir . normalized ( ) <EOL> self . yDir = self . zDir . cross ( self . xDir ) . normalized ( ) <EOL> def _calcTransforms ( self ) : <EOL> """<STR_LIT>""" <EOL> r = FreeCAD . Base . Matrix ( ) <EOL> ( r . A11 , r . A12 , r . A13 ) = ( self . xDir . x , self . xDir . y , self . xDir . z ) <EOL> ( r . A21 , r . A22 , r . A23 ) = ( self . yDir . x , self . yDir . y , self . yDir . z ) <EOL> ( r . A31 , r . A32 , r . A33 ) = ( self . zDir . x , self . zDir . y , self . zDir . z ) <EOL> invR = r . inverse ( ) <EOL> invR . A14 = self . origin . x <EOL> invR . A24 = self . origin . y <EOL> invR . A34 = self . origin . z <EOL> self . rG = invR <EOL> self . fG = invR . inverse ( ) <EOL> def computeTransform ( self , tMatrix ) : <EOL> """<STR_LIT>""" <EOL> return Matrix ( self . fG . multiply ( tMatrix . wrapped ) . multiply ( self . rG ) ) <EOL> class BoundBox ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , bb ) : <EOL> self . wrapped = bb <EOL> self . xmin = bb . XMin <EOL> self . xmax = bb . XMax <EOL> self . xlen = bb . XLength <EOL> self . ymin = bb . YMin <EOL> self . ymax = bb . YMax <EOL> self . ylen = bb . YLength <EOL> self . zmin = bb . ZMin <EOL> self . zmax = bb . ZMax <EOL> self . zlen = bb . ZLength <EOL> self . center = Vector ( bb . Center ) <EOL> self . DiagonalLength = bb . DiagonalLength <EOL> def add ( self , obj ) : <EOL> """<STR_LIT>""" <EOL> tmp = FreeCAD . Base . BoundBox ( self . wrapped ) <EOL> if isinstance ( obj , tuple ) : <EOL> tmp . add ( obj [ <NUM_LIT:0> ] , obj [ <NUM_LIT:1> ] , obj [ <NUM_LIT:2> ] ) <EOL> elif isinstance ( obj , Vector ) : <EOL> tmp . add ( obj . fV ) <EOL> elif isinstance ( obj , BoundBox ) : <EOL> tmp . add ( obj . wrapped ) <EOL> return BoundBox ( tmp ) <EOL> @ classmethod <EOL> def findOutsideBox2D ( cls , b1 , b2 ) : <EOL> """<STR_LIT>""" <EOL> fc_bb1 = b1 . wrapped <EOL> fc_bb2 = b2 . wrapped <EOL> if ( fc_bb1 . XMin < fc_bb2 . XMin and <EOL> fc_bb1 . XMax > fc_bb2 . XMax and <EOL> fc_bb1 . YMin < fc_bb2 . YMin and <EOL> fc_bb1 . YMax > fc_bb2 . YMax ) : <EOL> return b1 <EOL> if ( fc_bb2 . XMin < fc_bb1 . XMin and <EOL> fc_bb2 . XMax > fc_bb1 . XMax and <EOL> fc_bb2 . YMin < fc_bb1 . YMin and <EOL> fc_bb2 . YMax > fc_bb1 . YMax ) : <EOL> return b2 <EOL> return None <EOL> def isInside ( self , anotherBox ) : <EOL> """<STR_LIT>""" <EOL> return self . wrapped . isInside ( anotherBox . wrapped ) </s>
