2020-08-03 02:15:03 +02:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
'''
|
|
|
|
Copyright (C) 2019 Grant Patterson <grant@revoltlabs.co>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
'''
|
|
|
|
|
|
|
|
import gettext
|
|
|
|
import sys
|
|
|
|
import inkex
|
2020-09-03 00:35:27 +02:00
|
|
|
import rotate_helper
|
2020-08-03 02:15:03 +02:00
|
|
|
from inkex import Transform
|
|
|
|
import copy
|
|
|
|
|
|
|
|
debug = False
|
|
|
|
|
|
|
|
error = lambda msg: inkex.errormsg(gettext.gettext(msg))
|
|
|
|
if debug:
|
|
|
|
stderr = lambda msg: sys.stderr.write(msg + '\n')
|
|
|
|
else:
|
|
|
|
stderr = lambda msg: None
|
|
|
|
|
2021-06-02 23:30:37 +02:00
|
|
|
class RotationsFindAllOptimal(inkex.EffectExtension):
|
2021-04-16 14:41:12 +02:00
|
|
|
|
2020-08-03 02:15:03 +02:00
|
|
|
def effect(self):
|
2021-04-16 14:41:12 +02:00
|
|
|
|
2020-08-03 02:15:03 +02:00
|
|
|
def duplicateNodes(aList):
|
|
|
|
clones={}
|
|
|
|
for id,node in aList.items():
|
|
|
|
clone=copy.deepcopy(node)
|
|
|
|
#!!!--> should it be given an id?
|
|
|
|
#seems to work without this!?!
|
|
|
|
myid = node.tag.split('}')[-1]
|
|
|
|
clone.set("id", self.svg.get_unique_id(myid))
|
|
|
|
node.getparent().append(clone)
|
|
|
|
clones[clone.get("id")]=clone
|
|
|
|
return(clones)
|
|
|
|
|
|
|
|
for nid, node in self.svg.selected.items():
|
|
|
|
# set() removes duplicates
|
|
|
|
angles = set(
|
|
|
|
# and remove Nones
|
2020-09-03 00:35:27 +02:00
|
|
|
[x for x in rotate_helper.optimal_rotations(node)
|
2020-08-03 02:15:03 +02:00
|
|
|
if x is not None])
|
|
|
|
# Go backwards so we know if we need to duplicate the node for
|
|
|
|
# multiple rotations. (We don't want to rotate the main node
|
|
|
|
# before duplicating it.)
|
|
|
|
for i, angle in reversed(list(enumerate(angles))):
|
|
|
|
if i > 0:
|
|
|
|
# Rotate a duplicate of the node
|
|
|
|
rotate_node = list(duplicateNodes({nid: node}).items())[0][1]
|
|
|
|
else:
|
|
|
|
rotate_node = node
|
2020-09-03 00:35:27 +02:00
|
|
|
rotate_node.transform = rotate_helper.rotate_matrix(rotate_node, angle) * rotate_node.transform
|
2020-08-31 21:25:41 +02:00
|
|
|
|
2020-08-03 02:15:03 +02:00
|
|
|
if __name__ == '__main__':
|
2021-06-02 23:30:37 +02:00
|
|
|
RotationsFindAllOptimal().run()
|