hexagon logo

ADAMS Python, more in-depth examples?

​I am very excited about opening up the python interface for ADAMS 2017, but as I dig into the language, I'm finding lots of ways to crash view, and not much documentation.
 
The 2017 documentation is good at giving a very short introduction, showing how to create and modify objects, but these are frankly trivial examples and don't actually use much of the python language.
 
For example, if I want to iterate through all the parts in my model, I imagine I would do something like
 
prtNames=list()
for prt in Adams.getCurrentModel().Parts.iteritems():
  prtNames.append(prt.name)
 
I have tried several iterations of this, and most of them either throw an error or crash Aview (this one then crashes). Can anyone point me to some more complex examples, which take advantage of python coding patterns?
 
Crashing suggests the code is fragile, so I need more guidance than the help() command in how to do things as help() reveals things that are broken, like the iteritems function.
Parents
  • Martin,
    The keys() function is kind of like DB_CHILDREN except that it only returns one type. You can use it to search the whole database like DB_EXISTS but again it requires some knowledge of the type of thing you're looking for, and it's case-sensitive.
     
    If it's just one layer deep on something, like a part on a model, checking for existence is easy:
     
    ('partname' in Adams.getCurrentModel().Parts.keys())
     
    will return a Boolean
     
    Things are trickier but still possible if it's two layers deep like searching markers:
     
    any([('marker_name' in p[1].Markers.keys()) for p in Adams.getCurrentModel().Parts.iteritems()])
     
    Three layers gets a little silly (maybe multiple models?) but you can nest the for loops as far as you want.
     
     
Reply
  • Martin,
    The keys() function is kind of like DB_CHILDREN except that it only returns one type. You can use it to search the whole database like DB_EXISTS but again it requires some knowledge of the type of thing you're looking for, and it's case-sensitive.
     
    If it's just one layer deep on something, like a part on a model, checking for existence is easy:
     
    ('partname' in Adams.getCurrentModel().Parts.keys())
     
    will return a Boolean
     
    Things are trickier but still possible if it's two layers deep like searching markers:
     
    any([('marker_name' in p[1].Markers.keys()) for p in Adams.getCurrentModel().Parts.iteritems()])
     
    Three layers gets a little silly (maybe multiple models?) but you can nest the for loops as far as you want.
     
     
Children
No Data