Skip to content

Commit 31765b1

Browse files
jonahrbRobPasMue
andauthored
Add body.copy() method (#373)
Co-authored-by: Roberto Pastor Muela <roberto.pastormuela@ansys.com>
1 parent 01f56d5 commit 31765b1

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

README.rst

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,14 @@ If you want to change the defaults, modify the following environment variables:
119119
export ANSRV_GEO_HOST=127.0.0.1
120120
export ANSRV_GEO_PORT=50051
121121
122-
**On Windows**
122+
**On Windows Powershell**
123+
124+
.. code::
125+
126+
$env:ANSRV_GEO_HOST="127.0.0.1"
127+
$env:ANSRV_GEO_PORT=50051
128+
129+
**On Windows CMD**
123130

124131
.. code::
125132

doc/source/getting_started/docker.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ Depending on the mechanism chosen to launch the Geometry service, you can set th
140140
$env:ANSRV_GEO_ENABLE_TRACE=0
141141
$env:ANSRV_GEO_LOG_LEVEL=2
142142
143-
.. tab-item:: Windows
143+
.. tab-item:: Windows CMD
144144

145145
.. code-block:: bash
146146
@@ -218,7 +218,14 @@ If you want to change the defaults, modify environment variables and the
218218
export ANSRV_GEO_HOST=127.0.0.1
219219
export ANSRV_GEO_PORT=50051
220220
221-
.. tab-item:: Windows
221+
.. tab-item:: Powershell
222+
223+
.. code-block:: bash
224+
225+
$env:ANSRV_GEO_HOST="127.0.0.1"
226+
$env:ANSRV_GEO_PORT=50051
227+
228+
.. tab-item:: Windows CMD
222229

223230
.. code-block:: bash
224231

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
"""Provides the ``Body`` class module."""
22
from enum import Enum
33

4-
from ansys.api.geometry.v0.bodies_pb2 import SetAssignedMaterialRequest, TranslateRequest
4+
from ansys.api.geometry.v0.bodies_pb2 import (
5+
CopyRequest,
6+
SetAssignedMaterialRequest,
7+
TranslateRequest,
8+
)
59
from ansys.api.geometry.v0.bodies_pb2_grpc import BodiesStub
610
from ansys.api.geometry.v0.commands_pb2 import (
711
AssignMidSurfaceOffsetTypeRequest,
@@ -400,6 +404,46 @@ def translate(self, direction: UnitVector3D, distance: Union[Quantity, Distance]
400404
)
401405
)
402406

407+
@protect_grpc
408+
def copy(self, parent: "Component", name: str = None) -> "Body":
409+
"""Creates a copy of the geometry body and places it under the specified parent.
410+
411+
Parameters
412+
----------
413+
parent: Component
414+
The parent component that the new body should live under.
415+
name: str
416+
The name to give the new body.
417+
418+
Returns
419+
-------
420+
Body
421+
Copy of the body.
422+
"""
423+
from ansys.geometry.core.designer.component import Component
424+
425+
# Check input types
426+
check_type(parent, Component)
427+
check_type(name, (type(None), str))
428+
copy_name = self.name if name is None else name
429+
430+
self._grpc_client.log.debug(f"Copying body {self.id}.")
431+
432+
# Perform copy request to server
433+
response = self._bodies_stub.Copy(
434+
CopyRequest(
435+
id=self.id,
436+
parent=parent.id,
437+
name=copy_name,
438+
)
439+
)
440+
441+
# Assign the new body to its specified parent (and return the new body)
442+
parent._bodies.append(
443+
Body(response.id, copy_name, parent, self._grpc_client, is_surface=False)
444+
)
445+
return parent._bodies[-1]
446+
403447
@protect_grpc
404448
def tessellate(self, merge: Optional[bool] = False) -> Union["PolyData", "MultiBlock"]:
405449
"""Tessellate the body and return the geometry as triangles.

tests/integration/test_design.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,41 @@ def test_project_and_imprint_curves(modeler: Modeler):
875875
assert len(body.faces) == 8
876876

877877

878+
def test_copy_body(modeler: Modeler):
879+
"""Test copying a body."""
880+
881+
# Create your design on the server side
882+
design = modeler.create_design("Design")
883+
884+
sketch_1 = Sketch().circle(Point2D([10, 10], UNITS.mm), Quantity(10, UNITS.mm))
885+
body = design.extrude_sketch("Original", sketch_1, Distance(1, UNITS.mm))
886+
887+
# Copy body at same design level
888+
copy = body.copy(design, "Copy")
889+
assert len(design.bodies) == 2
890+
assert design.bodies[-1] == copy
891+
892+
# Bodies should be distinct
893+
assert body != copy
894+
895+
# Copy body into sub-component
896+
comp1 = design.add_component("comp1")
897+
copy2 = body.copy(comp1, "Subcopy")
898+
assert len(comp1.bodies) == 1
899+
assert comp1.bodies[-1] == copy2
900+
901+
# Copy a copy
902+
comp2 = comp1.add_component("comp2")
903+
copy3 = copy2.copy(comp2, "Copy3")
904+
assert len(comp2.bodies) == 1
905+
assert comp2.bodies[-1] == copy3
906+
907+
# Ensure deleting original doesn't affect the copies
908+
design.delete_body(body)
909+
assert not body.is_alive
910+
assert copy.is_alive
911+
912+
878913
def test_beams(modeler: Modeler):
879914
"""Test beam creation."""
880915
# Create your design on the server side

0 commit comments

Comments
 (0)