Skip to content

Commit 1489ba2

Browse files
committed
Migrate from compas_view2 to compas_viewer
compas_view2 is deprecated and is no longer supported with newer compas versions, which makes cra viewer inoperable
1 parent 59b97f2 commit 1489ba2

File tree

4 files changed

+78
-95
lines changed

4 files changed

+78
-95
lines changed

env_linux.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ dependencies:
66
- python ==3.9
77
- pip
88
- compas
9-
- compas_view2
10-
- ipopt
9+
- compas_viewer
10+
- ipopt ==3.11.1
1111
- pyomo ==6.4.2
1212
- matplotlib
1313
- pip:
14-
- git+https://github.com/compas-dev/compas_view2.git@compas2
1514
- compas_assembly >=0.7.0
1615
- -r requirements-dev.txt
1716
- -e .

env_osx.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ channels:
33
- conda-forge
44
dependencies:
55
- git
6-
- python ==3.9
6+
- python ==3.10
77
- pip
88
- compas
9-
- compas_view2
10-
- ipopt
9+
- compas_viewer
10+
- ipopt ==3.14.5
1111
- pyomo ==6.4.2
1212
- matplotlib
1313
- pip:
14-
- git+https://github.com/compas-dev/compas_view2.git@compas2
1514
- compas_assembly >=0.7.0
1615
- -r requirements-dev.txt
1716
- -e .

env_win.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ dependencies:
66
- python ==3.9
77
- pip
88
- compas
9-
- compas_view2
9+
- compas_viewer
1010
- ipopt ==3.11.1
1111
- pyomo ==6.4.2
1212
- matplotlib
1313
- pip:
14-
- git+https://github.com/compas-dev/compas_view2.git@compas2
1514
- compas_assembly >=0.7.0
1615
- -r requirements-dev.txt
1716
- -e .

src/compas_cra/viewers/cra_view.py

+72-86
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,29 @@
1111
from compas.geometry import Polyline
1212
from compas.geometry import Rotation
1313
from compas.geometry import Translation
14+
from compas.geometry import Vector
1415
from compas.geometry import is_coplanar
15-
16-
try:
17-
from compas_view2 import app
18-
from compas_view2.collections import Collection
19-
from compas_view2.shapes import Arrow
20-
except ImportError:
21-
pass
16+
from compas_viewer import Viewer
17+
from compas_viewer.config import Config
18+
19+
20+
class Arrow:
21+
def __init__(self, position=[0, 0, 0], direction=[0, 0, 1], linewidth=0.02):
22+
super().__init__()
23+
self.position = Vector(*position)
24+
self.direction = Vector(*direction)
25+
self.linewidth = linewidth
26+
27+
def add_to_scene(self, viewer, facecolor: Color, opacity=1):
28+
viewer.scene.add(
29+
Vector(*self.direction),
30+
anchor=Point(*self.position),
31+
facecolor=facecolor,
32+
linecolor=facecolor,
33+
linewidth=self.linewidth,
34+
show_lines=True,
35+
opacity=opacity,
36+
)
2237

2338

2439
def draw_blocks(assembly, viewer, edge=True, tol=0.0):
@@ -50,25 +65,25 @@ def draw_blocks(assembly, viewer, edge=True, tol=0.0):
5065
else:
5166
blockedges.append(Line(*block.edge_coordinates(edge)))
5267
if len(blocks) != 0:
53-
viewer.add(
54-
Collection(blocks),
68+
viewer.scene.add(
69+
blocks,
5570
show_faces=True,
5671
show_lines=False,
5772
opacity=0.6,
58-
facecolor=(0.9, 0.9, 0.9),
73+
facecolor=Color(0.9, 0.9, 0.9),
5974
)
6075
if len(supports) != 0:
61-
viewer.add(
62-
Collection(supports),
76+
viewer.scene.add(
77+
supports,
6378
show_faces=True,
6479
show_lines=False,
6580
opacity=0.5,
6681
facecolor=Color.from_hex("#f79d84"),
6782
)
6883
if len(blockedges) != 0:
69-
viewer.add(Collection(blockedges), linewidth=1.5)
84+
viewer.scene.add(blockedges, linewidth=1.5)
7085
if len(supportedges) != 0:
71-
viewer.add(Collection(supportedges), linecolor=Color.from_hex("#f79d84"), linewidth=4)
86+
viewer.scene.add(supportedges, linecolor=Color.from_hex("#f79d84"), linewidth=4)
7287

7388

7489
def draw_interfaces(assembly, viewer):
@@ -94,15 +109,15 @@ def draw_interfaces(assembly, viewer):
94109
interfaces.append(Mesh.from_polygons([polygon]))
95110

96111
if len(interfaces) != 0:
97-
viewer.add(
98-
Collection(interfaces),
112+
viewer.scene.add(
113+
interfaces,
99114
show_lines=False,
100115
show_points=False,
101116
facecolor=(0.8, 0.8, 0.8),
102117
)
103118
if len(faces) != 0:
104-
viewer.add(
105-
Collection(faces),
119+
viewer.scene.add(
120+
faces,
106121
linecolor=Color.from_hex("#fac05e"),
107122
linewidth=10,
108123
pointsize=10,
@@ -163,17 +178,17 @@ def draw_forces(assembly, viewer, scale=1.0, resultant=True, nodal=False):
163178
else:
164179
res_nn.append(Line(p1, p2))
165180
if len(locs) != 0:
166-
viewer.add(Collection(locs), size=12, color=Color.from_hex("#386641"))
181+
viewer.scene.add(locs, size=12, color=Color.from_hex("#386641"))
167182
if len(res_np) != 0:
168-
viewer.add(Collection(res_np), linewidth=8, linecolor=(0, 0.3, 0))
183+
viewer.scene.add(res_np, linewidth=8, linecolor=Color(0, 0.3, 0))
169184
if len(res_nn) != 0:
170-
viewer.add(Collection(res_nn), linewidth=8, linecolor=(0.8, 0, 0))
185+
viewer.scene.add(res_nn, linewidth=8, linecolor=Color(0.8, 0, 0))
171186
if len(fnn) != 0:
172-
viewer.add(Collection(fnn), linewidth=5, linecolor=Color.from_hex("#00468b"))
187+
viewer.scene.add(fnn, linewidth=5, linecolor=Color.from_hex("#00468b"))
173188
if len(fnp) != 0:
174-
viewer.add(Collection(fnp), linewidth=5, linecolor=(1, 0, 0))
189+
viewer.scene.add(fnp, linewidth=5, linecolor=Color(1, 0, 0))
175190
if len(ft) != 0:
176-
viewer.add(Collection(ft), linewidth=5, linecolor=(1.0, 0.5, 0.0))
191+
viewer.scene.add(ft, linewidth=5, linecolor=Color(1.0, 0.5, 0.0))
177192

178193

179194
def draw_forcesline(assembly, viewer, scale=1.0, resultant=True, nodal=False):
@@ -241,17 +256,17 @@ def draw_forcesline(assembly, viewer, scale=1.0, resultant=True, nodal=False):
241256
else:
242257
res_nn.append(Line(p1, p2))
243258
if len(locs) != 0:
244-
viewer.add(Collection(locs), pointsize=12, pointcolor=Color.from_hex("#386641"))
259+
viewer.scene.add(locs, pointsize=12, pointcolor=Color.from_hex("#386641"))
245260
if len(res_np) != 0:
246-
viewer.add(Collection(res_np), linewidth=8, linecolor=(0, 0.3, 0))
261+
viewer.scene.add(res_np, linewidth=8, linecolor=Color(0, 0.3, 0))
247262
if len(res_nn) != 0:
248-
viewer.add(Collection(res_nn), linewidth=8, linecolor=(0.8, 0, 0))
263+
viewer.scene.add(res_nn, linewidth=8, linecolor=Color(0.8, 0, 0))
249264
if len(fnn) != 0:
250-
viewer.add(Collection(fnn), linewidth=5, linecolor=Color.from_hex("#00468b"))
265+
viewer.scene.add(fnn, linewidth=5, linecolor=Color.from_hex("#00468b"))
251266
if len(fnp) != 0:
252-
viewer.add(Collection(fnp), linewidth=5, linecolor=(1, 0, 0))
267+
viewer.scene.add(fnp, linewidth=5, linecolor=Color(1, 0, 0))
253268
if len(ft) != 0:
254-
viewer.add(Collection(ft), linewidth=5, linecolor=(1.0, 0.5, 0.0))
269+
viewer.scene.add(ft, linewidth=5, linecolor=Color(1.0, 0.5, 0.0))
255270
# print("total reaction: ", total_reaction)
256271

257272

@@ -286,21 +301,9 @@ def draw_forcesdirect(assembly, viewer, scale=1.0, resultant=True, nodal=False):
286301
if (w * force * scale).length == 0:
287302
continue
288303
if flip:
289-
f = Arrow(
290-
pt,
291-
w * force * scale * -1,
292-
head_portion=0.2,
293-
head_width=0.07,
294-
body_width=0.02,
295-
)
304+
f = Arrow(pt, w * force * scale * -1, linewidth=10)
296305
else:
297-
f = Arrow(
298-
pt,
299-
w * force * scale,
300-
head_portion=0.2,
301-
head_width=0.07,
302-
body_width=0.02,
303-
)
306+
f = Arrow(pt, w * force * scale, linewidth=10)
304307
if force >= 0:
305308
fnp.append(f)
306309
else:
@@ -312,17 +315,13 @@ def draw_forcesdirect(assembly, viewer, scale=1.0, resultant=True, nodal=False):
312315
f = Arrow(
313316
pt,
314317
ft_uv * -1,
315-
head_portion=0.2,
316-
head_width=0.07,
317-
body_width=0.02,
318+
linewidth=10,
318319
)
319320
else:
320321
f = Arrow(
321322
pt,
322323
ft_uv,
323-
head_portion=0.2,
324-
head_width=0.07,
325-
body_width=0.02,
324+
linewidth=10,
326325
)
327326
ft.append(f)
328327
if resultant:
@@ -353,44 +352,32 @@ def draw_forcesdirect(assembly, viewer, scale=1.0, resultant=True, nodal=False):
353352
if resultant_f.length >= thres:
354353
locs.append(Point(*resultant_pos))
355354
if flip:
356-
f = Arrow(
357-
resultant_pos,
358-
resultant_f * -1,
359-
head_portion=0.2,
360-
head_width=0.07,
361-
body_width=0.02,
362-
)
355+
f = Arrow(resultant_pos, resultant_f * -1, linewidth=10)
363356
else:
364-
f = Arrow(
365-
resultant_pos,
366-
resultant_f,
367-
head_portion=0.2,
368-
head_width=0.07,
369-
body_width=0.02,
370-
)
357+
f = Arrow(resultant_pos, resultant_f, linewidth=10)
371358
if friction:
372-
viewer.add(f, facecolor=(1.0, 0.5, 0.0), show_lines=False)
359+
viewer.scene.add(f, facecolor=(1.0, 0.5, 0.0), show_lines=False)
373360
if not is_tension:
374361
res_np.append(f)
375362
else:
376363
res_nn.append(f)
377364
if len(locs) != 0:
378-
viewer.add(Collection(locs), size=12, color=Color.from_hex("#386641"))
365+
viewer.scene.add(locs, size=12, color=Color.from_hex("#386641"))
379366
if len(res_np) != 0:
380-
viewer.add(Collection(res_np), facecolor=Color.from_hex("#386641"), show_lines=False)
367+
for arrow in res_np:
368+
arrow.add_to_scene(viewer, facecolor=Color.from_hex("#386641"))
381369
if len(res_nn) != 0:
382-
viewer.add(Collection(res_nn), facecolor=(0.8, 0, 0), show_lines=False)
370+
for arrow in res_nn:
371+
arrow.add_to_scene(viewer, facecolor=Color(0.8, 0, 0))
383372
if len(fnp) != 0:
384-
viewer.add(
385-
Collection(fnp),
386-
facecolor=Color.from_hex("#00468b"),
387-
show_lines=False,
388-
opacity=0.5,
389-
)
373+
for arrow in fnp:
374+
arrow.add_to_scene(viewer, facecolor=Color.from_hex("#00468b"), opacity=0.5)
390375
if len(fnn) != 0:
391-
viewer.add(Collection(fnn), facecolor=(1, 0, 0), show_lines=False, opacity=0.5)
376+
for arrow in fnn:
377+
arrow.add_to_scene(viewer, facecolor=Color(1, 0, 0), opacity=0.5)
392378
if len(ft) != 0:
393-
viewer.add(Collection(ft), facecolor=(1.0, 0.5, 0.0), show_lines=False, opacity=0.5)
379+
for arrow in ft:
380+
arrow.add_to_scene(viewer, facecolor=Color(1.0, 0.5, 0.0), opacity=0.5)
394381

395382

396383
def draw_displacements(assembly, viewer, dispscale=1.0, tol=0.0):
@@ -425,9 +412,9 @@ def draw_displacements(assembly, viewer, dispscale=1.0, tol=0.0):
425412
continue
426413
blocks.append(Line(*new_block.edge_coordinates(edge)))
427414
if len(blocks) != 0:
428-
viewer.add(Collection(blocks), linewidth=1, linecolor=(0.7, 0.7, 0.7))
415+
viewer.scene.add(blocks, linewidth=1, linecolor=Color(0.7, 0.7, 0.7))
429416
if len(nodes) != 0:
430-
viewer.add(Collection(nodes), pointcolor=(0.7, 0.7, 0.7))
417+
viewer.scene.add(nodes, pointcolor=Color(0.7, 0.7, 0.7))
431418

432419

433420
def draw_weights(assembly, viewer, scale=1.0, density=1.0):
@@ -445,9 +432,7 @@ def draw_weights(assembly, viewer, scale=1.0, density=1.0):
445432
Arrow(
446433
block.center(),
447434
[0, 0, -block.volume() * d * scale],
448-
head_portion=0.2,
449-
head_width=0.07,
450-
body_width=0.02,
435+
linewidth=0.02,
451436
)
452437
)
453438
# print("self-weight", -block.volume() * density)
@@ -457,11 +442,12 @@ def draw_weights(assembly, viewer, scale=1.0, density=1.0):
457442
# print("total self-weight: ", total_weights)
458443

459444
if len(supports) != 0:
460-
viewer.add(Collection(supports), pointsize=20, pointcolor=Color.from_hex("#ee6352"))
445+
viewer.scene.add(supports, pointsize=20, pointcolor=Color.from_hex("#ee6352"))
461446
if len(blocks) != 0:
462-
viewer.add(Collection(blocks), pointsize=30, pointcolor=Color.from_hex("#3284a0"))
447+
viewer.scene.add(blocks, pointsize=30, pointcolor=Color.from_hex("#3284a0"))
463448
if len(weights) != 0:
464-
viewer.add(Collection(weights), facecolor=Color.from_hex("#59cd90"), show_lines=False)
449+
for weight in weights:
450+
weight.add_to_scene(viewer, facecolor=Color.from_hex("#59cd90"))
465451

466452

467453
def cra_view(
@@ -524,7 +510,7 @@ def cra_view(
524510
None
525511
"""
526512

527-
viewer = app.App(width=1600, height=1000, viewmode="shaded", show_grid=grid)
513+
viewer = Viewer(config=Config(vectorsize=0.15))
528514

529515
if blocks:
530516
draw_blocks(assembly, viewer, edge, tol)
@@ -541,7 +527,7 @@ def cra_view(
541527
if displacements:
542528
draw_displacements(assembly, viewer, dispscale, tol)
543529

544-
viewer.run()
530+
viewer.show()
545531

546532

547533
def cra_view_ex(

0 commit comments

Comments
 (0)