Hide Table of Contents

Create Nonlinear Study and Apply Materials Example (VBA)

This example shows how to create a nonlinear study and apply user-defined and SOLIDWORKS materials to an assembly's components.

'----------------------------------------------------------------------------

' Preconditions:

' 1. Add the SOLIDWORKS Simulation as an add-in

'    (in SOLIDWORKS, click Tools > Add-ins > SOLIDWORKS Simulation).

' 2. Add the SOLIDWORKS Simulation type library as a reference

'    (in the IDE, click Tools > References > SOLIDWORKS

'    Simulation version type library).

' 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.

'----------------------------------------------------------------------------

Option Explicit

 

    ' Get module handles to get SOLIDWORKS Simualation's executable's folder

    Private Declare Function GetModuleHandle Lib "KERNEL32" Alias "GetModuleHandleA" (ByVal lpModuleName As String) As Long

    Private Declare Function GetModuleFileName Lib "KERNEL32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long

 

Sub main()

 

   Dim SwApp As SldWorks.SldWorks

   Dim COSMOSWORKS As Object

   Dim COSMOSObject As CosmosWorksLib.CwAddincallback

   Dim ActDoc As CosmosWorksLib.CWModelDoc

   Dim StudyMngr As CosmosWorksLib.CWStudyManager

   Dim Study As CosmosWorksLib.CWStudy

   Dim SolidMgr As CosmosWorksLib.CWSolidManager

   Dim SolidComponent As CosmosWorksLib.CWSolidComponent

   Dim SolidBody As CosmosWorksLib.CWSolidBody

   Dim sCWDllFileName As String

   Dim CosmosFolder As String

   Dim CosmosPath As String

   CosmosPath = String(255, 0)

   Dim SName As String

   Dim errors As Long, warnings As Long

   Dim errorCode As Long

   Dim errCode As Long

   Dim ReturnVal As Long

   Dim lngCount As Long

   Dim CompCount As Integer

   Dim j As Integer

   Dim CWMat As CWMaterial

   Dim bApp As Boolean

 

    ' Connect to SOLIDWORKS

    If SwApp Is Nothing Then Set SwApp = Application.SldWorks

    

    ' Get the SOLIDWORKS Simulation object

    Set COSMOSObject = SwApp.GetAddInObject("SldWorks.Simulation")

    If COSMOSObject Is Nothing Then ErrorMsg SwApp, "COSMOSObject object not found.", True

    Set COSMOSWORKS = COSMOSObject.COSMOSWORKS

    If COSMOSWORKS Is Nothing Then ErrorMsg SwApp, "COSMOSWORKS object not found.", True

    

    ' Get path to SOLIDWORKS Simulation folder

    ReturnVal = GetModuleHandle("cosworks.dll")

    lngCount = GetModuleFileName(ReturnVal, CosmosPath, 255)

    CosmosFolder = Left(CosmosPath, InStrRev(CosmosPath, "\"))

    

        

    ' Open and get the active document

    SwApp.OpenDoc6 CosmosFolder + "\Examples\Nonlinear\nl_pipe_holder.sldasm", swDocASSEMBLY, swOpenDocOptions_Silent, "", errors, warnings

    Set ActDoc = COSMOSWORKS.ActiveDoc()

    If ActDoc Is Nothing Then ErrorMsg SwApp, "No active document.", True

    

    ' Create new nonlinear study

    Set StudyMngr = ActDoc.StudyManager()

    If StudyMngr Is Nothing Then ErrorMsg SwApp, "StudyMngr object not there.", True

    Set Study = StudyMngr.CreateNewStudy("NonLinear", swsAnalysisStudyTypeNonlinear, swsMeshTypeSolid, errCode)

    

    If Study Is Nothing Then ErrorMsg SwApp, "Study not created.", True

    

    ' Get number of solid components

    Set SolidMgr = Study.SolidManager

    If SolidMgr Is Nothing Then ErrorMsg SwApp, "SolidManager object not created.", True

    CompCount = SolidMgr.ComponentCount

    Set SolidComponent = SolidMgr.GetComponentAt(0, errorCode)

    If SolidComponent Is Nothing Then ErrorMsg SwApp, "SolidComponent not created.", True

    

    ' Get name of solid component

    SName = SolidComponent.ComponentName

    

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

    Set 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

    Set 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)

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

    Call CWMat.SetPropertyByName("GXY", 79000000000#, 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

    Set SolidBody = Nothing

    Set SolidComponent = Nothing

    For j = 1 To (CompCount - 1)

        Set SolidComponent = SolidMgr.GetComponentAt(j, errorCode)

        SName = SolidComponent.ComponentName

        Set 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

        Set SolidBody = Nothing

    Next j

 

End Sub

 

' Error function

Function ErrorMsg(SwApp As Object, Message As String, EndTest As Boolean)

    SwApp.SendMsgToUser2 Message, 0, 0

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

    SwApp.RecordLine "'*** " & Message

    SwApp.RecordLine ""

    If EndTest Then

    End If

End Function

 



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:   Create Nonlinear Study and Apply Materials Example (VBA)
*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) 2015 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.