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 .shapes import mirrorShape, lensShape, beamDumpShape, ghostShape, beamShape
11 from .features import FCMirror, FCLens, FCBeamDump, FCBeam
12
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
34
35 fact = 0.001
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
48 if component.Name in ['Mirror', 'ThickLens', 'ThinLens', 'BeamDump']:
49
50 if not component.Name == 'Ghost':
51 featureObj = doc.addObject("App::FeaturePython",
52 component.Ref + '_doc')
53 FCDic[component.Name](featureObj, component)
54
55
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
61 if component.Name == 'BeamTree':
62 writeTree(component, doc)
63
64
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
78
79 if tree.Root is not None:
80
81 featureObj = doc.addObject("App::FeaturePython", tree.Root.Ref + '_doc')
82 FCBeam(featureObj, tree.Root)
83
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
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
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