Inkscape.org
Creating New Extensions give the paths with multiple labels or tags
  1. #1
    czkPanda czkPanda @czkPanda
    *

    I was wondering how I can give the paths with labels or tags. Is it possible to set multiple labels/tags to one path? I'd need to give the paths multiple labels or tags for future data processing. I tried the code below but it doesn't work. 

    I want to work with a specific layer (called 'Polygon') of multiple paths, including two types of paths: 1, closed polygon paths and 2, paths that are line. I'd like to select all type 2 paths and give them a label called 'boundary'. Since the d string of the closed polygon paths ends up with 'Z',  the d string of the line paths ends up with numbers. I used this method to select the line paths and use item.set() to give the label to the path. 

     

    class Prepare(inkex.EffectExtension):
        
        def find_boundary(self,layer,boundary):
            item = self.svg.get('inkscape:label') 
            if item == layer:
                for child in item.getchidren():
                    dstr = child.get('d') 
                    if dstr[-1] != 'Z':
                        child.set('label',boundary)
    
        def effect(self): 
            self.find_boundary('Polygon','boundary')
    
    if __name__ == '__main__':
        Prepare().run()

     

  2. #2
    inklinea inklinea @inklinea⛰️
    *

    Labels are really there for the user experience not for coding as such.  They existing the Inkscape namespace, I would not try to manipulate them for assisting coding as such.

    A very simple way is to use svg custom data attributes.

    https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/data-*#:~:text=The%20data%2D*%20SVG%20attributes,belong%20to%2C%20with%20the%20SVGElement.

    As the page points out: 

    • Can't start with xml.
    • No semicolons (;U+003A).
    • No capital A to Z letters.

    I've used them before, very simple and easy.

     

     

  3. #3
    czkPanda czkPanda @czkPanda

    Hi inklinea,

    Thank you for your info. Would you mind providing a simple example?

    I read the webpage about data-* and wrote my code, but I couldn't get it to work. 

    Thanks!

  4. #4
    inklinea inklinea @inklinea⛰️
    for item in selection_list:
        item.set('data-czkpanda', 'Hello this is a custom data attribute :)')
        item.set('data-inklinea', 'Another custom data attribute !')

    they can be retrieved with item.get and removed using set None

    for item in selection_list:
        if item.get('data-czkpanda'):  # if None, is bool False
            item.set('data-czkpanda', None)

     

  5. #5
    czkPanda czkPanda @czkPanda

    Thanks!

     

  6. #6
    czkPanda czkPanda @czkPanda

    Hi, I wonder how I can find ALL the paths with the given attribute within a layer. If I use return, it only return one path and end the loop. 

        def paths_of_dAttr(self,layer,data_attr):
            """Select the paths labeled with the given attribute.
            """
            for item in self.svg:
                if item.get('inkscape:label') == layer:
                    paths = item.getchildren()
                    for path in paths:
                        if path.get(data_attr):
                           return path

     

  7. #7
    czkPanda czkPanda @czkPanda

    Hi, I wonder how I can find ALL the paths with the given attribute within a layer. If I use return, it only return one path and end the loop. 

        def paths_of_dAttr(self,layer,data_attr):
            """Select the paths labeled with the given attribute.
            """
            for item in self.svg:
                if item.get('inkscape:label') == layer:
                    paths = item.getchildren()
                    for path in paths:
                        if path.get(data_attr):
                           return path

     

  8. #8
    czkPanda czkPanda @czkPanda

    I solved the problem using xpath method.

Inkscape Inkscape.org Inkscape Forum Creating New Extensions give the paths with multiple labels or tags