Create Curvature-based Mesh (VBA)
This example shows how to create a curvature-based mesh for a part.
'----------------------------------------------------------------------------
' Preconditions:
' 1. Specified part document exists.
' 2. Add the SolidWorks Simulation as an add-in
' (in SolidWorks, click Tools > Add-ins > SolidWorks Simulation).
' 3. Add the SolidWorks Simulation type library as a reference
' (in the IDE, click Tools > References > SolidWorks
' Simulation version type library).
' 4. Open the Immediate window.
'
' Postconditions:
' 1. Part document is opened.
' 2. A curvature-based mesh is created for the Ready study.
' 3. In SolidWorks, click the Ready study tab, right-click Mesh
' in the Simulation tree, and select Show Mesh.
' 4. Zoom in on the part and examine the mesh.
'
' NOTE: 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 COSMOSWORKS As Object
Dim COSMOSObject As CosmosWorksLib.CwAddincallback
Dim ActDoc As CosmosWorksLib.CWModelDoc
Dim StudyMngr As CosmosWorksLib.CWStudyManager
Dim Study As CosmosWorksLib.CWStudy
Dim CwMesh As CosmosWorksLib.CwMesh
Dim errCode As Long
Dim errors As Long
Dim warnings As Long
Const fileName As String = "C:\Program Files\SolidWorks Corp\SolidWorks\Simulation\Examples\bikeframe.sldprt"
' Connect to SolidWork software
If swApp Is Nothing Then Set swApp = Application.SldWorks
' Get 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 document and get it
swApp.OpenDoc6 fileName, swDocumentTypes_e.swDocPART, swOpenDocOptions_Silent, "", errors, warnings
Set ActDoc = COSMOSWORKS.ActiveDoc()
If ActDoc Is Nothing Then ErrorMsg swApp, "No active document.", True
' Get Ready study
Set StudyMngr = ActDoc.StudyManager()
If StudyMngr Is Nothing Then ErrorMsg swApp, "No StudyMngr object.", True
Set Study = StudyMngr.GetStudy(0)
If Study Is Nothing Then ErrorMsg swApp, "No study.", True
If (Study.Name <> "Ready") Then ErrorMsg swApp, "Wrong study.", True
Debug.Print ("Name of study: " & Study.Name)
' Get mesh
Set CwMesh = Study.Mesh
If CwMesh Is Nothing Then ErrorMsg swApp, "No mesh object.", False
' Set type of mesh to curvature-based
CwMesh.MesherType = swsMesherTypeAlternate
' Set mesh parameters
CwMesh.GrowthRatio = 1.6
CwMesh.MinElementsInCircle = 8
CwMesh.GetDefaultMaxAndMinElementSize swsLinearUnitMillimeters, 20, 4
' Create mesh
errCode = Study.CreateMesh(swsLinearUnitMillimeters, 20, 1)
Debug.Print "Error code: " & errCode '0 indicates successful creation of the mesh
If errCode <> 0 Then ErrorMsg swApp, "Mesh failed; check error code.", True
' Get maximum and minimum element sizes used for boundaries
Debug.Print "Maximum element size used for boundaries with lowest curvature: " & CwMesh.MaxElementSize
Debug.Print "Minimum element size used for boundaries with highest curvature: " & CwMesh.MinElementSize
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 ""
End Function