Create Curvature-based Mesh (VB.NET)
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 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 <SolidWorks_install_dir>\api\redist\CLR2
folder, and
' select SolidWorks.Interop.cosworks.dll).
' 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.
'-------------------------------------------------------------
Imports
SolidWorks.Interop.sldworks
Imports
SolidWorks.Interop.swconst
Imports
System
Imports
System.Diagnostics
Imports
SolidWorks.Interop.cosworks
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
CwMesh As
CwMesh
Dim
errCode As
Integer
Dim
errors As
Integer
Dim
warnings As
Integer
Const
fileName As
String =
"C:\Program Files\SolidWorks
Corp\SolidWorks\Simulation\Examples\bikeframe.sldprt"
'
Get SolidWorks Simulation object
COSMOSObject = swApp.GetAddInObject("SldWorks.Simulation")
If
COSMOSObject Is
Nothing
Then
ErrorMsg(swApp, "COSMOSObject object not
found",
True)
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_e.swOpenDocOptions_Silent,
"",
errors, warnings)
ActDoc = COSMOSWORKS.ActiveDoc()
If
ActDoc Is
Nothing
Then
ErrorMsg(swApp, "No active document.",
True)
' Get Ready study
StudyMngr = ActDoc.StudyManager()
If
StudyMngr Is
Nothing
Then
ErrorMsg(swApp, "No StudyMngr object.",
True)
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
CwMesh = Study.Mesh
If
CwMesh Is
Nothing
Then
ErrorMsg(swApp, "No mesh object.",
False)
' Set type of mesh to
curvature-based
CwMesh.MesherType =
swsMesherType_e.swsMesherTypeAlternate
' Set mesh parameters
CwMesh.GrowthRatio = 1.6
CwMesh.MinElementsInCircle = 8
CwMesh.GetDefaultMaxAndMinElementSize(swsLinearUnit_e.swsLinearUnitMillimeters,
20, 4)
' Create mesh
errCode = Study.CreateMesh(swsLinearUnit_e.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
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)
swApp.RecordLine("")
End
Sub
'''
<summary>
'''
The SldWorks swApp variable is pre-assigned for you.
'''
</summary>
Public
swApp As
SldWorks
End
Class