Dia Shape Processing Tools
Provided by Sam Liddicott on the Dia mailing list through these threads:
"XSLT converts svg to shapes with basic transform handling." 2007/October - "XSLT converts svg to shapes" 2007/July - "XSLT converts shapes to svg" 2007/July
Sam has put the .shape and SVG files under the magnifying glass and proposed some useful tools with the aim to enable correct management of the shape creation process by the use of Inkscape (available both for Linux/Unix and for Windows(tm)).
His tools are good for cleaning, debugging, and correcting existing shape files as well, as we'll see later.
Two of the 3 files he provides are .XSLT files, to be applied by the use of xsltproc, a free, Open Source command-line tool for applying XSLT stylesheets to XML documents. This processor is part of libxslt(3), the XSLT C library for GNOME. While it was developed as part of the GNOME project, it runs at command-line level (in Linux/Unix shell CLI) and free (and commercial) Windows(tm) versions are available too.
The other of Sam's files is a BASH script (genshape) able to run on Linux/Unix shells only. Therefore it is used to automate clearly-explained processes that can be performed manually or by the use of corresponding scripts/macros by the Windows(tm) user.
Sam has agreed to have his tools available under the GPL.
Converting SVG files to Dia shapes
He describes in this post the process of creating Dia shape files from your Inkscape .SVG artwork. Don't forget to always save your artworks from Inkscape as SVG Plain files and not in the default Inkscape SVG format!
This xslt will convert inkscape's PLAIN svg files to dia shapes. xsltproc svg2shape.xslt --stringparam icon-file firewall.icon \ firewall.svg > firewall.shape The attached bash script "genshape" will use inkscape to export the icon and the simple svg and converts this to the shape file all in one go. DIA-isms Here is how the standard dia meta data is obtained: 1. The first "text" with id "textbox" is converted into the shape's textbox and removed from the svg. The text in this text becomes the <name> of the shape. 2. All items with an id that begins with "connector_" generate a <connections><point> tag with the connection point in the middle. This is calculated by taking the average of the min and max x points and the min and max y points. The min and max taken from attributes x, x1, x2, width and pulling out all the X coordinates in draw paths, or y, y1, y2, height or all of the Y coordinates in the draw paths (@d attribute) There are also some -isms to correlate the -isms in shape2svg, such that the connections were put into a <connections> layer and the shape was put into a shape layer. inkscape is explicit about draw styles, so there was no need to fixup assumptions when converting to shape. BUGS: Currently I do NOTHING about transforms or groups or other svg forms that dia can't handle. I don't preserve the order of connection points, ubt I don't think it matters. I still haven't worked out how to get xsltproc to generate the right namespace prefixes. So where does this leave us? I can make simple drawings (and moderately complex ones) in SVG. 1. I just make sure the ID for visual connection points begins with "connection_", 2. I make sure there is a text area with id "textbox" and contents "Network -Firewall" or whatever. then I can convert and make a shape file really easily!
So far the main SVG to shape conversion.
Converting Dia shape files to SVG files
Next, Sam explains in this post how to make the opposite conversion: Dia shape to SVG. This is useful for correcting a shape for your needs or to debug any errors in the shape file and to clean up old files. The 48.shape file is provided to test/play-with the tool before using it on your SVG artwork. Do this by use of Sam's example, substituting 48.xxx for the shown firewall.xxx filenames.
This xslt will convert dia shapes to svg editable by inkscape. xsltproc shape2svg.xslt firewall.shape > firewall.svg (A lot of the .shape files are BAD svg subset, with the semi-colon seperator missing in the style attributes sometimes). The connection points are preserved in a "connections" layer, and the textbox position is also preserved. The width and height (which dia ingores) ARE used to set the document size. The plan is to be able to convert inkscape back to shape files, regenerating the connections etc (doing what we can about transforms etc) so that I can use inkscape to create shapes. The general problem is matching dia style assumptions to svg style assumptions (i.e. whether or not a closed shape defaults to fill, or not), but I've got that down to: fill:none stroke:#000000 stroke-width:0.1 fill:default = fill:#ffffff fill:foreground = fill:#000000 fill:background = fill:ffffff A larger problem is to do with line-width and coordinate handling; it seems like I need to divide the stroke-width by 10 when converting to inkscape. An example is the Network/firewall.shape which has lines with length 0.4 and stroke-width 1. dia renders that specific line with the stroke-width 1/4 of the line length. (from the shape file: <svg:line style="stroke:#FFFFFF; stroke-width:1" x1=".7" y1="1" x2=".7" y2="1.4"/>)
Finally, for completeness Sam adds some comments:
This xslt converts svg to shapes and does basic transform handling. Skews and rotates are not handled but scales and offsets are. Transforms are not handled on groups, but any tag that has a @transform attribute will have scaling and offset applied to attributes x, x1, x2, y, y1, y2 and also to d, points. d, points are handled specially, the attribute value is split on white space and anything with a , in it is treated as an x-y pair, with the transform done on the x and y pair.
This is Sam's work dated mid-2007 and never updated or revised, AFAIK. So be careful using these tools with later versions of Dia. Never work on original files. Always do backups!
Any further suggestion and/or revision of the tools is obviously welcome, in the appropriate context (read "mailing list").