11import copy
22import ctypes
33import numpy as np
4+
5+ from ansys .dpf .core .check_version import server_meet_version
46from ansys .dpf .gate .generated import dpf_vector_capi
57from ansys .dpf .gate .integral_types import MutableListInt32 , MutableInt32 , MutableListDouble , \
68 MutableListString , MutableListChar
@@ -23,23 +25,22 @@ class DPFVectorBase:
2325
2426 Parameters
2527 ----------
26- client : object having _internal_obj attribute and a shared CLayer Client instance, optional
28+ owner : object having _internal_obj attribute and a shared CLayer Client instance, optional
2729 Enables DPFClientAPI to choose the right API to use.
2830
2931 api: DpfVectorAbstractAPI, optional
3032 """
3133
32- def __init__ (self , client , api ):
34+ def __init__ (self , owner , api ):
3335 self .dpf_vector_api = api
3436 self ._modified = False
3537 self ._check_changes = True
3638 try :
37- if not client :
38- self ._internal_obj = self .dpf_vector_api .dpf_vector_new ()
39+ self ._internal_obj = self .dpf_vector_api .dpf_vector_new_for_object (owner )
40+ if not server_meet_version ("4.1" ,
41+ owner ._server ) and owner ._server .client is None : # BUG in 22.2: DpfVector is not holding the data owner and not call to data owner should be done at delete
3942 self ._check_changes = False
40- else :
41- self ._internal_obj = self .dpf_vector_api .dpf_vector_new_for_object (client )
42- except AttributeError :
43+ except ctypes .ArgumentError :
4344 raise NotImplementedError
4445
4546 @property
@@ -81,7 +82,6 @@ def has_changed(self):
8182 self ._modified = True
8283 return self ._modified
8384
84-
8585 @property
8686 def np_array (self ) -> np .ndarray :
8787 """
@@ -94,7 +94,7 @@ def np_array(self) -> np.ndarray:
9494 --------
9595 Memory of the DPFVector is not managed in this object. Use a ```DPFArray``` instead.
9696 """
97- if not self ._array .pointer or self .size == 0 :
97+ if not self ._array .pointer or self .size == 0 :
9898 return np .empty ((0 ,), dtype = self ._array .np_type )
9999 return np .ctypeslib .as_array (self ._array .pointer , shape = (self .size ,))
100100
@@ -140,8 +140,8 @@ def __del__(self):
140140
141141
142142class DPFVectorInt (DPFVectorBase ):
143- def __init__ (self , client = None , api = dpf_vector_capi .DpfVectorCAPI ):
144- super ().__init__ (client , api )
143+ def __init__ (self , owner = None , api = dpf_vector_capi .DpfVectorCAPI ):
144+ super ().__init__ (owner , api )
145145 self ._array = MutableListInt32 ()
146146
147147 @property
@@ -164,15 +164,16 @@ def commit(self) -> None:
164164 def __del__ (self ):
165165 try :
166166 if self ._array :
167- self .dpf_vector_api .dpf_vector_int_free (self , self .internal_data , self .internal_size , self .has_changed ())
167+ self .dpf_vector_api .dpf_vector_int_free (self , self .internal_data , self .internal_size ,
168+ self .has_changed ())
168169 except :
169170 pass
170171 super ().__del__ ()
171172
172173
173174class DPFVectorDouble (DPFVectorBase ):
174- def __init__ (self , client = None , api = dpf_vector_capi .DpfVectorCAPI ):
175- super ().__init__ (client , api )
175+ def __init__ (self , owner = None , api = dpf_vector_capi .DpfVectorCAPI ):
176+ super ().__init__ (owner , api )
176177 self ._array = MutableListDouble ()
177178
178179 @property
@@ -195,16 +196,17 @@ def commit(self) -> None:
195196 def __del__ (self ):
196197 try :
197198 if self ._array :
198- self .dpf_vector_api .dpf_vector_double_free (self , self .internal_data , self .internal_size , self .has_changed ())
199+ self .dpf_vector_api .dpf_vector_double_free (self , self .internal_data , self .internal_size ,
200+ self .has_changed ())
199201 except :
200202 pass
201203 super ().__del__ ()
202204
203205
204206class DPFVectorCustomType (DPFVectorBase ):
205- def __init__ (self , unitary_type , client = None , api = dpf_vector_capi .DpfVectorCAPI ):
207+ def __init__ (self , unitary_type , owner = None , api = dpf_vector_capi .DpfVectorCAPI ):
206208 self .type = unitary_type
207- super ().__init__ (client , api )
209+ super ().__init__ (owner , api )
208210 self ._array = MutableListChar ()
209211
210212 @property
@@ -222,7 +224,8 @@ def commit(self) -> None:
222224 if self._check_changes is set to True, compares the initial data computed in
223225 ```start_checking_modification``` (which should have been called beforehand) to the current one.
224226 """
225- self .dpf_vector_api .dpf_vector_char_commit (self , self .internal_data , self .size * self .type .itemsize , self .has_changed ())
227+ self .dpf_vector_api .dpf_vector_char_commit (self , self .internal_data , self .size * self .type .itemsize ,
228+ self .has_changed ())
226229
227230 @property
228231 def size (self ) -> int :
@@ -241,7 +244,7 @@ def np_array(self) -> np.ndarray:
241244 --------
242245 Memory of the DPFVector is not managed in this object. Use a ```DPFArray``` instead.
243246 """
244- if not self ._array .pointer or self .size == 0 :
247+ if not self ._array .pointer or self .size == 0 :
245248 return np .empty ((0 ,), dtype = self ._array .np_type )
246249 return np .ctypeslib .as_array (
247250 ctypes .cast (self ._array .pointer , ctypes .POINTER (np .ctypeslib .as_ctypes_type (self .type ))),
@@ -251,15 +254,16 @@ def np_array(self) -> np.ndarray:
251254 def __del__ (self ):
252255 try :
253256 if self ._array :
254- self .dpf_vector_api .dpf_vector_char_free (self , self .internal_data , self .size * self .type .itemsize , self .has_changed ())
257+ self .dpf_vector_api .dpf_vector_char_free (self , self .internal_data , self .size * self .type .itemsize ,
258+ self .has_changed ())
255259 except :
256260 pass
257261 super ().__del__ ()
258262
259263
260264class DPFVectorString (DPFVectorBase ):
261- def __init__ (self , client = None , api = dpf_vector_capi .DpfVectorCAPI ):
262- super ().__init__ (client , api )
265+ def __init__ (self , owner = None , api = dpf_vector_capi .DpfVectorCAPI ):
266+ super ().__init__ (owner , api )
263267 self ._array = MutableListString ()
264268
265269 @property
@@ -275,7 +279,8 @@ def internal_data(self) -> MutableListString:
275279 def __del__ (self ):
276280 try :
277281 if self ._array :
278- self .dpf_vector_api .dpf_vector_char_ptr_free (self , self .internal_data , self .internal_size , self .has_changed ())
282+ self .dpf_vector_api .dpf_vector_char_ptr_free (self , self .internal_data , self .internal_size ,
283+ self .has_changed ())
279284 except :
280285 pass
281286 super ().__del__ ()
@@ -293,7 +298,7 @@ def __iter__(self):
293298 def __next__ (self ):
294299 if self .n < len (self ):
295300 self .n += 1
296- return self .__getitem__ (self .n - 1 )
301+ return self .__getitem__ (self .n - 1 )
297302 else :
298303 raise StopIteration
299304
@@ -310,12 +315,11 @@ def __ne__(self, other):
310315
311316 def __str__ (self ):
312317 out = f"DPFVectorString["
313- for i in range (min (5 , len (self )- 1 )):
318+ for i in range (min (5 , len (self ) - 1 )):
314319 out += f"'{ self [i ]} ', "
315- if len (self )> 2 :
320+ if len (self ) > 2 :
316321 out += "..., "
317322 if len (self ) >= 2 :
318- out += f"'{ self [len (self )- 1 ]} '"
323+ out += f"'{ self [len (self ) - 1 ]} '"
319324 out += "]"
320325 return out
321-
0 commit comments