Например, есть три выбранные группы прямоугольников:

После использования расширения на них отображается только первая группа преобразуется в путь объединения, остальные дублируются и больше не находятся на исходных позициях:

Ожидаемым результатом должно быть три пути объединения:

Вот расширение (сохраните его как "union_operation_on_each_selected_group.py"):
Код: Выделить всё
import inkex
import tempfile, os, shutil
from uuid import uuid4
from inkex import Group, Circle, Ellipse, Line, PathElement, Polygon, Polyline, Rectangle, Use
from inkex.paths import Path
from inkex.command import call
from math import ceil
from lxml import etree
def process_svg(svg, action_string):
temp_folder = tempfile.mkdtemp()
# Create a random filename for svg
svg_temp_filepath = os.path.join(temp_folder, f'original_{str(uuid4())}.svg')
with open(svg_temp_filepath, 'w') as output_file:
svg_string = svg.tostring().decode('utf-8')
output_file.write(svg_string)
processed_svg_temp_filepath = os.path.join(temp_folder, f'processed_{str(uuid4())}.svg')
my_actions = '--actions='
export_action_string = my_actions + f'export-filename:{processed_svg_temp_filepath};{action_string}export-do;'
# Run the command line
cmd_selection_list = inkex.command.inkscape(svg_temp_filepath, export_action_string)
# Replace the current document with the processed document
with open(processed_svg_temp_filepath, 'rb') as processed_svg:
loaded_svg = inkex.elements.load_svg(processed_svg).getroot()
shutil.rmtree(temp_folder)
return loaded_svg
def element_list_union(svg, element_list):
group = None
action_string = ''
for element in element_list:
group = element.getparent()
action_string = action_string + f'select-by-id:{element.get_id()};'
action_string = action_string + f'path-union;select-clear;'
processed_svg = process_svg(svg, action_string)
to_remove = []
for child in group:
to_remove.append(child)
for item in to_remove:
group.remove(item)
for elem in processed_svg:
group.add(elem)
class UnionizeEachGroupMembers(inkex.EffectExtension):
def add_arguments(self, pars):
pars.add_argument("--selection_type_radio", type=str, dest="selection_type_radio", default='all')
def effect(self):
SELECTION_TYPE = self.options.selection_type_radio
if SELECTION_TYPE == 'all':
selection_list = self.svg.descendants()
else:
selection_list = self.svg.selected
# Filter for only shapes
selected_elements = selection_list.filter(inkex.ShapeElement)
if len(selected_elements) < 1:
inkex.errormsg('Please select at least one object / No Objects found')
return
for elem in selected_elements:
if elem.tag == inkex.addNS('g', 'svg'): # Check if the tag is 'g' in the SVG namespace
group = elem
group_id = group.get_id()
elements_to_union = [child for child in group if isinstance(child, (Circle, Ellipse, Line, PathElement, Polygon, Polyline, Rectangle, Use))]
if len(elements_to_union) > 0:
element_list_union(self.svg, elements_to_union)
if __name__ == '__main__':
UnionizeEachGroupMembers().run()
Код: Выделить всё
Union Operation on Each Selected Group
union_operation_on_each_selected_group
Selected Objects
All Objects
all
union_operation_on_each_selected_group.py
Файл примера
Можно ли получить ожидаемый результат, используя расширение? Как я могу исправить расширение?
Подробнее здесь: https://stackoverflow.com/questions/797 ... ird-result
Мобильная версия