Problem with MotionStudy GetMotionFeatures() after rebuild

Hey,

i got a problem with MotionStudy GetMotionFeatures() after rebuilding a document. For testing you can use "valve_cam2.sldasm" of SW expamples.


Here is my code:

MotionStudyManager studyMan = default(MotionStudyManager);

             MotionStudy study = default(MotionStudy);

             ModelDoc2 swDoc = (ModelDoc2)swApp.ActiveDoc;

             if (swDoc != null)

             {

                 studyMan =

(MotionStudyManager)swDoc.Extension.GetMotionStudyManager();

                 study = studyMan.GetMotionStudy("1200");

                 if (study.IsActive == false)

                     study.Activate();

                 int[] positions = new int[6];

                 object[] motionFeat = (object[])study.GetMotionFeatures();

                 string featurename = ((IFeature)motionFeat[3]).Name;

                 MessageBox.Show(featurename);

             }

else ...

Study called "1200" is activated. I can see the window-switch in SolidWorks. But the function "GetMotionFeatures" doesn't work correctly.

If i start the code at first time, there will be an error on "string featurename = ((IFeature)motionFeat[3]).Name". A NullReference Exception is thrown, because the object-array motionFeat is still set to null. GetMotionFeatures doesn't return any object.

If i continue and start the code at second time, there will be no error.

Everything works fine and i get the Name of the third motionFeature.

Without rebuilding the document everything works fine at first time.

Is there any function which has to be called before calling "GetMotionFeatures"?.

The Quick&Dirty-Solution of executing the code two times is not very satisfying.

Thanks for your help!

SolidworksApi macros