Create Curvature-based Mesh (C#)
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.
//-------------------------------------------------------------
using
SolidWorks.Interop.sldworks;
using
SolidWorks.Interop.swconst;
using
System;
using
System.Diagnostics;
using
SolidWorks.Interop.cosworks;
using
System.Windows.Forms;
namespace
CWMeshSWSimulationCSharp.csproj
{
partial
class
SolidWorksMacro
{
public
void Main()
{
CosmosWorks COSMOSWORKS = null;
CwAddincallback COSMOSObject =
default(CwAddincallback);
CWModelDoc ActDoc = default(CWModelDoc);
CWStudyManager StudyMngr =
default(CWStudyManager);
CWStudy Study = default(CWStudy);
CWMesh CwMesh = default(CWMesh);
int
errCode = 0;
int
errors = 0;
int
warnings = 0;
double
maxElementSize = 20.0;
double
minElementSize = 4.0;
const
string
fileName = "C:\\Program Files\\SolidWorks
Corp\\SolidWorks (2)\\Simulation\\Examples\\bikeframe.sldprt";
// Get SolidWorks Simulation
object
COSMOSObject = (CwAddincallback)swApp.GetAddInObject("SldWorks.Simulation");
if
(COSMOSObject == null)
ErrorMsg(swApp, "COSMOSObject
object not found",
true);
COSMOSWORKS = (CosmosWorks)COSMOSObject.CosmosWorks;
if
(COSMOSWORKS == null)
ErrorMsg(swApp, "COSMOSWORKS
object not found",
true);
// Open document and get it
swApp.OpenDoc6(fileName,
(int)swDocumentTypes_e.swDocPART,
(int)swOpenDocOptions_e.swOpenDocOptions_Silent,
"",
ref errors,
ref
warnings);
ActDoc = (CWModelDoc)COSMOSWORKS.ActiveDoc;
if
(ActDoc == null)
ErrorMsg(swApp, "No
active document.",
true);
// Get Ready study
StudyMngr = (CWStudyManager)ActDoc.StudyManager;
if
(StudyMngr == null)
ErrorMsg(swApp, "No
StudyMngr object.",
true);
Study = (CWStudy)StudyMngr.GetStudy(0);
if
(Study == null)
ErrorMsg(swApp, "No
study.",
true);
if
((Study.Name != "Ready"))
ErrorMsg(swApp, "Wrong
study.",
true);
Debug.Print("Name
of study: " + Study.Name);
//
Get mesh
CwMesh = (CWMesh)Study.Mesh;
if
(CwMesh == null)
ErrorMsg(swApp, "No mesh
object.",
false);
// Set type of mesh to
curvature-based
CwMesh.MesherType = (int)swsMesherType_e.swsMesherTypeAlternate;
// Set mesh parameters
CwMesh.GrowthRatio =
1.6;
CwMesh.MinElementsInCircle = 8;
CwMesh.GetDefaultMaxAndMinElementSize((int)swsLinearUnit_e.swsLinearUnitMillimeters,
out
maxElementSize, out
minElementSize);
// Create mesh
errCode = Study.CreateMesh((int)swsLinearUnit_e.swsLinearUnitMillimeters,
20, 1);
Debug.Print("Error
code: " + errCode);
//0 indicates successful
creation of the mesh
if
(errCode != 0)
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);
}
// Error function
public
void
ErrorMsg(object
swApp, string
Message, bool
EndTest)
{
MessageBox.Show(Message);
MessageBox.Show("'*** WARNING
- General");
MessageBox.Show("'*** "
+ Message);
MessageBox.Show("");
if
(EndTest)
{
}
}
///
<summary>
///
The SldWorks swApp variable is pre-assigned for you.
///
</summary>
public
SldWorks swApp;
}
}