Create and Edit Edge Weld Connector Example (C#)
This example shows how to create and edit an edge weld connector.
//----------------------------------------------------------------------------
// 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, click Add Reference, click the Browse tab,
// navigate to install_dir\api\redist\CLR2,
// click SolidWorks.Interop.cosworks.dll, and click OK).
// 3. Open install_dir\samples\tutorial\api\tjoint.sldprt.
//
// Postconditions: Creates and modifies Edge Weld Connector-3.
//
// NOTE: Because the model is used elsewhere, do not save changes.
// ---------------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.cosworks;
using System.Runtime.InteropServices;
namespace EdgeWeldConnector_CSharp.csproj
{
partial class SolidWorksMacro
{
public void Main()
{
ModelDoc2 Part = default(ModelDoc2);
int errCode = 0;
CosmosWorks COSMOSWORKS = default(CosmosWorks);
CwAddincallback CWAddinCallBack = default(CwAddincallback);
CWModelDoc ActDoc = default(CWModelDoc);
CWStudyManager StudyMngr = default(CWStudyManager);
CWStudy Study = default(CWStudy);
CWLoadsAndRestraintsManager LoadAndRestraintMngr = default(CWLoadsAndRestraintsManager);
CWEdgeWeldConnector theCurrentLoad = default(CWEdgeWeldConnector);
CWEdgeWeldConnector theNewLoad = default(CWEdgeWeldConnector);
byte[] var1 = null;
byte[] var2 = null;
byte[] var3 = null;
object pDisp1 = null;
object pDisp2 = null;
object pDisp3 = null;
object FirstFace = null;
object SecondFace = null;
int NWeakMaterial = 0;
double DUltimateTensileStrength = 0;
int NTensileStrengthUnit = 0;
double DCorrelationFactor = 0;
double DPartialSafetyFactor = 0;
bool BIsEstimatedWeldSize = false;
int nElectrodeMaterial = 0;
double dWeldStrength = 0;
int nWeldStrengthUnit = 0;
int nFOSOption = 0;
double dFOS = 0;
bool bEstimatedWeldSize = false;
double dEstimatedWeldSize = 0;
int nEstimatedWeldSizeUnit = 0;
int EdgeWeldType = 0;
bool WeldOrientation = false;
bool InducedBendingMomentIncluded = false;
int CodeType = 0;
ArrayList PIDCollection = new ArrayList();
CWAddinCallBack = (CwAddincallback)swApp.GetAddInObject("CosmosWorks.CosmosWorks");
COSMOSWORKS = CWAddinCallBack.CosmosWorks;
ActDoc = (CWModelDoc)COSMOSWORKS.ActiveDoc;
StudyMngr = (CWStudyManager)ActDoc.StudyManager;
StudyMngr.ActiveStudy = 0;
Study = StudyMngr.GetStudy(StudyMngr.ActiveStudy);
LoadAndRestraintMngr = Study.LoadsAndRestraintsManager;
PIDCollection = PIDInitializer();
Part = (ModelDoc2)swApp.ActiveDoc;
SelectByPID("selection1", PIDCollection, ref var1);
SelectByPID("selection2", PIDCollection, ref var2);
SelectByPID("selection3", PIDCollection, ref var3);
pDisp1 = (object)Part.Extension.GetObjectByPersistReference((var1)); //face
pDisp2 = (object)Part.Extension.GetObjectByPersistReference((var2)); //face
pDisp3 = (object)Part.Extension.GetObjectByPersistReference((var3)); //edge
//theCurrentLoad = LoadAndRestraintMngr.GetEdgeWeldConnector(1, errCode)
//If theCurrentLoad Is Nothing Then ErrorMsg(swApp, "Failed to get edge weld connector")
object[] varArray9 = { pDisp3 };
theNewLoad = LoadAndRestraintMngr.AddEdgeWeldConnector(pDisp1, pDisp2, varArray9, 0, 0, out errCode);
if (errCode != 0)
ErrorMsg(swApp, "Failed to add edge weld connector");
if (theNewLoad == null)
ErrorMsg(swApp, "Failed to add edge weld connector");
errCode = theNewLoad.SetEdgeWeldType(3);
if (errCode != 0)
ErrorMsg(swApp, "Failed to set edge weld type");
EdgeWeldType = theNewLoad.GetEdgeWeldType(out errCode);
if (errCode != 0)
ErrorMsg(swApp, "Failed to get edge weld type");
FirstFace = theNewLoad.GetFirstFace(out errCode);
if (errCode != 0)
ErrorMsg(swApp, "Failed to get Face Set1");
SecondFace = theNewLoad.GetSecondFace(out errCode);
if (errCode != 0)
ErrorMsg(swApp, "Failed to get Face Set2");
errCode = theNewLoad.ReplaceFacesThenAutoGenerateTouchingEdges(0, pDisp1, pDisp2);
if (errCode != 0)
ErrorMsg(swApp, "Failed to replace faces and find touching edges");
object[] var = (object[])theNewLoad.GetEdgeList(out errCode);
if (errCode != 0)
ErrorMsg(swApp, "Failed to get edge list");
object[] varArray = { var[0] };
errCode = theNewLoad.ReplaceFacesAndEdges(0, pDisp1, pDisp2, varArray);
if (errCode != 0)
ErrorMsg(swApp, "Failed to replace faces and edges");
object[] varArray2 = { var[1] };
errCode = theNewLoad.AddEdges(0, varArray2);
if (errCode != 0)
ErrorMsg(swApp, "Failed to add edges");
errCode = theNewLoad.RemoveEdges(0, varArray);
if (errCode != 0)
ErrorMsg(swApp, "Failed to remove edges");
errCode = theNewLoad.SetEdgeWeldType(3);
if (errCode != 0)
ErrorMsg(swApp, "Failed to set edge weld type");
errCode = theNewLoad.SetWeldOrientation(true);
if (errCode != 0)
ErrorMsg(swApp, "Failed to set whether to show weld orientation");
WeldOrientation = theNewLoad.IsWeldOrientation(out errCode);
if (errCode != 0)
ErrorMsg(swApp, "Failed to get whether to show weld orientation");
errCode = theNewLoad.SetInducedBendingMomentIncluded(true);
if (errCode != 0)
ErrorMsg(swApp, "Failed to set whether induced bending moment is included");
InducedBendingMomentIncluded = theNewLoad.IsInducedBendingMomentIncluded(out errCode);
if (errCode != 0)
ErrorMsg(swApp, "Failed to get whether induced bending moment is included");
CodeType = theNewLoad.GetCodeType(out errCode);
if (errCode != 0)
ErrorMsg(swApp, "Failed to get weld code");
//American Standard weld sizing
errCode = theNewLoad.SetCodeType(0);
if (errCode != 0)
ErrorMsg(swApp, "Failed to set American Standard weld code");
errCode = theNewLoad.SetWeldSizingSettingUS(4, 100.1, 4, 1, 1, true, 98, 0);
if (errCode != 0)
ErrorMsg(swApp, "Failed to set American Standard weld sizing");
errCode = theNewLoad.GetWeldSizingSettingUS(out nElectrodeMaterial, out dWeldStrength, out nWeldStrengthUnit, out nFOSOption, out dFOS, out bEstimatedWeldSize, out dEstimatedWeldSize, out nEstimatedWeldSizeUnit);
if (errCode != 0)
ErrorMsg(swApp, "Failed to get American Standard weld sizing");
//European weld sizing
errCode = theNewLoad.SetCodeType(1);
if (errCode != 0)
ErrorMsg(swApp, "Failed to set european weld code");
errCode = theNewLoad.SetWeldSizingSettingEuro(1, 155.2, 4, 1, 1, true, 98, 0);
if (errCode != 0)
ErrorMsg(swApp, "Failed to set european weld sizing");
errCode = theNewLoad.GetWeldSizingSettingEuro(out NWeakMaterial, out DUltimateTensileStrength, out NTensileStrengthUnit, out DCorrelationFactor, out DPartialSafetyFactor, out BIsEstimatedWeldSize, out dEstimatedWeldSize, out nEstimatedWeldSizeUnit);
if (errCode != 0)
ErrorMsg(swApp, "Failed to get european weld sizing");
}
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;
case "selection3":
PIDString = (string)PIDCollection[2];
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;
}
public ArrayList PIDInitializer()
{
ArrayList PIDCollection = new ArrayList();
string selection1 = null;
//Constants
selection1 = "35,29,213,113,218,129,72,162,168,88,152,178,27,137,239,153,189,1,0,0,29,1,0,0,120,1,125,80,203,74,195,80,20,156,250,136,138,10,21,23,130,123,87,65,113,33,88,92,90,43,233,74,73,234,174,16,98,114,19,250,200,131,244,20,221,8,65,252,0,23,254,134,32,253,5,63,200,63,104,156,219,104,40,46,220,220,185,103,206,204,57,119,238,235,33,176,10,160,156,151,60,137,101,3,219,136,211,107,207,87,182,10,93,191,177,160,1,131,184,182,82,21,79,159,111,71,239,221,89,239,23,43,219,129,182,229,105,236,140,164,147,136,51,205,195,110,96,171,204,245,171,182,161,219,182,235,235,13,187,188,119,30,229,230,126,168,124,169,168,61,82,109,71,242,65,18,89,94,18,140,21,233,121,105,182,113,129,62,238,48,129,66,206,179,143,0,15,240,144,32,226,253,138,236,4,35,8,82,100,172,207,113,138,22,206,136,45,86,130,33,249,1,181,130,19,234,198,244,102,156,34,216,42,56,219,88,238,51,217,215,179,105,225,99,150,52,127,18,47,128,186,13,189,37,228,206,41,39,72,221,212,202,99,86,183,47,166,165,63,73,231,106,50,68,47,205,58,65,1";
selection1 = selection1 + "64,234,236,235,197,38,150,117,59,5,240,151,171,220,251,116,95,166,34,105,252,239,128,250,5,248,6,41,242,111,72,0,0,0,0,0,0,0,0";
selection1 = selection1 + ",Type=1";
//Store constants in a collection
PIDCollection.Add(selection1);
string selection2 = null;
//Constants
selection2 = "35,29,213,113,218,129,72,162,168,88,152,178,27,137,239,153,169,1,0,0,22,1,0,0,120,1,133,80,205,74,195,96,16,156,170,173,138,8,21,15,130,103,61,5,196,131,96,241,104,141,164,39,33,169,183,66,136,201,151,208,159,228,11,233,22,189,8,165,190,128,7,95,67,144,190,130,15,228,27,52,206,215,216,210,155,151,157,221,217,217,93,102,223,79,129,109,0,229,162,100,36,150,53,28,32,213,247,65,168,92,21,251,97,109,73,3,117,226,206,86,85,188,126,127,156,127,118,230,221,21,86,99,39,102,172,208,169,55,20,59,19,111,82,196,157,200,85,185,31,86,237,134,105,187,126,104,46,28,50,183,95,228,225,105,160,66,169,168,35,82,109,79,138,126,150,56,65,22,141,20,233,69,105,181,113,131,30,30,49,134,66,193,216,67,132,103,4,200,144,48,191,35,59,198,16,2,141,156,245,53,46,209,194,21,177,197,74,48,32,223,167,86,112,65,221,136,179,57,183,8,246,167,220,221,216,236,211,217,207,204,114,240,53,207,154,127,142,151,64,221,174,185,18,243,230,132,27,100,221,52,74,62,15,103,111,150,99,158,100,124,53,105,162,171,115,59,74,2";
selection2 = selection2 + "12,218,123,125,186,135,77,93,165,60,166,242,86,139,232,244,31,241,234,222,47,181,106,107,249,0,0,0,0,0,0,0,0";
selection2 = selection2 + ",Type=1";
//Store constants in a collection
PIDCollection.Add(selection2);
string selection3 = null;
//Constants
selection3 = "35,29,213,113,218,129,72,162,168,88,152,178,27,137,239,153,225,1,0,0,41,1,0,0,120,1,173,80,77,75,195,64,16,125,181,182,42,42,232,65,4,207,122,10,138,7,193,226,181,86,82,16,148,164,222,10,161,38,155,144,182,249,32,157,162,23,161,168,55,47,30,252,27,130,244,47,248,131,252,7,141,111,187,90,252,1,46,236,204,206,155,55,51,59,239,117,15,168,2,40,103,37,45,125,89,193,58,146,172,21,68,202,81,161,231,87,230,48,80,163,95,214,76,158,135,207,183,131,247,246,180,243,235,77,217,46,203,46,138,44,113,7,210,74,197,29,23,97,59,112,84,238,249,38,93,215,105,199,243,245,132,77,61,225,94,174,110,251,202,23,3,109,19,106,186,82,196,105,100,247,210,96,168,8,207,74,171,137,51,116,113,131,17,20,10,218,46,2,220,161,135,20,17,223,231,68,71,24,64,144,33,103,124,138,99,52,112,66,223,96,36,232,19,143,201,21,28,145,55,100,109,206,46,130,181,9,123,215,255,230,151,128,175,71,203,198,199,52,221,50,75,26,75,222,138,158,18,114,230,152,29,100,145,212,76,138,135,253,39,203,54,34,233,205,118,184,198,165,10,165,";
selection3 = selection3 + "83,196,137,214,112,33,65,109,178,202,79,0,47,207,150,93,157,80,79,198,135,140,175,25,235,114,125,55,126,240,255,224,177,221,252,124,3,77,212,112,103,0,0,0,0,0,0,0,0";
selection3 = selection3 + ",Type=1";
//Store constants in a collection
PIDCollection.Add(selection3);
return PIDCollection;
}
private void ErrorMsg(SldWorks SwApp, string Message)
{
SwApp.SendMsgToUser2(Message, 0, 0);
SwApp.RecordLine("'*** WARNING - General");
SwApp.RecordLine("'*** " + Message);
SwApp.RecordLine("");
}
public SldWorks swApp;
}
}