Package theia :: Package rendering :: Module writer
[hide private]
[frames] | no frames]

Source Code for Module theia.rendering.writer

  1  '''Writer module for theia, to write CAD content to files.''' 
  2   
  3  # Provides: 
  4  #   writeToCAD 
  5  #   writeTree 
  6   
  7  import FreeCAD as App 
  8  from FreeCAD import Base 
  9  import Part 
 10  from .shapes import mirrorShape, lensShape, beamDumpShape, ghostShape, beamShape 
 11  from .features import FCMirror, FCLens, FCBeamDump, FCBeam 
 12   
13 -def writeToCAD(component, doc):
14 '''Write the relevant shape and feature content of components in CAD file. 15 16 This function is for everython except for beams. 17 To the doc .fcstd file are added two objects, one of type 18 App::FeaturePython which will hold the internal data of the component 19 for reviewing in the side panel of FreeCAD, and one of type 20 Part::Feature for visualization. The classes for the App::FeaturePython 21 objects are i nthe features modules, and those for the shapes are in 22 the shapes module. 23 The important functions are the PythonFeatures 24 constructors found in features, and the shape functions found in shapes. 25 26 component: component to represent. [Mirror, Lens, BeamDump, Ghost, Beam] 27 doc: CAD file to write to. [CAD file] 28 29 No return value. 30 31 ''' 32 33 # here are some dics to refer to the right features and shapes for all 34 # components 35 fact = 0.001 #factor for units in CAD 36 FCDic = {'Mirror': FCMirror, 37 'ThickLens': FCLens, 38 'ThinLens': FCLens, 39 'BeamDump': FCBeamDump} 40 41 shapeDic = {'Mirror': mirrorShape, 42 'ThickLens': lensShape, 43 'ThinLens': lensShape, 44 'BeamDump': beamDumpShape, 45 'Ghost': ghostShape} 46 47 #First take care of optics 48 if component.Name in ['Mirror', 'ThickLens', 'ThinLens', 'BeamDump']: 49 # First feature (not for ghosts) 50 if not component.Name == 'Ghost': 51 featureObj = doc.addObject("App::FeaturePython", 52 component.Ref + '_doc') 53 FCDic[component.Name](featureObj, component) 54 55 # Then shape 56 shapeObj = doc.addObject("Part::Feature", component.Ref) 57 shapeObj.Shape = shapeDic[component.Name](component) 58 shapeObj.Placement.Base = Base.Vector(tuple(component.HRCenter/fact)) 59 60 # Then tree (call write tree function) 61 if component.Name == 'BeamTree': 62 writeTree(component, doc)
63 64
65 -def writeTree(tree, doc):
66 '''Recursively write the shape and feature content of the beams of a tree. 67 68 If the tree's root is not None, write the shape and feature for tree.Root 69 and start over for the daughter trees. 70 71 tree: beamtree to write the info. [BeamTree] 72 doc: CAD file to write to. [CAD file] 73 74 No return value. 75 76 ''' 77 fact = 0.001 #factor for units in CAD 78 79 if tree.Root is not None: 80 # write feature of beam 81 featureObj = doc.addObject("App::FeaturePython", tree.Root.Ref + '_doc') 82 FCBeam(featureObj, tree.Root) 83 # write shape of beam 84 shapeObj = doc.addObject("Part::Feature", tree.Root.Ref) 85 shapeObj.Shape = beamShape(tree.Root) 86 shapeObj.Placement.Base = Base.Vector(tuple(tree.Root.Pos/fact)) 87 # write laser shape object 88 if 't' not in tree.Root.Ref and 'r' not in tree.Root.Ref: 89 laserObj = doc.addObject("Part::Feature", 'laser') 90 laserObj.Shape = Part.makeCylinder(0.01/fact, 0.1/fact, 91 Base.Vector(0,0,0), 92 Base.Vector(tuple(-tree.Root.Dir))) 93 laserObj.Placement.Base = Base.Vector(tuple(tree.Root.Pos/fact)) 94 #recursively for daughter beams 95 if tree.T is not None: 96 writeTree(tree.T, doc) 97 if tree.R is not None: 98 writeTree(tree.R, doc)
99