Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 16 #17

Merged
merged 5 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
venv/*
__pycache__
grafana_test/
testlabsv2/
test/*
launch.json
712 changes: 467 additions & 245 deletions clab2drawio.py

Large diffs are not rendered by default.

283 changes: 182 additions & 101 deletions drawio2clab.py

Large diffs are not rendered by default.

111 changes: 68 additions & 43 deletions lib/CustomDrawioDiagram.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from N2G import drawio_diagram
import xml.etree.ElementTree as ET


class CustomDrawioDiagram(drawio_diagram):
# Overriding the drawio_diagram_xml with shadow=0
drawio_diagram_xml = """
Expand All @@ -12,15 +14,14 @@ class CustomDrawioDiagram(drawio_diagram):
</mxGraphModel>
</diagram>
"""

def __init__(self, styles=None, node_duplicates="skip", link_duplicates="skip"):

def __init__(self, styles=None, node_duplicates="skip", link_duplicates="skip"):
if styles:
background = styles['background']
shadow = styles['shadow']
grid = styles['grid']
pagew = styles['pagew']
pageh = styles['pageh']
background = styles["background"]
shadow = styles["shadow"]
grid = styles["grid"]
pagew = styles["pagew"]
pageh = styles["pageh"]

self.drawio_diagram_xml = f"""
<diagram id="{{id}}" name="{{name}}">
Expand All @@ -33,19 +34,22 @@ def __init__(self, styles=None, node_duplicates="skip", link_duplicates="skip"):
</diagram>
"""

super().__init__(node_duplicates, link_duplicates, )
super().__init__(
node_duplicates,
link_duplicates,
)

def calculate_new_group_positions(self, obj_pos_old, group_pos):
# Adjust object positions relative to the new group's position
obj_pos_new = (obj_pos_old[0] - group_pos[0], obj_pos_old[1] - group_pos[1])
return obj_pos_new

def update_style(self, styles):
background = styles['background']
shadow = styles['shadow']
grid = styles['grid']
pagew = styles['pagew']
pageh = styles['pageh']
background = styles["background"]
shadow = styles["shadow"]
grid = styles["grid"]
pagew = styles["pagew"]
pageh = styles["pageh"]

self.drawio_diagram_xml = f"""
<diagram id="{{id}}" name="{{name}}">
Expand All @@ -61,8 +65,8 @@ def update_style(self, styles):

def group_nodes(self, member_objects, group_id, style=""):
# Initialize bounding box coordinates
min_x = min_y = float('inf')
max_x = max_y = float('-inf')
min_x = min_y = float("inf")
max_x = max_y = float("-inf")

object_positions = [] # To store all object positions

Expand All @@ -72,8 +76,11 @@ def group_nodes(self, member_objects, group_id, style=""):
if obj_mxcell is not None:
geometry = obj_mxcell.find("./mxGeometry")
if geometry is not None:
x, y = float(geometry.get('x', '0')), float(geometry.get('y', '0'))
width, height = float(geometry.get('width', '0')), float(geometry.get('height', '0'))
x, y = float(geometry.get("x", "0")), float(geometry.get("y", "0"))
width, height = (
float(geometry.get("width", "0")),
float(geometry.get("height", "0")),
)

# Store object positions and update bounding box
object_positions.append((obj_id, x, y, width, height))
Expand All @@ -96,43 +103,47 @@ def group_nodes(self, member_objects, group_id, style=""):
# Update positions of all objects within the group
for obj_id, x, y, _, _ in object_positions:
obj_pos_old = (x, y)
obj_pos_new = self.calculate_new_group_positions(obj_pos_old, (group_x, group_y))
obj_pos_new = self.calculate_new_group_positions(
obj_pos_old, (group_x, group_y)
)

obj_mxcell = self.current_root.find(f".//object[@id='{obj_id}']/mxCell")
if obj_mxcell is not None:
geometry = obj_mxcell.find("./mxGeometry")
if geometry is not None:
geometry.set('x', str(obj_pos_new[0]))
geometry.set('y', str(obj_pos_new[1]))
obj_mxcell.set("parent", group_id) # Set the object's parent to the new group
geometry.set("x", str(obj_pos_new[0]))
geometry.set("y", str(obj_pos_new[1]))
obj_mxcell.set(
"parent", group_id
) # Set the object's parent to the new group

def get_used_levels(self):
return set([node.graph_level for node in self.nodes.values()])

def get_max_level(self):
return max([node.graph_level for node in self.nodes.values()])

def get_min_level(self):
return min([node.graph_level for node in self.nodes.values()])

def get_links_from_nodes(self):
links = []
for node in self.nodes.values():
links.extend(node.get_all_links())
return links

def get_upstream_links_from_nodes(self):
links = []
for node in self.nodes.values():
links.extend(node.get_upstream_links())
return links

def get_downstream_links_from_nodes(self):
links = []
for node in self.nodes.values():
links.extend(node.get_downstream_links())
return links

def get_lateral_links_from_nodes(self):
links = []
for node in self.nodes.values():
Expand All @@ -141,19 +152,21 @@ def get_lateral_links_from_nodes(self):

def get_target_link(self, source_link):
for link in self.get_links_from_nodes():
if link.source == source_link.target \
and link.target == source_link.source \
and (link.direction != 'lateral' or link.direction == source_link.direction) \
and source_link.source_intf == link.target_intf \
and source_link.target_intf == link.source_intf:
if (
link.source == source_link.target
and link.target == source_link.source
and (
link.direction != "lateral"
or link.direction == source_link.direction
)
and source_link.source_intf == link.target_intf
and source_link.target_intf == link.source_intf
):
return link
return None



def get_nodes(self):
return self.nodes


def get_nodes_with_same_xy(self):
nodes_with_same_x = {}
Expand Down Expand Up @@ -190,10 +203,16 @@ def get_nodes_between_interconnected(self):
# Check if there are any nodes between node1 and node2 based on their positions
for node_between in nodes:
if node_between != node1 and node_between != node2:
if (node1.pos_y < node_between.pos_y < node2.pos_y) or (node2.pos_y < node_between.pos_y < node1.pos_y):
if node_between not in nodes_between_interconnected_x:
nodes_between_interconnected_x.append(node_between)

if (node1.pos_y < node_between.pos_y < node2.pos_y) or (
node2.pos_y < node_between.pos_y < node1.pos_y
):
if (
node_between
not in nodes_between_interconnected_x
):
nodes_between_interconnected_x.append(
node_between
)

for coord, nodes in nodes_with_same_y.items():
for i in range(len(nodes)):
Expand All @@ -204,13 +223,19 @@ def get_nodes_between_interconnected(self):
# Check if there are any nodes between node1 and node2 based on their positions
for node_between in nodes:
if node_between != node1 and node_between != node2:
if (node1.pos_x < node_between.pos_x < node2.pos_x) or (node2.pos_x < node_between.pos_x < node1.pos_x):
if node_between not in nodes_between_interconnected_y:
nodes_between_interconnected_y.append(node_between)
if (node1.pos_x < node_between.pos_x < node2.pos_x) or (
node2.pos_x < node_between.pos_x < node1.pos_x
):
if (
node_between
not in nodes_between_interconnected_y
):
nodes_between_interconnected_y.append(
node_between
)

return nodes_between_interconnected_x, nodes_between_interconnected_y


def get_nodes_by_level(self, level):
nodes_by_level = {}
for node in self.nodes.values():
Expand Down
Loading