User Tools

Site Tools


Sidebar

Introduction

* Home

Documentation & help

LibreCAD Community

Help on DokuWiki

Temporary shorcuts

dev:v3:gui_api:dialog:dialogexample1

Dialog Example 1

In this simple example we attempt to use the dialog widget to allow the user to create a simple entity. The user would specify two points which we use to create a line, and then using the rotation angle specified by the user and the number of rotations we rotate and copy the entity that many times creating a simple entity like the one shown below.



Creating the gui widgets

We first create the dialog widget and the required gui widgets

local dia = CreateDialogWidget("ExampleDialog")
local coord1 = gui.Coordinate("First Point")
local coord2 = gui.Coordinate("Second Point")
local rotate1 = gui.Angle("Rotation Angle")
local numrotation1 = gui.Slider("Number of Rotations", 1, 7) -- 1 is the min value, 7 is the max value
local finishButton = gui.Button("Finish")

Adding the input widgets to the dialog widget

Now we add these created input gui widgets to the dialog widget and the set the finish button to be clicked when we are finished with the dialog. We also specify the key that we will use to access the values of the input widgets.

dia:addWidget("coord1", coord1)
dia:addWidget("coord2",coord2)
dia:addWidget("rotate1", rotate1)
dia:addWidget("numrotation1", numrotation1)
dia:addWidget("finish_button",finishButton)
dia:setFinishButton(finishButton)

Write the finish callback which creates the required entities

Now we write the drawEntities function which will be called when we click the finish button. The draw entities function takes in a values table containing the values of all the input gui widgets.

local drawEntities = function(values) 

-- Get the layer
local layer = document:layerByName("0")

-- Create a Line
local lb = lc.builder.LineBuilder()
lb:setStartPoint(values["coord1"])
lb:setEndPoint(values["coord2"])
lb:setLayer(layer)
local l = lb:build()

-- Get the builder object (will be renamed later)
local b=lc.operation.EntityBuilder(document)

-- Append the line
b:appendEntity(l)

-- Put the line in next operations stack
b:appendOperation(lc.operation.Push())

-- Make a copy 'in place' of the line
b:appendOperation(lc.operation.Copy(lc.geo.Coordinate(0,0))) 

-- Rotate the line
b:appendOperation(lc.operation.Rotate(values["coord1"]:mid(values["coord2"]), values["rotate1"]))

-- Do this 7 times
b:appendOperation(lc.operation.Loop(values["numrotation1"]))

-- Apply into the document
b:execute()

 end

finally we set the finish callback for the dialog widget

dia:addFinishCallback(drawEntities)

Result

dev/v3/gui_api/dialog/dialogexample1.txt · Last modified: 2020/07/10 05:24 by jedi18