Hide Table of Contents

Create Nonlinear Study and Apply Materials Example (VB.NET)

This example shows how to create a nonlinear study and apply user-defined and SolidWorks materials to assembly components.


' 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. Modify the path to solidworks materials.sldmat if needed.

' 4. Run the macro.


' Postconditions:

' 1. Assembly opened.

' 2. A nonlinear study created.

' 3. User-defined material applied to the first component, and

'    SolidWorks material applied to the remaining components.

' 4. To verify, expand the Parts folder in the Simulation Study tree,

'    which is located beneath the FeatureManager design tree, and

'    observe the names of the materials applied to Holder-1 and Pipe-1.


' NOTE: Because the assembly document is used with

' a SolidWorks Simulation online tutorial, do not save

' any changes when closing the document.


Imports SolidWorks.Interop.sldworks

Imports SolidWorks.Interop.swconst

Imports SolidWorks.Interop.cosworks

Imports System

Imports System.Diagnostics

Imports System.Windows.Forms


Partial Class SolidWorksMacro


    Public Sub main()


        Dim COSMOSWORKS As Object

        Dim COSMOSObject As CwAddincallback

        Dim ActDoc As CWModelDoc

        Dim StudyMngr As CWStudyManager

        Dim Study As CWStudy

        Dim SolidMgr As CWSolidManager

        Dim SolidComponent As CWSolidComponent

        Dim SolidBody As CWSolidBody

        Dim SName As String

        Dim errors As Long, warnings As Long

        Dim errorCode As Integer

        Dim errCode As Long

        Dim CompCount As Integer

        Dim j As Integer

        Dim CWMat As CWMaterial

        Dim bApp As Boolean


        ' Get the SolidWorks Simulation object

        COSMOSObject = swApp.GetAddInObject("SldWorks.Simulation")

        If COSMOSObject Is Nothing Then ErrorMsg(swApp, "COSMOSObject object not found.", True)


        If COSMOSWORKS Is Nothing Then ErrorMsg(swApp, "COSMOSWORKS object not found.", True)


        ' Open and get the active document

        swApp.OpenDoc6("C:\Program Files\SolidWorks Corp\SolidWorks\Simulation\Examples\Nonlinear\nl_pipe_holder.sldasm", swDocumentTypes_e.swDocASSEMBLY, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)

        ActDoc = COSMOSWORKS.ActiveDoc()

        If ActDoc Is Nothing Then ErrorMsg(swApp, "No active document.", True)


        ' Create new nonlinear study

        StudyMngr = ActDoc.StudyManager()

        If StudyMngr Is Nothing Then ErrorMsg(swApp, "StudyMngr object not there.", True)

        Study = StudyMngr.CreateNewStudy("Nonlinear", swsAnalysisStudyType_e.swsAnalysisStudyTypeNonlinear, swsMeshType_e.swsMeshTypeSolid, errCode)

        If Study Is Nothing Then ErrorMsg(swApp, "Study not created.", True)


        ' Get number of solid components

        SolidMgr = Study.SolidManager

        If SolidMgr Is Nothing Then ErrorMsg(swApp, "SolidMgr object not created.", True)

        CompCount = SolidMgr.ComponentCount

        SolidComponent = SolidMgr.GetComponentAt(0, errorCode)

        If SolidComponent Is Nothing Then ErrorMsg(swApp, "Solid component not created.", True)


        ' Get name of solid component

        SName = SolidComponent.ComponentName


        ' Apply user-defined material to the first component in the tree

        SolidBody = SolidComponent.GetSolidBodyAt(0, errCode)

        If errCode <> 0 Then ErrorMsg(swApp, "No solid body.", True)

        If SolidBody Is Nothing Then ErrorMsg(swApp, "SolidBody not created.", True)

        CWMat = SolidBody.GetDefaultMaterial

        CWMat.MaterialUnits = 0

        If CWMat Is Nothing Then ErrorMsg(swApp, "No default material object.", True)

        CWMat.MaterialName = "Alloy Steel"

        Call CWMat.SetPropertyByName("EX", 210000000000.0#, 0)

        Call CWMat.SetPropertyByName("NUXY", 0.28, 0)

        Call CWMat.SetPropertyByName("GXY", 79000000000.0#, 0)

        Call CWMat.SetPropertyByName("DENS", 7700, 0)

        Call CWMat.SetPropertyByName("SIGXT", 723825600, 0)

        Call CWMat.SetPropertyByName("SIGYLD", 620422000, 0)

        Call CWMat.SetPropertyByName("ALPX", 0.000013, 0)

        Call CWMat.SetPropertyByName("KX", 50, 0)

        Call CWMat.SetPropertyByName("C", 460, 0)

        errCode = SolidBody.SetSolidBodyMaterial(CWMat)

        If errCode <> 0 Then ErrorMsg(swApp, "Failed to apply material.", True)


        ' Apply SolidWorks library material to rest of components

        SolidBody = Nothing

        SolidComponent = Nothing

        For j = 1 To (CompCount - 1)

            SolidComponent = SolidMgr.GetComponentAt(j, errorCode)

            SName = SolidComponent.ComponentName

            SolidBody = SolidComponent.GetSolidBodyAt(0, errCode)

            If errCode <> 0 Then ErrorMsg(swApp, "No solid body", True)

            bApp = SolidBody.SetLibraryMaterial("C:\Program Files\SolidWorks Corp\SolidWorks\lang\english\sldmaterials\solidworks materials.sldmat", "Gray Cast Iron (SN)")

            If bApp = False Then ErrorMsg(swApp, "No material applied.", True)

            SolidBody = Nothing

        Next j


    End Sub


    'Error routine

    Sub ErrorMsg(ByVal SwApp As Object, ByVal Message As String, ByVal EndTest As Boolean)

        SwApp.SendMsgToUser2(Message, 0, 0)

        SwApp.RecordLine("'*** WARNING - General")

        SwApp.RecordLine("'*** " & Message)


        If EndTest Then

        End If

    End Sub


    ''' <summary>

    ''' The SldWorks swApp variable is pre-assigned for you.

    ''' </summary>

    Public swApp As SldWorks.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

Subject:   Feedback on Help Topics
Page:   Create Nonlinear Study and Apply Materials Example (VB.NET)
*   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:


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

Web Help Content Version: API Help (English only) 2014 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.