Code

fix Bug 308204, add Visual bbox option
[inkscape.git] / share / extensions / dimension.py
index cda3a96fda1e568db784a03f413efd0c7475a4a8..1b84642eab44cd2d18b96fb1029c52de2250dd11 100644 (file)
@@ -37,6 +37,12 @@ from simpletransform import *
 import gettext
 _ = gettext.gettext
 
+try:
+    from subprocess import Popen, PIPE
+    bsubprocess = True
+except:
+    bsubprocess = False
+
 class Dimension(pathmodifier.PathModifier):
     def __init__(self):
         inkex.Effect.__init__(self)
@@ -48,6 +54,10 @@ class Dimension(pathmodifier.PathModifier):
                         action="store", type="float", 
                         dest="yoffset", default=100.0,
                         help="y offset of the horizontal dimension arrow")    
+        self.OptionParser.add_option("-t", "--type",
+                        action="store", type="string", 
+                        dest="type", default="geometric",
+                        help="Bounding box type")
 
     def addMarker(self, name, rotate):
         defs = self.xpathSingle('/svg:svg//svg:defs')
@@ -90,7 +100,28 @@ class Dimension(pathmodifier.PathModifier):
         self.xoffset = self.options.xoffset
         self.yoffset = self.options.yoffset
 
-        self.bbox = computeBBox(self.selected.values())
+        # query inkscape about the bounding box
+        if len(self.options.ids) == 0:
+            inkex.errormsg(_("Please select an object."))
+            exit()
+        if self.options.type == "geometric":
+            self.bbox = computeBBox(self.selected.values())
+        else:
+            q = {'x':0,'y':0,'width':0,'height':0}
+            file = self.args[-1]
+            id = self.options.ids[0]
+            for query in q.keys():
+                if bsubprocess:
+                    p = Popen('inkscape --query-%s --query-id=%s "%s"' % (query,id,file), shell=True, stdout=PIPE, stderr=PIPE)
+                    rc = p.wait()
+                    q[query] = float(p.stdout.read())
+                    err = p.stderr.read()
+                else:
+                    f,err = os.popen3('inkscape --query-%s --query-id=%s "%s"' % (query,id,file))[1:]
+                    q[query] = float(f.read())
+                    f.close()
+                    err.close()
+            self.bbox = (q['x'], q['x']+q['width'], q['y'], q['y']+q['height'])
 
         # Avoid ugly failure on rects and texts.
         try:
@@ -103,7 +134,8 @@ class Dimension(pathmodifier.PathModifier):
         self.addMarker('Arrow1Lstart', False)
         self.addMarker('Arrow1Lend',  True)
 
-        group = inkex.etree.Element("g")
+        group = inkex.etree.SubElement(layer, 'g')
+        # group = inkex.etree.Element("g")
         group.set('fill', 'none')
         group.set('stroke', 'black')