framework/freecad_workbench/freecad/robossembler/get_sequences.py

61 lines
1.9 KiB
Python

import json
import networkx as nx
def load_data(file_path):
with open(file_path, 'r') as file:
return json.load(file)
def create_graph(data):
G = nx.Graph()
for part in data['allParts']:
G.add_node(part)
for part, connections in data['matrix'].items():
for connected_part in connections:
G.add_edge(part, connected_part)
return G
def find_leaf_nodes(graph, central_node):
leaf_nodes = []
for node in graph.nodes:
if node != central_node and graph.degree(node) == 1:
leaf_nodes.append(node)
return leaf_nodes
def find_all_paths(graph, start_node, end_node):
try:
return list(nx.all_simple_paths(graph, start_node, end_node))
except nx.NetworkXNoPath:
return []
def load_constraints(file_path):
with open(file_path, 'r') as file:
return json.load(file)
def is_valid_sequence(sequence, constraints):
for constraint in constraints:
if constraint[0] in sequence and constraint[1] in sequence:
if sequence.index(constraint[0]) > sequence.index(constraint[1]):
return False
return True
def save_sequences(sequences, file_path):
with open(file_path, 'w') as file:
json.dump(sequences, file, indent=4)
data = load_data('adjacency_matrix.json')
constraints = load_constraints('constraints.json')
all_parts = data['allParts']
graph = create_graph(data)
first_detail = data['firstDetail']
leaf_nodes = find_leaf_nodes(graph, first_detail)
all_sequences = []
for leaf in leaf_nodes:
paths = find_all_paths(graph, leaf, first_detail)
for path in paths:
if set(path) == set(all_parts) and is_valid_sequence(path, constraints):
all_sequences.append(path)
save_sequences(all_sequences, 'valid_sequences.json')
print(f"Найдено {len(all_sequences)} допустимых последовательностей сборки.")