Create Body From Deformed Shape Example (C#)
This example shows how to save the deformed shape resulting from
running a static study.
//---------------------------------------------------------------------------
// Preconditions:
// 1. Add the SOLIDWORKS Simulation as an add-in
// (in SOLIDWORKS, select Tools > Add-ins > SOLIDWORKS Simulation,
// and click OK).
// 2. Add the SOLIDWORKS Simulation primary interop assembly as a reference
// (in the IDE's Project Explorer, right-click
// the project name, click Add Reference, click the Browse tab,
// navigate to install_dir\api\redist\CLR2,
// click SolidWorks.Interop.cosworks.dll, and click OK).
// 3. Ensure that the specified model exists.
// 4. Open an Immediate window.
//
// Postconditions:
// 1. Opens the model document.
// 2. Gets the static study.
// 3. Meshes the model.
// 4. Sets the solver type.
// 5. Analyzes the study.
// 6. Saves the deformed shape as
// install_dir\samples\tutorial\api\deformedBody.sldprt.
// 7. Inspect the Immediate window.
//
// NOTE: Because the model is used elsewhere, do not save changes.
//---------------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.cosworks;
using System.Runtime.InteropServices;
namespace SaveDeformedShape_CSharp.csproj
{
partial class SolidWorksMacro
{
public void Main()
{
ModelDoc2 Part = default(ModelDoc2);
string fileName = null;
int errors = 0;
int warnings = 0;
int errCode = 0;
CosmosWorks COSMOSWORKS = default(CosmosWorks);
CwAddincallback CWAddinCallBack = default(CwAddincallback);
CWModelDoc ActDoc = default(CWModelDoc);
CWStudyManager StudyMngr = default(CWStudyManager);
CWStudy Study = default(CWStudy);
CWStaticStudyOptions StaticOptions = default(CWStaticStudyOptions);
CWMesh CWFeatObj = default(CWMesh);
CWResults res = default(CWResults);
const double MeshEleSize = 1.4769;
const double MeshTol = 0.073847;
if (swApp == null)
return;
fileName = "C:\\Program Files\\SOLIDWORKS Corp\\SOLIDWORKS\\samples\\tutorial\\api\\tjoint.sldprt";
Part = swApp.OpenDoc6(fileName, (int)swDocumentTypes_e.swDocPART, 1, "", ref errors, ref warnings);
CWAddinCallBack = (CwAddincallback)swApp.GetAddInObject("SldWorks.Simulation");
COSMOSWORKS = CWAddinCallBack.CosmosWorks;
ActDoc = COSMOSWORKS.ActiveDoc;
StudyMngr = ActDoc.StudyManager;
Study = StudyMngr.GetStudy(0);
Study.ActivateConfiguration();
StudyMngr.ActiveStudy = 0;
//Mesh
CWFeatObj = Study.Mesh;
CWFeatObj.MesherType = 0;
CWFeatObj.Quality = 0;
errCode = Study.CreateMesh(0, MeshEleSize, MeshTol);
CWFeatObj = null;
//Set solver type to FFEPlus
StaticOptions = Study.StaticStudyOptions;
StaticOptions.SolverType = 2;
//Run analysis
errCode = Study.RunAnalysis();
res = Study.Results;
res.CreateDeformedBody((int)swsCreateDeformedBodyOption_e.swsCreateDeformedBodyAsPart, "deformedBody", out errCode);
Debug.Print("Create deformed body result code as defined in swsCreateDeformedBodyError_e: " + errCode);
}
/// <summary>
/// The SldWorks swApp variable is pre-assigned for you.
/// </summary>
public SldWorks swApp;
}
}