/
graphobject.py
364 lines (270 loc) · 10.9 KB
/
graphobject.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
190
191
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import Rhino # type: ignore
import scriptcontext as sc # type: ignore
import compas_rhino
from compas.geometry import Cylinder
from compas.geometry import Line
from compas.geometry import Sphere
from compas.scene import GraphObject
from compas_rhino.conversions import cylinder_to_rhino_brep
from compas_rhino.conversions import line_to_rhino
from compas_rhino.conversions import point_to_rhino
from compas_rhino.conversions import sphere_to_rhino
from .sceneobject import RhinoSceneObject
class RhinoGraphObject(RhinoSceneObject, GraphObject):
"""Scene object for drawing graph data structures.
Parameters
----------
graph : :class:`compas.datastructures.Graph`
A COMPAS graph.
**kwargs : dict, optional
Additional keyword arguments.
"""
def __init__(self, graph, **kwargs):
super(RhinoGraphObject, self).__init__(graph=graph, **kwargs)
self._guids_nodes = None
self._guids_edges = None
self._guids_nodelabels = None
self._guids_edgelabels = None
self._guids_spheres = None
self._guids_pipes = None
# ==========================================================================
# clear
# ==========================================================================
def clear(self):
"""Delete all objects drawn by this scene object.
Returns
-------
None
"""
compas_rhino.objects.delete_objects(self.guids, purge=True)
def clear_nodes(self):
"""Delete all nodes drawn by this scene object.
Returns
-------
None
"""
compas_rhino.objects.delete_objects(self._guids_nodes, purge=True)
def clear_edges(self):
"""Delete all edges drawn by this scene object.
Returns
-------
None
"""
compas_rhino.objects.delete_objects(self._guids_edges, purge=True)
# ==========================================================================
# draw
# ==========================================================================
def draw(self):
"""Draw the graph using the chosen visualisation settings.
Returns
-------
list[System.Guid]
The GUIDs of the created Rhino objects.
"""
self.clear()
guids = self.draw_nodes(nodes=self.show_nodes, color=self.nodecolor, group=self.group)
guids += self.draw_edges(edges=self.show_edges, color=self.edgecolor, group=self.group)
self._guids = guids
return self.guids
def draw_nodes(self, nodes=None, color=None, group=None):
"""Draw a selection of nodes.
Parameters
----------
nodes : list[int], optional
A list of nodes to draw.
Default is None, in which case all nodes are drawn.
color : :class:`compas.colors.Color` | dict[int, :class:`compas.colors.Color`], optional
Color of the nodes.
Returns
-------
list[System.Guid]
The GUIDs of the created Rhino point objects.
"""
guids = []
self.nodecolor = color
if nodes is True:
nodes = list(self.graph.nodes())
for node in nodes or self.graph.nodes(): # type: ignore
name = "{}.node.{}".format(self.graph.name, node) # type: ignore
attr = self.compile_attributes(name=name, color=self.nodecolor[node])
geometry = point_to_rhino(self.node_xyz[node])
guid = sc.doc.Objects.AddPoint(geometry, attr)
guids.append(guid)
if group:
self.add_to_group(group, guids)
self._guids_nodes = guids
return guids
def draw_edges(self, edges=None, color=None, group=None, show_direction=False):
"""Draw a selection of edges.
Parameters
----------
edges : list[tuple[int, int]], optional
A list of edges to draw.
The default is None, in which case all edges are drawn.
color : :class:`compas.colors.Color` | dict[tuple[int, int], :class:`compas.colors.Color`], optional
Color of the edges.
group : str, optional
The name of a group to add the edges to.
show_direction : bool, optional
Show the direction of the edges.
Returns
-------
list[System.Guid]
The GUIDs of the created Rhino objects.
"""
guids = []
arrow = "end" if show_direction else None
self.edgecolor = color
if edges is True:
edges = list(self.graph.edges())
for edge in edges or self.graph.edges(): # type: ignore
u, v = edge
color = self.edgecolor[edge]
name = "{}.edge.{}-{}".format(self.graph.name, u, v) # type: ignore
attr = self.compile_attributes(name=name, color=color, arrow=arrow)
geometry = line_to_rhino((self.node_xyz[u], self.node_xyz[v]))
guid = sc.doc.Objects.AddLine(geometry, attr)
guids.append(guid)
if group:
self.add_to_group(group, guids)
self._guids_edges = guids
return guids
# =============================================================================
# draw labels
# =============================================================================
def draw_nodelabels(self, text, color=None, group=None, fontheight=10, fontface="Arial Regular"):
"""Draw labels for a selection of nodes.
Parameters
----------
text : dict[int, str]
A dictionary of node labels as node-text pairs.
color : :class:`compas.colors.Color` | dict[int, :class:`compas.colors.Color`], optional
Color of the labels.
group : str, optional
The name of a group to add the labels to.
fontheight : float, optional
Font height of the labels.
fontface : str, optional
Font face of the labels.
Returns
-------
list[System.Guid]
The GUIDs of the created Rhino text objects.
"""
guids = []
self.nodecolor = color
for node in text:
name = "{}.node.{}.label".format(self.graph.name, node) # type: ignore
attr = self.compile_attributes(name=name, color=self.nodecolor[node])
point = point_to_rhino(self.node_xyz[node])
dot = Rhino.Geometry.TextDot(str(text[node]), point)
dot.FontHeight = fontheight
dot.FontFace = fontface
guid = sc.doc.Objects.AddTextDot(dot, attr)
guids.append(guid)
if group:
self.add_to_group(group, guids)
self._guids_nodelabels = guids
return guids
def draw_edgelabels(self, text, color=None, group=None, fontheight=10, fontface="Arial Regular"):
"""Draw labels for a selection of edges.
Parameters
----------
text : dict[tuple[int, int], str]
A dictionary of edge labels as edge-text pairs.
color : :class:`compas.colors.Color` | dict[tuple[int, int], :class:`compas.colors.Color`], optional
Color of the labels.
group : str, optional
The name of a group to add the labels to.
fontheight : float, optional
Font height of the labels.
fontface : str, optional
Font face of the labels.
Returns
-------
list[System.Guid]
The GUIDs of the created Rhino text objects.
"""
guids = []
self.edgecolor = color
for edge in text:
u, v = edge
color = self.edgecolor[edge]
name = "{}.edge.{}-{}.label".format(self.graph.name, u, v) # type: ignore
attr = self.compile_attributes(name=name, color=color)
line = Line(self.node_xyz[u], self.node_xyz[v])
point = point_to_rhino(line.midpoint)
dot = Rhino.Geometry.TextDot(str(text[edge]), point)
dot.FontHeight = fontheight
dot.FontFace = fontface
guid = sc.doc.Objects.AddTextDot(dot, attr)
guids.append(guid)
if group:
self.add_to_group(group, guids)
self._guids_edgelabels = guids
return guids
# =============================================================================
# draw miscellaneous
# =============================================================================
def draw_spheres(self, radius, color=None, group=None):
"""Draw spheres at the vertices of the graph.
Parameters
----------
radius : dict[int, float], optional
The radius of the spheres.
color : :class:`compas.colors.Color` | dict[int, :class:`compas.colors.Color`], optional
The color of the spheres.
group : str, optional
The name of a group to join the created Rhino objects in.
Returns
-------
list[System.Guid]
The GUIDs of the created Rhino objects.
"""
guids = []
self.nodecolor = color
for node in radius:
name = "{}.node.{}.sphere".format(self.graph.name, node) # type: ignore
color = self.nodecolor[node]
attr = self.compile_attributes(name=name, color=color)
sphere = Sphere.from_point_and_radius(self.node_xyz[node], radius[node])
geometry = sphere_to_rhino(sphere)
guid = sc.doc.Objects.AddSphere(geometry, attr)
guids.append(guid)
if group:
self.add_to_group(group, guids)
self._guids_spheres = guids
return guids
def draw_pipes(self, radius, color=None, group=None):
"""Draw pipes around the edges of the graph.
Parameters
----------
radius : dict[tuple[int, int], float]
The radius per edge.
color : :class:`compas.colors.Color` | dict[tuple[int, int], :class:`compas.colors.Color`], optional
The color of the pipes.
group : str, optional
The name of a group to join the created Rhino objects in.
Returns
-------
list[System.Guid]
The GUIDs of the created Rhino objects.
"""
guids = []
self.edgecolor = color
for edge in radius:
name = "{}.edge.{}-{}.pipe".format(self.graph.name, *edge) # type: ignore
color = self.edgecolor[edge]
attr = self.compile_attributes(name=name, color=color)
line = Line(self.node_xyz[edge[0]], self.node_xyz[edge[1]])
cylinder = Cylinder.from_line_and_radius(line, radius[edge])
geometry = cylinder_to_rhino_brep(cylinder)
guid = sc.doc.Objects.AddBrep(geometry, attr)
guids.append(guid)
if group:
self.add_to_group(group, guids)
self._guids_pipes = guids
return guids