Modifying .osim XML file via API

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
POST REPLY
User avatar
Marc Carmichael
Posts: 45
Joined: Thu Jul 16, 2009 2:50 am

Modifying .osim XML file via API

Post by Marc Carmichael » Sun May 09, 2010 4:22 am

Hi, I'm fairly new to c++ and even newer to opensim, so please be gentle :)

I want to perform some simulations using the Stanford upper limb model in which I repeat the analysis with variations to the model (different inertia properties, scale, etc). I was thinking rather than edit the model's XML file every time I could simply use opensim's API to open the model, make changes to the model's Opensim::Model class, and then save the modified model as a new XML file.

I have written some code to do this, but it only half works. I am able to open the file and print model information to the screen. I was also able to open the model, edit the credits, and save the modifed model and the altered credits did show up in the new XML file. However when I try to change the mass properties of some of the models bodies these changes are not saved in the new XML file. I am able to read and print the modified values to the screen (and they do appear to change) but still they are not saved when I save the new XML file.

Here are the steps I use:


// Open existing XML model
Model osimModel(osim_file);

// Create a pointer to the body set
// within the osimModel class
BodySet bodyset = osimModel.updBodySet();

// Modify the mass of the "humerus" body
bodyset[bodyset.getIndex("humerus",0)].setMass(10);

// Save modified model class to new XML file
osimModel.print("model_modified.osim");



So when I take these steps, a new XML file is made, but the mass of the humerus body remains its previous value (0kg). If someone could give me a clue to what I am doing wrong then I would greatly appreciate it.

Cheers

User avatar
Ayman Habib
Posts: 2235
Joined: Fri Apr 01, 2005 12:24 pm

RE: Modifying .osim XML file via API

Post by Ayman Habib » Sun May 09, 2010 6:45 am

Dear Marc,

That's a very good use of the API.
The signature of the method Model::updBodySet() shows that it returns a reference to a BodySet. If you replace the line
BodySet bodyset = osimModel.updBodySet();

which makes a new "copy" of the BodySet by the line
BodySet& bodyset = osimModel.updBodySet();
^
|--- use a reference to the model's BodySet

Your code then would work.

You can also use a simplified syntax to get a reference to the Body as in bodyset.get("humerus").setMass(10);

Please let us know how that goes,
Good Luck,
-Ayman

User avatar
Marc Carmichael
Posts: 45
Joined: Thu Jul 16, 2009 2:50 am

RE: Modifying .osim XML file via API

Post by Marc Carmichael » Sun May 09, 2010 8:29 pm

Thanks for the help Ayman. You were right, I made the change and my program now works great.

Thanks again!

Marc

POST REPLY