Hide Table of Contents

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 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("CosmosWorks.CosmosWorks")
        
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

 

 



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 Curvature-based Mesh (VB.NET)
*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) 2011 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.