Permalink
Browse files

Lua plugins

  • Loading branch information...
feragon committed Jul 13, 2016
1 parent 5e40d68 commit 6f772910eded2622164efac7dc321eff6da108be
View
@@ -199,6 +199,7 @@ void addLCBindings(lua_State *L) {
.beginClass<LuaInterface>("LuaInterface")
.addFunction("luaConnect", &LuaInterface::luaConnect)
.addFunction("connect", &LuaInterface::qtConnect)
.addFunction("pluginList", &LuaInterface::pluginList)
.endClass()
.beginExtendClass<LuaScript, QDockWidget>("LuaScript")
View
@@ -1,13 +1,6 @@
#include "luainterface.h"
LuaInterface::LuaInterface() {
_L = LuaIntf::LuaState::newState();
luaL_openlibs(_L);
luaOpenQtBridge(_L);
lua_openlckernel(_L);
LuaIntf::Lua::setGlobal(_L, "luaInterface", this);
}
LuaInterface::~LuaInterface() {
@@ -17,9 +10,21 @@ LuaInterface::~LuaInterface() {
}
void LuaInterface::initLua() {
std::string out;
QString luaFile = QCoreApplication::applicationDirPath() + "/path.lua";
_L.doFile(luaFile.toStdString().c_str());
_L = LuaIntf::LuaState::newState();
luaL_openlibs(_L);
luaOpenQtBridge(_L);
lua_openlckernel(_L);
LuaIntf::Lua::setGlobal(_L, "luaInterface", this);
QString luaFile = QCoreApplication::applicationDirPath() + "/path.lua";
int s = _L.doFile(luaFile.toStdString().c_str());
if (s != 0) {
std::cout << lua_tostring(_L, -1) << std::endl;
lua_pop(_L, 1);
}
}
bool LuaInterface::luaConnect(
@@ -87,4 +92,16 @@ void LuaInterface::hideUI(bool hidden) {
LuaIntf::LuaState LuaInterface::luaState() {
return _L;
}
std::vector<std::string> LuaInterface::pluginList(const char* path) {
std::vector<std::string> plugins;
QDir dir(path);
auto list = dir.entryList(QDir::Filter::Dirs | QDir::Filter::NoDotAndDotDot);
for(auto dir : list) {
plugins.push_back(dir.toStdString());
}
return plugins;
}
View
@@ -45,6 +45,8 @@ class LuaInterface {
static std::shared_ptr<QWidget> loadUiFile(const char* fileName);
std::vector<std::string> pluginList(const char* path);
void hideUI(bool hidden);
LuaIntf::LuaState luaState();
View
@@ -5,6 +5,7 @@ file(
"${PROJECT_BINARY_DIR}/lcUI/path.lua"
"package.path = package.path .. ';${CMAKE_CURRENT_LIST_DIR}/?.lua' \n"
"ui_path='${PROJECT_SOURCE_DIR}/lcUI/ui' \n"
"plugin_path='${PROJECT_SOURCE_DIR}/lcUILua/plugins' \n"
"require 'ui.init'"
)
@@ -14,6 +15,7 @@ if(WITH_UNITTESTS)
"${PROJECT_BINARY_DIR}/unittest/path.lua"
"package.path = package.path .. ';${CMAKE_CURRENT_LIST_DIR}/?.lua' \n"
"ui_path='${PROJECT_SOURCE_DIR}/lcUI/ui' \n"
"plugin_path='${PROJECT_SOURCE_DIR}/lcUILua/plugins' \n"
"require 'ui.init'"
)
endif()
@@ -1,4 +1,4 @@
require 'actions.event'
require 'kernel.event'
require 'actions.lineoperations'
require 'actions.circleoperations'
require 'actions.arcoperations'
File renamed without changes.
@@ -0,0 +1,48 @@
function createSandboxEnv()
local env = {}
for k, v in pairs(_G) do
env[k] = v
end
env.loadstring = function(str)
return load(str, str, "t", createSandboxEnv())
end
env.dofile = function(file)
return loadfile(file, "t", createSandboxEnv())
end
env.load = function(...)
print("Function load is disabled. Use loadstring instead.")
end
env.loadfile = function(...)
print("Function loadfile is disabled. Use dofile instead.")
end
env.require = function(...)
print("Function require is disabled.")
end
env.package = nil
env.io = nil
env.os = {
clock = os.clock,
difftime = os.difftime
}
return env
end
function load_plugins()
local plugins = luaInterface:pluginList(plugin_path)
for k, v in pairs(plugins) do
plugin, err = loadfile(plugin_path .. "/" .. v .. "/plugin.lua", "t", createSandboxEnv())
if(err == nil) then
plugin()
else
message(err)
end
end
end
@@ -0,0 +1,23 @@
layer = Layer("Fractal", Color(255,255,255,255))
function drawTree( ce, x1, y1, angle, depth)
if depth == 0 then return end;
local x2 = x1 + (math.cos(math.rad(angle)) * depth * 10.0);
local y2 = y1 + (math.sin(math.rad(angle)) * depth * 10.0);
ce:append(Line(Coord(x1, y1), Coord(x2, y2),layer));
drawTree(ce, x2, y2, angle - 20, depth - 1);
drawTree(ce, x2, y2, angle + 20, depth - 1);
end
local tab = toolbar:tabByName("Quick Access")
local group = tab:addGroup("Fractal")
local fractalButton = create_button("Fractal tree")
tab:addButton(group, fractalButton, 0, 0, 1, 1)
luaInterface:luaConnect(fractalButton, "pressed()", function()
local b=Builder(active_widget():document())
drawTree(b, 0, 0, 90, 14);
b:execute()
end)
@@ -0,0 +1,148 @@
local layer = Layer("Gear", Color(255,255,255,255))
local Gear = {}
Gear.__index = Gear
setmetatable(Gear, {
__call = function (cls, ...)
return cls.new(...)
end,
})
function Gear.new()
local self = setmetatable({}, Gear)
return self
end
function Gear:involute_intersect_angle(Rb, R)
return (math.sqrt(R*R - Rb*Rb) / (Rb)) - (math.acos(Rb / R))
end
function Gear:point_on_circle(radius, angle)
p = {}
p.x= radius * math.cos(angle)
p.y= radius * math.sin(angle)
return p
end
--ce CreateEntities object
--phi = pressure angle
--PC = Circular Pitch
--teeth = no of teeth
function Gear:calc(builder, N, phi, Pc)
-- Pitch Circle
local D = N * Pc / math.pi
local R = D / 2.0
-- Diametrial pitch
local Pd = N / D
-- Base Circle
local Db = D * math.cos(phi)
local Rb = Db / 2.0
-- Addendum
local a = 1.0 / Pd
-- Outside Circle
local Ro = R + a
local Do = 2 * Ro
-- Tooth thickness
local T = (math.pi * D) / (2 * N)
-- undercut?
local U = (2 / (math.sin(phi) * (math.sin(phi))))
local needs_undercut = N < U
-- sys.stderr.write("N:%s R:%s Rb:%s\n" % (N,R,Rb))
-- Clearance
local c = 0.0
-- Dedendum
local b = a + c
-- Root Circle
local Rr = R - b
local Dr = 2 * Rr
local two_pi = 2 * math.pi
local half_thick_angle = two_pi / (4 * N)
local pitch_to_base_angle = self:involute_intersect_angle(Rb, R)
local pitch_to_outer_angle = self:involute_intersect_angle(Rb, Ro) -- pitch_to_base_angle
local points ={}
for x=1,N do
local c = x * two_pi / N;
-- angles
local pitch1 = c - half_thick_angle
local base1 = pitch1 - pitch_to_base_angle
local outer1 = pitch1 + pitch_to_outer_angle
local pitch2 = c + half_thick_angle
local base2 = pitch2 + pitch_to_base_angle
local outer2 = pitch2 - pitch_to_outer_angle
-- points
local b1 = self:point_on_circle(Rb, base1)
local p1 = self:point_on_circle(R, pitch1)
local o1 = self:point_on_circle(Ro, outer1)
local o2 = self:point_on_circle(Ro, outer2)
local p2 = self:point_on_circle(R, pitch2)
local b2 = self:point_on_circle(Rb, base2)
if Rr >= Rb then
local pitch_to_root_angle = pitch_to_base_angle - self:involute_intersect_angle(Rb, Rr)
local root1 = pitch1 - pitch_to_root_angle
local root2 = pitch2 + pitch_to_root_angle
local r1 = self:point_on_circle(Rr, root1)
local r2 = self:point_on_circle(Rr, root2)
table.insert(points, r1)
table.insert(points, p1)
table.insert(points, o1)
table.insert(points, o2)
table.insert(points, p2)
table.insert(points, r2)
else
local r1 = self:point_on_circle(Rr, base1)
local r2 = self:point_on_circle(Rr, base2)
table.insert(points, r1)
table.insert(points, b1)
table.insert(points, p1)
table.insert(points, o1)
table.insert(points, o2)
table.insert(points, p2)
table.insert(points, b2)
table.insert(points, r2)
end
end
local first = points[1]
local fKeep = first
table.remove(points, 1)
for k,v in pairs(points) do
builder:append(Line(Coord(first.x,first.y), Coord(v.x,v.y),layer))
first=v
end
builder:append(Line(Coord(first.x,first.y), Coord(fKeep.x,fKeep.y),layer))
end
local tab = toolbar:tabByName("Quick Access")
local group = tab:addGroup("Gear")
local gearButton = create_button("Gear")
tab:addButton(group, gearButton, 0, 0, 1, 1)
luaInterface:luaConnect(gearButton, "pressed()", function()
local gear=Gear()
b=Builder(active_widget():document())
gear:calc(b, 20,math.rad(10),math.rad(10))
gear:calc(b, 10,math.rad(10),math.rad(10))
b:push()
b:scale(Coord(0,0),Coord(10,10))
b:execute()
end)
View
@@ -4,5 +4,7 @@ require 'ui.commandline'
require 'ui.toolbar'
require 'actions.operations'
require 'ui.operations'
require 'kernel.pluginmanager'
create_main_window()
create_main_window()
load_plugins()
@@ -27,7 +27,7 @@ local function create_cancel_button()
end
end
local function new_operation()
function new_operation()
finish_operation()
create_cancel_button()
@@ -192,8 +192,8 @@ void lua_openlckernel(lua_State* L) {
.addProperty("entityType", [](entity::Line*) {
return "line";
})
.addFunction("nearestPointOnEntity", &geo::Vector::nearestPointOnEntity)
.addFunction("nearestPointOnPath", &geo::Vector::nearestPointOnPath)
.addFunction("nearestPointOnEntity", &entity::Line::nearestPointOnEntity)
.addFunction("nearestPointOnPath", &entity::Line::nearestPointOnPath)
.addFunction("start", &geo::Vector::start)
.addFunction("finish", &geo::Vector::end) //"end" will make Lua crash
.endClass()

0 comments on commit 6f77291

Please sign in to comment.