AnsweredAssumed Answered

AddForce3 C#

Question asked by Takumi Yokoi on Jun 26, 2019

Please help me!!!

I want to use AddForce3. But this program get errors.

Please tell me why this program get errors.

I show the program below.

This program uses C#.

 

using System;
using SolidWorks.Interop.cosworks;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Diagnostics;


namespace Simulation
{
class Program
{
SldWorks.ISldWorks swApp;
ICosmosWorks COSMOSWORKS = null;
IModelDoc2 model = default(ModelDoc2);
ICWStudy study = default(CWStudy);

void startSW()
{
swApp = new SldWorks.SldWorks();
Debug.Assert(swApp != null);
swApp.Visible = true;
}

 

void OpenPartDoc(string fileName)
{
int errorCode = 0;
int warningCode = 0;

model = (IModelDoc2)swApp.OpenDoc6(fileName,
(int)swDocumentTypes_e.swDocPART,
(int)swOpenDocOptions_e.swOpenDocOptions_Silent,
"",
ref errorCode,                    
ref warningCode);

checkStatus("openPartDoc", errorCode);

}

public static void checkStatus(string methodName, int code)
{
int success = 0;
if (code != success)
{
Console.WriteLine(methodName + ": " + code);
}
}
void loadSimulationAddIn()
{
string path_to_cosworks_dll = swApp.GetExecutablePath() + @"\Simulation\cosworks.dll";
int errors = swApp.LoadAddIn(path_to_cosworks_dll);
checkStatus("Load cosworks.dll: ", errors);

string addInName = "SldWorks.Simulation";
CwAddincallback COSMOSObject = (CwAddincallback)swApp.GetAddInObject(addInName);
Debug.Assert(COSMOSObject != null);

COSMOSWORKS = (ICosmosWorks)COSMOSObject.CosmosWorks;
Debug.Assert(COSMOSWORKS != null);
}


void createStudy(string name, int type)
{
ICWModelDoc actDoc = (ICWModelDoc)COSMOSWORKS.ActiveDoc;
Debug.Assert(actDoc != null);

ICWStudyManager studyMgr = (ICWStudyManager)actDoc.StudyManager;
Debug.Assert(studyMgr != null);

int errors = 0;
study = (ICWStudy)studyMgr.CreateNewStudy3(name,
type,
0,
out errors);

checkStatus("createStudy", errors);
}


void createStaticStudy(string name)
{
createStudy(name, (int)swsAnalysisStudyType_e.swsAnalysisStudyTypeStatic);
}


void applyMaterial(int componentNum, int bodyNum, string materialName)
{
string materialLibrary = swApp.GetExecutablePath() + @"\lang\english\sldmaterials\solidworks materials.sldmat";

ICWSolidManager SolidMgr = (ICWSolidManager)study.SolidManager;

int errorCode = 0;
ICWSolidComponent SolidComponent = SolidMgr.GetComponentAt(componentNum, out errorCode);

if (errorCode == 0)
{
//string SolidComponentName = SolidComponent.ComponentName;
ICWSolidBody SolidBody = (ICWSolidBody)SolidComponent.GetSolidBodyAt(bodyNum, out errorCode);
//if (errorCode != 0) ErrorMsg("No solid body.");

errorCode = (int)SolidBody.SetLibraryMaterial(materialLibrary, materialName);
Debug.WriteLine("Mat: " + errorCode);
}
else
{
checkStatus("applyMaterial", errorCode);
}
}


void applyFixed()
{
if (model == null) { model = (IModelDoc2)swApp.ActiveDoc; }

ISelectionMgr selectionMgr = (ISelectionMgr)model.SelectionManager;
bool isSelected = model.Extension.SelectByID2("", "FACE", 2.81519136727046E-02, 0, 4.24519591314118E-02, false, 0, null, 0);

if (isSelected)
{

object selectedFace = (object)selectionMgr.GetSelectedObject6(1, -1);
object[] fixedFaces = { selectedFace };

ICWLoadsAndRestraintsManager BCMgr = (ICWLoadsAndRestraintsManager)study.LoadsAndRestraintsManager;
int errorCode = 0;
ICWRestraint restraint = (ICWRestraint)BCMgr.AddRestraint((int)swsRestraintType_e.swsRestraintTypeFixed,
fixedFaces, null, out errorCode);

checkStatus("Apply Fixed BC", errorCode);
}

model.ClearSelection2(true);
}


void ApplyForce()
{
if (model == null) { model = (IModelDoc2)swApp.ActiveDoc; }
ISelectionMgr selectionMgr = model.ISelectionManager;
bool isselected = model.Extension.SelectByID2("", "FACE", 25E-03, 0, 0.5E-03, false, 0, null, 0);

if (isselected)
{
object selectedFace = (object)selectionMgr.GetSelectedObject6(1, -1);

ICWLoadsAndRestraintsManager BCMgr = study.LoadsAndRestraintsManager;
ICWForce Load= BCMgr.AddForce3(
0, // System.int ForceType,
0, // System.int SelectionType,
2, // System.int RefDirType,
0, // System.int InterpolationMode,
2, // System.int DistPercentageOpt,
1, // System.int NumOfRows,
"", //System.object DistValue,
10000, //System.object ForceValue,
false, //System.bool NonUniformLoading,
false, //System.bool NULoadingOnBeam,
0, //System.int NonUniformLoadDistDef,
0, //System.int NonUniformLoadDistType,
2, //System.int Ucode,
1000, //System.double TorqueNFVal,
1, //System.object Comps,
false, //System.bool FlipOrigin,
false, //System.bool IsCurvedBeam,
selectedFace, //System.object DispArray,
"", //System.object RefGeom,
true, //System.bool PerUnitLength,
out int errorCode //out System.int ErrorCode
);

Load.ForceBeginEdit();
Load.ForceEndEdit();

checkStatus("ApplyForce", errorCode);

}
model.ClearSelection2(true);
}


static void Main(string[] args)
{
var p = new Program();

Console.WriteLine("Starting SolidWorks");
p.startSW();

Console.WriteLine("Open Part Document");
string fileName = @"C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\Simulation\Examples\Nonlinear\v1.SLDPRT";
p.OpenPartDoc(fileName);


Console.WriteLine("Loading Simulation AddIn");
p.loadSimulationAddIn();

Console.WriteLine("Creating Static Study");
string studyName = "StaticStudy";
p.createStaticStudy(studyName);

Console.WriteLine("Applying Materials");
int componentNum = 0;
int bodyNum = 0;
string materialName = "Gray Cast Iron (SN)";
p.applyMaterial(componentNum, bodyNum, materialName);

Console.WriteLine("Apply Fixed ");
p.applyFixed();

Console.WriteLine("Apply Force");
p.ApplyForce();
}
}

Outcomes