Code

add some convenience methods to inkex and allow the possiblity of replacing the marke...
authoracspike <acspike@users.sourceforge.net>
Thu, 20 Jul 2006 14:54:57 +0000 (14:54 +0000)
committeracspike <acspike@users.sourceforge.net>
Thu, 20 Jul 2006 14:54:57 +0000 (14:54 +0000)
share/extensions/inkex.py
share/extensions/markers_strokepaint.py

index e1711105751dc02bd8dd18cc7944f0066c746a76..e210e90c2d0dbf3e29615664ca702415f77bed40 100755 (executable)
@@ -19,7 +19,7 @@ You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 """
-import sys, copy, optparse
+import sys, copy, optparse, random
 
 #a dictionary of all of the xmlns prefixes in a standard inkscape doc
 NSS = {
@@ -60,8 +60,11 @@ class InkOption(optparse.Option):
 class Effect:
     """A class for creating Inkscape SVG Effects"""
     def __init__(self):
+        self.id_characters = '0123456789abcdefghijklmnopqrstuvwkyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
         self.document=None
+        self.ctx=None
         self.selected={}
+        self.doc_ids={}
         self.options=None
         self.args=None
         self.OptionParser = optparse.OptionParser(usage="usage: %prog [options] SVGfile",option_class=InkOption)
@@ -84,6 +87,7 @@ class Effect:
         except:
             stream = sys.stdin
         self.document = reader.fromStream(stream)
+        self.ctx = xml.xpath.Context.Context(self.document,processorNss=NSS)
         stream.close()
     def getposinlayer(self):
         ctx = xml.xpath.Context.Context(self.document,processorNss=NSS)
@@ -111,6 +115,10 @@ class Effect:
             path = '//*[@id="%s"]' % id
             for node in xml.xpath.Evaluate(path,self.document):
                 self.selected[id] = node
+    def getdocids(self):
+        docIdNodes = xml.xpath.Evaluate('//@id',self.document,context=self.ctx)
+        for m in docIdNodes:
+            self.doc_ids[m.value] = 1
     def output(self):
         """Serialize document into XML on stdout"""
         xml.dom.ext.Print(self.document)
@@ -120,5 +128,22 @@ class Effect:
         self.parse()
         self.getposinlayer()
         self.getselected()
+        self.getdocids()
         self.effect()
         self.output()
+        
+    def uniqueId(self, old_id, make_new_id = True):
+        new_id = old_id
+        if make_new_id:
+            while new_id in self.doc_ids:
+                new_id = "%s%s" % (new_id,random.choice(self.id_characters))
+            self.doc_ids[new_id] = 1
+        return new_id
+    def xpathSingle(self, path):
+        try:
+            retval = xml.xpath.Evaluate(path,self.document,context=self.ctx)[0]
+        except:
+            debug("No matching node for expression: %s" % path)
+            retval = None
+        return retval
+    
index 4b261cba6046180fd7a3df037fac26b802143d5d..6d6331cdbadaffdbec872dc3f253b630a3df2e07 100644 (file)
@@ -21,28 +21,17 @@ import random, inkex, simplestyle
 class MyEffect(inkex.Effect):\r
     def __init__(self):\r
         inkex.Effect.__init__(self)\r
-    def xpathSingle(self, path):\r
-        try:\r
-            retval = inkex.xml.xpath.Evaluate(path,self.document,context=self.ctx)[0]\r
-        except:\r
-            inkex.debug("No matching node for expression: %s" % path)\r
-            retval = None\r
-        return retval\r
+        self.OptionParser.add_option("-m", "--modify",\r
+                        action="store", type="inkbool", \r
+                        dest="modify", default=False,\r
+                        help="do not create a copy, modify the markers")\r
         \r
     def effect(self):\r
-        self.ctx = inkex.xml.xpath.Context.Context(self.document,processorNss=inkex.NSS)\r
-        id_characters = '0123456789abcdefghijklmnopqrstuvwkyzABCDEFGHIJKLMNOPQRSTUVWXYZ'\r
-        \r
         defs = self.xpathSingle('/svg//defs')\r
         if not defs:\r
             defs = self.document.createElement('svg:defs')\r
             self.document.documentElement.appendChile(defs)\r
         \r
-        doc_ids = {}\r
-        docIdNodes = inkex.xml.xpath.Evaluate('//@id',self.document,context=self.ctx)\r
-        for m in docIdNodes:\r
-            doc_ids[m.value] = 1\r
-        \r
         for id, node in self.selected.iteritems():\r
             mprops = ['marker','marker-start','marker-mid','marker-end']\r
             try:\r
@@ -58,19 +47,19 @@ class MyEffect(inkex.Effect):
                     marker_id = style[mprop][5:-1]\r
                     try:\r
                         old_mnode = self.xpathSingle('/svg//marker[@id="%s"]' % marker_id)\r
-                        mnode = old_mnode.cloneNode(True)\r
+                        if not self.options.modify:\r
+                            mnode = old_mnode.cloneNode(True)\r
+                        else:\r
+                            mnode = old_mnode\r
                     except:\r
                         inkex.debug("unable to locate marker: %s" % marker_id)\r
                         continue\r
                         \r
-                    #generate a unique id\r
-                    new_id = marker_id\r
-                    while new_id in doc_ids:\r
-                        new_id = "%s%s" % (new_id,random.choice(id_characters))\r
-                    doc_ids[new_id] = 1\r
+                    new_id = self.uniqueId(marker_id, not self.options.modify)\r
                     \r
                     style[mprop] = "url(#%s)" % new_id\r
                     mnode.attributes.getNamedItem('id').value = new_id\r
+                    mnode.attributes.getNamedItemNS(inkex.NSS['inkscape'],'stockid').value = new_id\r
                     defs.appendChild(mnode)\r
                     \r
                     children = inkex.xml.xpath.Evaluate('/svg//marker[@id="%s"]//*[@style]' % new_id,self.document,context=self.ctx)\r