Hide Table of Contents

Add Force Example (VB.NET)

This example shows how to add a normal force of nonuniform distribution to a static study.

NOTE: To get persistent reference identifiers (PIDs) for model selections, you can use pidcollector.exe or IModelDocExtension::GetPersistReference3.

'---------------------------------------------------------------------------
' 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 install_dir\api\redist\CLR2 folder and
'    select SolidWorks.Interop.cosworks.dll).
' 3. Verify that the assembly exists.
'
' Postconditions:
'  1. Opens the assembly.
'  2. Creates a static study.
'  3. Adds Force-1 to the study.
'  4. To verify, examine the Simulation study tree.
'
' NOTE: Because this assembly is used elsewhere, do not save changes.
'---------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports SolidWorks.Interop.cosworks
Imports System.Runtime.InteropServices
Imports System
 
Partial Class SolidWorksMacro
 
    Sub main()
 
        Dim COSMOSWORKS As Object
        Dim COSMOSObject As CwAddincallback
        Dim ActDoc As CWModelDoc
        Dim StudyMngr As CWStudyManager
        Dim Study As CWStudy
        Dim swModel As ModelDoc2
        Dim swModelDocExt As ModelDocExtension
        Dim LBCMgr As CWLoadsAndRestraintsManager
        Dim CWForce As CWForce
        Dim var1 As Object = Nothing
        Dim oSelect1 As Object
        Dim status As Integer, warnings As Integer
        Dim errCode As Integer
        Dim DistanceValues As Object = Nothing
        Dim ForceValues As Object = Nothing
        Dim data(6) As Double
        Dim PIDCollection As New Collection
 
        ' Initialize PIDs
        PIDCollection = PIDInitializer()
 
        swApp.OpenDoc6("C:\Program Files\SOLIDWORKS Corp\SOLIDWORKS\Simulation\Examples\mixedmesh-1.sldasm", swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", status, warnings)
 
        swModel = swApp.ActiveDoc()
        swModelDocExt = swModel.Extension
 
        COSMOSObject = swApp.GetAddInObject("SldWorks.Simulation")
        If COSMOSObject Is Nothing Then ErrorMsg(swApp, "COSMOSObject object not found")
 
        COSMOSWORKS = COSMOSObject.CosmosWorks
        If COSMOSWORKS Is Nothing Then ErrorMsg(swApp, "COSMOSWORKS object not found")
 
        ' Open and get active document
        ActDoc = COSMOSWORKS.ActiveDoc()
        If ActDoc Is Nothing Then ErrorMsg(swApp, "No active document")
 
        ' Create new static study
        StudyMngr = ActDoc.StudyManager()
        If StudyMngr Is Nothing Then ErrorMsg(swApp, "StudyMngr object not there")
 
        Study = StudyMngr.CreateNewStudy3("Static", swsAnalysisStudyType_e.swsAnalysisStudyTypeStatic, 0, errCode)
        If Study Is Nothing Then ErrorMsg(swApp, "Study not created")
 
        ' Select face of solid
        LBCMgr = Study.LoadsAndRestraintsManager
 
        ' Get PIDs for selection
        SelectByPID("selection1", PIDCollection, var1)
        oSelect1 = swModelDocExt.GetObjectByPersistReference3((var1), status)
        Dim DispArray1 As Object() = {oSelect1}
 
        data(0) = 2.0#
        data(1) = 3.0#
        data(2) = 1.0#
        data(3) = 1.5
        data(4) = 1.0#
        data(5) = 1.0#
        Dim ComponentValues As Object() = {data(0), data(1), data(2), data(3), data(4), data(5)}
 
        ' Add force
        CWForce = LBCMgr.AddForce3(swsForceType_e.swsForceTypeNormal, 0, -1, 0, 0, 0, (DistanceValues), (ForceValues), TrueFalse, 0, 0, 0, 1.0#, (ComponentValues), FalseFalse, (DispArray1), NothingFalse, errCode)
        If errCode <> 0 Then ErrorMsg(swApp, "No force applied")
 
    End Sub
 
    Sub ErrorMsg(ByVal SwApp As SldWorks, ByVal Message As String)
 
        SwApp.SendMsgToUser2(Message, 0, 0)
        SwApp.RecordLine("'*** WARNING - General")
        SwApp.RecordLine("'*** " & Message)
        SwApp.RecordLine("")
 
    End Sub
 
 
    Public Function PIDInitializer() As Collection
        ' Initialize PIDs
        Dim PIDCollection As New Collection
        Dim selection1 As String
        ' Constant
        selection1 = "13,17,0,0,3,0,0,0,255,254,255,27,77,0,105,0,120,0,101,0,100,0,45,0,49,0,45,0,83,0,111,0,108,0,105,0,100,0,45,0,51,0,64,0,109,0,105,0,120,0,101,0,100,0,109,0,101,0,115,0,104,0,45,0,49,0,4,0,0,0,16,0,0,0,1,0,0,0,1,0,0,0,17,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,79,67,0,58,0,92,0,80,0,114,0,111,0,103,0,114,0,97,0,109,0,32,0,70,0,105,0,108,0,101,0,115,0,92,0,83,0,111,0,108,0,105,0,100,0,87,0,111,0,114,0,107,0,115,0,92,0,83,0,111,0,108,0,105,0,100,0,87,0,111,0,114,0,107,0,115,0,32,0,83,0,105,0,109,0,117,0,108,0,97,0,116,0,105,0,111,0,110,0,92,0,69,0,120,0,97,0,109,"
        selection1 = selection1 & "0,112,0,108,0,101,0,115,0,92,0,77,0,105,0,120,0,101,0,100,0,45,0,49,0,45,0,83,0,111,0,108,0,105,0,100,0,46,0,83,0,76,0,68,0,80,0,82,0,84,0,9,128,255,254,255,13,77,0,105,0,120,0,101,0,100,0,45,0,49,0,45,0,83,0,111,0,108,0,105,0,100,0,2,0,0,124,49,104,66,0,0,0,48,0,0,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,0,0,48,0,24,0,0,0,26,50,104,66,4,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,24,0,0,0,26,50,104,66,0,0,0,0,0,0,0,0,3,128,0,0,5,128,8,0,24,0,0,0,26,50,104,66,11,0,0,0,12,128,0,0,5,128,8,0,24,0,0,0,26,50,104,66,1,0,0,0,0,0,0,0,3,128,0,0,5,128,8,0,24,0,0,0,26,50,104,66,1,0,0,0,0,0,0,0,0,0,0,0,0,0"
        ' Store constant in a collection
        PIDCollection.Add(selection1, "selection1")
        ' Pass this back
        PIDInitializer = PIDCollection
    End Function
 
    Private Sub SelectByPID(ByVal PIDName As StringByVal PIDCollection As Collection, ByRef varEntity As Object)
        ' Select by PID
        Dim PID() As Byte
        Dim PIDVariant As Object
        Dim PIDString As String
        Dim i As Integer
        ' Get the string from the collection
        PIDString = ""
        PIDString = PIDCollection.Item(PIDName)
        ' Parse the string into an array
        PIDVariant = Split(PIDString, ",")
        ReDim PID(UBound(PIDVariant))
        ' Change the array to a byte array
        For i = 0 To (UBound(PIDVariant) - 1)
            PID(i) = Convert.ToByte(PIDVariant(i))
        Next i
        varEntity = PID
    End Sub
 
 
    ''' <summary>
    ''' The SldWorks swApp variable is pre-assigned for you.
    ''' </summary>
    Public swApp As SldWorks
 
 
End Class



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:   Add Force Example (VB.NET)
*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) 2016 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.