Skip to content

Commit 750ed35

Browse files
authored
Adding repr methods (#185)
1 parent 2b3a262 commit 750ed35

File tree

5 files changed

+91
-9
lines changed

5 files changed

+91
-9
lines changed

src/ansys/geometry/core/designer/body.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,3 +440,12 @@ def plot(self, merge: Optional[bool] = False, **kwargs: Optional[dict]) -> None:
440440
pl = Plotter()
441441
pl.add_body(self, merge=merge, **kwargs)
442442
pl.show()
443+
444+
def __repr__(self) -> str:
445+
"""String representation of the body."""
446+
lines = [f"ansys.geometry.core.designer.Body {hex(id(self))}"]
447+
lines.append(f" Name : {self.name}")
448+
lines.append(f" Exists : {self.is_alive}")
449+
lines.append(f" Surface body : {self.is_surface}")
450+
lines.append(f" Parent component : {self._parent_component.name}")
451+
return "\n".join(lines)

src/ansys/geometry/core/designer/component.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -715,11 +715,13 @@ def plot(
715715
... sketch.circle(Point2D([x, y]), 0.2*u.m)
716716
... mycomp.extrude_sketch(f"body-{x}-{y}", sketch, 1 * u.m)
717717
>>> mycomp
718-
ansys.geometry.core.designer.Component 0x7f45c3396370
719-
Exists : True
720-
N Bodies : 25
721-
N Components : 0
722-
N Coordinate Systems : 0
718+
ansys.geometry.core.designer.Component 0x2203cc9ec50
719+
Name : my-comp
720+
Exists : True
721+
Parent component : my-design
722+
N Bodies : 25
723+
N Components : 0
724+
N Coordinate Systems : 0
723725
>>> mycomp.plot(pbr=True, metallic=1.0)
724726
725727
"""
@@ -730,11 +732,14 @@ def plot(
730732
pl.show()
731733

732734
def __repr__(self) -> str:
733-
"""Representation of the component."""
735+
"""String representation of the component."""
736+
alive_bodies = [1 if body.is_alive else 0 for body in self.bodies]
737+
alive_comps = [1 if comp.is_alive else 0 for comp in self.components]
734738
lines = [f"ansys.geometry.core.designer.Component {hex(id(self))}"]
739+
lines.append(f" Name : {self.name}")
735740
lines.append(f" Exists : {self.is_alive}")
736-
lines.append(f" N Bodies : {len(self.bodies)}")
737-
lines.append(f" N Components : {len(self.components)}")
741+
lines.append(f" Parent component : {self.parent_component.name}")
742+
lines.append(f" N Bodies : {sum(alive_bodies)}")
743+
lines.append(f" N Components : {sum(alive_comps)}")
738744
lines.append(f" N Coordinate Systems : {len(self.coordinate_systems)}")
739-
740745
return "\n".join(lines)

src/ansys/geometry/core/designer/coordinatesystem.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,22 @@ def frame(self) -> Frame:
9898
def parent_component(self) -> "Component":
9999
"""Parent component of the coordinate system."""
100100
return self._parent_component
101+
102+
def __repr__(self):
103+
"""String representation of the coordinate system."""
104+
lines = [f"ansys.geometry.core.designer.CoordinateSystem {hex(id(self))}"]
105+
lines.append(f" Name : {self.name}")
106+
lines.append(f" Parent component : {self.parent_component.name}")
107+
lines.append(
108+
f" Frame origin : [{','.join([str(x) for x in self.frame.origin])}] in meters"
109+
)
110+
lines.append(
111+
f" Frame X-direction : [{','.join([str(x) for x in self.frame.direction_x])}]"
112+
)
113+
lines.append(
114+
f" Frame Y-direction : [{','.join([str(x) for x in self.frame.direction_y])}]"
115+
)
116+
lines.append(
117+
f" Frame Z-direction : [{','.join([str(x) for x in self.frame.direction_z])}]"
118+
)
119+
return "\n".join(lines)

src/ansys/geometry/core/designer/design.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,16 @@ def set_shared_topology(self, share_type: SharedTopologyType) -> None:
303303
Shared topology does not apply on ``Design``.
304304
"""
305305
raise ValueError("The Design object itself cannot have a shared topology.")
306+
307+
def __repr__(self):
308+
"""String representation of the design."""
309+
alive_bodies = [1 if body.is_alive else 0 for body in self.bodies]
310+
alive_comps = [1 if comp.is_alive else 0 for comp in self.components]
311+
lines = [f"ansys.geometry.core.designer.Design {hex(id(self))}"]
312+
lines.append(f" Name : {self.name}")
313+
lines.append(f" N Bodies : {sum(alive_bodies)}")
314+
lines.append(f" N Components : {sum(alive_comps)}")
315+
lines.append(f" N Coordinate Systems : {len(self.coordinate_systems)}")
316+
lines.append(f" N Named Selections : {len(self.named_selections)}")
317+
lines.append(f" N Materials : {len(self.materials)}")
318+
return "\n".join(lines)

tests/integration/test_design.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,16 @@ def test_coordinate_system_creation(modeler: Modeler):
408408
assert dir.z == pytest.approx(dir_ref.z, rel=1e-8, abs=1e-14)
409409
assert nested_comp_cs2.parent_component.id == nested_comp.id
410410

411+
# Let's check the representation of the coordinate system
412+
nested_comp_cs1_str = str(nested_comp_cs1)
413+
assert "ansys.geometry.core.designer.CoordinateSystem" in nested_comp_cs1_str
414+
assert " Name : CompCS1" in nested_comp_cs1_str
415+
assert " Parent component : NestedComponent" in nested_comp_cs1_str
416+
assert " Frame origin : [0.01,0.2,3.0] in meters" in nested_comp_cs1_str
417+
assert " Frame X-direction : " in nested_comp_cs1_str
418+
assert " Frame Y-direction : " in nested_comp_cs1_str
419+
assert " Frame Z-direction : " in nested_comp_cs1_str
420+
411421

412422
def test_delete_body_component(modeler: Modeler):
413423
"""Test for verifying the deletion of ``Component`` and ``Body`` objects.
@@ -620,6 +630,32 @@ def test_delete_body_component(modeler: Modeler):
620630
with pytest.raises(ValueError, match="The Design object itself cannot be deleted."):
621631
design.delete_component(design)
622632

633+
# Let's try out the representation methods
634+
design_str = str(design)
635+
assert "ansys.geometry.core.designer.Design" in design_str
636+
assert "Name : Deletion_Test" in design_str
637+
assert "N Bodies : 0" in design_str
638+
assert "N Components : 0" in design_str
639+
assert "N Coordinate Systems : 0" in design_str
640+
assert "N Named Selections : 0" in design_str
641+
assert "N Materials : 0" in design_str
642+
643+
comp_1_str = str(comp_1)
644+
assert "ansys.geometry.core.designer.Component" in comp_1_str
645+
assert "Name : Component_1" in comp_1_str
646+
assert "Exists : False" in comp_1_str
647+
assert "Parent component : Deletion_Test" in comp_1_str
648+
assert "N Bodies : 0" in comp_1_str
649+
assert "N Components : 0" in comp_1_str
650+
assert "N Coordinate Systems : 0" in comp_1_str
651+
652+
body_1_str = str(body_1)
653+
assert "ansys.geometry.core.designer.Body" in body_1_str
654+
assert "Name : comp_3_circle" in body_1_str
655+
assert "Exists : False" in body_1_str
656+
assert "Surface body : False" in body_1_str
657+
assert "Parent component : Component_3" in body_1_str
658+
623659

624660
def test_shared_topology(modeler: Modeler):
625661
"""Test for checking the correct setting of shared topology on the server.

0 commit comments

Comments
 (0)