@@ -482,6 +482,91 @@ def get_as(self, name, type_to_return=types.string):
482482 out = DataTree (data_tree = obj , server = self ._server )
483483 return out
484484
485+ def get_attribute_names (self ):
486+ """
487+ Returns a list of defined attribute names.
488+
489+ Returns
490+ -------
491+ list[str]
492+
493+ Examples
494+ --------
495+ >>> from ansys.dpf import core as dpf
496+ >>> data_tree = dpf.DataTree()
497+ >>> data_tree.add(id=3, qualities=["nice", "funny"], name="George")
498+ >>> data_tree.get_attribute_names()
499+ ['id', 'name', 'qualities']
500+ """
501+ coll_obj = collection .StringCollection (
502+ collection = self ._api .dpf_data_tree_get_available_attributes_names_in_string_collection (
503+ self
504+ ),
505+ server = self ._server ,
506+ )
507+
508+ return coll_obj .get_integral_entries ()
509+
510+ def get_sub_tree_names (self ):
511+ """
512+ Returns a list of defined sub-tree names.
513+
514+ Returns
515+ -------
516+ list[str]
517+
518+ Examples
519+ --------
520+ >>> from ansys.dpf import core as dpf
521+ >>> data_tree = dpf.DataTree()
522+ >>> first_subtree = dpf.DataTree()
523+ >>> second_subtree = dpf.DataTree()
524+ >>> data_tree.add(first=first_subtree, second=second_subtree)
525+ >>> data_tree.get_sub_tree_names()
526+ ['first', 'second']
527+ """
528+ coll_obj = collection .StringCollection (
529+ collection = self ._api .dpf_data_tree_get_available_sub_tree_names_in_string_collection (
530+ self
531+ ),
532+ server = self ._server ,
533+ )
534+
535+ return coll_obj .get_integral_entries ()
536+
537+ def __to_dict (self , dic ):
538+ for attribute_name in self .get_attribute_names ():
539+ dic [attribute_name ] = self .get_as (attribute_name )
540+
541+ for sub_tree_name in self .get_sub_tree_names ():
542+ sub_tree = self .get_as (sub_tree_name , types .data_tree )
543+ sub_dic = {}
544+ sub_tree .__to_dict (sub_dic )
545+ dic [sub_tree_name ] = sub_dic
546+
547+ def to_dict (self ):
548+ """
549+ Returns a dictionary representation of the DataTree
550+
551+ Returns
552+ -------
553+ dict
554+
555+ Examples
556+ --------
557+ >>> from ansys.dpf import core as dpf
558+ >>> data_tree = dpf.DataTree()
559+ >>> sub = dpf.DataTree()
560+ >>> sub.add(str="hello world")
561+ >>> data_tree.add(id=3, sub_tree=sub)
562+ >>> data_tree.to_dict()
563+ {'id': '3', 'sub_tree': {'str': 'hello world'}}
564+ """
565+ dic = {}
566+ self .__to_dict (dic )
567+
568+ return dic
569+
485570 def __setattr__ (self , key , value ):
486571 if key == "_common_keys" or key in self ._common_keys :
487572 return super .__setattr__ (self , key , value )
0 commit comments