Add Prescribed Displacement Example (VBA)
This example shows how to add a prescribed displacement to a study to create a fixture.
'----------------------------------------------------------------------------
' Preconditions:
' 1. Add the SOLIDWORKS Simulation as an add-in (in SOLIDWORKS, click
' Tools > Add-ins > SOLIDWORKS Simulation > OK).
' 2. Add the SOLIDWORKS Simulation type library as a reference (in the IDE,
' click Tools > References > SOLIDWORKS
Simulation version type library).
' 3. Ensure that the specified model exists.
' 4. Ensure that the specified material library exists.
'
' Postconditions:
' 1. Opens the model document.
' 2. Creates a static study.
' 3. Adds materials.
' 4. Adds a prescribed displacement using reference geometry.
' 5. Creates Reference Geometry-1 in the Simulation study tree.
' 6. Meshes the model.
' 7. Analyzes the study.
'
' NOTE: Because the model is used elsewhere, do not save changes.
' ---------------------------------------------------------------------------
Option Explicit
Dim SwApp
As SldWorks.SldWorks
Dim Part As
SldWorks.ModelDoc2
Dim COSMOSWORKS As CosmosWorksLib.COSMOSWORKS
Dim CWAddinCallBack As CosmosWorksLib.CWAddinCallBack
Dim ActDoc As
CosmosWorksLib.CWModelDoc
Dim StudyMngr As
CosmosWorksLib.CWStudyManager
Dim Study As
CosmosWorksLib.CWStudy
Dim SolidMgr As
CosmosWorksLib.CWSolidManager
Dim SolidComp As
CosmosWorksLib.CWSolidComponent
Dim SolidBody As CosmosWorksLib.CWSolidBody
Dim LBCMgr As
CosmosWorksLib.CWLoadsAndRestraintsManager
Dim CWFeatObj As CosmosWorksLib.CWRestraint
Dim strMaterialLib As String
Dim errCode As Long
Dim boolstatus As Boolean
Dim longstatus As Long, longwarnings As Long
Dim StaticOptions As CosmosWorksLib.CWStaticStudyOptions
Dim DispArray1 As Variant, DispArray2 As Variant
Const MeshEleSize As Double = 4.48279654351123
Const MeshTol As Double = 0.224139827175561
Const Tol1 As Double =
0.05
Sub main()
Dim PIDCollection As New Collection
Set PIDCollection = PIDInitializer()
Set SwApp = Application.SldWorks
If SwApp Is Nothing Then Exit Sub
Set Part = SwApp.OpenDoc6("C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS
2019\samples\Simulation Examples\Thermal\computer_chip.sldasm",
swDocASSEMBLY, 2, "", longstatus, longwarnings)
If Part Is Nothing Then ErrorMsg SwApp, "Failed to open
computer_chip.sldasm"
strMaterialLib = SwApp.GetExecutablePath
& "\lang\english\sldmaterials\solidworks materials.sldmat"
Set CWAddinCallBack = SwApp.GetAddInObject("CosmosWorks.CosmosWorks")
If CWAddinCallBack Is Nothing Then ErrorMsg SwApp, "Failed to
get Simulation add-in"
Set COSMOSWORKS = CWAddinCallBack.COSMOSWORKS
If COSMOSWORKS Is Nothing Then ErrorMsg SwApp, "Failed to get
COSMOSWORKS"
Set ActDoc = COSMOSWORKS.ActiveDoc()
If ActDoc Is Nothing Then ErrorMsg SwApp, "Failed to get
active document"
Set StudyMngr = ActDoc.StudyManager()
If StudyMngr Is Nothing Then ErrorMsg SwApp, "Failed to get
study manager"
Set Study = StudyMngr.CreateNewStudy3("Static_solid_new",
swsAnalysisStudyTypeStatic, 0, errCode)
If Study Is Nothing Then ErrorMsg SwApp, "Failed to create
study"
'Add materials
Set SolidMgr = Study.SolidManager
If SolidMgr Is Nothing Then ErrorMsg SwApp, "Failed to get
solid manager"
Set SolidComp = SolidMgr.GetComponentAt(0, errCode)
If errCode <> 0 Then ErrorMsg SwApp, "Failed to get
component"
Set SolidBody = SolidComp.GetSolidBodyAt(0, errCode)
If errCode <> 0 Then ErrorMsg SwApp, "Failed to get solid
body"
boolstatus = SolidBody.SetLibraryMaterial(strMaterialLib,
"AISI 1020")
If boolstatus = False Then ErrorMsg SwApp, "Failed to apply
material"
Set SolidComp = SolidMgr.GetComponentAt(1,
errCode)
If errCode <> 0 Then ErrorMsg SwApp, "Failed to get
component"
Set SolidBody = SolidComp.GetSolidBodyAt(0, errCode)
If errCode <> 0 Then ErrorMsg SwApp, "Failed to get solid
body"
boolstatus = SolidBody.SetLibraryMaterial(strMaterialLib,
"AISI 1020")
If boolstatus = False Then ErrorMsg SwApp, "Failed to apply
material"
Set SolidComp = SolidMgr.GetComponentAt(2,
errCode)
If errCode <> 0 Then ErrorMsg SwApp, "Failed to get
component"
Set SolidBody = SolidComp.GetSolidBodyAt(0, errCode)
If errCode <> 0 Then ErrorMsg SwApp, "Failed to get solid
body"
boolstatus = SolidBody.SetLibraryMaterial(strMaterialLib,
"AISI 1020")
If boolstatus = False Then ErrorMsg SwApp, "Failed to apply
material"
Set SolidComp = SolidMgr.GetComponentAt(3,
errCode)
If errCode <> 0 Then ErrorMsg SwApp, "Failed to get
component"
Set SolidBody = SolidComp.GetSolidBodyAt(0, errCode)
If errCode <> 0 Then ErrorMsg SwApp, "Failed to get solid
body"
boolstatus = SolidBody.SetLibraryMaterial(strMaterialLib,
"AISI 1020")
If boolstatus = False Then ErrorMsg SwApp, "Failed to apply
material"
Set SolidComp = SolidMgr.GetComponentAt(4,
errCode)
If errCode <> 0 Then ErrorMsg SwApp, "Failed to get
component"
Set SolidBody = SolidComp.GetSolidBodyAt(0, errCode)
If errCode <> 0 Then ErrorMsg SwApp, "Failed to get solid
body"
boolstatus = SolidBody.SetLibraryMaterial(strMaterialLib,
"AISI 1020")
If boolstatus = False Then ErrorMsg SwApp, "Failed to apply
material"
Set LBCMgr = Study.LoadsAndRestraintsManager
If LBCMgr Is Nothing Then ErrorMsg SwApp, "Failed to get
loads and restraints manager"
'Add prescribed displacement
DispArray1 = Array(SelectByPID(Part, "selection1",
PIDCollection))
DispArray2 = Array(0#, 0.01, 0#, 0, 1, 0)
Set CWFeatObj = LBCMgr.AddPrescribedDisplacement((DispArray2),
0, (DispArray1), SelectByPID(Part, "selection7", PIDCollection), errCode)
If errCode <> 0 Then ErrorMsg SwApp, "Failed to create
prescribed displacement"
'Create mesh
Dim CWMeshObj As CosmosWorksLib.CWMesh
Set CWMeshObj = Study.Mesh
If CWMeshObj Is Nothing Then ErrorMsg SwApp, "Failed to
create mesh object"
CWMeshObj.MesherType = 0
CWMeshObj.Quality = 0
errCode = Study.CreateMesh(0, MeshEleSize, MeshTol)
If errCode <> 0 Then ErrorMsg SwApp, "Failed to create mesh"
Set CWMeshObj = Nothing
'Set static study options
Set StaticOptions = Study.StaticStudyOptions
If StaticOptions Is Nothing Then ErrorMsg SwApp, "Failed to
get static study options"
StaticOptions.SolverType = 1
StaticOptions.UseSoftSpring = 1
StaticOptions.LargeDisplacement = 0
'Run analysis
errCode = Study.RunAnalysis
If errCode <> 0 Then ErrorMsg SwApp, "Analysis failed with
error as defined in swsRunAnalysisError_e: " & errCode
End Sub
Sub ErrorMsg(SwApp As SldWorks.SldWorks, Message As String)
SwApp.SendMsgToUser2 Message, 0, 0
SwApp.RecordLine "'*** WARNING - General"
SwApp.RecordLine "'*** " & Message
SwApp.RecordLine ""
End Sub
Function SelectByPID(Part As SldWorks.ModelDoc2, PIDName As
String, PIDCollection As Collection) As Object
Dim PID()
As Byte
Dim PIDVariant As Variant
Dim PIDString As String
Dim i
As Integer
Dim SelObj As Object
PIDString = ""
PIDString = PIDCollection.Item(PIDName)
PIDVariant = Split(PIDString, ",")
ReDim PID(UBound(PIDVariant))
For i = 0 To (UBound(PIDVariant) - 1)
PID(i) = PIDVariant(i)
Next i
Set SelObj = Part.Extension.GetObjectByPersistReference((PID))
Set SelectByPID = SelObj
Set SelObj = Nothing
End Function
Function PIDInitializer() As Collection
Dim PIDCollection As New Collection
Dim selection1 As String
Dim selection2 As String
Dim selection3 As String
Dim selection4 As String
Dim selection5 As String
Dim selection6 As String
Dim selection7 As String
Dim selection8 As String
selection1 =
"35,29,213,113,218,129,72,162,168,88,152,178,27,137,239,153,46,2,0,0,75,1,0,0,120,1,109,81,193,74,195,64,20,156,90,173,150,170,88,168,136,226,213,163,130,87,177,160,164,21,11,74,37,233,177,16,210,77,162,213,38,45,233,6,122,17,250,9,30,60,244,39,60,232,47,248,7,234,247,52,206,107,26,80,235,194,190,221,157,55,239,61,102,246,105,15,200,3,72,166,201,174,133,24,29,12,161,17,193,97,244,112,136,99,156,65,161,143,0,3,102,5,139,96,19,185,67,151,200,50,43,183,184,115,243,93,146,78,73,14,37,4,253,11,71,121,166,231,219,74,146,178,10,18,100,22,215,227,251,243,193,75,227,173,149,157,105,217,142,148,69,253,192,122,208,245,80,91,113,228,55,92,211,27,216,42,77,23,36,109,218,74,38,108,240,94,31,233,102,231,222,83,58,133,202,132,12,75,71,221,240,246,210,9,221,158,71,120,154,156,26,56,65,27,6,154,176,112,61,139,53,234,81,212,217,70,29,35,42,21,109,61,42,27,82,153,207,92,68,118,60,243,65,116,139,98,225,182,168,89,238,1,43,122,124,47,186,117,68,236,10,53,220,192,36,187,56,230,244,226,34,107,9,"
selection1 = selection1 &
"120,253,156,84,241,177,185,127,158,89,51,255,129,213,26,39,248,236,31,115,130,78,157,98,20,102,133,231,246,215,164,202,234,153,195,21,209,31,186,98,242,47,159,86,198,107,200,184,89,131,252,24,248,137,203,212,245,127,48,225,255,229,206,255,75,82,92,223,103,242,125,74,0,0,0,0,0,0,0,0"
selection1 = selection1 & ",Type=1"
selection2 =
"35,29,213,113,218,129,72,162,168,88,152,178,27,137,239,153,12,2,0,0,64,1,0,0,120,1,93,81,205,74,195,64,24,156,106,173,138,255,16,16,164,87,143,122,18,4,65,80,146,136,5,165,146,244,40,132,184,73,180,154,164,37,221,64,47,133,60,130,7,15,190,132,23,95,193,71,240,125,26,231,75,12,104,23,118,55,59,223,55,179,153,217,215,3,96,25,64,57,47,13,19,79,24,98,140,35,156,224,2,10,35,36,60,229,208,8,145,193,35,82,215,219,236,223,229,108,253,206,29,225,151,45,108,32,25,93,249,42,116,194,200,83,82,148,209,145,69,110,224,152,125,189,29,126,244,62,7,205,94,211,12,210,236,52,16,166,155,103,81,47,112,194,177,167,234,90,71,36,29,79,137,252,150,244,77,117,255,225,57,84,186,134,246,8,153,174,206,134,233,227,181,159,6,113,72,120,94,158,154,56,195,61,76,244,225,226,182,90,45,154,81,152,16,181,49,133,95,25,139,105,107,66,91,17,107,25,187,115,158,116,101,90,236,74,239,128,134,229,59,33,35,174,20,235,0,142,169,123,3,11,119,112,216,179,94,240,206,118,19,222,18,208,253,126,63,135,189,221,189,108,50,"
selection2 = selection2 &
"32,38,1,175,90,84,139,168,149,83,77,87,137,200,34,157,6,247,130,180,134,33,126,247,197,123,54,74,220,23,109,167,250,95,52,43,197,26,26,138,188,198,102,1,252,197,36,239,69,76,126,98,17,107,174,99,137,227,7,37,71,119,15,0,0,0,0,0,0,0,0"
selection2 = selection2 & ",Type=1"
selection3 =
"35,29,213,113,218,129,72,162,168,88,152,178,27,137,239,153,12,2,0,0,64,1,0,0,120,1,93,81,205,74,195,64,24,156,106,173,138,85,20,2,130,244,234,81,193,147,32,8,74,26,177,160,84,146,30,133,16,55,137,86,243,83,210,13,244,82,200,35,120,240,224,75,120,241,21,124,4,223,167,113,190,196,128,118,97,119,179,243,125,51,155,153,125,61,0,86,1,148,139,210,48,241,132,49,38,56,194,9,46,160,144,34,230,41,135,70,128,12,46,145,186,222,102,255,46,103,235,119,118,133,95,182,176,133,56,189,242,84,96,7,161,171,164,40,163,35,139,220,192,49,255,122,59,252,24,124,142,154,189,166,25,164,89,137,47,76,39,207,194,129,111,7,19,87,213,181,142,72,218,174,18,249,109,233,155,233,225,195,115,160,116,13,237,17,50,29,157,141,147,199,107,47,241,163,128,240,162,60,53,113,134,123,152,24,194,193,109,181,246,105,70,97,74,212,194,12,94,101,44,162,173,41,109,133,172,101,236,206,121,210,149,105,177,43,189,35,26,150,239,152,140,168,82,172,3,56,166,238,13,250,184,131,205,158,205,130,119,182,155,240,86,128,222,247,251"
selection3 = selection3 &
",57,172,157,222,101,147,1,49,9,120,189,79,181,144,90,57,213,116,149,136,44,210,105,112,47,72,107,24,226,119,95,188,103,105,236,188,104,43,209,255,162,89,43,54,208,80,228,53,186,5,240,23,147,188,151,49,249,137,101,172,185,142,37,142,31,26,13,119,9,0,0,0,0,0,0,0,0"
selection3 = selection3 & ",Type=1"
selection4 =
"35,29,213,113,218,129,72,162,168,88,152,178,27,137,239,153,12,2,0,0,65,1,0,0,120,1,93,81,205,74,195,64,24,156,106,173,138,90,20,2,130,244,234,81,47,30,4,65,80,210,136,5,165,146,244,40,132,184,73,180,154,159,146,110,160,151,66,30,193,131,7,95,194,139,175,224,35,248,62,141,243,37,6,180,11,187,155,157,239,155,217,204,236,235,1,176,10,160,92,148,134,137,39,140,49,193,17,78,112,1,133,20,49,79,57,52,2,100,112,137,212,245,54,251,119,57,91,191,179,43,252,178,133,45,196,233,149,167,2,59,8,93,37,69,25,29,89,228,6,142,249,215,219,225,199,224,115,212,236,53,205,32,205,74,124,97,58,121,22,14,124,59,152,184,170,174,117,68,210,118,149,200,239,72,223,76,15,31,158,3,165,107,104,143,144,233,232,108,156,60,94,123,137,31,5,132,23,229,169,137,51,220,195,196,16,14,110,171,181,79,51,10,83,162,22,102,240,42,99,17,109,77,105,43,100,45,99,119,206,147,174,76,139,93,233,29,209,176,124,199,100,68,149,98,29,192,49,117,111,208,199,29,108,246,108,22,188,179,221,132,183,2,244,190,223,207,97,117,123,15"
selection4 = selection4 &
"1,77,6,196,36,224,245,62,213,66,106,229,84,211,85,34,178,72,167,193,189,32,173,97,136,223,125,241,158,165,177,243,162,173,68,255,139,102,173,216,64,67,145,215,216,46,128,191,152,228,189,140,201,79,44,99,205,117,44,113,252,0,33,137,119,13,0,0,0,0,0,0,0,0"
selection4 = selection4 & ",Type=1"
selection5 =
"35,29,213,113,218,129,72,162,168,88,152,178,27,137,239,153,12,2,0,0,65,1,0,0,120,1,93,81,205,74,195,64,24,156,106,173,138,86,20,2,130,244,234,81,15,94,4,65,80,210,136,5,165,146,244,40,132,184,73,180,154,159,146,110,160,151,66,30,193,131,7,95,194,139,175,224,35,248,62,141,243,37,6,180,11,187,155,157,239,155,217,204,236,235,1,176,10,160,92,148,134,137,39,140,49,193,17,78,112,1,133,20,49,79,57,52,2,100,112,137,212,245,54,251,119,57,91,191,179,43,252,178,133,45,196,233,149,167,2,59,8,93,37,69,25,29,89,228,6,142,249,215,219,225,199,224,115,212,236,53,205,32,205,74,124,97,58,121,22,14,124,59,152,184,170,174,117,68,210,118,149,200,119,165,111,166,135,15,207,129,210,53,180,71,200,116,116,54,78,30,175,189,196,143,2,194,139,242,212,196,25,238,97,98,8,7,183,213,218,167,25,133,41,81,11,51,120,149,177,136,182,166,180,21,178,150,177,59,231,73,87,166,197,174,244,142,104,88,190,99,50,162,74,177,14,224,152,186,55,232,227,14,54,123,54,11,222,217,110,194,91,1,122,223,239,231,176,118,122,"
selection5 = selection5 &
"151,77,6,196,36,224,245,62,213,66,106,229,84,211,85,34,178,72,167,193,189,32,173,97,136,223,125,241,158,165,177,243,162,173,68,255,139,102,173,216,64,67,145,215,216,46,128,191,152,228,189,140,201,79,44,99,205,117,44,113,252,0,29,203,119,11,0,0,0,0,0,0,0,0"
selection5 = selection5 & ",Type=1"
selection6 =
"35,29,213,113,218,129,72,162,168,88,152,178,27,137,239,153,42,2,0,0,73,1,0,0,120,1,101,81,205,74,195,96,16,156,250,83,45,85,177,80,17,197,171,71,5,175,162,160,36,21,11,74,37,233,177,16,218,175,137,86,155,164,164,95,160,23,33,143,224,193,131,47,225,65,95,193,55,80,159,167,113,182,73,65,205,66,246,75,102,119,118,51,243,61,237,2,139,0,210,105,186,99,35,70,15,99,104,68,232,50,187,56,192,17,206,160,16,194,199,136,85,193,34,56,68,238,48,32,178,68,230,38,159,82,254,84,101,82,90,66,21,126,120,209,85,174,229,122,142,146,162,68,89,146,236,98,60,126,60,239,191,54,223,219,243,51,163,213,73,107,4,125,97,218,113,228,53,251,150,59,114,84,86,43,203,72,203,81,50,126,93,250,38,186,213,187,119,149,206,160,26,33,195,214,209,32,184,189,236,6,253,161,75,120,154,158,24,56,70,7,6,90,176,113,61,203,38,197,40,138,236,160,129,9,101,138,176,33,101,141,41,203,99,45,98,119,60,51,65,68,139,92,233,109,83,176,188,251,100,12,249,93,180,234,144,216,21,76,220,192,98,119,37,225,246,74,177,107,1,120,251,12"
selection6 = selection6 &
"2,57,197,231,198,222,249,220,151,220,254,21,147,27,60,206,143,185,65,103,54,49,75,103,157,231,214,55,105,121,136,7,219,226,71,20,250,246,131,110,4,250,143,93,203,201,42,230,20,185,161,181,4,248,141,201,226,255,24,127,172,128,229,119,197,138,196,15,139,128,125,95,0,0,0,0,0,0,0,0"
selection6 = selection6 & ",Type=1"
selection7 =
"35,29,213,113,218,129,72,162,168,88,152,178,27,137,239,153,12,2,0,0,65,1,0,0,120,1,93,81,203,74,195,64,20,61,213,90,21,223,80,16,164,91,151,186,18,4,65,80,146,136,5,165,146,116,41,132,58,157,104,53,143,146,78,160,27,33,159,224,194,133,63,225,198,95,240,19,252,159,198,115,147,6,180,23,102,38,115,238,61,231,230,220,121,59,0,150,1,20,179,162,109,225,9,35,140,113,132,19,92,64,33,65,196,91,6,3,141,20,62,145,42,223,100,253,46,87,99,190,118,132,95,52,176,129,40,185,26,40,237,234,192,87,146,148,104,201,38,29,24,175,223,239,135,159,221,175,126,125,86,180,54,105,78,60,20,166,151,165,65,119,232,234,177,175,170,92,75,36,93,95,137,252,150,212,77,77,239,225,89,43,83,65,123,132,44,207,164,163,248,241,122,16,15,67,77,120,86,156,90,56,195,61,44,244,224,225,182,220,109,154,81,152,16,117,48,197,160,52,22,210,214,132,182,2,230,82,86,103,188,153,210,180,216,149,218,62,13,203,119,68,70,88,42,86,3,56,166,238,13,108,220,193,101,205,122,206,158,205,122,120,75,64,231,231,227,28,206,118,231,178,"
selection7 = selection7 &
"158,1,49,25,240,170,77,181,128,90,25,213,76,57,17,217,164,178,205,51,23,218,60,196,239,190,120,79,147,200,123,49,78,108,254,141,102,37,95,67,77,145,215,216,204,129,191,152,52,94,196,228,39,22,177,249,187,48,35,241,11,36,122,119,14,0,0,0,0,0,0,0,0"
selection7 = selection7 & ",Type=1"
selection8 =
"35,29,213,113,218,129,72,162,168,88,152,178,27,137,239,153,12,2,0,0,65,1,0,0,120,1,93,81,193,74,195,64,20,156,106,173,138,85,20,2,130,244,234,81,193,147,32,8,74,18,177,160,84,146,30,133,80,183,27,173,54,73,73,55,208,139,144,79,240,224,193,159,240,226,47,248,9,254,79,227,188,164,1,237,131,221,205,206,123,51,47,243,246,237,0,88,5,80,204,11,203,198,19,70,152,224,8,39,184,128,66,130,136,183,12,6,26,41,2,34,85,190,201,250,93,174,198,98,181,133,95,52,176,133,40,185,26,40,237,233,48,80,146,148,104,201,38,29,24,175,223,239,135,159,221,175,126,125,86,52,139,52,55,30,10,211,207,210,176,59,244,244,36,80,85,174,37,146,94,160,68,126,91,234,102,166,247,240,172,149,169,160,61,66,182,111,210,81,252,120,61,136,135,99,77,120,94,156,218,56,195,61,108,244,224,227,182,220,29,154,81,152,18,117,49,195,160,52,54,166,173,41,109,133,204,165,172,206,120,51,165,105,177,43,181,125,26,150,239,136,140,113,169,88,13,224,152,186,55,112,112,7,143,53,155,57,123,54,235,225,173,0,157,159,143,115,184,59,15"
selection8 = selection8 &
"7,203,122,6,196,100,192,235,14,213,66,106,101,84,51,229,68,100,147,74,139,103,46,180,69,136,223,125,241,158,38,145,255,98,220,216,252,27,205,90,190,129,154,34,175,209,206,129,191,152,52,94,198,228,39,150,177,197,187,48,35,241,11,25,64,119,8,0,0,0,0,0,0,0,0"
selection8 = selection8 & ",Type=1"
PIDCollection.Add selection1, "selection1"
PIDCollection.Add selection2, "selection2"
PIDCollection.Add selection3, "selection3"
PIDCollection.Add selection4, "selection4"
PIDCollection.Add selection5, "selection5"
PIDCollection.Add selection6, "selection6"
PIDCollection.Add selection7, "selection7"
PIDCollection.Add selection8, "selection8"
Set PIDInitializer = PIDCollection
End Function