Skip to content

Commit

Permalink
Lua trim lines
Browse files Browse the repository at this point in the history
  • Loading branch information
feragon committed Jul 6, 2016
1 parent 39d82b4 commit 40b888f
Show file tree
Hide file tree
Showing 13 changed files with 165 additions and 10 deletions.
Binary file added lcUI/ui/icons/modifytrim.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions lcUI/ui/resource.qrc
Expand Up @@ -21,5 +21,6 @@
<file alias="scale.png">icons/scale.png</file>
<file alias="delete.svg">icons/delete.svg</file>
<file alias="modifymove.png">icons/modifymove.png</file>
<file alias="modifytrim.png">icons/modifytrim.png</file>
</qresource>
</RCC>
1 change: 1 addition & 0 deletions lcUILua/actions/operations.lua
Expand Up @@ -16,6 +16,7 @@ require 'actions.rotateoperation'
require 'actions.copyoperation'
require 'actions.scaleoperation'
require 'actions.removeoperation'
require 'actions.trimoperation'

Operations = {}
Operations.__index = Operations
Expand Down
116 changes: 116 additions & 0 deletions lcUILua/actions/trimoperation.lua
@@ -0,0 +1,116 @@
TrimOperation = {}
TrimOperation.__index = TrimOperation

setmetatable(TrimOperation, {
__index = Operations,
__call = function (o, ...)
local self = setmetatable({}, o)
self:_init(...)
return self
end,
})

function TrimOperation:_init(id)
Operations._init(self, id)

self.limit = nil
self.toTrim = nil
self.intersectionPoints = {}
self.toRemovePoint = nil

event.register("point", self)
event.register("selectionChanged", self)

message("Select limit entity")
end

function TrimOperation:onEvent(eventName, ...)
if(Operations.forMe(self) == false) then
return
end

if(eventName == "selectionChanged") then
self:selectionChanged()
elseif(eventName == "point") then
self:newPoint(...)
end
end

function TrimOperation:selectionChanged()
if(self.toTrim == nil) then
nbEntities = #active_widget():selection()
if(nbEntities == 1) then
if(self.limit == nil) then
self.limit = active_widget():selection()[1]

message("Select entity to trim")
elseif(self.toTrim == nil) then
self.toTrim = active_widget():selection()[1]

self:getIntersectionPoints()
end
elseif(nbEntities > 1) then
message("Select only one entity")
end
end
end

function TrimOperation:newPoint(point)
if(#self.intersectionPoints >= 1) then
self.toRemovePoint = Operations:getCoordinate(point)

self:trim()
end
end

function TrimOperation:getIntersectionPoints()
local intersect = IntersectMany({self.toTrim, self.limit})
self.intersectionPoints = intersect:result()

if(#self.intersectionPoints == 0) then
message("No intersection found")

self:close()
elseif(#self.intersectionPoints >= 1) then
message("Click on the part of the entity to remove")
end
end

function TrimOperation:trim()
local newEntity
if(self.toTrim.entityType == "line") then
local point = self.toTrim:nearestPointOnEntity(self.toRemovePoint)

local startToPoint = point:distanceTo(self.toTrim:start())
local startToIntersect = point:distanceTo(self.intersectionPoints[1])

if(startToPoint >= startToIntersect) then
newEntity = Line(self.toTrim:start(), self.intersectionPoints[1], self.toTrim:layer())
else
newEntity = Line(self.intersectionPoints[1], self.toTrim:finish(), self.toTrim:layer())
end
end

local b = Builder(active_widget():document())
b:append(self.toTrim)

b:push()
b:remove()
b:processStack()

b:append(newEntity)
b:execute()

self:close()
end

function TrimOperation:close()
if(not self.finished) then
self.finished = true

event.delete("point", self)
event.delete("selectionChanged", self)

event.trigger('operationFinished')
end
end
6 changes: 6 additions & 0 deletions lcUILua/ui/cadmdichild.lua
Expand Up @@ -12,13 +12,18 @@ local function mouseMove()
event.trigger('mouseMove', position)
end

local function mouseRelease()
event.trigger('selectionChanged')
end

function new_document()
cadMdiChild = lc.CadMdiChild()
cadMdiChild:newDocument()
cadMdiChild:viewer():autoScale()
cadMdiChild:setDestroyCallback(onMdiChildDestroyed)

luaInterface:luaConnect(cadMdiChild:view(), "mousePressEvent()", click)
luaInterface:luaConnect(cadMdiChild:view(), "mouseReleaseEvent()", mouseRelease)
luaInterface:luaConnect(cadMdiChild:view(), "mouseMoveEvent()", mouseMove)
luaInterface:connect(cadMdiChild, "keyPressed(QKeyEvent*)", cliCommand, "onKeyPressed(QKeyEvent*)")

Expand All @@ -31,6 +36,7 @@ function load_document(fileName)
cadMdiChild:viewer():autoScale()

luaInterface:luaConnect(cadMdiChild:view(), "mousePressEvent()", click)
luaInterface:luaConnect(cadMdiChild:view(), "mouseReleaseEvent()", mouseRelease)
luaInterface:luaConnect(cadMdiChild:view(), "mouseMoveEvent()", mouseMove)
luaInterface:connect(cadMdiChild, "keyPressed(QKeyEvent*)", cliCommand, "onKeyPressed(QKeyEvent*)")

Expand Down
1 change: 1 addition & 0 deletions lcUILua/ui/commandline.lua
Expand Up @@ -69,6 +69,7 @@ function add_commandline()
add_command("COPY", copy_selected_entities)
add_command("SCALE", scale_selected_entities)
add_command("REMOVE", remove_selected_entities)
add_command("TRIM", trim_entity)

event.register("point", setLastPoint)
end
5 changes: 5 additions & 0 deletions lcUILua/ui/operations.lua
Expand Up @@ -139,4 +139,9 @@ end
function remove_selected_entities()
new_operation()
op[active_widget().id] = RemoveOperation(active_widget().id)
end

function trim_entity()
new_operation()
op[active_widget().id] = TrimOperation(active_widget().id)
end
4 changes: 4 additions & 0 deletions lcUILua/ui/toolbar.lua
Expand Up @@ -116,4 +116,8 @@ function add_toolbar()
local removeButton = create_button("", ":/icons/delete.svg")
quickAccess:addButton(modifyGroup, removeButton, 2, 0, 1, 1)
luaInterface:luaConnect(removeButton, "pressed()", remove_selected_entities)

local removeButton = create_button("", ":/icons/modifytrim.png")
quickAccess:addButton(modifyGroup, removeButton, 2, 1, 1, 1)
luaInterface:luaConnect(removeButton, "pressed()", trim_entity)
end
18 changes: 18 additions & 0 deletions lcadluascript/cad/lualibrecadbridge.cpp
Expand Up @@ -177,13 +177,25 @@ void lua_openlckernel(lua_State* L) {
.addFunction("move", &entity::CADEntity::move)
.addFunction("rotate", &entity::CADEntity::rotate)
.addFunction("copy", &entity::CADEntity::copy)

.addFunction("layer", &entity::CADEntity::layer)

.addProperty("entityType", [](entity::CADEntity*) {
return "unknown";
})
.endClass()
.beginExtendClass<entity::Line, entity::CADEntity>("Line")
.addConstructor(LUA_SP(entity::Line_SPtr), LUA_ARGS(
const geo::Coordinate & start,
const geo::Coordinate & end,
const Layer_CSPtr))
.addProperty("entityType", [](entity::Line*) {
return "line";
})
.addFunction("nearestPointOnEntity", &geo::Vector::nearestPointOnEntity)
.addFunction("nearestPointOnPath", &geo::Vector::nearestPointOnPath)
.addFunction("start", &geo::Vector::start)
.addFunction("finish", &geo::Vector::end) //"end" will make Lua crash
.endClass()
.beginExtendClass<entity::Circle, entity::CADEntity>("Circle")
.addConstructor(LUA_SP(entity::Circle_SPtr), LUA_ARGS(
Expand Down Expand Up @@ -312,6 +324,12 @@ void lua_openlckernel(lua_State* L) {
.addFunction("begin", &lc::operation::Builder::begin)
.addFunction("selectByLayer", &lc::operation::Builder::selectByLayer)
.addFunction("remove", &lc::operation::Builder::remove)
.addFunction("processStack", &lc::operation::Builder::processStack)
.endClass()

.beginClass<lc::IntersectMany>("IntersectMany")
.addConstructor(LUA_ARGS(std::vector<lc::entity::CADEntity_CSPtr>, _opt<lc::Intersect::Method>, _opt<double>))
.addFunction("result", &lc::IntersectMany::result)
.endClass()

.beginClass<operation::Base>("Base")
Expand Down
4 changes: 2 additions & 2 deletions lckernel/cad/functions/intersect.cpp
Expand Up @@ -607,8 +607,8 @@ std::vector<geo::Coordinate> IntersectMany::result() const {
Intersect intersect(_method, _tolerance);
if (_entities.size() > 1) {
for (size_t outer = 0; outer < (_entities.size() - 1); outer++) {
for (size_t inner = ++outer; inner < _entities.size(); inner++) {
// visitorDispatcher<bool, lc::GeoEntityVisitor>(intersect, *_entities.at(outer).get(), *_entities.at(inner).get());
for (size_t inner = outer + 1; inner < _entities.size(); inner++) {
visitorDispatcher<bool, lc::GeoEntityVisitor>(intersect, *_entities.at(outer).get(), *_entities.at(inner).get());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lckernel/cad/functions/intersect.h
Expand Up @@ -184,7 +184,7 @@ namespace lc {
*/
class IntersectMany {
public:
IntersectMany(std::vector<entity::CADEntity_CSPtr>, Intersect::Method, double tolerance);
IntersectMany(std::vector<entity::CADEntity_CSPtr>, Intersect::Method = Intersect::OnEntity, double tolerance = LCTOLERANCE);

std::vector<geo::Coordinate> result() const;

Expand Down
15 changes: 9 additions & 6 deletions lcviewerqt/lcadviewer.cpp
Expand Up @@ -180,12 +180,16 @@ void LCADViewer::mouseMoveEvent(QMouseEvent *event) {
void LCADViewer::mousePressEvent(QMouseEvent *event) {
QWidget::mousePressEvent(event);

startSelectPos = event->pos();

if (!_ctrlKeyActive) {
_docCanvas->removeSelection();
}

if(!_operationActive) {
_dragManager->onMousePress();
_disableSelection = _dragManager->entityDragged();

startSelectPos = event->pos();

posX = event->x();
posY = event->y();

Expand All @@ -195,10 +199,6 @@ void LCADViewer::mousePressEvent(QMouseEvent *event) {
}
break;
}

if (!_ctrlKeyActive) {
_docCanvas->removeSelection();
}
}

_docCanvas->device_to_user(&posX, &posY);
Expand Down Expand Up @@ -227,6 +227,9 @@ void LCADViewer::mouseReleaseEvent(QMouseEvent *event) {
}

_docCanvas->removeSelectionArea();

emit mouseReleaseEvent();

update();
}

Expand Down
2 changes: 1 addition & 1 deletion lcviewerqt/lcadviewer.h
Expand Up @@ -87,7 +87,7 @@ class LCADViewer : public QWidget {
signals:
void mouseMoveEvent();
void mousePressEvent();
void mouseReleaseEvent(const MouseReleaseEvent&);
void mouseReleaseEvent();
void drawEvent(const DrawEvent&);
void selectedItemsEvent(const SelectedItemsEvent&);
public slots:
Expand Down

0 comments on commit 40b888f

Please sign in to comment.