Skip to content

Commit

Permalink
cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
sunilshetye committed Dec 19, 2024
1 parent a79c216 commit 00c0cd6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 54 deletions.
23 changes: 1 addition & 22 deletions blocks/Xcos/MxGraphParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ def check_point_on_array(array, point, left_right_direction=True):
rightX = float(array[i + 1]['x'])
rightY = float(array[i + 1]['y'])

print("RANGE:", pointX, pointY, leftX, leftY, rightX, rightY, left_right_direction)

# Check if the point lies on the line segment between array[i] and array[i + 1]
if -40 <= leftY - pointY <= 40 and \
-40 <= rightY - pointY <= 40 and \
Expand Down Expand Up @@ -106,9 +104,8 @@ def identify_segment(array, point):
print('COUNT:', segment[6])
result, left_array, right_array = check_point_on_array(segment[6], point)
if result:
print("OK")
return result, i, left_array, right_array
print("ERror11:", point, "does not lie on", array)
print("Error:", point, "does not lie on", array)
return False, -1, array, []


Expand Down Expand Up @@ -329,7 +326,6 @@ def addPort3ForSplit(outroot, splitblockid, sourceVertex, targetVertex, sourceTy
try:
sourceType = IDLIST[sourceVertex]
targetType = IDLIST[targetVertex]
print('ST,TT', sourceType, targetType)
except KeyError:
remainingcells.append(cell)
continue
Expand Down Expand Up @@ -395,7 +391,6 @@ def addPort3ForSplit(outroot, splitblockid, sourceVertex, targetVertex, sourceTy
waypoints.append(point)
else:
split_point = point
print('SPPPx:', attribid, split_point)
waypoints.insert(0, point)
elif sourceVertex in blkgeometry:
vertex = blkgeometry[sourceVertex]
Expand All @@ -409,7 +404,6 @@ def addPort3ForSplit(outroot, splitblockid, sourceVertex, targetVertex, sourceTy
waypoints.insert(0, point)
else:
split_point2 = point
print('SPPP2x:', attribid, split_point2)
waypoints.append(point)
elif targetVertex in blkgeometry:
vertex = blkgeometry[targetVertex]
Expand All @@ -418,7 +412,6 @@ def addPort3ForSplit(outroot, splitblockid, sourceVertex, targetVertex, sourceTy

IDLIST[attribid] = style
link_data = (attribid, sourceVertex, targetVertex, sourceType, targetType, style, waypoints, addSplit, split_point, split_point2)
print('SPPP1:', attribid, waypoints, split_point, split_point2)
edgeDict[attribid] = link_data
edgeList.append(link_data)
except BaseException:
Expand All @@ -430,16 +423,10 @@ def addPort3ForSplit(outroot, splitblockid, sourceVertex, targetVertex, sourceTy
remainingcells = []
print('cellslength=', cellslength, ', oldcellslength=', oldcellslength)

print('EDGES:')
for key, value in edgeDict.items():
print(f'{key}: {value}')
print()

newEdgeDict = {}
LINKTOPORT = {}
for (attribid, sourceVertex, targetVertex, sourceType, targetType, style, waypoints, addSplit, split_point, split_point2) in edgeList:
link_data = (attribid, sourceVertex, targetVertex, sourceType, targetType, style, waypoints, addSplit, split_point, split_point2)
print('NEWEDGE:', attribid, waypoints, split_point, split_point2, addSplit)

if not addSplit:
newEdgeDict[attribid] = [link_data]
Expand All @@ -448,9 +435,6 @@ def addPort3ForSplit(outroot, splitblockid, sourceVertex, targetVertex, sourceTy
for attribid2 in sourceVertex, targetVertex:
try:
linkSegments = newEdgeDict[attribid2]
print('linkSegments:', attribid2, sourceVertex, attribid2 == sourceVertex, targetVertex)
for link in linkSegments:
print('LINKS:', link)
except KeyError:
continue

Expand Down Expand Up @@ -506,24 +490,19 @@ def addPort3ForSplit(outroot, splitblockid, sourceVertex, targetVertex, sourceTy
nextAttribForSplit += 1
newEdgeDict[attribid2].insert(i + 1, (nextAttribForSplit, port2, targetVertex2, sourceType, targetType2, style2, right_array, addSplit2, split_point, split_point2))
nextAttribForSplit += 1
# for (__, __, __, __, __, __, tmp_array, __, tmp_split_point, tmp_split_point2) in newEdgeDict[attribid2]:
# print('NEWEDGE2:', attribid2, tmp_array, tmp_split_point, tmp_split_point2)
if attribid2 == sourceVertex:
waypoints.reverse()
newEdgeDict[attribid] = [(nextAttribForSplit, port3, targetVertex, sourceType, targetType, style, waypoints, addSplit, split_point, split_point2)]
else:
newEdgeDict[attribid] = [(nextAttribForSplit, port3, sourceVertex, sourceType, targetType, style, waypoints, addSplit, split_point, split_point2)]
nextAttribForSplit += 1
# for (__, __, __, __, __, __, tmp_array, __, tmp_split_point, tmp_split_point2) in newEdgeDict[attribid]:
# print('NEWEDGE3:', attribid, tmp_array, tmp_split_point, tmp_split_point2)
LINKTOPORT[linkid] = port3
print('LINKTOPORT', linkid, port3)


print()

for key, newEdges in newEdgeDict.items():
print(f'{key}: {newEdges}')
for (attribid, sourceVertex, targetVertex, sourceType, targetType, style, waypoints, addSplit, split_point, split_point2) in newEdges:
try:
sourceVertex = LINKTOPORT[sourceVertex]
Expand Down
44 changes: 12 additions & 32 deletions blocks/Xcos/XmlParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,6 @@ def check_point_on_array(array, point, left_right_direction=True):
rightX = float(array[i + 1]['x'])
rightY = float(array[i + 1]['y'])

print("RANGE:", pointX, pointY, leftX, leftY, rightX, rightY, left_right_direction)

# Check if the point lies on the line segment between array[i] and array[i + 1]
if -40 <= leftY - pointY <= 40 and \
-40 <= rightY - pointY <= 40 and \
Expand Down Expand Up @@ -300,9 +298,8 @@ def identify_segment(array, point):
for i, waypoint in enumerate(array):
result, left_array, right_array = check_point_on_array(waypoint, point)
if result:
print("OK")
return result, i, left_array, right_array
print("ERror11:", point, "does not lie on", array)
print("Error:", point, "does not lie on", array)
return False, -1, array, []


Expand Down Expand Up @@ -514,13 +511,10 @@ def mergeLinks(vertex, key1, graph_link, removable_link):
point = {'x': vertex['x'], 'y': vertex['y']}
waypoints.append(point)

print("WAYPOINTS:", attribid, waypoints)
IDLIST[attribid] = style

link_data = (attribid, sourceVertex, targetVertex, sourceType, targetType, style, waypoints, addSplit, split_point, split_point2)
edgeDict[attribid] = link_data

print("LINKDATA:", link_data)

initLinks(attribid, key1, graph_link, [attribid], removable_link, [attribid] if addSplit else [])
mergeLinks(sourceVertex, key1, graph_link, removable_link)
Expand All @@ -539,7 +533,7 @@ def mergeLinks(vertex, key1, graph_link, removable_link):

linklist = []
return_value = 0
LINKTOPORT = {}
LINKTOLINK = {}
for k, r_link in removable_link.items():
if len(r_link) == 0:
continue
Expand Down Expand Up @@ -600,8 +594,6 @@ def mergeLinks(vertex, key1, graph_link, removable_link):
tType = IDLIST[otherVertex]
sType2 = IDLIST[sourceVertex2]
tType2 = IDLIST[targetVertex2]
# print("IDLIST:", link_data2, IDLIST[r_link[0]], r_link[0])
print(sType, tType, sType2, tType2)
height = 7
width = 7

Expand All @@ -612,13 +604,11 @@ def mergeLinks(vertex, key1, graph_link, removable_link):
smalllinkid = link_data[0]

result, left_array, right_array = check_point_on_array(waypoints2, split_point)
print('LR:', left_array, right_array)
array3 = waypoints

port1 = portType1(sType, sType2, tType2)
port2 = portType2(sType, sType2, tType2)
port3 = portType3(sType, tType)
# print("PORT:", link_data, port1, port2, port3)

ports = [port1, port2, port3]
implicitInputPorts = 0
Expand Down Expand Up @@ -748,26 +738,20 @@ def mergeLinks(vertex, key1, graph_link, removable_link):
root.append(xml_output_port)

# add splitblock edges
# print("TP:", len(linklist))

LINKWAYPOINTS = {}

for edge_index, (link_type, source_vertex, sourcex, sourcey, target_vertex, targetx, targety, waypoints, linkid) in enumerate(linklist):
edge_id = nextattribid
print('linkid:', linkid)

if linkid not in LINKTOPORT:
LINKTOPORT[linkid] = [edge_id]

if linkid not in LINKTOLINK:
LINKTOLINK[linkid] = [edge_id]
LINKWAYPOINTS[linkid] = [waypoints]
else:
LINKTOPORT[linkid].append(edge_id)
LINKTOLINK[linkid].append(edge_id)
LINKWAYPOINTS[linkid].append(waypoints)


print("SV, TV:", source_vertex, target_vertex)
# LINKTOPORT update

print("SV1, TV1:", source_vertex, target_vertex)
# LINKTOLINK update

xml_output_edge = create_mxCell_edge(
id=edge_id,
Expand Down Expand Up @@ -806,25 +790,23 @@ def mergeLinks(vertex, key1, graph_link, removable_link):
except KeyError:
continue

if sourceVertex not in LINKTOPORT and targetVertex not in LINKTOPORT:
if sourceVertex not in LINKTOLINK and targetVertex not in LINKTOLINK:
continue

print("REPLACING SV, TV:", sourceVertex, targetVertex)

try:
print("waypoint of biglink:", LINKWAYPOINTS[sourceVertex])
print("NODE:", nodeList[sourceVertex].attrib['tarx'], nodeList[sourceVertex].attrib['tary'])
point = {'x': nodeList[sourceVertex].attrib['tarx'], 'y': nodeList[sourceVertex].attrib['tary']}
result, i, left_array, right_array = identify_segment(LINKWAYPOINTS[sourceVertex], point)
sourceVertex = str(LINKTOPORT[sourceVertex][i])
sourceVertex = str(LINKTOLINK[sourceVertex][i])
except KeyError:
pass

try:
point = {'x': nodeList[targetVertex].attrib['tar2x'], 'y': nodeList[targetVertex].attrib['tar2y']}
result, i, left_array, right_array = identify_segment(LINKWAYPOINTS[targetVertex], point)
targetVertex = str(LINKTOPORT[targetVertex][i])

targetVertex = str(LINKTOLINK[targetVertex][i])
except KeyError:
pass
# depending upon waypoint of secondary link and link1 or link2
Expand All @@ -833,8 +815,6 @@ def mergeLinks(vertex, key1, graph_link, removable_link):

print("REPLACE SV, TV:", sourceVertex, targetVertex)

print("ROOT:", root)

# Save the modified XML
output_path = f'{remove_dot_number(basename)}.{return_value}.xml'
tree.write(output_path)
Expand Down

0 comments on commit 00c0cd6

Please sign in to comment.