|
2 | 2 | # venv: brg-csd |
3 | 3 | # r: compas_masonry>=0.2.7 |
4 | 4 |
|
5 | | -# import pathlib |
| 5 | +import pathlib |
6 | 6 |
|
7 | | -# from compas_masonry.session import MasonrySession as Session |
8 | | -from compas_rui.feedback import warn |
| 7 | +import rhinoscriptsyntax as rs # type: ignore |
| 8 | + |
| 9 | +import compas |
| 10 | +import compas_rhino.layers |
| 11 | +from compas_dem.elements import Block |
| 12 | +from compas_dem.interactions import FrictionContact |
| 13 | +from compas_dem.models import BlockModel |
| 14 | +from compas_masonry.session import MasonrySession as Session |
| 15 | +from compas_model.models import InteractionGraph |
| 16 | +from compas_rui import feedback |
| 17 | +from compas_rui.forms import FileForm |
9 | 18 |
|
10 | 19 |
|
11 | 20 | def RunCommand(): |
12 | | - # session = Session(basedir=pathlib.Path().home() / ".compas_session", name="COMPAS-Masonry") |
| 21 | + session = Session(basedir=pathlib.Path().home() / ".compas_session", name="COMPAS-Masonry") |
| 22 | + |
| 23 | + path = rs.DocumentPath() # to make sure the document has a path |
| 24 | + if path: |
| 25 | + basedir = pathlib.Path(path).parent |
| 26 | + else: |
| 27 | + basedir = pathlib.Path().home() |
| 28 | + |
| 29 | + filepath = FileForm.open(str(basedir)) |
| 30 | + if not filepath: |
| 31 | + return |
| 32 | + |
| 33 | + try: |
| 34 | + result = compas.json_load(filepath) |
| 35 | + except Exception as e: |
| 36 | + feedback.warn(f"Failed to load datafrom {filepath}: {e}") |
| 37 | + return |
| 38 | + |
| 39 | + if not isinstance(result, BlockModel): |
| 40 | + feedback.warn(f"The data in the file is not a BlockModel: {type(result)}") |
| 41 | + return |
| 42 | + |
| 43 | + # ============================================================================= |
| 44 | + # Clear existing model |
| 45 | + # ============================================================================= |
| 46 | + |
| 47 | + session.delete("blockmodel") |
| 48 | + |
| 49 | + for obj in session.scene.find_all_by_itemtype(Block): |
| 50 | + session.scene.remove(obj) |
| 51 | + |
| 52 | + for obj in session.scene.find_all_by_itemtype(FrictionContact): |
| 53 | + session.scene.remove(obj) |
| 54 | + |
| 55 | + for obj in session.scene.find_all_by_itemtype(InteractionGraph): |
| 56 | + session.scene.remove(obj) |
| 57 | + |
| 58 | + compas_rhino.layers.clear_layer("Masonry::DEA::Blocks") |
| 59 | + compas_rhino.layers.clear_layer("Masonry::DEA::Interactions") |
| 60 | + compas_rhino.layers.clear_layer("Masonry::DEA::Contacts") |
| 61 | + |
| 62 | + # ============================================================================= |
| 63 | + # Add new model |
| 64 | + # ============================================================================= |
| 65 | + |
| 66 | + model: BlockModel = result |
| 67 | + session["blockmodel"] = model |
| 68 | + |
| 69 | + # ============================================================================= |
| 70 | + # Update scene |
| 71 | + # ============================================================================= |
| 72 | + |
| 73 | + # this should be simplifed by adding a helper method to the session |
| 74 | + |
| 75 | + for block in model.elements(): |
| 76 | + node = block.graphnode |
| 77 | + session.scene.add( |
| 78 | + block, # type: ignore |
| 79 | + name=f"Block_{node}", # type: ignore |
| 80 | + group=f"Masonry::DEA::Blocks::{node}", # type: ignore |
| 81 | + layer="Masonry::DEA::Blocks", # type: ignore |
| 82 | + ) |
| 83 | + |
| 84 | + if model.graph.number_of_edges() > 0: |
| 85 | + session.scene.add(model.graph, layer="Masonry::DEA::Interactions") # type: ignore |
| 86 | + |
| 87 | + for contact in model.contacts(): |
| 88 | + session.scene.add(contact, layer="Masonry::DEA::Contacts") # type: ignore |
| 89 | + |
| 90 | + session.scene.redraw() |
13 | 91 |
|
14 | | - warn("Not available yet.") |
| 92 | + rs.Redraw() |
15 | 93 |
|
16 | 94 |
|
17 | 95 | # ============================================================================= |
|
0 commit comments