forked from chbergmann/OpticsWorkbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OpticsWorkbench.py
122 lines (103 loc) · 3.76 KB
/
OpticsWorkbench.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# -*- coding: utf-8 -*-
import os
from FreeCAD import Vector, Rotation, activeDocument
import Ray
import OpticalObject
from numpy import linspace
from importlib import reload
def recompute():
activeDocument().recompute()
def get_module_path():
''' Returns the current module path.
Determines where this file is running from, so works regardless of whether
the module is installed in the app's module directory or the user's app data folder.
(The second overrides the first.)
'''
return os.path.dirname(__file__)
def makeRay(position = Vector(0, 0, 0),
direction = Vector(1, 0, 0),
power = True,
beamNrColumns = 1,
beamNrRows = 1,
beamDistance = 0.1,
spherical = False,
hideFirst = False,
maxRayLength = 1000000,
maxNrReflections = 200,
wavelength = 580):
reload(Ray)
'''Python command to create a light ray.'''
name = 'Ray'
if beamNrColumns * beamNrRows > 1:
name = 'Beam'
fp = activeDocument().addObject('Part::FeaturePython', name)
fp.Placement.Base = position
fp.Placement.Rotation = Rotation(Vector(1, 0, 0), direction)
Ray.RayWorker(fp, power, spherical, beamNrColumns, beamNrRows, beamDistance, hideFirst, maxRayLength, maxNrReflections, wavelength)
Ray.RayViewProvider(fp.ViewObject)
recompute()
return fp
def makeSunRay(position = Vector(0, 0, 0),
direction = Vector(1, 0, 0),
power = True,
hideFirst = False,
maxRayLength = 1000000,
maxNrReflections = 200,
wavelength_from = 400,
wavelength_to = 800,
num_rays = 100):
reload(Ray)
doc = activeDocument()
rays = []
for l in linspace(wavelength_from, wavelength_to, num_rays):
ray = makeRay(position = position,
direction = direction,
power = power,
hideFirst = hideFirst,
maxRayLength = maxRayLength,
maxNrReflections = maxNrReflections,
wavelength = l)
ray.ViewObject.LineWidth = 1
rays.append(ray)
group = doc.addObject('App::DocumentObjectGroup','SunRay')
group.Group = rays
recompute()
def restartAll():
for obj in activeDocument().Objects:
if isRay(obj):
obj.Power = True
obj.touch()
recompute()
def allOff():
for obj in activeDocument().Objects:
if isRay(obj):
obj.Power = False
recompute()
def makeMirror(base = []):
reload(OpticalObject)
'''All FreeCAD objects in base will be optical mirrors.'''
fp = activeDocument().addObject('Part::FeaturePython', 'Mirror')
OpticalObject.OpticalObjectWorker(fp, base)
OpticalObject.OpticalObjectViewProvider(fp.ViewObject)
recompute()
return fp
def makeAbsorber(base = []):
reload(OpticalObject)
'''All FreeCAD objects in base will be optical light absorbers.'''
fp = activeDocument().addObject('Part::FeaturePython', 'Absorber')
OpticalObject.OpticalObjectWorker(fp, base, type = 'absorber')
OpticalObject.OpticalObjectViewProvider(fp.ViewObject)
recompute()
return fp
def makeLens(base = [], RefractionIndex = 0, material = 'Quartz'):
reload(OpticalObject)
'''All FreeCAD objects in base will be optical lenses.'''
fp = activeDocument().addObject('Part::FeaturePython', 'Lens')
OpticalObject.LensWorker(fp, base, RefractionIndex, material)
OpticalObject.OpticalObjectViewProvider(fp.ViewObject)
recompute()
return fp
def isRay(obj):
return hasattr(obj, 'Power') and hasattr(obj, 'BeamNrColumns')
def isOpticalObject(obj):
return obj.TypeId == 'Part::FeaturePython' and hasattr(obj, 'OpticalType') and hasattr(obj, 'Base')