Skip to content

Commit

Permalink
Implement Boolean Methods for Bodies (#474)
Browse files Browse the repository at this point in the history
Co-authored-by: Roberto Pastor Muela <[email protected]>
  • Loading branch information
jonahrb and RobPasMue authored Apr 17, 2023
1 parent c40ddde commit d27b1d9
Show file tree
Hide file tree
Showing 2 changed files with 366 additions and 1 deletion.
118 changes: 118 additions & 0 deletions src/ansys/geometry/core/designer/body.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from functools import wraps

from ansys.api.geometry.v0.bodies_pb2 import (
BooleanRequest,
CopyRequest,
SetAssignedMaterialRequest,
TranslateRequest,
Expand Down Expand Up @@ -367,6 +368,55 @@ def plot(
"""
return

def intersect(self, other: "Body") -> None:
"""
Intersect two bodies. `self` will be directly modified with the result, and `other` will be
consumed, so it is important to make copies if needed.
Parameters
----------
other : Body
The body to intersect with.
Raises
------
ValueError
If the bodies do not intersect.
"""
return

@protect_grpc
def subtract(self, other: "Body") -> None:
"""
Subtract two bodies. `self` is the minuend, and `other` is the subtrahend
(`self` - `other`). `self` will be directly modified with the result, and
`other` will be consumed, so it is important to make copies if needed.
Parameters
----------
other : Body
The body to subtract from self.
Raises
------
ValueError
If the subtraction results in an empty (complete) subtraction.
"""
return

@protect_grpc
def unite(self, other: "Body") -> None:
"""
Unite two bodies. `self` will be directly modified with the resulting union, and `other`
will be consumed, so it is important to make copies if needed.
Parameters
----------
other : Body
The body to unite with self.
"""
return


class TemplateBody(IBody):
"""
Expand Down Expand Up @@ -687,6 +737,21 @@ def plot(
pl.add_body(self, merge=merge, **plotting_options)
pl_helper.show_plotter(pl, screenshot=screenshot)

def intersect(self, other: "Body") -> None:
raise NotImplementedError(
"TemplateBody does not implement boolean methods. Call this method on a Body instead."
)

def subtract(self, other: "Body") -> None:
raise NotImplementedError(
"TemplateBody does not implement boolean methods. Call this method on a Body instead."
)

def unite(self, other: "Body") -> None:
raise NotImplementedError(
"TemplateBody does not implement boolean methods. Call this method on a Body instead."
)

def __repr__(self) -> str:
"""String representation of the body."""
lines = [f"ansys.geometry.core.designer.TemplateBody {hex(id(self))}"]
Expand Down Expand Up @@ -725,6 +790,27 @@ def __init__(self, id, name, parent: "Component", template: TemplateBody) -> Non
self._parent = parent
self._template = template

def reset_tessellation_cache(func):
"""Decorator for ``Body`` methods that require a tessellation cache update.
Parameters
----------
func : method
The method being called.
Returns
-------
Any
The output of the method, if any.
"""

@wraps(func)
def wrapper(self: "Body", *args, **kwargs):
self._template._tessellation = None
return func(self, *args, **kwargs)

return wrapper

@property
def id(self) -> str:
return self._id
Expand Down Expand Up @@ -845,6 +931,38 @@ def plot(
) -> None:
return self._template.plot(merge, screenshot, use_trame, **plotting_options)

@protect_grpc
@reset_tessellation_cache
def intersect(self, other: "Body") -> None:
response = self._template._bodies_stub.Boolean(
BooleanRequest(body1=self.id, body2=other.id, method="intersect")
).empty_result

if response == 1:
raise ValueError("Bodies do not intersect.")

other.parent.delete_body(other)

@protect_grpc
@reset_tessellation_cache
def subtract(self, other: "Body") -> None:
response = self._template._bodies_stub.Boolean(
BooleanRequest(body1=self.id, body2=other.id, method="subtract")
).empty_result

if response == 1:
raise ValueError("Subtraction of bodies results in an empty (complete) subtraction.")

other.parent.delete_body(other)

@protect_grpc
@reset_tessellation_cache
def unite(self, other: "Body") -> None:
self._template._bodies_stub.Boolean(
BooleanRequest(body1=self.id, body2=other.id, method="unite")
)
other.parent.delete_body(other)

def __repr__(self) -> str:
"""String representation of the body."""
lines = [f"ansys.geometry.core.designer.Body {hex(id(self))}"]
Expand Down
Loading

0 comments on commit d27b1d9

Please sign in to comment.