SolidWorks二次开发 API-SOLIDWORKS Simulation分析参数修改
今天我们来讲个小例子。
是关于SOLIDWORKS Simulation的。
先说明一点,这东西我也不熟。有问题别问我
首先,我做了一个很难的分析,条件也是很复杂,具体操作我就不说了,分析结果如下:
当然这个图和我们今天要做的事情 关系不大,我们主要了解 一下如何连接到SOLIDWORKS Simulation 上并修改载荷参数,重新运行分析,得到新的结果 。
具体的api查找过滤和之前看帮助类似,我就直接上图了:
下面是关键代码:
把上面的力的大小从100 改成150,然后重新进行风格的划分,运行计算。
`
private void btnSimStudy_Click(object sender, EventArgs e)
{
var actPath = RegDllPath(“”);
var start = actPath.Substring(0,actPath.IndexOf("CSharpAndSolidWorks",0));
var partPath = $@"{start}CSharpAndSolidWorks\CSharpAndSolidWorks\TemplateModel\Simulation API Demo.SLDPRT";
SldWorks swApp = PStandAlone.GetSolidWorks();
CWModelDoc swsActDoc = default(CWModelDoc);
CWStudyManager swsStudyMngr = default(CWStudyManager);
CWStudy swsStudy = default(CWStudy);
CWLoadsAndRestraintsManager swsLBCMgr = default(CWLoadsAndRestraintsManager);
CWForce swsCWForce = default(CWForce);
int errors = 0;
int warnings = 0;
string fileName = partPath;
// 打开模型
var swModel = (ModelDoc2)swApp.OpenDoc6(fileName, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
// Get the SOLIDWORKS Simulation object
dynamic COSMOSWORKS = default(dynamic);
dynamic COSMOSObject = default(dynamic);
// Determine host SOLIDWORKS major version
int swVersion = Convert.ToInt32(swApp.RevisionNumber().Substring(0, 2));
// Calculate the version-specific ProgID of the Simulation add-in that is compatible with this version of SOLIDWORKS
int cwVersion = swVersion - 15;
String cwProgID = String.Format("SldWorks.Simulation.{0}", cwVersion);
Debug.Print(cwProgID);
// Get the SOLIDWORKS Simulation object
COSMOSObject = swApp.GetAddInObject(cwProgID);
COSMOSWORKS = COSMOSObject.CosmosWorks;
// Open and get active document
swsActDoc = (CWModelDoc)COSMOSWORKS.ActiveDoc;
if (swsActDoc == null) ErrorMsg(swApp, "No active document");
// Create new static study
swsStudyMngr = (CWStudyManager)swsActDoc.StudyManager;
if (swsStudyMngr == null) ErrorMsg(swApp, "No CWStudyManager object");
//得到第一个算例对象
swsStudy = (CWStudy)swsStudyMngr.GetStudy(0);
//算例名称,可以区分多个算例 swsStudy.Name
if (swsStudy == null) ErrorMsg(swApp, "No CWStudy object");
swsLBCMgr = (CWLoadsAndRestraintsManager)swsStudy.LoadsAndRestraintsManager;
//get the Force Feature
//这是是第一个条件,所以参数写0
var sCwForce = swsLBCMgr.GetLoadsAndRestraints(0, out int errcode);
if (sCwForce != null)
{
var swForce = (CWForce)sCwForce;
swForce.ForceBeginEdit();
//修改值为150
swForce.NormalForceOrTorqueValue = 150;
var res = swForce.ForceEndEdit();
}
//Create mesh
var CwMesh = (CWMesh)swsStudy.Mesh;
if (CwMesh == null) ErrorMsg(swApp, "No mesh object");
CwMesh.Quality = 1;
CwMesh.GetDefaultElementSizeAndTolerance(0, out double el, out double tl);
var errCode = swsStudy.CreateMesh(0, el, tl);
if (errCode != 0) ErrorMsg(swApp, "Mesh failed");
//Run
swsStudy.RunAnalysis();
}
public void ErrorMsg(object swApp, string Message)
{
MessageBox.Show(Message);
MessageBox.Show("'*** WARNING - General");
MessageBox.Show("'*** " + Message);
MessageBox.Show("");
}
`
这样结果出来了,至于怎么拿到最新的结果 ,或者说截图的话后面有机会再讲了。
源码位置:
https://gitee.com/painezeng/CSharpAndSolidWorks
posted @
2022-11-25 20:00
painezeng 阅读(
0) 评论(
0)
编辑
收藏
举报
来源