Code

fix bug [ 1685070 ] PathAlongPath - Error message when no paths selected
authoracspike <acspike@users.sourceforge.net>
Thu, 29 Mar 2007 02:35:11 +0000 (02:35 +0000)
committeracspike <acspike@users.sourceforge.net>
Thu, 29 Mar 2007 02:35:11 +0000 (02:35 +0000)
share/extensions/pathalongpath.py

index ba4534b13f419a5bf26fd62b6d637f8f2c2677ee..af42a2dee48e47cda2b564b2acf11a64b025f76e 100644 (file)
@@ -38,11 +38,11 @@ import copy, math, re, random, xml.xpath
 \r
 def flipxy(path):\r
     for pathcomp in path:\r
-       for ctl in pathcomp:\r
-           for pt in ctl:\r
-               tmp=pt[0]\r
-               pt[0]=-pt[1]\r
-               pt[1]=-tmp\r
+        for ctl in pathcomp:\r
+            for pt in ctl:\r
+                tmp=pt[0]\r
+                pt[0]=-pt[1]\r
+                pt[1]=-tmp\r
 \r
 def offset(pathcomp,dx,dy):\r
     for ctl in pathcomp:\r
@@ -69,19 +69,19 @@ def linearize(p,tolerance=0.001):
     d=0\r
     lengths=[]\r
     while i<len(p)-1:\r
-       box  = bezmisc.pointdistance(p[i  ][1],p[i  ][2])\r
-       box += bezmisc.pointdistance(p[i  ][2],p[i+1][0])\r
-       box += bezmisc.pointdistance(p[i+1][0],p[i+1][1])\r
-       chord = bezmisc.pointdistance(p[i][1], p[i+1][1])\r
-       if (box - chord) > tolerance:\r
-           b1, b2 = bezmisc.beziersplitatt([p[i][1],p[i][2],p[i+1][0],p[i+1][1]], 0.5)\r
-           p[i  ][2][0],p[i  ][2][1]=b1[1]\r
-           p[i+1][0][0],p[i+1][0][1]=b2[2]\r
-           p.insert(i+1,[[b1[2][0],b1[2][1]],[b1[3][0],b1[3][1]],[b2[1][0],b2[1][1]]])\r
-       else:\r
-           d=(box+chord)/2\r
-           lengths.append(d)\r
-           i+=1\r
+        box  = bezmisc.pointdistance(p[i  ][1],p[i  ][2])\r
+        box += bezmisc.pointdistance(p[i  ][2],p[i+1][0])\r
+        box += bezmisc.pointdistance(p[i+1][0],p[i+1][1])\r
+        chord = bezmisc.pointdistance(p[i][1], p[i+1][1])\r
+        if (box - chord) > tolerance:\r
+            b1, b2 = bezmisc.beziersplitatt([p[i][1],p[i][2],p[i+1][0],p[i+1][1]], 0.5)\r
+            p[i  ][2][0],p[i  ][2][1]=b1[1]\r
+            p[i+1][0][0],p[i+1][0][1]=b2[2]\r
+            p.insert(i+1,[[b1[2][0],b1[2][1]],[b1[3][0],b1[3][1]],[b2[1][0],b2[1][1]]])\r
+        else:\r
+            d=(box+chord)/2\r
+            lengths.append(d)\r
+            i+=1\r
     new=[p[i][1] for i in range(0,len(p)-1) if lengths[i]>zero]\r
     new.append(p[-1][1])\r
     lengths=[l for l in lengths if l>zero]\r
@@ -118,149 +118,152 @@ class PathAlongPath(pathmodifier.Diffeo):
                         help="duplicate pattern before deformation")\r
 \r
     def prepareSelectionList(self):\r
-##first selected->pattern, all but first selected-> skeletons\r
+        ##first selected->pattern, all but first selected-> skeletons\r
         id = self.options.ids[-1]\r
-       self.patterns={id:self.selected[id]}\r
-       if self.options.duplicate:\r
-           self.patterns=self.duplicateNodes(self.patterns)\r
+        self.patterns={id:self.selected[id]}\r
+        if self.options.duplicate:\r
+            self.patterns=self.duplicateNodes(self.patterns)\r
         self.expandGroupsUnlinkClones(self.patterns, True, True)\r
-       self.objectsToPaths(self.patterns)\r
-       del self.selected[id]\r
+        self.objectsToPaths(self.patterns)\r
+        del self.selected[id]\r
 \r
         self.skeletons=self.selected\r
         self.expandGroupsUnlinkClones(self.skeletons, True, False)\r
-       self.objectsToPaths(self.skeletons)\r
+        self.objectsToPaths(self.skeletons)\r
 \r
     def lengthtotime(self,l):\r
-       '''\r
-       Recieves an arc length l, and returns the index of the segment in self.skelcomp \r
-       containing the coresponding point, to gether with the position of the point on this segment.\r
-\r
-       If the deformer is closed, do computations modulo the toal length.\r
-       '''\r
-       if self.skelcompIsClosed:\r
-           l=l % sum(self.lengths)\r
-       if l<=0:\r
-           return 0,l/self.lengths[0]\r
-       i=0\r
-       while (i<len(self.lengths)) and (self.lengths[i]<=l):\r
-           l-=self.lengths[i]\r
-           i+=1\r
-       t=l/self.lengths[min(i,len(self.lengths)-1)]\r
-       return i, t\r
+        '''\r
+        Recieves an arc length l, and returns the index of the segment in self.skelcomp \r
+        containing the coresponding point, to gether with the position of the point on this segment.\r
+\r
+        If the deformer is closed, do computations modulo the toal length.\r
+        '''\r
+        if self.skelcompIsClosed:\r
+            l=l % sum(self.lengths)\r
+        if l<=0:\r
+            return 0,l/self.lengths[0]\r
+        i=0\r
+        while (i<len(self.lengths)) and (self.lengths[i]<=l):\r
+            l-=self.lengths[i]\r
+            i+=1\r
+        t=l/self.lengths[min(i,len(self.lengths)-1)]\r
+        return i, t\r
 \r
     def applyDiffeo(self,bpt,vects=()):\r
-       '''\r
-       The kernel of this stuff:\r
-       bpt is a base point and for v in vectors, v'=v-p is a tangent vector at bpt.\r
-       '''\r
+        '''\r
+        The kernel of this stuff:\r
+        bpt is a base point and for v in vectors, v'=v-p is a tangent vector at bpt.\r
+        '''\r
         s=bpt[0]-self.skelcomp[0][0]\r
-       i,t=self.lengthtotime(s)\r
+        i,t=self.lengthtotime(s)\r
         if i==len(self.skelcomp)-1:\r
             x,y=bezmisc.tpoint(self.skelcomp[i-1],self.skelcomp[i],1+t)\r
-           dx=(self.skelcomp[i][0]-self.skelcomp[i-1][0])/self.lengths[-1]\r
-           dy=(self.skelcomp[i][1]-self.skelcomp[i-1][1])/self.lengths[-1]\r
+            dx=(self.skelcomp[i][0]-self.skelcomp[i-1][0])/self.lengths[-1]\r
+            dy=(self.skelcomp[i][1]-self.skelcomp[i-1][1])/self.lengths[-1]\r
         else:\r
             x,y=bezmisc.tpoint(self.skelcomp[i],self.skelcomp[i+1],t)\r
             dx=(self.skelcomp[i+1][0]-self.skelcomp[i][0])/self.lengths[i]\r
-           dy=(self.skelcomp[i+1][1]-self.skelcomp[i][1])/self.lengths[i]\r
-\r
-       vx=0\r
-       vy=bpt[1]-self.skelcomp[0][1]\r
-       if self.options.wave:\r
-           bpt[0]=x+vx*dx\r
-           bpt[1]=y+vy+vx*dy\r
-       else:\r
-           bpt[0]=x+vx*dx-vy*dy\r
-           bpt[1]=y+vx*dy+vy*dx\r
-\r
-       for v in vects:\r
-           vx=v[0]-self.skelcomp[0][0]-s\r
-           vy=v[1]-self.skelcomp[0][1]\r
-           if self.options.wave:\r
-               v[0]=x+vx*dx\r
-               v[1]=y+vy+vx*dy\r
-           else:\r
-               v[0]=x+vx*dx-vy*dy\r
-               v[1]=y+vx*dy+vy*dx\r
+            dy=(self.skelcomp[i+1][1]-self.skelcomp[i][1])/self.lengths[i]\r
+\r
+        vx=0\r
+        vy=bpt[1]-self.skelcomp[0][1]\r
+        if self.options.wave:\r
+            bpt[0]=x+vx*dx\r
+            bpt[1]=y+vy+vx*dy\r
+        else:\r
+            bpt[0]=x+vx*dx-vy*dy\r
+            bpt[1]=y+vx*dy+vy*dx\r
+\r
+        for v in vects:\r
+            vx=v[0]-self.skelcomp[0][0]-s\r
+            vy=v[1]-self.skelcomp[0][1]\r
+            if self.options.wave:\r
+                v[0]=x+vx*dx\r
+                v[1]=y+vy+vx*dy\r
+            else:\r
+                v[0]=x+vx*dx-vy*dy\r
+                v[1]=y+vx*dy+vy*dx\r
 \r
     def effect(self):\r
-       self.prepareSelectionList()\r
-       self.options.wave = (self.options.kind=="Ribbon")\r
-       if self.options.copymode=="Single":\r
-           self.options.repeat =False\r
-           self.options.stretch=False\r
-       elif self.options.copymode=="Repeated":\r
-           self.options.repeat =True\r
-           self.options.stretch=False\r
-       elif self.options.copymode=="Single, stretched":\r
-           self.options.repeat =False\r
-           self.options.stretch=True\r
-       elif self.options.copymode=="Repeated, stretched":\r
-           self.options.repeat =True\r
-           self.options.stretch=True\r
-\r
-       bbox=self.computeBBox(self.patterns)\r
-       if self.options.vertical:\r
-           #flipxy(bbox)...\r
-           bbox=(-bbox[3],-bbox[2],-bbox[1],-bbox[0])\r
-           \r
-       width=bbox[1]-bbox[0]\r
-       dx=width+self.options.space\r
+        if len(self.options.ids)<2:\r
+            inkex.debug("This extension requires that you select two paths.")\r
+            return\r
+        self.prepareSelectionList()\r
+        self.options.wave = (self.options.kind=="Ribbon")\r
+        if self.options.copymode=="Single":\r
+            self.options.repeat =False\r
+            self.options.stretch=False\r
+        elif self.options.copymode=="Repeated":\r
+            self.options.repeat =True\r
+            self.options.stretch=False\r
+        elif self.options.copymode=="Single, stretched":\r
+            self.options.repeat =False\r
+            self.options.stretch=True\r
+        elif self.options.copymode=="Repeated, stretched":\r
+            self.options.repeat =True\r
+            self.options.stretch=True\r
+\r
+        bbox=self.computeBBox(self.patterns)\r
+        if self.options.vertical:\r
+            #flipxy(bbox)...\r
+            bbox=(-bbox[3],-bbox[2],-bbox[1],-bbox[0])\r
+            \r
+        width=bbox[1]-bbox[0]\r
+        dx=width+self.options.space\r
 \r
         for id, node in self.patterns.iteritems():\r
             if node.tagName == 'path':\r
                 d = node.attributes.getNamedItem('d')\r
                 p0 = cubicsuperpath.parsePath(d.value)\r
                 if self.options.vertical:\r
-                   flipxy(p0)\r
-\r
-               newp=[]\r
-               for skelnode in self.skeletons.itervalues(): \r
-                   self.curSekeleton=cubicsuperpath.parsePath(skelnode.getAttribute('d'))\r
-                   if self.options.vertical:\r
-                       flipxy(self.curSekeleton)\r
-                   for comp in self.curSekeleton:\r
-                       p=copy.deepcopy(p0)\r
-                       self.skelcomp,self.lengths=linearize(comp)\r
-       #!!!!>----> TODO: really test if path is closed! end point==start point is not enough!\r
-                       self.skelcompIsClosed = (self.skelcomp[0]==self.skelcomp[-1])\r
-\r
-                       length=sum(self.lengths)\r
-                       xoffset=self.skelcomp[0][0]-bbox[0]+self.options.toffset\r
-                       yoffset=self.skelcomp[0][1]-(bbox[2]+bbox[3])/2-self.options.noffset\r
-\r
-                       if self.options.repeat:\r
-                           NbCopies=max(1,int(round((length+self.options.space)/dx)))\r
-                           width=dx*NbCopies\r
-                           if not self.skelcompIsClosed:\r
-                               width-=self.options.space\r
-                           bbox=bbox[0],bbox[0]+width,bbox[2],bbox[3]\r
-                           new=[]\r
-                           for sub in p:\r
-                               for i in range(0,NbCopies,1):\r
-                                   new.append(copy.deepcopy(sub))\r
-                                   offset(sub,dx,0)\r
-                           p=new\r
-\r
-                       for sub in p:\r
-                           offset(sub,xoffset,yoffset)\r
-\r
-                       if self.options.stretch:\r
-                           for sub in p:\r
-                               stretch(sub,length/width,1,self.skelcomp[0])\r
-\r
-                       for sub in p:\r
-                           for ctlpt in sub:\r
-                               self.applyDiffeo(ctlpt[1],(ctlpt[0],ctlpt[2]))\r
-\r
-                       if self.options.vertical:\r
-                           flipxy(p)\r
-                       newp+=p\r
-\r
-               d.value = cubicsuperpath.formatPath(newp)\r
+                    flipxy(p0)\r
+\r
+                newp=[]\r
+                for skelnode in self.skeletons.itervalues(): \r
+                    self.curSekeleton=cubicsuperpath.parsePath(skelnode.getAttribute('d'))\r
+                    if self.options.vertical:\r
+                        flipxy(self.curSekeleton)\r
+                    for comp in self.curSekeleton:\r
+                        p=copy.deepcopy(p0)\r
+                        self.skelcomp,self.lengths=linearize(comp)\r
+                        #!!!!>----> TODO: really test if path is closed! end point==start point is not enough!\r
+                        self.skelcompIsClosed = (self.skelcomp[0]==self.skelcomp[-1])\r
+\r
+                        length=sum(self.lengths)\r
+                        xoffset=self.skelcomp[0][0]-bbox[0]+self.options.toffset\r
+                        yoffset=self.skelcomp[0][1]-(bbox[2]+bbox[3])/2-self.options.noffset\r
+\r
+                        if self.options.repeat:\r
+                            NbCopies=max(1,int(round((length+self.options.space)/dx)))\r
+                            width=dx*NbCopies\r
+                            if not self.skelcompIsClosed:\r
+                                width-=self.options.space\r
+                            bbox=bbox[0],bbox[0]+width,bbox[2],bbox[3]\r
+                            new=[]\r
+                            for sub in p:\r
+                                for i in range(0,NbCopies,1):\r
+                                    new.append(copy.deepcopy(sub))\r
+                                    offset(sub,dx,0)\r
+                            p=new\r
+\r
+                        for sub in p:\r
+                            offset(sub,xoffset,yoffset)\r
+\r
+                        if self.options.stretch:\r
+                            for sub in p:\r
+                                stretch(sub,length/width,1,self.skelcomp[0])\r
+\r
+                        for sub in p:\r
+                            for ctlpt in sub:\r
+                                self.applyDiffeo(ctlpt[1],(ctlpt[0],ctlpt[2]))\r
+\r
+                        if self.options.vertical:\r
+                            flipxy(p)\r
+                        newp+=p\r
+\r
+                d.value = cubicsuperpath.formatPath(newp)\r
 \r
 e = PathAlongPath()\r
 e.affect()\r
 \r
-                \r
+                    \r