Hi All,
I'm trying to write a macro that will execute code when a collision is detected.
So far I have
- built a model that has a rotating part that collides with another part once each revolution
- recorded a macro
- modified the macro to register for the PartCollideNotify Event.
However when I run the animation and the parts collide, the event is apparently not fired, or I am not picking it up and I have no idea why. Any help would be great.
Please see the code below:
swMotionStudy1 = swMotionMgr.GetMotionStudy("Motion Study 1");
swMotionStudy1.Activate();
swMotionStudy1.StudyType = (int)SolidWorks.Interop.swmotionstudy.swMotionStudyType_e.swMotionStudyTypePhysicalSimulation;
swMotionStudy1.SetFireOutputTimeStepEvents(true);
EventListener eventTest = new EventListener();
swMotionStudy1.PartCollideNotify += new DMotionStudyEvents_PartCollideNotifyEventHandler(eventTest.collisionTest);
swMotionStudy1.MotorOutputTimeStepChangeNotify += new DMotionStudyEvents_MotorOutputTimeStepChangeNotifyEventHandler(eventTest.timeTest);
swMotionStudy1.Calculate();
class EventListener
{
public int collisionTest(double Time, object Component1, object Component2, object Vector1, object Vector2, object SurfaceNormal1, object SurfaceNormal2)
{
Console.WriteLine("This is called when the event fires.");
return 1;
}
public int timeTest(double Time,object MotorNames,object Position,object Velocity,object Acceleration,object ForceOrTorque,ref object MotorValue)
{
Console.WriteLine("This is called when the event fires.");
return 1;
}
}
The PartCollideNotify event is supported only for "Motion Analysis" study type and not for other study types. It seems like you are using "Basic Motion" (previously known as Physical Simulation) as your study type.
Regards,
Shrikant