1 '''Shapes module for theia, provides shape-calculating for 3D rendering.'''
2
3
4
5
6
7
8
9
10 import numpy as np
11 import Part
12 from FreeCAD import Base
13 from ..helpers import settings
14 from ..helpers.units import deg
15
17 '''Computes the 3D representation of the mirror, a shape for a CAD file obj.
18
19 beam: beam to represent. [GaussianBeam]
20
21 Returns a shape for a CAD file object.
22
23 '''
24 fact = settings.FCFactor
25 return Part.makeCylinder((mirror.Dia/2.)/fact, mirror.Thick/fact,
26 Base.Vector(0,0,0),
27 Base.Vector(tuple(-mirror.HRNorm)))
28
30 '''Computes the 3D representation of the beamsplitter, for a CAD file obj.
31
32 beam: beam to represent. [GaussianBeam]
33
34 Returns a shape for a CAD file object.
35
36 '''
37 fact = settings.FCFactor
38 return Part.makeCylinder((bs.Dia/2.)/fact, bs.Thick/fact,
39 Base.Vector(0,0,0),
40 Base.Vector(tuple(-bs.HRNorm)))
41
42
44 '''Computes the 3D representation of the lens, a shape for a CAD file obj.
45
46 lens: lens to represent. [GaussianBeam]
47
48 Returns a shape for a CAD file object.
49
50 '''
51 fact = settings.FCFactor
52 return Part.makeCylinder((lens.Dia/2.)/fact, max(lens.Thick/fact,0.01/fact),
53 Base.Vector(0,0,0),
54 Base.Vector(tuple(-lens.HRNorm)))
55
57 '''Computes the 3D representation of the beamdump, for a CAD file obj.
58
59 beam: beam to represent. [GaussianBeam]
60
61 Returns a shape for a CAD file object.
62
63 '''
64 fact = settings.FCFactor
65 return Part.makeCylinder((beamDump.Dia/2.)/fact, beamDump.Thick/fact,
66 Base.Vector(0,0,0),
67 Base.Vector(tuple(-beamDump.HRNorm)))
68
70 '''Computes the 3D representation of the beam, a shape for a CAD file obj.
71
72 beam: beam to represent. [GaussianBeam]
73
74 Returns a shape for a CAD file object.
75
76 '''
77 fact = settings.FCFactor
78 L = beam.Length if beam.Length > 0. else 1.e6
79
80
81 theta = np.real(beam.QParam()[2])
82 Wx = beam.Wx
83 Wy = beam.Wy
84
85 DWx = beam.DWx
86 DWy = beam.DWy
87 Ux0 = beam.U[0]
88 Uy0 = beam.U[1]
89
90
91 Ux = np.cos(theta) * Ux0 + np.sin(theta) * Uy0
92 Uy = -np.sin(theta) * Ux0 + np.cos(theta) * Uy0
93
94
95 Xalpha = (beam.Wl/beam.N)/(np.pi * Wx)
96 Xc = np.tan(Xalpha/2.)
97 Yalpha = (beam.Wl/beam.N)/(np.pi * Wy)
98 Yc = np.tan(Yalpha/2.)
99 E2 = L * beam.Dir/fact
100
101 XA1 = Base.Vector(tuple( - DWx * Xc * Ux/fact))
102 XB1 = Base.Vector(tuple(DWx * Xc * Ux/fact \
103 + (0.01 * Ux if DWx == 0. else 0.)))
104 XA2 = Base.Vector(tuple(E2 + (L - DWx) * Xc * Ux/fact))
105 XB2 = Base.Vector(tuple(E2 - (L - DWx) * Xc * Ux/fact \
106 +(0.01 * Ux if (L -DWx) == 0. else 0.)))
107
108 YA1 = Base.Vector(tuple( - DWy * Yc * Uy/fact))
109 YB1 = Base.Vector(tuple( + DWy * Yc * Uy/fact \
110 + (0.01 * Ux if DWx == 0. else 0.)))
111 YA2 = Base.Vector(tuple(E2 + (L - DWy) * Yc * Uy/fact))
112 YB2 = Base.Vector(tuple(E2 - (L - DWy) * Yc * Uy/fact \
113 +(0.01 * Ux if (L -DWx) == 0. else 0.)))
114
115 L1 = Part.Line(XA1, XB1).toShape()
116 L2 = Part.Line(XB1, XB2).toShape()
117 L3 = Part.Line(XB2, XA2).toShape()
118 L4 = Part.Line(XA1, XA2).toShape()
119 L5 = Part.Line(YA1, YB1).toShape()
120 L6 = Part.Line(YB1, YB2).toShape()
121 L7 = Part.Line(YA2, YB2).toShape()
122 L8 = Part.Line(YA1, YA2).toShape()
123
124 try:
125 S1 = Part.Face(Part.Wire([L1, L2, L3, L4]))
126 except Part.OCCError:
127 S1 = L1.fuse(L2).fuse(L3).fuse(L4)
128
129 try:
130 S2 = Part.Face(Part.Wire([L5, L6, L7, L8]))
131 except Part.OCCError:
132 S2 = L5.fuse(L6).fuse(L7).fuse(L8)
133
134 try:
135 return S1.fuse(S2)
136 except Part.OCCError:
137 return Part.makeLine(Base.Vector(0., 0., 0.),
138 Base.Vector(tuple(L * beam.Dir/fact)))
139