Extension to offset a path

Discussion about writing code for Inkscape.
tonyallen28
Posts: 4
Joined: Fri Oct 28, 2016 11:27 pm

Extension to offset a path

Postby tonyallen28 » Fri Oct 28, 2016 11:36 pm

I want to simplify some tasks I have to do. I've been looking through some extensions but haven't been able to find anything on offsetting a path. I realize its a very simple operation to offset a path but I'm wanting to do it several times and with different widths so an extension would help out a lot. Can someone help point me in the right direction how to accomplish this? Thanks in advance.

User avatar
prkos
Posts: 1625
Joined: Tue Nov 06, 2007 8:45 am
Location: Croatia

Re: Extension to offset a path

Postby prkos » Fri Oct 28, 2016 11:44 pm

Welcome to the forum!

You can find several offset options under the Path menu. Do they help or are you after something else?

Path Offset Commands
http://tavmjong.free.fr/INKSCAPE/MANUAL ... hs-Offsets
just hand over the chocolate and nobody gets hurt

Inkscape Manual on Floss
Inkscape FAQ
very comprehensive Inkscape guide
Inkscape 0.48 Illustrator's Cookbook - 109 recipes to learn and explore Inkscape - with SVG examples to download

tonyallen28
Posts: 4
Joined: Fri Oct 28, 2016 11:27 pm

Re: Extension to offset a path

Postby tonyallen28 » Sat Oct 29, 2016 3:18 am

Thanks for the response. I should have been more precise; I'm trying to make my own extension to automate offsetting a lot of paths. Its hard to find documentation on the extensions so far I've found what I believe is an offset statement in the gcodetools extension.

inkex.etree.SubElement( area_group, inkex.addNS('path','svg'),
{
inkex.addNS('type','sodipodi'): 'inkscape:offset',
inkex.addNS('radius','inkscape'): str(radius),
inkex.addNS('original','inkscape'): d,
'style': styles["biarc_style_i"]['area']
})
This very well may be what I need but I'm having issues passing in the 'd' argument. The gcodetools extension is very elaborate and thorough so its hard for me to decipher everything going on with it. I'll post what I have when I get a chance and maybe someone can help me with my extension.

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Extension to offset a path

Postby Moini » Sat Oct 29, 2016 6:34 am

Yes, the extension produces outsets all with the same value, for filling areas. Is that what you mean?
(What will you do with the different, irregular outsets?)
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Extension to offset a path

Postby Moini » Sat Oct 29, 2016 6:36 am

d must be the original path data, I believe. What is the problem with inserting it?
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

tonyallen28
Posts: 4
Joined: Fri Oct 28, 2016 11:27 pm

Re: Extension to offset a path

Postby tonyallen28 » Sat Oct 29, 2016 8:10 am

I'm still learning python so it may just be a programming error. Here is what I have

Code: Select all

def recursivelyTraverseSvg( self, aNodeList):
      for node in aNodeList:
         if node.tag == inkex.addNS( 'g', 'svg' ) or node.tag == 'g':
            self.recursivelyTraverseSvg( node )
         if node.tag == inkex.addNS( 'path', 'svg' ):
            return node.get('d')
      
   def effect( self ):
      aNodeList = self.document.getroot()
      d = self.recursivelyTraverseSvg(aNodeList)
      inkex.errormsg('Size of d element:%d' % len(d))
      parent = self.current_layer
      s = {'stroke':'#000000', 'stroke-width':'1', 'fill':'none', 'stroke-opacity':'1'}
      attr = {'style':simplestyle.formatStyle(s),
         inkex.addNS('type','sodipodi'):   'inkscape:offset',
         inkex.addNS('radius','inkscape'):   '5',
         inkex.addNS('original','inkscape'):   d
      }
      inkex.etree.SubElement(   parent, inkex.addNS('path','svg'), attr)
         


I only have one path in the svg that I'm testing and I dont know an easier way to obtain it other than doing this recursive search which I lifted from another extension. The error message shows that d is 'NoneType' so I'm not actually getting the original path.

Again thanks for the help with this.

Moini
Posts: 3381
Joined: Mon Oct 05, 2015 10:44 am

Re: Extension to offset a path

Postby Moini » Sat Oct 29, 2016 8:58 am

Q1: Extensions have an option to retrieve the currently selected path(s), did you see that? It's self.selected for the inkex.Effect class.
Or is it your intention to apply an offset to *each* path in the document?

Q2: I don't know right now without testing why you get 'None' (well, the indentation looks weird, but I guess that's a copy-paste problem), but couldn't you do some debugging in the recursivelyTraverseSvg method already, to see what's going on? Use inkex.debug("your error message: %s" % your_formatting_data), this won't stop the program from running, but will only pop up messages on the screen.

Maybe it's just a missing import or so. For a good start for retrieving path data, I'd suggest taking a look at the 'Whirl' extension (whirl.py in /usr/share/inkscape/extensions if you're on Linux), which takes a path and modifies its data.
Something doesn't work? - Keeping an eye on the status bar can save you a lot of time!

Inkscape FAQ - Learning Resources - Website with tutorials (German and English)

hulf2012
Posts: 716
Joined: Sat Nov 24, 2012 12:37 pm

Re: Extension to offset a path

Postby hulf2012 » Sat Oct 29, 2016 9:56 am

Hello

Code: Select all

return node.get('d')


Are you sure that it is the correct way to retrieve the "d" element?

As Moini says, try this (If I'm not wrong):

Code: Select all

inkex.debug( node.get('d') )
or

print( node.get('d')  )
If you have problems:
1.- Post a sample (or samples) of your file please.
2.- Please check here:
http://tavmjong.free.fr/INKSCAPE/MANUAL/html/index.html
3.- If you manage to solve your problem, please post here your solution.

tonyallen28
Posts: 4
Joined: Fri Oct 28, 2016 11:27 pm

Re: Extension to offset a path

Postby tonyallen28 » Sat Oct 29, 2016 10:13 am

Thanks for the help guys the whirl extension gave me what I needed. I would like to be able to run the extension without having to select the path but this is progress for sure. One hurdle down many more to go. Here is the working code:

Code: Select all

   def effect( self ):
      for id, node in self.selected.iteritems():
         if node.tag == inkex.addNS('path','svg'):
            d = node.get('d')
            inkex.errormsg('Size of d element:%d' % len(d))
            parent = self.current_layer
            s = {'stroke':'#000000', 'stroke-width':'1', 'fill':'none', 'stroke-opacity':'1'}
            attr = {'style':simplestyle.formatStyle(s),
               inkex.addNS('type','sodipodi'):   'inkscape:offset',
               inkex.addNS('radius','inkscape'):   '5',
               inkex.addNS('original','inkscape'):   d
            }
         
            inkex.etree.SubElement(   parent, inkex.addNS('path','svg'), attr)


Return to “Programming”