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.
  • Hi Chris,
     
    I've spent some time with the new API and it sounds like I've had a bit more luck than you - I have seen a few crashes, but not many. In fact I spent 3 days demoing aspects of this to customers and didn't have a single crash! Some thoughts:
     
    1. iteritems() returns a tuple of (key, value), so try correcting to:
     
    mod = Adams.defaults.model
    for pName, pObj in mod.Parts.iteritems():
    prtNames.append(pName)
     
     
    2. I've attached an example of auto-renaming joints in your model, for interest's sake.
     
    3. I've attached an 'Adams Python cheatsheet' for common issues.
     
    4. We have a training class that will be available in a month or so; that has a fair bit of basic Python to get one going, but there are vast resources out there. I stumbled upon this cheatsheet and it's more of a 'cheat book' that looks quite good:
     
     
    If you post ideas for examples/questions then we'll add articles to our SimCompanion on this..
     
    Thanks,
    Kent
     
     
     
     

    Attached Files (1)
  • My other attachment seems to have wandered off, attaching to this 'answer'..

    Attached Files (1)
  • Thanks, Kent. Python is nice because it is so easy to find help with the language. I thought that I was doing things wrong, but it looks like I was on the right track and your example helps with that. Unfortunately, using iteritems() as you suggest still crashes view! It must be something specific about my model database, I will ​try to nail down what it is and submit a ticket.
  • No idea about the crash, but I'd do this pronto if you're dealing with a bin file:
    • dump out cmd file from View
    • restart View and load the cmd file
    Hopefully that fixes it. Otherwise please send us (mscadams.support@mscsoftware.com) the file so we can reproduce it..
     
    Thanks,
    Kent
     
  • Short reminder for Kent and other staff involved in the Python lib:
    Would be nice to have A/V`Car and A/View functions in Python like DB_EXISTS() or DB_CHILDREN(), wouldn't it ?
     
    Last week we had a severe issue with a SDK function I wrote a decade ago.
    The view dll created on win7 crashed on win10 during execution of a function to create requests.
    After re-compiling the view dll and creating the plugin binary on win10 it still crashes. Seems to not like the memory allocation at the beginning of the code which worked just fine on SGI, Linux, WinNT, WinVista and Win7.
    Now I'm seriously tempted to re-write that function in Python as it'll be nearly the same effort (couted in day of precious work lost) than debugging that old code ....
     
     
  • 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.
     
     
  • One more thing, Martin, you can hack cmd expressions into python with, for example:
     
    Adams.evaluate_exp("​DB_EXISTS(\"testname\")")
     
    being careful to use escape quotes \" to stay inside the string.
  • Hi Martin,
     
    I'll discuss that with Product Development this week, thanks for the suggestion. In the meantime I figure that the Python equivalent of these functions would be:
     
    DB_EXISTS() --> if Adams.stoo('some_element'):
    (that returns the element if found, 'None' otherwise).
     
    DB_CHILDREN --> that is the .Parts.values(), .Markers.values(), etc 'Manager' objects of the various other objects..
     
    So I'm still inclined to think that this is just a different way of solving the problem, but I'll convey your thoughts in detail to my colleagues, thanks.
     
    Kent
     
  • Why "renaming" everything just for the sake of renaming and nuking experienced users to a less than rookie level ? Reminds me of that <insert curse here> ribbon GUI that nobody wanted except your bosses in order to sell something "new" to the people.
    Why not keeping the old names in order to at least keep some of the experience gathered over decades ?
     
    I prefer stuff like this to stay consistent with the command language pendants:
     
    def db_exists(entity):
     elements_exist = 0
     expression = "DB_EXISTS(\"%s\")" % entity
     elements_exist = aview_main.evaluate_exp(expression)
     return elements_exist
     
    and later use as:
      if db_exists(req_name):
        cremod = 'modify'
      else:
        cremod = 'create'
     
      command = "output_control %s request request_name = %s" % (cremod,req_name)
     
    I could easily do this in my own little universe for the two dozen functions I mostly use, but you know what ? In my opinion that's your homework you guys get paid for.
    My wishlist would be to have all ADAMS functions available under the same names and arguments.