Hide Table of Contents

Apply Mesh Control (VBA)

This example shows how to get and set beams and joints, create a mesh, and apply a mesh control.

'----------------------------------------------------------------------------
' 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 the part document and
'    solidworks materials.sldmat as needed.
' 4. Open the Immediate window.
'
' Postconditions:
' 1. A static study, frame, is created.
' 2. Beam information is shown in the Immediate window.
' 3. Plain Carbon Steel material is applied to all beams.
' 4. Joints are calculated for all beams, and
'    a neutral axis is shown for each beam. The pinball tolerance
'    value and unit are shown in the Immediate window.
' 5. A mixed mesh is created, and its type and state are shown in
'    the Immediate window.
'
'    NOTE: Step 5 can take several minutes to complete.
'
' 6. A mesh control is applied, and its various values are shown
'    in the Immediate window.
'
' NOTES:
' *  Beam elements are created by default for parts with
'    structural members.
' *  Because the part document is used elsewhere, do not
'    save any changes when closing it.
'----------------------------------------------------------------------------
Option Explicit
 
Sub main()
   Dim SwApp As SldWorks.SldWorks
   Dim swModel As SldWorks.ModelDoc2
   Dim swModelDocExt As SldWorks.ModelDocExtension
   Dim swSelMgr As SldWorks.SelectionMgr
   Dim COSMOSWORKS As CosmosWorksLib.COSMOSWORKS
   Dim COSMOSObject As CosmosWorksLib.CwAddincallback
   Dim ActDoc As CosmosWorksLib.CWModelDoc
   Dim StudyMngr As CosmosWorksLib.CWStudyManager
   Dim Study As CosmosWorksLib.CWStudy
   Dim BeamMgr As CosmosWorksLib.CWBeamManager
   Dim BeamBody As CosmosWorksLib.CWBeamBody
   Dim Joints As CosmosWorksLib.CWJoints
   Dim Mesh As CosmosWorksLib.CWMesh
   Dim MeshControl As CosmosWorksLib.CWMeshControl
   Dim nbrBeamBodies As Long
   Dim beamBodyType As Long
   Dim ElementSize As Double
   Dim Tolerance As Double
   Dim errors As Long, warnings As Long
   Dim errCode As Long
   Dim j As Integer
   Dim bApp As Boolean
   Dim keepJointUpdates As Boolean
   Dim status As Boolean
   Dim selEntity As Object
   Dim selEntities As Variant  
    ' 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
    
    'Open and get the active document
    Set swModel = SwApp.OpenDoc6("c:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\weldments\weldment_box2.sldprt", swDocPART, swOpenDocOptions_Silent, "", errors, warnings)
    Set swModelDocExt = swModel.Extension
    Set swSelMgr = swModel.SelectionManager
    Set ActDoc = COSMOSWORKS.ActiveDoc()
    If ActDoc Is Nothing Then ErrorMsg SwApp, "No active document.", True
    'Create new static study named frame
    Set StudyMngr = ActDoc.StudyManager()
    If StudyMngr Is Nothing Then ErrorMsg SwApp, "StudyMngr object not there.", True
    Set Study = StudyMngr.CreateNewStudy2("frame", swsAnalysisStudyTypeStatic, errCode)
    If Study Is Nothing Then ErrorMsg SwApp, "Study not created.", True
    ' Get and set beam info
    Set BeamMgr = Study.BeamManager
    nbrBeamBodies = BeamMgr.BeamCount
    Debug.Print "Beams..."
    Debug.Print "  Number of beams: " & nbrBeamBodies
    Set BeamBody = Nothing
    For j = 0 To (nbrBeamBodies - 1)
        Set BeamBody = BeamMgr.GetBeamBodyAt(j, errCode)
        If errCode <> 0 Then ErrorMsg SwApp, "No beam body.", True
        Debug.Print "    Name of beam body: " & BeamBody.BeamBodyName
        beamBodyType = BeamBody.BeamType
        If beamBodyType = 0 Then
            Debug.Print "      Type of beam body: beam"
        Else
            Debug.Print "      Type of beam body: truss"
        End If
        bApp = BeamBody.SetLibraryMaterial("C:\Program Files\SolidWorks Corp\SolidWorks\lang\english\sldmaterials\solidworks materials.sldmat", "Plain Carbon Steel")
        If bApp = False Then ErrorMsg SwApp, "No material applied.", True
        Set BeamBody = Nothing
    Next j
        
    ' Calculate joints
    Set Joints = BeamMgr.GetJointGroup(errCode)
    Debug.Print " "
    Debug.Print "Joints..."
    If errCode <> 0 Then ErrorMsg SwApp, "No joint group.", True
    Joints.JointsBeginEdit
    Joints.IncludeAllSelectedBeam = True
    Joints.IncludeDisplayNeutralAxis = True
    Joints.CalculateJoints
    Joints.JointsEndEdit
    keepJointUpdates = Joints.IncludeKeepModifiedJointOnUpdate
    If (keepJointUpdates = True) Then
        Debug.Print ("  Keep joint updates? yes")
    Else
        Debug.Print ("  Keep joint updates? no")
    End If
    Debug.Print "  Overwrite the pinball value: " & Joints.IncludeTreatAsJointForClearanceLessThan
    Debug.Print "  Pinball radius: " & Joints.PinBallRadius * 0.001
    Select Case Joints.PinBallRadiusUnit
        Case 0
            Debug.Print "  Pinball radius unit: mm"
        Case 1
            Debug.Print "  Pinball radius unit: cm"
        Case 2
            Debug.Print "  Pinball radius unit: m"
        Case 3
            Debug.Print "  Pinball radius unit: in"
        Case 4
            Debug.Print "  Pinball radius unit: ft"
        Case 5
            Debug.Print "  Pinball radius unit: ft-in"
        Case 6
            Debug.Print "  Pinball radius unit: am"
        Case 7
            Debug.Print "  Pinball radius unit: nm"
        Case 8
            Debug.Print "  Pinball radius unit: micron"
        Case 9
            Debug.Print "  Pinball radius unit: mil"
        Case 10
            Debug.Print "  Pinball radius unit: microin"
    End Select
    
    ' Mesh the part
    Debug.Print " "
    Debug.Print "*** Creating a mesh next, which can take several minutes. Please wait. ***"    
    Set Mesh = Study.Mesh
    If Mesh Is Nothing Then ErrorMsg SwApp, "No mesh object.", False
    Mesh.Quality = swsMeshQualityHigh
    Mesh.GetDefaultElementSizeAndTolerance swsLinearUnitMillimeters, ElementSize, Tolerance
    errCode = Study.CreateMesh(swsLinearUnitMillimeters, ElementSize, Tolerance)
    If errCode <> 0 Then ErrorMsg SwApp, "Mesh failed.", True
    Debug.Print " "
    Debug.Print "Mesh..."
    Debug.Print "  Time to create mesh: " & Mesh.TimeToCompleteMesh
    Select Case Mesh.MeshType
        Case 0
            Debug.Print "  Mesh type: solid"
        Case 1
            Debug.Print "  Mesh type: midsurface"
        Case 2
            Debug.Print "  Mesh type: surface"
        Case 3
            Debug.Print "  Mesh type: mixed"
        Case 4
            Debug.Print "  Mesh type: beam"
    End Select
    Debug.Print "  Number of mesh controls: " & Mesh.MeshControlCount
    Select Case Mesh.MeshState
        Case 0
            Debug.Print "  Mesh state: no mesh"
        Case 1
            Debug.Print "  Mesh state: exists and is current"
        Case 2
            Debug.Print "  Mesh state: exists and is not current"
        Case 3
            Debug.Print "  Mesh state: failed"
        Case 4
            Debug.Print "  Mesh state: interrupted"
    End Select
    
    ' Select structural members for mesh control
    Debug.Print ""
    Debug.Print ("Mesh control...")
       
    status = swModelDocExt.SelectByID2("Structural Member1[4]", "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)
    Set selEntity = swSelMgr.GetSelectedObject6(2, -1)
    selEntities = Array(selEntity)
    Set selEntity = Nothing
    
    status = swModelDocExt.SelectByID2("Structural Member3[2]", "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)
    Set selEntity = swSelMgr.GetSelectedObject6(3, -1)
    selEntities = Array(selEntity)
    Set selEntity = Nothing
    
    status = swModelDocExt.SelectByID2("Structural Member3[3]", "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)
    Set selEntity = swSelMgr.GetSelectedObject6(4, -1)
    selEntities = Array(selEntity)
    Set selEntity = Nothing
    
    status = swModelDocExt.SelectByID2("Structural Member3[4]", "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)
    Set selEntity = swSelMgr.GetSelectedObject6(6, -1)
    selEntities = Array(selEntity)
    Set selEntity = Nothing
        
    status = swModelDocExt.SelectByID2("Structural Member3[1]", "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)
    Set selEntity = swSelMgr.GetSelectedObject6(8, -1)
    selEntities = Array(selEntity)
    Set selEntity = Nothing
  
    status = swModelDocExt.SelectByID2("Structural Member1[1]", "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)
    Set selEntity = swSelMgr.GetSelectedObject6(12, -1)
    selEntities = Array(selEntity)
    Set selEntity = Nothing
    
    status = swModelDocExt.SelectByID2("Structural Member5[1]", "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)
    Set selEntity = swSelMgr.GetSelectedObject6(14, -1)
    selEntities = Array(selEntity)
    Set selEntity = Nothing
    
    status = swModelDocExt.SelectByID2("Structural Member5[2]", "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)
    Set selEntity = swSelMgr.GetSelectedObject6(15, -1)
    selEntities = Array(selEntity)
    Set selEntity = Nothing
    
    status = swModelDocExt.SelectByID2("Structural Member1[3]", "SOLIDBODY", 0, 0, 0, False, 0, Nothing, 0)
    Set selEntity = swSelMgr.GetSelectedObject6(16, -1)
    selEntities = Array(selEntity)
    Set selEntity = Nothing
    
    Set MeshControl = Mesh.ApplyMeshControl(selEntities, errCode)
    Debug.Print "  Name of mesh control: " & MeshControl.Name
    Debug.Print "  Element size for mesh control: " & MeshControl.ElementSize
    Debug.Print "  Number of entities in mesh control: " & MeshControl.EntityCount
    Debug.Print "  Number of elements on a beam: " & MeshControl.NumofElementsforBeams
    Debug.Print "  State of mesh control (0 = suppressed; 1 = not suppressed): " & MeshControl.State
    
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:   Apply Mesh Control (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) 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.