Re: Scripting Brainstorming
Posted by
dxli on
Oct 25, 2012; 3:41pm
URL: https://forum.librecad.org/Scripting-Brainstorming-tp5707168p5707179.html
entity ID is defined in rs_entity.h
unsigned long int getId() const {
return id;
}
On Thu, Oct 25, 2012 at 11:31 AM, xnakos [via LibreCAD]
<[hidden email]> wrote:
Hi! Thanks for the info! I have some very basic quesions now. First of all, I tried using SWIG in an extend-and-embed fashion, i.e. create Python wrappers for c++ functions that could be used in Python scripts (extend) and call these scripts from inside LibreCAD (embed). I tried this as a proof of concept and had some initial success. But I need a lot of information to go on. The sample script I ran was this:
import script
p1 = (8,2)
p2 = (180,238)
print script.line(p1, p2)
print script.line((20, 20), (130, 180))
print script.line((0, 20), (70, 120))
"print " was used just to debug. Omit "print " and you have a script that draws three lines when you run it.
I tried to build this on top of the plugin system, working on the sample plugin of LibreCAD. This had a few caveats but I will get to that. First of all, I used simple 2-tuples to represent points as a demonstration of using SWIG's typemaps. I created the "line" C++ function that takes as arguments double p1[2] and double p2[2], that is arrays of length 2. Using a typemap I told SWIG that 2-tuples are to be converted to arrays of length 2. This is just an example. "line" used the code of the sample plugin to create the line. "script" is the Python module that is created by SWIG, the one which contains all wrapper functions.
One little problem was this: The function of the plugin that runs the code takes the doc as a parameter, but I also needed to have access to the doc through the "line" function. To achieve that I used a global variable gdoc that was initialized to doc when the plugin code started to run. This gdoc was used in the "line" function. What if I wanted to select another document through the script? Or to create a new one rather than work on the current one. Furthermore, how can I get a hold of the document entities through the plugin API? How can I get all polylines, for instance? Is the plugin API the way to go? Are there entity ids in LibreCAD that can be used in scripts, as in "trim entity 1 by entity 2"?
--
Dongxu Li, Ph.D.
www.librecad.org