Code

Correct load order of user icons.svg icons with legacy names.
[inkscape.git] / share / extensions / perspective.py
index c00a8d65d846659940c0a118e22f58d62add1d0c..4caaa68a1314b8b2cb65b6073550c6ddd3db5a1c 100755 (executable)
@@ -19,13 +19,21 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 Perspective approach & math by Dmitry Platonov, shadowjack@mail.ru, 2006
 """
 import sys, inkex, os, re, simplepath, cubicsuperpath 
+import gettext
+_ = gettext.gettext
 from ffgeom import *
 try:
     from numpy import *
     from numpy.linalg import *
 except:
-    inkex.debug("Failed to import the numpy or numpy.linalg modules. These modules are required by this extension. Please install them and try again.")
-    sys.exit()
+    inkex.errormsg(_("Failed to import the numpy or numpy.linalg modules. These modules are required by this extension. Please install them and try again.  On a Debian-like system this can be done with the command, sudo apt-get install python-numpy."))
+    exit()
+
+try:
+    from subprocess import Popen, PIPE
+    bsubprocess = True
+except:
+    bsubprocess = False
 
 uuconv = {'in':90.0, 'pt':1.25, 'px':1, 'mm':3.5433070866, 'cm':35.433070866, 'pc':15.0}
 def unittouu(string):
@@ -50,29 +58,51 @@ class Project(inkex.Effect):
         inkex.Effect.__init__(self)
     def effect(self):
         if len(self.options.ids) < 2:
-            inkex.debug("Requires two selected paths. The second must be exactly four nodes long.")
-            sys.exit()            
+            inkex.errormsg(_("This extension requires two selected paths."))
+            exit()            
             
         #obj is selected second
         obj = self.selected[self.options.ids[0]]
         envelope = self.selected[self.options.ids[1]]
-        if (obj.tag == inkex.addNS('path','svg') or obj.tag == inkex.addNS('g','svg')) and envelope.tag == inkex.addNS('path','svg'):
-            path = cubicsuperpath.parsePath(envelope.get('d'))
-            dp = zeros((4,2), dtype=float64)
-            for i in range(4):
-                dp[i][0] = path[0][i][1][0]
-                dp[i][1] = path[0][i][1][1]
-
-            #query inkscape about the bounding box of obj
-            q = {'x':0,'y':0,'width':0,'height':0}
-            file = self.args[-1]
-            id = self.options.ids[0]
-            for query in q.keys():
-                _,f,err = os.popen3("inkscape --query-%s --query-id=%s %s" % (query,id,file))
-                q[query] = float(f.read())
-                f.close()
-                err.close()
-            sp = array([[q['x'], q['y']+q['height']],[q['x'], q['y']],[q['x']+q['width'], q['y']],[q['x']+q['width'], q['y']+q['height']]], dtype=float64)
+        if obj.get(inkex.addNS('type','sodipodi')):
+            inkex.errormsg(_("The first selected object is of type '%s'.\nTry using the procedure Path->Object to Path." % obj.get(inkex.addNS('type','sodipodi'))))
+            exit()
+        if obj.tag == inkex.addNS('path','svg') or obj.tag == inkex.addNS('g','svg'):
+            if envelope.tag == inkex.addNS('path','svg'):
+                path = cubicsuperpath.parsePath(envelope.get('d'))
+                if len(path) < 1 or len(path[0]) < 4:
+                    inkex.errormsg(_("This extension requires that the second selected path be four nodes long."))
+                    exit()
+                dp = zeros((4,2), dtype=float64)
+                for i in range(4):
+                    dp[i][0] = path[0][i][1][0]
+                    dp[i][1] = path[0][i][1][1]
+
+                #query inkscape about the bounding box of obj
+                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()
+                sp = array([[q['x'], q['y']+q['height']],[q['x'], q['y']],[q['x']+q['width'], q['y']],[q['x']+q['width'], q['y']+q['height']]], dtype=float64)
+            else:
+                if envelope.tag == inkex.addNS('g','svg'):
+                    inkex.errormsg(_("The second selected object is a group, not a path.\nTry using the procedure Object->Ungroup."))
+                else:
+                    inkex.errormsg(_("The second selected object is not a path.\nTry using the procedure Path->Object to Path."))
+                exit()
+        else:
+            inkex.errormsg(_("The first selected object is not a path.\nTry using the procedure Path->Object to Path."))
+            exit()
 
         solmatrix = zeros((8,8), dtype=float64)
         free_term = zeros((8), dtype=float64)
@@ -123,5 +153,9 @@ class Project(inkex.Effect):
         y = p[1]
         return [(x*m[0][0] + y*m[0][1] + m[0][2])/(x*m[2][0]+y*m[2][1]+m[2][2]),(x*m[1][0] + y*m[1][1] + m[1][2])/(x*m[2][0]+y*m[2][1]+m[2][2])]
 
-e = Project()
-e.affect()
+if __name__ == '__main__':
+    e = Project()
+    e.affect()
+
+
+# vim: expandtab shiftwidth=4 tabstop=8 softtabstop=4 fileencoding=utf-8 textwidth=99