We have moved to https://dokuwiki.librecad.org/

Lots of content was already moved to the new wiki, but there is still work to do. If you want to contribute, please register a new account at https://dokuwiki.librecad.org/

This wiki will be kept for a while to keep search engine results valid. Moved sites may be deleted here in future.

Difference between revisions of "LibreCAD 3 - Lua Scripting"

From LibreCAD wiki
Jump to: navigation, search
(Created page with " == LibreCAD Scripting with lua == One of the scripting languages LibreCAD support's is //www.lua.org:Lua. Lua is the default scripting language and will be available for...")
 
(Update Lua scripts and give more details)
(10 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
Lua is a powerful, fast, lightweight, embeddable scripting language developed by a team at PUC-Rio, the Pontifical Catholic University of Rio de Janeiro in Brazil.
  
 
== LibreCAD Scripting with lua ==
 
== LibreCAD Scripting with lua ==
  
One of the scripting languages LibreCAD support's is [[//www.lua.org:Lua]]. Lua is the default scripting language and will be available for most simple scripting needs.
+
One of the scripting languages LibreCAD support's is [[//www.lua.org Lua]]. Lua is the default scripting language and will be available for most simple scripting needs.
 +
Lua is very powerful but at the same time provides a easy to understand programming language. Please see [http://www.lua.org/manual/5.2/ Lua's reference manual] for more information.
  
 +
To create entities within a document there is a builder object that allows to add new entities or append selected entities into the build. From there you can apply steps to process the entities. After each step the result of the step is passed along to the next step.
 +
 +
To run the examples, open the Lua script window in the toolbar "Lua" > "Run script".
 +
 +
== Example 1: ==
 +
Create a line and copy/rotate the line 7 times.
 +
This create 128 entities, because the Copy operation copy all the lines on each loop.
  
Example:
 
 
<pre>
 
<pre>
.. draw a line from coordinate 0,0 to coordinate 100,00
+
-- Get the layer
l=Line(Coord(0,0), Coord(100,100));
+
local layer = document:layerByName("0")
d=app.currentDocument()
+
 
ce = CreateEntities(d, "0");
+
-- Create a Line
ce:append(l)
+
local l=Line(Coord(0,0), Coord(10,100), layer);
ce:execute()
+
 
 +
-- Get the builder object (will be renamed later)
 +
local b=EntityBuilder(document)
 +
 
 +
-- Append the line
 +
b:appendEntity(l)
 +
 
 +
-- Put the line in next operations stack
 +
b:appendOperation(Push())
 +
 
 +
-- Make a copy 'in place' of the line
 +
b:appendOperation(Copy(Coord(0,0)))
 +
 
 +
-- Rotate the line
 +
b:appendOperation(Rotate(Coord(0,0), math.rad(45)))
 +
 
 +
-- Do this 7 times
 +
b:appendOperation(Loop(7))
 +
 
 +
-- Apply into the document
 +
b:execute()
 
</pre>
 
</pre>
 +
 +
Result:
 +
 +
http://skitch.rvantwisk.nl/~rvt/blog/LibreCAD_-20140418-132431.jpg
 +
 +
== Example 2: ==
 +
Create a line and copy/rotate the line 7 times, then copy the result and move that to location 100,0
 +
Same as abore, it creates 256 lines.
 +
 +
<pre>
 +
-- Get the layer
 +
local layer = document:layerByName("0")
 +
 +
-- Create a Line
 +
local l=Line(Coord(0,0), Coord(10,100), layer);
 +
 +
-- Get the builder object
 +
local b=EntityBuilder(document)
 +
 +
-- Append the line
 +
b:appendEntity(l)
 +
 +
-- Push all entities into working buffer
 +
b:appendOperation(Push())
 +
 +
-- Make a copy 'in place' of the line
 +
b:appendOperation(Copy(Coord(0,0)))
 +
 +
-- Rotate the line
 +
b:appendOperation(Rotate(Coord(0,0), math.rad(45)))
 +
 +
-- Do this 7 times
 +
b:appendOperation(Loop(7))
 +
 +
-- Push all entities into working buffer
 +
b:appendOperation(Push())
 +
 +
-- copy
 +
b:appendOperation(Copy(Coord(200,0)))
 +
 +
-- Apply into the document
 +
b:execute()
 +
</pre>
 +
 +
Result:
 +
 +
http://skitch.rvantwisk.nl/~rvt/blog/LibreCAD_-20140418-145037.jpg
 +
 +
[[Category:Usage]]

Revision as of 16:52, 25 July 2017

Lua is a powerful, fast, lightweight, embeddable scripting language developed by a team at PUC-Rio, the Pontifical Catholic University of Rio de Janeiro in Brazil.

LibreCAD Scripting with lua

One of the scripting languages LibreCAD support's is [Lua]. Lua is the default scripting language and will be available for most simple scripting needs. Lua is very powerful but at the same time provides a easy to understand programming language. Please see Lua's reference manual for more information.

To create entities within a document there is a builder object that allows to add new entities or append selected entities into the build. From there you can apply steps to process the entities. After each step the result of the step is passed along to the next step.

To run the examples, open the Lua script window in the toolbar "Lua" > "Run script".

Example 1:

Create a line and copy/rotate the line 7 times. This create 128 entities, because the Copy operation copy all the lines on each loop.

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

-- Create a Line
local l=Line(Coord(0,0), Coord(10,100), layer);

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

-- Append the line
b:appendEntity(l)

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

-- Make a copy 'in place' of the line
b:appendOperation(Copy(Coord(0,0))) 

-- Rotate the line
b:appendOperation(Rotate(Coord(0,0), math.rad(45)))

-- Do this 7 times
b:appendOperation(Loop(7))

-- Apply into the document
b:execute()

Result:

LibreCAD_-20140418-132431.jpg

Example 2:

Create a line and copy/rotate the line 7 times, then copy the result and move that to location 100,0 Same as abore, it creates 256 lines.

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

-- Create a Line
local l=Line(Coord(0,0), Coord(10,100), layer);

-- Get the builder object
local b=EntityBuilder(document)

-- Append the line
b:appendEntity(l)

-- Push all entities into working buffer
b:appendOperation(Push())

-- Make a copy 'in place' of the line
b:appendOperation(Copy(Coord(0,0))) 

-- Rotate the line
b:appendOperation(Rotate(Coord(0,0), math.rad(45)))

-- Do this 7 times
b:appendOperation(Loop(7))

-- Push all entities into working buffer
b:appendOperation(Push())

-- copy
b:appendOperation(Copy(Coord(200,0)))

-- Apply into the document
b:execute()

Result:

LibreCAD_-20140418-145037.jpg