Skip to content

Commit 872acbc

Browse files
feat: add any_operator support
1 parent dc84f8b commit 872acbc

File tree

5 files changed

+54
-7
lines changed

5 files changed

+54
-7
lines changed

src/ansys/dpf/core/any.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def _type_to_new_from_get_as_method(self, obj):
122122
data_tree,
123123
custom_type_field,
124124
collection,
125+
dpf_operator,
125126
)
126127

127128
if issubclass(obj, int):
@@ -190,6 +191,11 @@ def _type_to_new_from_get_as_method(self, obj):
190191
self._api.any_new_from_int_collection,
191192
self._api.any_get_as_int_collection,
192193
)
194+
elif issubclass(obj, dpf_operator.Operator):
195+
return (
196+
self._api.any_new_from_operator,
197+
self._api.any_get_as_operator,
198+
)
193199

194200
@staticmethod
195201
def new_from(obj, server=None):

src/ansys/dpf/core/dpf_operator.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class Operator:
121121
122122
"""
123123

124-
def __init__(self, name, config=None, server=None):
124+
def __init__(self, name=None, operator=None, config=None, server=None):
125125
"""Initialize the operator with its name by connecting to a stub."""
126126
self.name = name
127127
self._internal_obj = None
@@ -136,14 +136,32 @@ def __init__(self, name, config=None, server=None):
136136
# step 2: get api
137137
self._api_instance = None # see _api property
138138

139-
# step3: init environment
140-
self._api.init_operator_environment(self) # creates stub when gRPC
141139

142-
# step4: if object exists: take instance, else create it (server)
143-
if self._server.has_client():
144-
self._internal_obj = self._api.operator_new_on_client(self.name, self._server.client)
140+
# step2: if object exists, take the instance, else create it
141+
if operator is not None:
142+
if isinstance(operator, Operator):
143+
self._server = operator._server
144+
self._api = self._server.get_api_for_type(
145+
capi=operator_capi.OperatorCAPI,
146+
grpcapi=operator_grpcapi.OperatorGRPCAPI,
147+
)
148+
# step3: init environment
149+
self._api.init_operator_environment(self) # creates stub when gRPC
150+
core_api = self._server.get_api_for_type(
151+
capi=data_processing_capi.DataProcessingCAPI,
152+
grpcapi=data_processing_grpcapi.DataProcessingGRPCAPI,
153+
)
154+
core_api.init_data_processing_environment(self)
155+
self._internal_obj = core_api.data_processing_duplicate_object_reference(operator)
156+
self.name = operator.name
157+
else:
158+
self._internal_obj = operator
159+
self.name = self._api.operator_name(self)
145160
else:
146-
self._internal_obj = self._api.operator_new(self.name)
161+
if self._server.has_client():
162+
self._internal_obj = self._api.operator_new_on_client(self.name, self._server.client)
163+
else:
164+
self._internal_obj = self._api.operator_new(self.name)
147165

148166
if self._internal_obj is None:
149167
raise KeyError(

src/ansys/dpf/gate/any_grpcapi.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def _type_to_message_type():
4141
data_tree,
4242
custom_type_field,
4343
collection_base,
44+
dpf_operator,
4445
)
4546

4647
return [(int, base_pb2.Type.INT),
@@ -56,6 +57,7 @@ def _type_to_message_type():
5657
(data_tree.DataTree, base_pb2.Type.DATA_TREE),
5758
(collection_base.CollectionBase, base_pb2.Type.COLLECTION, base_pb2.Type.ANY),
5859
(dpf_vector.DPFVectorInt, base_pb2.Type.COLLECTION, base_pb2.Type.INT),
60+
(dpf_operator.Operator, base_pb2.Type.OPERATOR),
5961
]
6062

6163
@staticmethod
@@ -139,6 +141,10 @@ def any_get_as_any_collection(any):
139141
def any_get_as_int_collection(any):
140142
return AnyGRPCAPI._get_as(any).collection
141143

144+
@staticmethod
145+
def any_get_as_operator(any):
146+
return AnyGRPCAPI._get_as(any).operator
147+
142148
@staticmethod
143149
def _new_from(any, client=None):
144150
from ansys.grpc.dpf import dpf_any_pb2
@@ -220,3 +226,7 @@ def any_new_from_scoping(any):
220226
@staticmethod
221227
def any_new_from_data_tree(any):
222228
return AnyGRPCAPI._new_from(any, any._server)
229+
230+
@staticmethod
231+
def any_new_from_operator(any):
232+
return AnyGRPCAPI._new_from(any, any._server)

src/ansys/dpf/gate/operator_grpcapi.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def get_list(op):
5151
def operator_get_config(op):
5252
return OperatorGRPCAPI.get_list(op).config
5353

54+
@staticmethod
55+
def operator_name(op):
56+
return OperatorGRPCAPI.get_list(op).op_name
57+
5458
@staticmethod
5559
def update_init(op, pin):
5660
from ansys.grpc.dpf import operator_pb2

tests/test_any.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,12 @@ def test_cast_scoping_any(server_type):
120120
new_entity = any_dpf.cast()
121121

122122
assert entity.location == new_entity.location
123+
124+
125+
@conftest.raises_for_servers_version_under("7.0")
126+
def test_cast_operator_any(server_type):
127+
entity = dpf.Operator(server=server_type, name="U")
128+
any_dpf = dpf.Any.new_from(entity)
129+
new_entity = any_dpf.cast()
130+
131+
assert entity.name == new_entity.name

0 commit comments

Comments
 (0)