Resources for Writing an python export script
Hi,
I want to write an exporter to another file format for inkscape. Basicly I want to take only polygons, rectangles and circles from the svg, perform some operation on them and save the result to my own xml format.
Now, I managed to write the inx file. But for how to write the python script I just cannot find enough information.
Can someone point me to information or an example giving the necessary information on how to do this?
Thanks! Nathan
in the directory \Inkscape\share\extensions\ there are some examples of Python exporters, for example, .dxf, .hpgl, .xaml
hth, Alvin Penner
Hey,
Thanks for the reply. I came up with this, trying to extract all the rectangles from the svg:
import sys sys.path.append('/usr/share/inkscape/extensions') import inkex from simplestyle import *
class RectOutput(inkex.Effect): def __init__(self): inkex.Effect.__init__(self) def output(self): for r in self.rects: print "Rectangle occupying: ", r.attrib.get("x"),",",r.attrib.get("y")," - ",r.attrib.get("width"),",",r.attrib.get("height")
def effect(self): dom_svg = self.document.getroot() self.rects = dom_svg.xpath('//svg:rect',namespaces=inkex.NSS) self.circles = dom_svg.xpath('//svg:circle',namespaces=inkex.NSS) self.polygons = dom_svg.xpath('//svg:polygon',namespaces=inkex.NSS)
if __name__ == "__main__": e = RectOutput() e.affect()
I get something, but I am unsure what its meaning is. What I want to get: - The center of the rectangle - Its width and height - Its rotation angle
I am noticing that when I am rotating my rectangle, the "x" and "y" component change. So what exactly are there meaning? How do I get the rotation angle?
Thanks! Nathan
On 03/20/2011 03:59 AM, Alvin Penner wrote:
in the directory \Inkscape\share\extensions\ there are some examples of Python exporters, for example, .dxf, .hpgl, .xaml
hth, Alvin Penner
the x and y coordinates are probably the coordinates of the upper left corner, so they will change when you rotate the object. The rotation angle will often be embedded in an affine transform element that will look like this : matrix(0.90791043,-0.41916422,0.41916422,0.90791043,0,0)
Alvin
OK, and how do I get this matrix? It does not seem to be an element of the rectangle itself ...
I am guessing that the transformation is hidden in some parent element of the rectangle. So using xpath is probably not the best way to find all rectangles. How can I iterate over all rectangles and also find the proper transformation while doing so?
Thanks! Nathan
On 03/20/2011 04:41 PM, Alvin Penner wrote:
the x and y coordinates are probably the coordinates of the upper left corner, so they will change when you rotate the object. The rotation angle will often be embedded in an affine transform element that will look like this : matrix(0.90791043,-0.41916422,0.41916422,0.90791043,0,0)
Alvin
you will need to inspect the svg file. Attached is a typical result, obtained by drawing a rectangle and then rotating it.
<rect
style="color:#000000;fill:none;stroke:#000000;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-opacity:1;stroke-dasharray:none;stroke-dashoffset:0;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate" id="rect2985" width="348.57144" height="140" x="-32.067043" y="446.03098" transform="matrix(0.94936411,-0.31417794,0.31417794,0.94936411,0,0)" />
participants (2)
-
Alvin Penner
-
Nathan Hüsken