Jon wrote:
The problem that I'm having is in what should be a simple task -
that of
changing the style attributes of an existing line. I can create new
dash styles by editing the preferences.xml file in my personal user area
but, for many reasons, do not see this as as a realistic option and not
one that could be distributed to other users. I've been trying to write
a simple Python extension that will take a selected line or number of
lines and replace the style of the original with a different set of
style attributes without changing its path. I've looked through the
currently distributed Python extensions for clues as to how to do this,
but have not been successful. I've even started looking at the 1000+
page Python 2.6.4 online manual (is this the version of Python
implemented in Inkscape?) but this hasn't really helped, although I've
picked up some pointers for better Python coding. I'm assuming that
there is a subroutine in one of the 'helper' files - inkex.py,
pathmodifier.py, ... etc. that would help with this but haven't
determined which one it could be. I would be grateful if someone could
point me in the right direction.
I think this might be simpler than you expect. If I'm understanding
correctly you would like to modify the style attribute of various path
elements. This is as simple as using the get attribute and set attribute
functionality of your favorite xml manipulation library. My favorite is
LXML and this is the library most Inkscape extensions use. LXML (mostly)
follows the ElementTree API:
http://effbot.org/zone/pythondoc-elementtree-ElementTree.htm#elementtree....
http://effbot.org/zone/pythondoc-elementtree-ElementTree.htm#elementtree....
http://effbot.org/zone/pythondoc-elementtree-ElementTree.htm#elementtree....
One little annoyance is that the style attribute is multivalued. To make
this easier to work with we've made a few simple routines in
simplestyle.py to break a style attribute into a python dictionary and
put such a dictionary back together as a style attribute string,
parseStyle() and formatStyle() respectively.
http://bazaar.launchpad.net/~inkscape.dev/inkscape/trunk/annotate/head%3A...
Perhaps you already know this but it is important to know how your
desired change affects the XML representation. Inkscape can help you
figure this out. Make a simple example file and save a copy. Now modify
the line styles as you desire in Inkscape and save another copy of the
file. By comparing these files in a text editor or with an XML diff tool
you can learn what changes you need to make to the XML with your
extension. You could also refer to the appropriate section of the SVG
specification:
http://www.w3.org/TR/SVG11/painting.html#StrokeProperties
To see an example from the extensions distributed with Inkscape check:
http://bazaar.launchpad.net/~inkscape.dev/inkscape/trunk/annotate/head%3A...
I hope this helps.
Aaron Spike