-
Notifications
You must be signed in to change notification settings - Fork 0
/
EquationPlotter.lua
167 lines (157 loc) · 4.42 KB
/
EquationPlotter.lua
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
-- VECTRIC LUA SCRIPT
require "strict"
a = 0
y = 0
xMin = 0
xMax = 1
res = 0.026
equation = ""
g_dialogHtml = [[]]
.. [[<body bgcolor="#CCCCFF">]]
.. [[<table>]]
.. [[<tr><td width="30%"><b>Equation</b></td><td><input id="equation" name="textfield" type="text"></td></tr>]]
.. [[<tr><td width="30%"><b>X Minimum</b></td><td><input id="xmin" name="textfield" type="text"></td></tr>]]
.. [[<tr><td width="30%"><b>X Maximum</b></td><td><input id="xmax" name="textfield" type="text"></td></tr>]]
.. [[<tr><td width="30%"><b>Resolution</b></td><td><input id="res" name="textfield" type="text"></td></tr>]]
.. [[</table>]]
.. [[<input id="PlotEQ" class="LuaButton" name="PlotEQ" type="button" value="Plot">]]
.. [[</body>]]
function OnLuaButton_PlotEQ(dialog)
local job = VectricJob()
if not job.Exists then
return true
end
if not UpdateOptionsFromDialog(dialog) then
return true
end
PlotPoints()
job:Refresh2DView()
return true
end
function UpdateOptionsFromDialog(dialog)
equation = dialog:GetTextField("equation")
equation = equationParse(equation)
xMin = dialog:GetDoubleField("xMin")
xMax = dialog:GetDoubleField("xMax")
res = dialog:GetDoubleField("res")
equation = "return function(x) return " .. equation .. " end"
return true
end
function equationParse(equation)
local eq = equation
eq = string.gsub(eq,"[math%.]-sin","math.sin")
eq = string.gsub(eq,"[math%.]-cos","math.cos")
eq = string.gsub(eq,"[math%.]-tan","math.tan")
eq = string.gsub(eq,"[math%.]-asin","math.asin")
eq = string.gsub(eq,"[math%.]-acos","math.acos")
eq = string.gsub(eq,"[math%.]-atan","math.atan")
eq = string.gsub(eq,"[math%.]-deg","math.deg")
eq = string.gsub(eq,"[math%.]-degree","math.deg")
eq = string.gsub(eq,"[math%.]-degrees","math.deg")
eq = string.gsub(eq,"[math%.]-rad","math.radians")
eq = string.gsub(eq,"[math%.]-radian","math.radians")
eq = string.gsub(eq,"[math%.]-radians","math.radians")
eq = string.gsub(eq,"[math%.]-exp","math.exp")
eq = string.gsub(eq,"[math%.]-log","math.log")
eq = string.gsub(eq,"[math%.]-log10","math.log10")
eq = string.gsub(eq,"[math%.]-pi","math.pi")
return eq
end
function main(script_path)
local retry_dialog = true
local job = VectricJob()
if not job.Exists then
DisplayMessageBox("There is no existing job open")
return false;
end
job:Refresh2DView()
local frmMain = HTML_Dialog(true, g_dialogHtml, 350, 200, "Equation Plotter")
frmMain:AddTextField("equation", equation)
frmMain:AddDoubleField("xMin", xMin)
frmMain:AddDoubleField("xMax", xMax)
frmMain:AddDoubleField("res", res)
frmMain:ShowDialog()
return true
end
function findNextPt(a)
local x1 = a
local y1 = calcY(x1)
local x2 = 0
local y2 = 0
local length = res
local resVar = res
local done = 0
while done < 1 do
x2 = x1 + resVar
y2 = calcY(x2)
length = math.sqrt(math.abs(math.pow(x2-x1,2))+math.abs(math.pow(y2-y1,2)))
if length >= res * 0.975 and length <= res*1.025 then
done = 1
end
resVar = resVar * (res/length)
end
return x2
end
function PlotPoints()
local mydoc = VectricJob()
local linePTs = {}
local MyLine
if not mydoc.Exists then
MessageBox("There is no existing doc loaded")
return false;
end
local done = 0
local xbound = xMax
a = xMin
y = calcY(a)
local i = 0
while done < 1 do
linePTs[i] = Point2D(1*a,y)
a = findNextPt(a,res)
y = calcY(a)
if a > xbound then
done = 1
end
i= i + 1
end
MyLine = Draw_Line(mydoc,linePTs)
Refresh(mydoc,MyLine,"Sprial")
return true
end
function calcY(xVal)
a = xVal
local func, err = load(equation)
if func then
local ok, calc = pcall(func)
if ok then
y = calc(a)
else
print("Execution error:", calc)
end
else
print("Compilation error:", err)
end
return y
end
function Draw_Line(doc,XYPoints)
local MyContour = Contour(0.0)
local p1 = XYPoints[0]
MyContour:AppendPoint(p1)
for i=1, #XYPoints do
local p2 = XYPoints[i]
MyContour:LineTo(p2)
end
return MyContour;
end
function Refresh (doc,Contour,LayerName)
local cad_object = CreateCadContour(Contour)
local cur_layer = doc.LayerManager:GetActiveLayer()
local layer = doc.LayerManager:GetLayerWithName(LayerName)
layer:AddObject(cad_object, true)
layer.Colour = 0
layer.Visible = true
doc.LayerManager:SetActiveLayer(cur_layer)
doc.Selection:Add(cad_object, true, false)
doc:Refresh2DView()
return true
end