horizontal joins work for components with the same number of wires

main
Howard DaCosta 2021-12-02 17:20:40 -05:00
rodzic e7e98123f2
commit 39cda7bffc
3 zmienionych plików z 250 dodań i 69 usunięć

BIN
.DS_Store vendored

Plik binarny nie jest wyświetlany.

Wyświetl plik

@ -22,7 +22,26 @@ class CombineGridsFrame(wx.Frame):
lc.Init(wx.LANGUAGE_DEFAULT)
pass
class Connector():
'''
Object to represent connector of wires
'''
def __init__(self, connector_points, bbox):
self.connector_points = connector_points # all coords where wires need to route to
self.open_wire_idx = 0 # idx of next available wire
self.bbox = bbox
inkex.errormsg("num connectors:{}".format(len(self.connector_points)))
def has_available_wires(self):
return self.open_wire_idx <= len(self.connector_points) - 4 # every connector is 4 points
def connect_wire(self):
if self.has_available_wires():
points = self.connector_points[self.open_wire_idx:self.open_wire_idx + 4]
self.open_wire_idx += 2
return points
else:
inkex.errormsg("connector has no more open connections. Decrease the number of wires!")
return None
class CombineGrids(InkstitchExtension):
COMMANDS = ["combine_grids"]
def __init__(self, *args, **kwargs):
@ -30,55 +49,169 @@ class CombineGrids(InkstitchExtension):
InkstitchExtension.__init__(self, *args, **kwargs)
self.arg_parser.add_argument("--alignment")
args, _ = self.arg_parser.parse_known_args()
inkex.errormsg("args:{}".format(args.alignment))
self.is_horizontal_connection = True if args.alignment == 1 else False
self.is_horizontal_connection = True if args.alignment == "1" else False
inkex.errormsg(self.is_horizontal_connection)
self.wires = []
self.wire_rectangles = []
# self.wire_rectangles = []
self.connector = None
def cancel(self):
self.cancelled = True
def get_points(self, line):
# return [p for p in line.path.end_points]
return line.points
def check_horizontal_wire_directions(self, num_left_wires, num_right_wires):
'''
Checks that the wires can be connected in such a way that union
goes in the SAME direction (so that they can be hooked up to connectors later)
'''
if num_left_wires < num_right_wires and num_left_wires % 2 == 0:
return False
if num_right_wires < num_left_wires and num_right_wires % 2 == 1:
return False
return True
def connect_horizontally(self):
rect1, rect2 = self.wire_rectangles
if not rect1.is_horizontally_aligned(rect2):
inkex.errormsg("Unable to horizontally connect the two objects.")
return
leftmost_rectangle = None
letmost_wire = None
other_rectangle = None
other_wire = None
# V1 implementation without connectors present
# rect1, rect2 = self.wire_rectangles
# V2
rect1,rect2 = self.wires[0].bbox, self.wires[1].bbox
left_wire = None
right_wire = None
if rect1.left < rect2.left:
leftmost_rectangle = rect1
letmost_wire = self.wires[0]
other_rectangle = rect2
other_wire = self.wires[1]
left_wire = self.wires[0]
right_wire = self.wires[1]
else:
leftmost_rectangle = rect2
letmost_wire = self.wires[1]
other_rectangle = rect1
other_wire = self.wires[0]
left_wire = self.wires[1]
right_wire = self.wires[0]
# is there a need for this?
# valid_connection = self.check_horizontal_wire_directions(num_left_wires, num_right_wires)
# inkex.errormsg("num lr:{},{}".format(num_left_wires, num_right_wires))
# if not valid_connection:
# if not(num_left_wires == num_right_wires):
# min_wire_side = "left" if num_left_wires < num_right_wires else "right"
# inkex.errormsg("Please add or subtract a wire from the {} shape in order to ensure that \
# the wires can be connected properrly.".format(min_wire_side))
# return
self.union_wires(left_wire, right_wire, True)
def union_wires(self, min_wire, max_wire, is_horizontal):
min_wire_points = self.get_points(min_wire)
max_wire_points = self.get_points(max_wire)
min_multiplier = min_wire.get_num_wire_joins(is_horizontal)
max_multiplier = max_wire.get_num_wire_joins(is_horizontal)
inkex.errormsg("mult:{}, {}".format(min_multiplier, max_multiplier))
min_wire_idx = 2 * min_multiplier
max_wire_idx = 0
min_points = ['{},{}'.format(p.x,p.y) for p in min_wire_points[0: min_wire_idx]]
union_wire_points = []
union_wire_points.extend(min_points)
while min_wire_idx != len(min_wire_points): # leave hanging wire
# 4 points on max wire constitutes a wrap around from one wire path to the next
# this will not always be true as one may need to connect multiple shapes together in sets of two
max_wire_splice_length = min(4 * max_multiplier, len(max_wire_points) - max_wire_idx)
inkex.errormsg("max wire idx:{}".format(max_wire_idx))
max_points = ['{},{}'.format(p.x,p.y) for p in max_wire_points[max_wire_idx: max_wire_idx + max_wire_splice_length]]
union_wire_points.extend(max_points)
max_wire_idx += max_wire_splice_length
inkex.errormsg("max wire idx:{}".format(max_wire_idx))
inkex.errormsg("min wire idx:{}".format(min_wire_idx))
min_wire_splice_length = min(4 * min_multiplier, len(min_wire_points) - min_wire_idx)
min_points = ['{},{}'.format(p.x,p.y) for p in min_wire_points[min_wire_idx: min_wire_idx + min_wire_splice_length]]
union_wire_points.extend(min_points)
min_wire_idx += min_wire_splice_length
inkex.errormsg("min wire idx:{}".format(min_wire_idx))
max_points = ['{},{}'.format(p.x,p.y) for p in max_wire_points[max_wire_idx: len(max_wire_points)]]
union_wire_points.extend(max_points)
# done unionizing the wires
# remove old wires
min_wire.wire.getparent().remove(min_wire.wire)
max_wire.wire.getparent().remove(max_wire.wire)
# add new combined one
inkex.errormsg(union_wire_points)
self.create_path(union_wire_points, is_horizontal)
def create_path(self, points, is_horizontal):
'''
Creates a wire segment path given all of the points sequentially
'''
color = "red" if is_horizontal else "blue"
path_str = ' '.join(points)
path = inkex.Polyline(attrib={
'id': "wire_segment",
'style': "stroke: %s; stroke-width: 0.4; fill: none; stroke-dasharray:0.4,0.4" % color,
'points': path_str,
})
self.svg.get_current_layer().append(path)
def effect(self):
connector_points = []
connector_bbox = None
for elem in self.svg.get_selected():
# inkex.errormsg("things selected:{}".format(len(self.svg.get_selected())))
inkex.errormsg("type of elem:{}".format(type(elem)))
# have to separate shapes and wires here!
wire_points = [p for p in elem.path.end_points]
if type(elem) == Polyline:
self.wires.append(wire_points)
self.wire_rectangles.append(elem.bounding_box())
# self.wires.append(elem)
# self.wire_rectangles.append(elem.bounding_box())
#V2
wire = Wire(elem)
self.wires.append(wire)
# elif type(elem) == PathElement: #connector
# self.wire_rectangles.append(elem.bounding_box())
# connector_bbox = elem.bbox()
# points = self.get_points(elem)
# for p in points:
# connector_points.append(p)
self.connector = Connector(connector_points, connector_bbox)
if len(self.wires) != 2:
inkex.errormsg(len(self.wires))
inkex.errormsg("Please select only two wires to combine!")
return
if self.is_horizontal_connection:
self.connect_horizontally()
class Wire:
def __init__(self, wire):
self.wire = wire
self.points = [p for p in self.wire.path.end_points]
self.bbox = self.wire.bounding_box()
def get_num_wire_joins(self, is_horizontal):
'''
Determines how many wires were horizontally joined together to create the current wire object
The default is 1
'''
point_counter = 1
for i in range(len(self.points) - 1):
p1 = self.points[i]
p2 = self.points[i+1]
inkex.errormsg("points:{},{}".format(p1,p2))
if (is_horizontal and p1.x == p2.x) or (not is_horizontal and p1.y == p2.y):
inkex.errormsg("coming in here")
return point_counter // 2
else:
inkex.errormsg("adding 1!")
point_counter += 1
# should never get here?
return None
if __name__ == '__main__':
inkex.errormsg(sys.argv[1:])
@ -88,4 +221,4 @@ if __name__ == '__main__':
parser.add_argument('args', nargs=REMAINDER)
args, _ = parser.parse_known_args()
inkex.errormsg("args:{}".format(args))
CombineGrids(args.horizontal_wires, args.vertical_wires).run()
CombineGrids().run()

Wyświetl plik

@ -28,6 +28,71 @@ import numpy as np
MIN_GRID_SPACING = 2.5
BBOX_SPACING = 5
class BoundingBoxMetadata():
'''
Storage class to hold important information about rectangle
'''
def __init__(self, width, height, top, bottom, left, right):
self.width = width
self.height = height
self.top = top
self.bottom = bottom
self.left = left
self.right = right
self.horizontal_wire_points = None
self.vertical_wire_points = None
def get_rectangle_points(self):
'''
returns upper_left , upper_right, lower_left, lower_right points as list of tuples
in that order
'''
return [
(self.left, self.top),
(self.right, self.top),
(self.left, self.bottom),
(self.right, self.bottom)
]
def is_horizontally_aligned(self, other):
'''
Verifies whether or not two bounding boxes can be
connected horizontally
Essentially other cannot be directly under this bbox with some buffer
'''
return abs(self.left - other.left) >= self.width
def is_vertically_aligned(self, other):
'''
Verifies whether or not two bounding boxes can be
connected vertically
Essentially other cannot be directly next to this bbox with some buffer
'''
return abs(self.top - other.top) >= self.height
def add_wire(self, wire_points):
'''
If wire is contained in BBOX, adds it to object
'''
# wire will always go to left - BBOX_SPACING and top - BBOX_SPACING
x_coord = self.left - BBOX_SPACING
y_coord = self.top - BBOX_SPACING
for point in wire_points:
if x_coord == point.x:
self.horizontal_wire_points = wire_points
elif y_coord == point.y:
self.vertical_wire_points = wire_points
def get_wire_points(self, is_horizontal):
return self.horizontal_wire_points if is_horizontal else self.vertical_wire_points
def wires_grouped(self):
inkex.errormsg("yuo yo")
inkex.errormsg("horiz_points:{}".format(self.horizontal_wire_points))
inkex.errormsg("vertical_points:{}".format(self.vertical_wire_points))
return self.horizontal_wire_points is not None and self.vertical_wire_points is not None
class CreateGridFrame(wx.Frame):
DEFAULT_FONT = "small_font"
@ -202,32 +267,6 @@ class CreateGridFrame(wx.Frame):
size.height = size.height + 200
self.SetSize(size)
class BoundingBoxMetadata():
'''
Storage class to hold important information about rectangle
'''
def __init__(self, width, height, top, bottom, left, right):
self.width = width
self.height = height
self.top = top
self.bottom = bottom
self.left = left
self.right = right
def get_rectangle_points(self):
'''
returns upper_left , upper_right, lower_left, lower_right points as list of tuples
in that order
'''
return [
(self.left, self.top),
(self.right, self.top),
(self.left, self.bottom),
(self.right, self.bottom)
]
class CreateGrid(InkstitchExtension):
COMMANDS = ["create_grid"]
@ -236,6 +275,15 @@ class CreateGrid(InkstitchExtension):
InkstitchExtension.__init__(self, *args, **kwargs)
for command in self.COMMANDS:
self.arg_parser.add_argument("--%s" % command, type=inkex.Boolean)
self.arg_parser.add_argument("--horizontal_wires")
self.arg_parser.add_argument("--vertical_wires")
self.arg_parser.add_argument('args', nargs=REMAINDER)
args, _ = self.arg_parser.parse_known_args()
inkex.errormsg("args:{}".format(args))
def cancel(self):
self.cancelled = True
def effect(self):
rectangle = None
@ -274,12 +322,12 @@ class CreateGrid(InkstitchExtension):
if __name__ == '__main__':
inkex.errormsg(sys.argv[1:])
parser = ArgumentParser()
parser.add_argument("--horizontal_wires")
parser.add_argument("--vertical_wires")
parser.add_argument('args', nargs=REMAINDER)
args, _ = parser.parse_known_args()
inkex.errormsg("args:{}".format(args))
CreateGrid(args.horizontal_wires, args.vertical_wires).run()
# if __name__ == '__main__':
# inkex.errormsg(sys.argv[1:])
# parser = ArgumentParser()
# parser.add_argument("--horizontal_wires")
# parser.add_argument("--vertical_wires")
# parser.add_argument('args', nargs=REMAINDER)
# args, _ = parser.parse_known_args()
# inkex.errormsg("args:{}".format(args))
# # CreateGrid(args.horizontal_wires, args.vertical_wires).run()