Get Normal Contact/Friction Force (C#)
This example shows how to get the normal contact/friction x, y, z, and
resultant force for the selected entities.
//--------------------------------------------
//
// Preconditions:
// 1. Add the SolidWorks Simulation as an add-in
// (in SolidWorks, click Tools > Add-ins > SolidWorks Simulation).
// 2. Add the SolidWorks Simulation primary interop assembly as
// a reference (in the IDE's Project Explorer, right-click
// the project name, select Add Reference, click the Browse tab,
// navigate to the <SolidWorks_install_dir>\api\redist folder
and
// select SolidWorks.Interop.cosworks.dll).
// 3. Open <SolidWorks_install_dir>\Simulation\Examples\Contact\slider_locker_mechanism.sldasm.
// 4. Open the Immediate window.
// 5. Select the Ready Simulation study tab.
// 6. Right-click Ready in the Simulation study tree and
// select Run. The study may take several minutes to solve.
// Click Yes if the message box informing you
// of excessive displacements is displayed.
// 7. Run the macro.
//
//
// Postconditions: The x, y, z, and resultant
// force are printed to the Immediate window.
//
//
// NOTE: Because this assembly document is used by
// SolidWorks Simulation tutorial, do not save any
// changes when closing the document.
//
//-------------------------------
using
SolidWorks.Interop.sldworks;
using
SolidWorks.Interop.swconst;
using
SolidWorks.Interop.cosworks;
using System;
using System.Diagnostics;
using System.Collections;
namespace
GetContactForcesAndFrictionCWResultsCSharp.csproj
{
partial
class
SolidWorksMacro
{
public
void Main()
{
ModelDoc2 swModel;
ModelDocExtension swModelDocExt;
CwAddincallback COSMOSObject;
CosmosWorks COSMOSWORKS;
CWModelDoc ActDoc;
CWStudyManager StudyMngr;
CWStudy Study;
CWResults Results;
int errors;
byte[] sel1 =
null;
byte[] sel2 =
null;
object
selObject1 = null;
object
selObject2 = null;
int state;
object[]
Forces;
int i;
ArrayList
PIDCollection = new
ArrayList();
// Initialize PIDs
PIDCollection = PIDInitializer();
// Connect to SolidWorks
swModel = (ModelDoc2)swApp.ActiveDoc;
swModelDocExt = (ModelDocExtension)swModel.Extension;
// Get the SolidWorks Simulation object
COSMOSObject = (CwAddincallback)swApp.GetAddInObject("CosmosWorks.CosmosWorks");
if (COSMOSObject
== null) ErrorMsg(swApp,
"COSMOSObject object not found.",
true);
COSMOSWORKS = (CosmosWorks)COSMOSObject.CosmosWorks;
if (COSMOSWORKS
== null) ErrorMsg(swApp,
"COSMOSWORKS object not found.",
true);
// Get the active document
ActDoc = (CWModelDoc)COSMOSWORKS.ActiveDoc;
if (ActDoc ==
null) ErrorMsg(swApp,
"No active document.",
true);
// Get the Ready study
StudyMngr = (CWStudyManager)ActDoc.StudyManager;
if (StudyMngr
== null) ErrorMsg(swApp,
"StudyMngr object not there.",
true);
StudyMngr.ActiveStudy = 0;
Study = (CWStudy)StudyMngr.GetStudy(0);
if (Study ==
null) ErrorMsg(swApp,
"Study not created.",
true);
// Get the results
Debug.Print("Active
study: " + Study.Name);
Results = (CWResults)Study.Results;
if (Results ==
null) ErrorMsg(swApp,
"No Results object.",
true);
// Get PIDs for selection
SelectByPID("selection1",
PIDCollection, ref sel1);
SelectByPID("selection2",
PIDCollection, ref sel2);
selObject1 = (object)swModelDocExt.GetObjectByPersistReference3((sel1),
out state);
selObject2 = (object)swModelDocExt.GetObjectByPersistReference3((sel2),
out state);
object[]
arraySelections = { selObject1, selObject2 };
// Get the normal contact/friction force
for the selected entities
Forces = (object[])Results.GetContactForcesAndFriction(0,
null, (arraySelections), 0,
(int)swsUnit_e.swsUnitSI,
out errors);
Debug.Print(" x,
y, z, and Resultant:");
for (i = 0; i
<= Forces.GetUpperBound(0); i++)
{
Debug.Print(" "
+ Forces[i]);
}
}
// Error function
private
void ErrorMsg(object
SwApp, string Message,
bool EndTest)
{
swApp.SendMsgToUser2(Message, 0, 0);
swApp.RecordLine("'*** WARNING -
General");
swApp.RecordLine("'*** "
+ Message);
swApp.RecordLine("");
if (EndTest)
{
}
}
public
ArrayList PIDInitializer()
{
// Initialize PIDs
ArrayList
PIDCollection = new
ArrayList();
string
selection1;
string
selection2;
// Get the PIDs of the faces to select
selection1 =
"48,17,0,0,3,0,0,0,255,254,255,32,76,0,111,0,99,0,107,0,101,0,114,0,45,0,49,0,64,0,115,0,108,0,105,0,100,0,101,0,114,0,95,0,108,0,111,0,99,0,107,0,101,0,114,0,95,0,109,0,101,0,99,0,104,0,97,0,110,0,105,0,115,0,109,0,4,0,0,0,16,0,0,0,1,0,0,0,1,0,0,0,11,0,0,0,255,255,1,0,11,0,109,111,70,97,99,101,82,101,102,95,99,1,0,0,0,0,0,0,0,6,0,0,0,0,3,0,0,0,0,0,0,125,195,148,37,173,73,178,84,125,195,148,37,173,73,178,84,0,0,255,255,1,0,23,0,109,111,70,114,111,109,83,107,116,69,110,116,83,117,114,102,73,100,82,101,112,95,99,0,0,255,255,1,0,6,0,109,111,70,82,95,99,255,255,1,0,13,0,109,111,69,120,116,79,98,106,101,99,116,95,99,255,255,1,0,17,0,109,111,67,83,116,114,105,110,103,72,97,110,100,108,101,95,99,255,254,255,57,67,0,58,0,92,0,67,0,79,0,83,0,77,0,79,0,83,0,68,0,111,0,99,0,115,0,92,0,69,0,120,0,97,0,109,0,112,0,108,0,101,0,115,0,95,0,102,0,111,0,114,0,67,0,117,0,115,0,116,0,111,0,109,0,101,0,114,0,115,0,92,0,67,0,111,0,110,0,116,0,97,0,99,0,116,0,92,0,76,0,111,0,99,0,107,0,101,0,11";
selection1 = selection1 +
"4,0,46,0,83,0,76,0,68,0,80,0,82,0,84,0,9,128,255,254,255,6,76,0,111,0,99,0,107,0,101,0,114,0,2,0,0,9,255,26,58,0,166,20,28,65,1,0,0,0,0,0,0,0,3,0,0,0,255,254,255,7,68,0,101,0,102,0,97,0,117,0,108,0,116,0,0,0,0,0,0,0,0,0,166,20,28,65,33,0,0,0,63,0,27,58,12,0,0,0,255,255,1,0,20,0,109,111,69,110,100,70,97,99,101,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,33,0,0,0,63,0,27,58,0,0,0,0,255,255,255,255,3,128,0,0,5,128,8,0,33,0,0,0,63,0,27,58,19,0,0,0,12,128,0,0,5,128,8,0,33,0,0,0,63,0,27,58,1,0,0,0,255,255,255,255,3,128,0,0,5,128,8,0,33,0,0,0,63,0,27,58,13,0,0,0,0,0,0,0,0,0,0,0,0,0";
selection2 =
"48,17,0,0,3,0,0,0,255,254,255,32,83,0,108,0,105,0,100,0,101,0,114,0,45,0,49,0,64,0,115,0,108,0,105,0,100,0,101,0,114,0,95,0,108,0,111,0,99,0,107,0,101,0,114,0,95,0,109,0,101,0,99,0,104,0,97,0,110,0,105,0,115,0,109,0,4,0,0,0,16,0,0,0,1,0,0,0,1,0,0,0,16,0,0,0,255,255,1,0,11,0,109,111,70,97,99,101,82,101,102,95,99,1,0,0,0,0,0,0,0,6,0,0,0,0,3,0,0,0,0,0,0,125,195,148,37,173,73,178,84,125,195,148,37,173,73,178,84,0,0,255,255,1,0,23,0,109,111,70,114,111,109,83,107,116,69,110,116,83,117,114,102,73,100,82,101,112,95,99,0,0,255,255,1,0,6,0,109,111,70,82,95,99,255,255,1,0,13,0,109,111,69,120,116,79,98,106,101,99,116,95,99,255,255,1,0,17,0,109,111,67,83,116,114,105,110,103,72,97,110,100,108,101,95,99,255,254,255,57,67,0,58,0,92,0,67,0,79,0,83,0,77,0,79,0,83,0,68,0,111,0,99,0,115,0,92,0,69,0,120,0,97,0,109,0,112,0,108,0,101,0,115,0,95,0,102,0,111,0,114,0,67,0,117,0,115,0,116,0,111,0,109,0,101,0,114,0,115,0,92,0,67,0,111,0,110,0,116,0,97,0,99,0,116,0,92,0,83,0,108,0,105,0,100,0,101,0,";
selection2 = selection2 +
"114,0,46,0,83,0,76,0,68,0,80,0,82,0,84,0,9,128,255,254,255,6,83,0,108,0,105,0,100,0,101,0,114,0,2,0,0,90,0,27,58,0,9,21,28,65,1,0,0,0,0,0,0,0,2,0,0,0,255,254,255,7,68,0,101,0,102,0,97,0,117,0,108,0,116,0,0,0,0,0,0,0,0,0,9,21,28,65,16,0,0,0,131,0,27,58,2,0,0,0,255,255,1,0,20,0,109,111,69,110,100,70,97,99,101,83,117,114,102,73,100,82,101,112,95,99,0,0,5,128,8,0,16,0,0,0,131,0,27,58,0,0,0,0,255,255,255,255,3,128,0,0,5,128,8,0,16,0,0,0,131,0,27,58,9,0,0,0,12,128,0,0,5,128,8,0,16,0,0,0,131,0,27,58,1,0,0,0,255,255,255,255,3,128,0,0,5,128,8,0,16,0,0,0,131,0,27,58,3,0,0,0,0,0,0,0,0,0,0,0,0,0";
// Store constant in a collection
PIDCollection.Add(selection1);
PIDCollection.Add(selection2);
// Pass this back
return
PIDCollection;
}
private
void SelectByPID(string
PIDName, ArrayList
PIDCollection, ref
byte[] varEntity)
{
// Select by PID
byte[]
PID = null;
string[]
PIDVariant = null;
string
PIDString = null;
int i = 0;
// Get string from array
PIDString =
"";
switch (PIDName)
{
case
"selection1":
PIDString = (string)PIDCollection[0];
break;
case
"selection2":
PIDString = (string)PIDCollection[1];
break;
default:
break;
}
// Parse the string into an array
PIDVariant = PIDString.Split(new
char[] {
',' });
// Convert string array to byte array
int
sizeArray = PIDVariant.Length;
PID = new
byte[sizeArray];
for (i = 0; i <
PIDVariant.Length - 1; i++)
{
PID[i] = Convert.ToByte(PIDVariant[i]);
}
varEntity = PID;
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
}
}