Graph
This module contains functions to create and manipulate graphs from Onshape assembly data.
add_edges_to_graph(graph, mates)
¶
Add edges to the graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
Graph
|
The graph to add edges to. |
required |
mates
|
dict[str, Union[MateFeatureData]]
|
Dictionary of mates in the assembly. |
required |
Examples:
>>> add_edges_to_graph(graph, mates)
Source code in onshape_robotics_toolkit\graph.py
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
|
add_nodes_to_graph(graph, occurrences, instances, parts, use_user_defined_root)
¶
Add nodes to the graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
Graph
|
The graph to add nodes to. |
required |
occurrences
|
dict[str, Occurrence]
|
Dictionary of occurrences in the assembly. |
required |
instances
|
dict[str, Union[PartInstance, AssemblyInstance]]
|
Dictionary of instances in the assembly. |
required |
parts
|
dict[str, Part]
|
Dictionary of parts in the assembly. |
required |
use_user_defined_root
|
bool
|
Whether to use the user defined root node. |
required |
Returns:
Type | Description |
---|---|
str
|
The user defined root node if it exists. |
Examples:
>>> add_nodes_to_graph(graph, occurrences, instances, parts, use_user_defined_root=True)
Source code in onshape_robotics_toolkit\graph.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
|
convert_to_digraph(graph, user_defined_root=None)
¶
Convert a graph to a directed graph and calculate the root node using closeness centrality.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
Graph
|
The graph to convert. |
required |
user_defined_root
|
Union[str, None]
|
The node to use as the root node. |
None
|
Returns:
Type | Description |
---|---|
DiGraph
|
The directed graph and the root node of the graph, calculated using closeness centrality. |
Examples:
>>> graph = nx.Graph()
>>> convert_to_digraph(graph)
(digraph, root_node)
Source code in onshape_robotics_toolkit\graph.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
|
create_graph(occurrences, instances, parts, mates, directed=True, use_user_defined_root=True)
¶
Create a graph from onshape assembly data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
occurrences
|
dict[str, Occurrence]
|
Dictionary of occurrences in the assembly. |
required |
instances
|
dict[str, Union[PartInstance, AssemblyInstance]]
|
Dictionary of instances in the assembly. |
required |
parts
|
dict[str, Part]
|
Dictionary of parts in the assembly. |
required |
mates
|
dict[str, Union[MateFeatureData]]
|
Dictionary of mates in the assembly. |
required |
Returns:
Type | Description |
---|---|
tuple[DiGraph, str]
|
The graph created from the assembly data. |
Examples:
>>> occurrences = get_occurrences(assembly)
>>> instances = get_instances(assembly)
>>> parts = get_parts(assembly, client)
>>> mates = get_mates(assembly)
>>> create_graph(occurrences, instances, parts, mates, directed=True)
Source code in onshape_robotics_toolkit\graph.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
|
get_root_node(graph)
¶
Get the root node of a directed graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
DiGraph
|
The directed graph. |
required |
Returns:
Type | Description |
---|---|
str
|
The root node of the graph. |
Examples:
>>> graph = nx.DiGraph()
>>> get_root_node(graph)
Source code in onshape_robotics_toolkit\graph.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
|
get_topological_order(graph)
¶
Get the topological order of a directed graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
DiGraph
|
The directed graph. |
required |
Returns:
Type | Description |
---|---|
tuple[str]
|
The topological order of the graph. |
Examples:
>>> graph = nx.DiGraph()
>>> get_topological_order(graph)
Source code in onshape_robotics_toolkit\graph.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
|
plot_graph(graph, file_name=None)
¶
Display the graph using networkx and matplotlib, or save it as an image file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
Union[Graph, DiGraph]
|
The graph to display or save. |
required |
file_name
|
Optional[str]
|
The name of the image file to save. If None, the graph will be displayed. |
None
|
Examples:
>>> graph = nx.Graph()
>>> plot_graph(graph)
>>> plot_graph(graph, "graph.png")
Source code in onshape_robotics_toolkit\graph.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
|
remove_unconnected_subgraphs(graph)
¶
Remove unconnected subgraphs from the graph.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
graph
|
Graph
|
The graph to remove unconnected subgraphs from. |
required |
Returns:
Type | Description |
---|---|
Graph
|
The main connected subgraph of the graph, which is the largest connected subgraph. |
Source code in onshape_robotics_toolkit\graph.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
|