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