Inkscape.org
Creating New Extensions Find siblings of an element
  1. #1
    hex mex hex mex @littledemon

    I find an element by the following code:

    self.document.xpath("//*[name()='desc']")

    This element has brothers and sisters who do not have special characteristics to identify them and can only be recognized by their tags.

    <g id="shape875-64" v:mID="875" v:groupContext="shape" transform="translate(43.0197,-65.2966)">
                <title>Sheet.875</title>
                <desc>ML_OP1</desc>
                <v:textBlock v:margins="rect(1,1,1,1)" v:tabSpace="42.5197" v:verticalAlign="0"/>
                <v:textRect cx="39.685" cy="243.262" width="79.38" height="17.0079"/>
                <rect x="0" y="234.758" width="79.3701" height="17.0079" class="st9"/>
                <text x="15.47" y="248.36" class="st11" v:langID="1033">ML_OP1</text>

    </g>

    Here I need last sibling <text>.

    Note: I can only find objects using the string inside <desc> , that is, I find all the elements that have <desc> and then read the text inside them one by one and compare it with a dictionary.

    If the mapping is done, I have to change the sibling element..

    Now I need to find the sibling in one of the following two ways:
    1- Finding parent and finding sibling through him
    2- Finding sibling directly

    I think my problem is that I can get everything by getting the first element xpath. If the element was among the selected objects, I could easily get its xpath in the loop(iterItems). But here no object is selected and my code is responsible for intelligently finding objects.

     

    Sorry if it's a bit long. hope it is not complicated

  2. #2
    inklinea inklinea @inklinea⛰️

     

     
    selection_list = self.svg.selection
    
    if len(selection_list) < 1:
        inkex.errormsg('Please select an object')
        return
    
    next_object = selection_list[0].getnext()
    previous_object = selection_list[0].getprevious()
    if next_object != None:
        next_object_id = next_object.get_id()
    else:
        next_object_id = 'none'
    
    if previous_object != None:
        previous_object_id = previous_object.get_id()
    else:
        previous_object_id = 'none'
    
    inkex.errormsg(f'Next Object {next_object} Previous Object {previous_object}')
    inkex.errormsg('---------------------')
    inkex.errormsg(f'Next Object ID {next_object_id} Previous Object ID {previous_object_id}')