Hide Table of Contents

Fire Events for External Output Time Step Changes (C#)

This example shows how to listen and catch events for external force and motor output time step changes.

//--------------------------------------------------
// Preconditions:
// 1. SolidWorks Premium, which includes SolidWorks Motion,
//    is installed.
// 2. Specified assembly document exists.
// 3. c:\temp folder exists. Also, if
//    c:\temp\ForceAndmotorValuesCSharp.txt exists,
//    then delete it so that a new file is created
//    when this macro runs.
// 4. In SolidWorks:
//    a. Load SolidWorks Motion Study add-in (in
//       SolidWorks, click Tools > Add-Ins > SolidWorks Motion).
//    b. Open:
//       <SolidWorks_install_dir>\samples\tutorial\api\simple-block.sldasm
//    c. Select Motion Analysis in the Type of Study list at the
//       upper-left corner of the MotionManager.
// 5. In the IDE, reference the SolidWorks Motion Study interop
//    assembly (click Project > Add Reference >
//    SolidWorks.Interop.swmotionstudy).
//
// Postconditions:
// 1. Motion analysis is calculated.
// 2. c:\temp\ForceAndMotorValuesCSharp.txt is opened for output.
// 3. Forces and motors are set to external.
// 4. Motion analysis is recalculated.
// 5. Every force and motor output time step change fires
//    an event. The force magnitude or motor speed is
//    recalculated and written to the output file,
//    c:\temp\ForceAndMotorValuesCSharp.txt.
//
// NOTE: Because this model is used elsewhere, do
// not save changes when closing it.
//--------------------------------------------------

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swmotionstudy;
using System.Runtime.InteropServices;


namespace MotionExternalForceMotorEventsCSharp.csproj
{

    
partial class SolidWorksMacro
    {
        
public MotionStudy motionstudy;
        
public MotionStudy swMotionStudy;
        
public ModelDoc2 swModel;

        
public void Main()
        {
            ModelDocExtension swModelDocExt =
default(ModelDocExtension);
            MotionStudyManager swMotionMgr =
default(MotionStudyManager);
            
string studyName = null;
            
int numStudies = 0;

            swModel = (ModelDoc2)swApp.ActiveDoc;
            swModelDocExt = (ModelDocExtension)swModel.Extension;

            
// Motion study name
            studyName = "Motion Study 1";

            
// Get the MotionManager
            swMotionMgr = (MotionStudyManager)swModelDocExt.GetMotionStudyManager();
            numStudies = swMotionMgr.GetMotionStudyCount();
            swMotionStudy = (MotionStudy)swMotionMgr.GetMotionStudy(studyName);
            
if (!swMotionStudy.IsActive)
                swMotionStudy.Activate();

            
//Calculate the motion study
            swMotionStudy.Calculate();

            
// Set up events
            motionstudy = (MotionStudy)swMotionStudy;
            AttachEventHandlers();

            
// Set feature Force1 as external force
            SetForceExternalFlag("Force1", true);

            
// Set feature LinearMotor1 as external motor
            SetMotorExternalFlag("LinearMotor1", true);

            
// Fire event at every output time step
            swMotionStudy.SetFireOutputTimeStepEvents(true);

            
//Recalculate the motion study
            swMotionStudy.Calculate();

        }

        
private void SetForceExternalFlag(string featureName, bool setFlag)
        {
            
// Set forces to external; i.e., listen for events

            Feature swFeature = default(Feature);
            SimulationForceFeatureData swSimulationFeatureData =
default(SimulationForceFeatureData);
            
string simTypeName = null;
            
object[] motionFeatures = null;
            
bool ret = false;
            
int i = 0;

            motionFeatures = (
object[])swMotionStudy.GetMotionFeatures();
            
for (i = 0; i <= motionFeatures.GetUpperBound(0); i++)
            {
                swFeature = (Feature)motionFeatures[i];
                simTypeName = swFeature.GetTypeName();
                
if (simTypeName == "AEMLinearForce" | simTypeName == "AEMTorque")
                {
                    
if (swFeature.Name == featureName)
                    {
                        System.IO.
StreamWriter outputWrite = new System.IO.StreamWriter("c:\\temp\\ForceAndMotorValuesCSharp.txt", true);

                        swSimulationFeatureData = (SimulationForceFeatureData)swFeature.GetDefinition();
                        swSimulationFeatureData.ExternalState = setFlag;
                        ret = swFeature.ModifyDefinition(swSimulationFeatureData, swModel,
null);
                        outputWrite.WriteLine(
"Feature: " + swFeature.Name);
                        outputWrite.WriteLine(
"  Listen for events: " + swSimulationFeatureData.ExternalState);
                        outputWrite.WriteLine(
" ");

                        outputWrite.Close();

                    }
                }
            }
        }


        
private void SetMotorExternalFlag(string featureName, bool setFlag)
        {
            
// Set motors to external; i.e., listen for events

            Feature swFeature = default(Feature);
            SimulationMotorFeatureData swSimulationFeatureData =
default(SimulationMotorFeatureData);
            
string simTypeName = null;
            
object[] motionFeatures = null;
            
int i = 0;
            
bool ret = false;

            motionFeatures = (
object[])swMotionStudy.GetMotionFeatures();
            
for (i = 0; i <= motionFeatures.GetUpperBound(0); i++)
            {
                swFeature = (Feature)motionFeatures[i];
                simTypeName = swFeature.GetTypeName();
                
if (simTypeName == "AEMLinearMotor" | simTypeName == "AEMRotationalMotor")
                {
                    
if (swFeature.Name == featureName)
                    {
                        System.IO.
StreamWriter outputWrite = new System.IO.StreamWriter("c:\\temp\\ForceAndMotorValuesCSharp.txt", true);

                        swSimulationFeatureData = (SimulationMotorFeatureData)swFeature.GetDefinition();
                        swSimulationFeatureData.ExternalState = setFlag;
                        ret = swFeature.ModifyDefinition(swSimulationFeatureData, swModel,
null);
                        outputWrite.WriteLine(
"Feature name: " + swFeature.Name);
                        outputWrite.WriteLine(
"  Listen for events: " + swSimulationFeatureData.ExternalState);
                        outputWrite.WriteLine(
" ");

                        outputWrite.Close();
                    }
                }
            }
        }

        
public void writeOut(object name, double value)
        
// Write force and motor values to file
        {

            
string Name;

            Name = (
string)name;
            System.IO.
StreamWriter outputWrite = new System.IO.StreamWriter("c:\\temp\\ForceAndMotorValuesCSharp.txt", true);

            outputWrite.WriteLine(
"Name: " + Name);
            outputWrite.WriteLine(
"  Value: " + value.ToString());

            outputWrite.Close();

        }

        
public void AttachEventHandlers()
        {
            AttachSWEvents();
        }

        
public void AttachSWEvents()
        {
            motionstudy.ForceOutputTimeStepChangeNotify +=
this.motionStudy_ForceOutputTimeStepChangeNotify;
            motionstudy.MotorOutputTimeStepChangeNotify +=
this.motionStudy_MotorOutputTimeStepChangeNotify;
            motionstudy.OutputTimeStepChangeNotify +=
this.motionStudy_OutputTimeStepChangeNotify;
            motionstudy.StartCalculateNotify +=
this.motionStudy_StartCalculateNotify;
            motionstudy.StopCalculateNotify +=
this.motionStudy_StopCalculateNotify;
        }

        
public int motionStudy_ForceOutputTimeStepChangeNotify(double Time, object ForceNames, object Disp, object Velocity, object Acceleration, object ForceOrTorque, ref object ForceTorqueValue)
        {
            
// Fire event for every external force or torque output time step change
            MotionStudyProperties swMotionStudyProperties = default(MotionStudyProperties);
            
int i = 0;
            
double frameRate = 0;
            
int numForces = 0;
            
double deltaT = 0;
            
double pi = 0;
            
double w = 0;
            
double A = 0;
            
object[] arrForceNames;
            
double[] arrForceTorqueValue;
            
double[] arrAcceleration;

            arrForceNames = (
object[])ForceNames;
            arrForceTorqueValue = (
double[])ForceTorqueValue;
            arrAcceleration = (
double[])Acceleration;

            pi = 3.14159265358979;

            swMotionStudyProperties = (MotionStudyProperties)motionstudy.GetProperties((
int)swMotionStudyType_e.swMotionStudyTypeCosmosMotion);
            frameRate = swMotionStudyProperties.GetFrameRate();
            deltaT = 1 / frameRate;
            numForces = motionstudy.GetNumOfExternalForces();

            
for (i = 0; i <= numForces - 1; i++)
            {
                A = (
double)arrAcceleration[i];

                
if ((arrForceNames[i].ToString() == "Force1") || (arrForceNames[i].ToString() == "Force2") || (arrForceNames[i].ToString() == "Force3"))
                {
                    A = 0.8;
                    w = 2 * pi / 1.5;
                }
                
else if ((arrForceNames[i].ToString() == "Torque1" | arrForceNames[i].ToString() == "Torque2" | arrForceNames[i].ToString() == "Torque3"))
                {
                    A = 0.005;
                    w = pi;
                }
                
else if ((arrForceNames[i].ToString() == "Action Reaction Force1" | arrForceNames[i].ToString() == "Action Reaction Force2" | arrForceNames[i].ToString() == "Action Reaction Force3"))
                {
                    A = 0.002;
                    w = 2 * pi;
                }
                
else
                {
                    A = 0;
                    w = 0;
                }

                
// Compute the return value and write to file
                arrForceTorqueValue[i] = A * System.Math.Sin(w * (Time + deltaT));
                writeOut(arrForceNames[i], arrForceTorqueValue[i]);
            }
            
return 1;
        }

        
public int motionStudy_MotorOutputTimeStepChangeNotify(double Time, object MotorNames, object Position, object Velocity, object Acceleration, object ForceOrTorque, ref object MotorValue)
        {
            
// Fire event for every external motor output time step change
            int numMotors = 0;
            MotionStudyProperties swMotionStudyProperties =
default(MotionStudyProperties);
            
double frameRate = 0;
            
double deltaT = 0;
            
double pi = 0;
            
double w = 0;
            
double A = 0;
            
int i = 0;
            
object[] arrMotorNames;
            
double[] arrMotorValue;

            arrMotorNames = (
object[])MotorNames;
            arrMotorValue = (
double[])MotorValue;

            pi = 3.14159265358979;

            swMotionStudyProperties = (MotionStudyProperties)motionstudy.GetProperties((
int)swMotionStudyType_e.swMotionStudyTypeCosmosMotion);
            frameRate = swMotionStudyProperties.GetFrameRate();
            deltaT = 1 / frameRate;
            numMotors = motionstudy.GetNumOfExternalMotors();


            
for (i = 0; i <= numMotors - 1; i++)
            {
                
if ((arrMotorNames[i].ToString() == "RotaryMotor1") | (arrMotorNames[i].ToString() == "RotaryMotor2"))
                {
                    A = (pi / 4);
                    w = (pi / 2);
                }
                
else if ((arrMotorNames[i].ToString() == "LinearMotor1"))
                {
                    A = 0.05;
                    w = 2 * pi / 3;
                }
                
else if ((arrMotorNames[i].ToString() == "LinearMotor2"))
                {
                    A = 0.025;
                    w = 2 * pi;
                }
                
else if ((arrMotorNames[i].ToString() == "LinearMotor3"))
                {
                    A = 1.0472;
                    w = 2 * pi;
                }
                
else
                {
                    A = 0;
                    w = 0;
                }

                
//Compute the return value and write to file
                arrMotorValue[i] = A * System.Math.Sin(w * (Time + deltaT));
                writeOut(arrMotorNames[i], arrMotorValue[i]);
            }
            
return 1;
        }


        
public int motionStudy_OutputTimeStepChangeNotify(double Time)
        {
            
// Fire event at every output step

            double myTime = 0;
            myTime = Time;
            
return 1;

        }

        
public int motionStudy_StartCalculateNotify(double Time)
        {
            
// Fire event when recalculation starts

            double myTime = 0;
            myTime = Time;

            System.IO.
StreamWriter outputWrite = new System.IO.StreamWriter("c:\\temp\\ForceAndMotorValuesCSharp.txt", true);
            outputWrite.WriteLine(
"Recalculation started...");
            outputWrite.WriteLine(
"  ");
            outputWrite.Close();

            
return 1;

        }

        
public int motionStudy_StopCalculateNotify(double Time)
        {
            
// Fire event when recalculation stops

            double myTime = 0;
            myTime = Time;

            System.IO.
StreamWriter outputWrite = new System.IO.StreamWriter("c:\\temp\\ForceAndMotorValuesCSharp.txt", true);
            outputWrite.WriteLine(
" ");
            outputWrite.WriteLine(
"Recalculation stopped...");
            outputWrite.Close();

            
return 1;

        }


        
/// <summary>
        /// The SldWorks swApp variable is pre-assigned for you.
        /// </summary>

        public
SldWorks swApp;

    }
}



Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Fire Events for External Output Time Step Changes (C#)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:




x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2012 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.