Skip to content

Commit 6c350c0

Browse files
WIP modifiers are working
1 parent cdca144 commit 6c350c0

File tree

4 files changed

+58
-15
lines changed

4 files changed

+58
-15
lines changed

src/compas_model/elements/element.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from compas_model.algorithms import mesh_mesh_collision
1717
from compas_model.algorithms import mesh_mesh_contacts
1818
from compas_model.interactions import Contact
19+
from compas_model.interactions import Modifier
1920
from compas_model.materials import Material
2021

2122
if TYPE_CHECKING:
@@ -322,16 +323,16 @@ def compute_modelgeometry(self) -> Union[Brep, Mesh]:
322323
xform = self.modeltransformation
323324
modelgeometry = self.elementgeometry.transformed(xform)
324325

325-
# this needs to be updated
326+
# Modifiers updated
327+
# TODO: contacts this needs to be updated
326328

327-
# graph = self.model.graph
328-
# elements = list(self.model.elements())
329-
# for neighbor in graph.neighbors_in(self.graphnode):
330-
# for interaction in graph.edge_interactions((neighbor, self.graphnode)):
331-
# if isinstance(interaction, Contact):
332-
# modelgeometry = interaction.apply(modelgeometry)
333-
# elif isinstance(interaction, BooleanModifier):
334-
# modelgeometry = interaction.apply(modelgeometry, elements[neighbor].modelgeometry)
329+
graph = self.model.graph
330+
for neighbor in graph.neighbors_in(self.graphnode):
331+
modifiers: Union[list[Modifier], None] = graph.edge_attribute((neighbor, self.graphnode), "modifiers")
332+
333+
if modifiers:
334+
for modifer in modifiers:
335+
modelgeometry = modifer.apply(modelgeometry)
335336

336337
self.is_dirty = False
337338

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from .contact import Contact
22
from .modifiers.modifier import Modifier
3+
from .modifiers.slicer_modifier import SlicerModifier
4+
from .modifiers.boolean_modifier import BooleanModifier
35

46
__all__ = [
57
"Contact",
68
"Modifier",
9+
"SlicerModifier",
10+
"BooleanModifier",
711
]

src/compas_model/models/interactiongraph.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,13 @@ def _build_interactions_str(self) -> str:
8989
edge = node, nbr
9090
if not self.has_edge(edge):
9191
edge = nbr, node
92+
9293
lines.append(
93-
"- {}: {}".format(
94+
"- {}: {} {} {}".format(
9495
nbr,
95-
self.edge_interactions(edge), # type: ignore
96+
self.edge_attribute(edge, "modifiers"),
97+
self.edge_attribute(edge, "contacts"),
98+
self.edge_attribute(edge, "collisons"),
9699
) # type: ignore
97100
)
98101
return "\n".join(lines) + "\n"

src/compas_model/models/model.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from compas.geometry import Transformation
88
from compas_model.datastructures import KDTree
99
from compas_model.elements import Element
10+
from compas_model.interactions import Modifier
1011
from compas_model.materials import Material
1112

1213
from .bvh import ElementBVH
@@ -387,12 +388,13 @@ def add_interaction(self, a: Element, b: Element) -> tuple[int, int]:
387388
If one or both of the elements are not in the graph.
388389
389390
"""
390-
if not self.has_element(a) or not self.has_element(b):
391-
raise Exception("Please add both elements to the model first.")
392391

393392
node_a = a.graphnode
394393
node_b = b.graphnode
395394

395+
if not self.has_element(a) or not self.has_element(b):
396+
raise Exception("Please add both elements to the model first.")
397+
396398
if not self.graph.has_node(node_a) or not self.graph.has_node(node_b):
397399
raise Exception("Something went wrong: the elements are not in the interaction graph.")
398400

@@ -405,8 +407,41 @@ def add_interaction(self, a: Element, b: Element) -> tuple[int, int]:
405407
def identify_interactions(self):
406408
raise NotImplementedError
407409

408-
def add_modifier(self):
409-
raise NotImplementedError
410+
def add_modifier(self, a: Element, b: Element, type: str = "") -> tuple[int, int]:
411+
"""Add a modifier between two elements.
412+
413+
Parameters
414+
----------
415+
edge : tuple[int, int]
416+
The edge of the interaction graph representing the interaction between the two elements.
417+
Order matters: interaction is applied from node V0 to node V1.
418+
The first element create and instance of the interaction.
419+
type : str, optional
420+
The type of modifier, if different contact are possible between the two elements.
421+
422+
Returns
423+
-------
424+
None
425+
426+
"""
427+
node_a = a.graphnode
428+
node_b = b.graphnode
429+
430+
if not self.graph.has_node(node_a) or not self.graph.has_node(node_b):
431+
raise Exception("Something went wrong: the elements are not in the interaction graph.")
432+
433+
if not self._graph.has_edge((node_a, node_b)):
434+
raise Exception("Edge is not in the interaction graph. Add the edge first.")
435+
436+
modifier: Modifier = a.add_modifier(b, type)
437+
438+
if modifier:
439+
if not self.graph.edge_attribute((node_a, node_b), "modifiers"):
440+
self.graph.edge_attribute((node_a, node_b), "modifiers", [modifier])
441+
else:
442+
self.graph.edge_attribute((node_a, node_b), modifier).append(modifier)
443+
self._guid_element[str(b.guid)].is_dirty = True
444+
return node_a, node_b
410445

411446
def compute_contacts(self):
412447
raise NotImplementedError

0 commit comments

Comments
 (0)