SimSolid JavaScript 参考

本文档提供了一份全面指南,指导如何通过 JavaScript 实现 SimSolid 程序化交互。它详细说明了可用的对象、其属性和功能,并附有清晰的示例,助您实现 SimSolid 工作流自动化。

1.全局对象

全局对象是管理整个应用程序信息和控制的中心枢纽。可实现与 SimSolid 应用程序核心状态进行交互,包括应用程序的执行状态和错误处理。

函数

全局对象的一个关键功能是 errorCode,用于诊断和传达脚本执行的成功或失败。该对象是编写健壮脚本的基础,健壮脚本可优雅地处理各种错误,并就分析过程提供有意义的反馈。

errorCode
  • 整数
  • 该函数返回一个整数,表示 SimSolid.exe 进程的退出代码。通常情况下,数值 0 表示执行成功。

示例:

(在批处理脚本/命令提示符中检查退出代码)
// check if any disconnected groups of parts
if (!study.hasDisconnectedGroupsOfParts())
{
    // ... (rest of the analysis setup)
}
else
this.errorCode = 1; // error code if disconnected groups of parts
在继续分析之前,该 if 语句将检查是否有零件或零件组未连接到主装配。
!study.hasDisconnectedGroupsOfParts()
如果该条件为真(即不存在断开连接),脚本会继续设置并运行模态分析。
this.errorCode = 1
如果发现断开连接的组,脚本会设置错误代码 1 并跳过分析,因为断开连接的模型会产生不正确或不可行的结果。

在 JavaScript 中,该属性主要用于脚本在 SimSolid 上下文中运行后的诊断。

2.项目对象

可以通过项目对象管理项目的全局设置。

函数

setDefaultUnits
  • 设置整个项目中使用的默认单位
  • 有助于保持一致性,避免每次操作都要重复指定单位
参数:
1.
名称 类型 接受的值 默认值
长度 字符串 ‘mm’, ‘cm’, ‘m’, ‘in’ ‘mm’
角度 字符串 ‘deg’, ‘rad’ ‘deg’
字符串 'N', 'lbf', 'kip' 'N'
力矩 字符串 'N*m', 'N*mm', 'lbf*in' 'N*m'
示例:
// Set default units for the project 
Project.setDefaultUnits
({ 
length: 'cm', 
force: 'N' 
});
addDesignStudy
  • 通过从 CAD 文件导入几何体来创建新的设计研究并返回设计研究对象
参数:
2.
名称 类型 接受的值 默认值
文件 字符串 CAD 文件的路径
阅读器 字符串 ‘parasolid’, ‘ct’, 'parasolid+spatial' ‘parasolid’
分辨率 字符串 ‘standard’, ‘enhanced’, ‘fine’, ‘custom’ ‘standard’
geomType 字符串 'solids+skins', 'solids', 'skins' 'solids+skins'
angularDev 数值 15.0, deg
chordalDev 数值 0.635, mm
单位 对象 setDefaultUnits 相同 取自项目级别
示例:
// create a new design study
var study = Project.addDesignStudy({
		file: 'D:/Batch mode/Modal_Analysis/Pullup bar V1.step',
		reader: ‘ct’, //ct is the SimSolid legacy reader
		resolution: 'custom',
		angularDev: 12,
		chordalDev: 0.5,
		units: { length: 'cm', force: ‘N’ } });
保存
  • 将项目保存为 .ssp,如果失败则返回 false。
参数:
3.
名称 类型 接受的值 默认值
文件 字符串 .ssp 文件的路径。 不适用
语法:
Project.save ({ file: ‘<File_path>/<File_name>.ssp' });
打开
  • 打开现有的 .ssp
  • 如果失败则返回 false
参数:
名称 类型 接受的值 默认值
文件 字符串 .ssp 文件的路径。 不适用
语法:
Project.open ({ file: ‘<File_path>/<File_name>.ssp' });
initializeSolutions
  • 删除所有求解和响应网格
语法:
Project.initializeSolutions();
solveAllAnalyses
  • 求解所有分析
语法:
Project.solveAllAnalyses();
getDesignStudies
  • 返回所有设计研究对象的集合
语法:
Project.getDesignStudies())
示例:
// open project
Project.open({ file: D:/Batchmode/ fin2.ssp});
//Set number of logical cores for the solution
Project.setOptions({ numOfCoresToUse: 12 }); // Adjust to your desired number of cores
// run specific analysis
var analysisSpecific = null;
for (var study of Project.getDesignStudies()) {
if (study.getName() == 'Design study 1') { // Specify the exact name of your design study
              	for (var analysis of study.getAnalyses()) {
                     	if (analysis.getName() == 'Modal 1') { // Specify the exact name of your analysis
                           	analysisSpecific = analysis;
                          	break;
                     	}
              	}
              	break;
       	}
}
if (analysisSpecific) {
       	analysisSpecific.solve(); // Solve the identified specific analysis
} else { // Optional: Add logging or error handling if the specified analysis is not found 
Log.write ('Error: Specified design study or analysis not found.'); 
}
// save project
Project.save({ file: '%1%_solved.ssp'});
上述脚本执行一系列操作,即打开 SSP 文件、在设计研究 1 中运行模态 1 分析,并通过将 _solved 追加到原始文件名来保存 SSP。
setOptions
  • 设置用于选取求解使用的内核数的选项
参数:
名称 类型 接受的值 默认值
numOfCoresToUse 数值 所有可用内核
语法:
//Set number of logical cores for the solution
Project.setOptions({ numOfCoresToUse: 12 });

3.设计研究对象

设计研究对象代表 SimSolid 中当前的研究分析。它提供了各种功能来定义和修改仿真的各个方面,如施加材料、定义连接以及设置载荷和边界条件。

函数

applyMaterial
在当前设计研究的一个或多个零件上施加指定材料
参数:
4.
名称 类型 接受的值 默认值
材料 字符串 SimSolid 中定义的材料名称 不适用
partNameMask 字符串 通配符模式(例如,'Part 1', '*plate*', '*')。 ‘*’
partNameMaskDelimiter 字符串 任意单个字符(例如,',' ';' ' ')。 ','
注:
  • 确保材料已在 SimSolid 材料库中提供或已在项目中定义。
  • 蒙板(模式)用于指定要施加材料的零件。支持通配符 (*)。使用 * 可施加于所有零件。
  • 向特定零件集施加材料时,用于分隔多个 partNameMask 条目的分隔符。默认使用逗号 (,)。
示例:
  1. 向装配中的所有零件施加常见材料
    study.applyMaterial({
    		material: 'Aluminum',
    		partNameMask: '*' });
  2. 向装配中的零件施加不同材料
    // Apply 'Steel' to a part exactly named 'plate 1' 
    study.applyMaterial({ 
    material: 'Steel', 
    partNameMask: 'plate 1' 
    }); 
    
    // Apply 'Rubber' to parts named 'plate 2' and 'plate 4' 
    // The default delimiter (comma) is used here. 
    study.applyMaterial({ 
    material: 'Rubber', 
    partNameMask: 'plate 2, plate 4' 
    }); 
    
    // Apply 'Nickel' to parts named 'plate 3' and 'plate 5' 
    // Explicitly setting the delimiter, though not strictly necessary here as it's the default. study.applyMaterial({ 
    material: 'Nickel', 
    partNameMask: 'plate 3, plate 5', 
    partNameMaskDelimiter: ',' 
    });
提示:
  • 区分大小写: SimSolid 零件名称通常区分大小写。确保 partNameMask 与装配树中零件名称的大小写完全匹配。
  • 通配符的使用:

    'Part*' 将匹配 'Part A'、'Part B'、'Particle'

    '*Bolt*' 将匹配 'Front Bolt'、'Bolt 123'、'Hex Bolt'

    'Plate 1' 只能与精确命名为 'Plate 1' 的零件匹配

  • 材料库:使用 applyMaterial 之前,确保指定的材料名称存在于 SimSolid 材料库中。
addConnections
在所有零件之间自动创建常规连接
参数:
5.
名称 类型 接受的值 默认值
gap 数值 任意正数值 根据几何体计算得出
穿透 数值 任意正数值 根据几何体计算得出
分辨率 字符串 ‘normal, ‘increased’, ‘high’ ‘normal’
单位 对象 与 setDefaultUnits 相同 取自项目级别
描述:
  • 间隙和穿透:定义了面与面之间建立连接时允许的最大间隙或重叠量。SimSolid可自动计算该数值,但如果零件未按预期建立连接,或出现非预期的连接时,可进行调整。
  • 分辨率:控制连接检测的灵敏度。调高该参数(即增强、高)可识别更多细微的连接,但会增加运行时间。“标准”适用于大多数情况;“高”适用于精细细节或复杂几何体形。
  • 单位:这主要使用长度属性来指定间隙和穿透参数的单位。如果未指定,则使用项目级长度单位。请参阅 setDefaultUnits 文档,了解可接受的单位属性。
示例:
// Automatically find all connections 
study.addConnections({ 
gap: 0.1 
penetration: 0.2
resolution: 'normal',
units: { length: 'mm' } });
提示:
  • 查看连接:运行 addConnections 后,始终在 SimSolid 用户界面中查看已创建的连接。
  • 调整缺失连接的参数:如果发现没有形成预期的连接,请考虑:
    • 增加间隙:如果零件稍有分离。
    • 增加穿透:如果零件略有重叠。
    • 将分辨率提高到增强:仅在处理极小特征或复杂界面面时,才需调高分辨率。请注意,分辨率越高,处理时间越长。
  • 单位一致性:注意所使用的单位,尤其是在手动指定间隙和穿透值时。确保单位与模型的几何体和单位参数一致。
hasDisconnectedGroupsOfParts
检查是否有多个断开连接的零件组
语法:
study.hasDisconnectedGroupsOfParts()

示例:

在进行模态分析前检查断开连接的组:

本例演示了一个常见用例:在进行模态分析之前,使用 hasDisconnectedGroupsOfParts 作为条件检查。如果零件断开连接,模态分析可能会错误地显示刚体模式或无法求解。
// Check if there are any disconnected groups of parts 
if (!study.hasDisconnectedGroupsOfParts()) { 
// If no disconnected groups are found (i.e., returns false), proceed with analysis setup 
// Create a modal analysis object with 9 modes to find 
var analysis = study.addModalAnalysis({ numOfModes: 9 }); 
// Set global and local solution settings for the analysis 
analysis.setSolutionSettings({ 
adaptation: 'global+local', // Define the solution adaptation method 
}); 
// Add a specific solution settings group for parts matching 'PLATE*' 
analysis.addSolutionSettingsGroup({ 
partNameMask: 'PLATE*', // Apply these settings to parts whose names start with 
'PLATE'
adaptToFeatures: true, // Enable adaptation around geometric features 
adaptToThinSolids: true, // Enable adaptation for thin-solid geometries 
refinement: 'standard' // Set refinement level to standard 
}); 
// Attempt to solve the analysis 
if (analysis.solve()) { 
// If the analysis solves successfully 
// Import a set of datum points from a CSV file 
var datumPointSet = study.addDatumPointSet({ 
file: 'D:/Batch mode/Modal_Analysis/DatumPoints_Bar.csv', 
units: { length: 'mm' } // Specify units for the imported points 
}); 
// Export analysis results to a UNV file 
analysis.exportToUNV({ 
file: 'D:/Batch mode/Modal_Analysis/DatumPoints_Bar.unv', 
datumPointSet: datumPointSet // Include the imported datum points in the export }); 
// Save the current project state to an .ssp file 
Project.save({ 
file: 'D:/Batch mode/Modal_Analysis/Pullup bar.ssp' 
}); 
} else {
// If analysis fails to solve this.errorCode = 2; // Custom error code indicating analysis
 failure 
} 
}else { 
// If disconnected groups are found (i.e., returns true) 
this.errorCode = 1; // Custom error code indicating disconnected parts issue 
}
提示:
  • 预分析检查:始终将 hasDisconnectedGroupsOfParts 纳入批脚本或自动化工作流程,作为重要的分析前检查。这样可以避免因连接问题导致模型定义存在根本性缺陷时,浪费不必要的运算时间。
  • 断开连接故障排除:如果 hasDisconnectedGroupsOfParts 返回 true,则表明连接出现了问题。用户应该:
    • 在 SimSolid GUI 中查看连接:目视检查装配和连接显示屏。
    • 调整 addConnections 参数:如果使用 addConnections 自动生成连接,可尝试修改间隙、穿透或分辨率,以捕捉缺失的连接。
  • 错误处理:该示例演示了使用 errorCode 来提示是否发现了断开连接的组或分析是否失败。实施强大的错误处理对于自动化工作流程至关重要。
addModalAnalysis
  • 创建新的模态分析
  • 返回分析对象
参数:
6.
名称 类型 接受的值 默认值
numOfModes 数值
语法:
// create a modal analysis
	var analysis = study.addModalAnalysis({ numOfModes: 9 });
addDatumPointSet
从 .csv 文件导入基准点并返回基准点集对象。
参数:
7.
名称 类型 接受的值 默认值
文件 字符串 CSV 文件的路径
单位 对象 与 setDefaultUnits 相同 取自项目级别
语法:
// import datum points
		var datumPointSet = study.addDatumPointSet({
file: ‘<File_path>/<File_name>.csv',
			units: {length: 'mm'} });
addStructuralAnalysis
创建新的结构分析并返回分析对象
参数:
8.
名称 类型 接受的值 默认值
类型 字符串 'multiload'
语法:
// create a multi-loadcase structural analysis
var analysis = study.addStructuralAnalysis({ type: 'multiload' });
getName
  • 返回设计研究的名称
  • 这主要用于求解特定的设计研究或分析。
getAnalyses
  • 返回所有分析对象的集合
  • 这有助于收集特定设计研究下的所有分析。

示例:

该脚本演示了如何使用 API( getNamegetAnalysis)在指定的设计研究中定位并求解特定分析。
// open project
Project.open({ file: D:/Batchmode/ fin2.ssp});
//Set number of logical cores for the solution
Project.setOptions({ numOfCoresToUse: 12 }); // Adjust to your desired number of cores
// run specific analysis
var analysisSpecific = null;
for (var study of Project.getDesignStudies()) {
if (study.getName() == 'Design study 1') { // Specify the exact name of your design study
              	for (var analysis of study.getAnalyses()) {
                     	if (analysis.getName() == 'Modal 1') { // Specify the exact name of your analysis
                           	analysisSpecific = analysis;
                          	break;
                     	}
              	}
              	break;
       	}
}
if (analysisSpecific) {
       	analysisSpecific.solve(); // Solve the identified specific analysis
} else { // Optional: Add logging or error handling if the specified analysis is not found 
Log.write ('Error: Specified design study or analysis not found.'); 
}
// save project
Project.save({ file: '%1%_solved.ssp'});

上述脚本执行一系列操作,即打开 SSP 文件、在设计研究 1 中运行模态 1 分析,并通过将 _solved 追加到原始文件名来保存 SSP。

addGravityLoad
在多载荷工况结构分析下,为特定载荷工况施加重力载荷。

示例:

该脚本演示了如何通过三种方法将重力荷载施加于多载荷工况分析下的荷载工况。
  1. 直接向载荷工况添加重力载荷。
  2. 通过按载荷工况名称检索分析的方式,向载荷工况添加重力载荷。
  3. 根据名称查找载荷工况,并向该载荷工况添加重力载荷。
// create a new design study
var study = Project.addDesignStudy({
	file: 'C:/Users/Batchmode/Pullup bar V1.x_t',
	reader: 'parasolid',
	resolution: 'standard'
	});
// apply material to all parts
study.applyMaterial({
	material: 'Steel',
	partNameMask: '*' });
	//partNameMask:


// find all connections automatically
study.addConnections({
	resolution: 'normal',
	//units: { length: 'mm' } 
	});
	
// take the first design study
//var study = Project.getDesignStudies()[0];

// create a new multi-load analysis
var analysis = study.addStructuralAnalysis({ type: 'multiload'});

// set global+local solution settings
	analysis.setSolutionSettings({
		adaptation: 'global+local',
	});

// create a new load case 1
var loadCase = analysis.addLoadCase();


// Option 1 - add a gravity load to the load case directly
loadCase.addGravityLoad({ direction: [0.2, 0.5, 0], factor: 10 });

// add constraint and loads
analysis.importSpotDisplacements({ file: 'C:/Users/Batchmode/spot_displacement.csv'});
analysis.importRemoteLoads({ file: 'C:/Users/Batchmode/import_loads.csv'});
 
// Option 2 - add a gravity load to the load case via analysis by load case name
analysis.addGravityLoad({ direction: [0.5, 0.5, 0], factor: 4, loadCaseName: 'Vertical' });
analysis.addGravityLoad({ direction: [0.5, 0.6, 0], factor: 8, loadCaseName: 'Horizontal' }); 


// Option 3 - find the load case by the name and add a gravity load to that load case
for (var loadCase of analysis.getLoadCases()) {
	if (loadCase.getName() == 'Vertical_right')
		loadCase.addGravityLoad({ direction: [0, 0, -1], factor: 2 });
}
for (var loadCase of analysis.getLoadCases()) {
	if (loadCase.getName() == 'Horizontal_Right')
		loadCase.addGravityLoad({ direction: [0, 0, 1], factor: 3 });
}

// solve
	if (analysis.solve())
	{
		//import datum points
		var datumPointSet = study.addDatumPointSet({
		file: 'C:/Users/Batchmode/DatumPoints_Bar.csv',
			units: {length: 'mm'}
			});
	
			
			analysis.exportToCSV({
               file: 'C:/Users/Batchmode/gravity_test_result_datum_points.csv' , datumPointSet: datumPointSet});
			   
		Project.exportToCSV({ file: 'C:/Users/Batchmode/gravity_export_results.csv', maxVonMises: true, maxDisplacement: true }); 

		Project.save({ file: 'C:/Users/Batchmode/Pullup_bar_gravity_results.ssp' });
	}
	else
		this.errorCode = 2; // error code if analysis fails
getDatumPointSets
返回所有基准点集对象的集合
addParts
  • 将新零件导入设计研究
  • 可以自动添加新连接。
参数:
9.
名称 类型 接受的值 默认值
文件 字符串 CAD 文件的路径
阅读器 字符串 'parasolid', 'ct', 'parasolid+spatial' 'parasolid'
分辨率 字符串 'standard', 'enhanced', 'fine', 'custom' 'standard'
angularDev 数值 '15.0', 'deg'
chordalDev 数值 '0.5', 'mm'
单位 对象 与 setDefaultUnits 相同 取自项目级别
addConnections 布尔运算 'true', 'false' 'false'
connGap 数值 根据几何体计算得出
connPenetration 数值 根据几何体计算得出
connResolution 字符串 'normal', 'increased', 'high' 'normal'
示例:
//import part in assembly 
study.addParts({
  file: D:/Batchmode/plate_1.x_b',
  reader:'parasolid',
  resolution:'fine',
  addConnections:true,
  connGap:2,
  connPenetration: 2});
deleteParts
  • 从设计研究删除指定的零件
  • 要设置多个蒙板,请使用由 partNameMaskDelimiter 指定的分隔符;例如,partNameMask:‘Part 1, Part 2’。
参数:
10.
名称 类型 接受的值 默认值
partNameMask 字符串
partNameMaskDelimiter 字符串
示例:
// delete part from assembly 
study.deleteParts({partNameMask: 'plate 1'});
Project.save({ file: ' D:/Batchmode//3_plates_part_delete.ssp' })

几何体缺陷检查功能

通过下面的 API,用户可以在导入文件时检查几何体缺陷,并将面缺陷、零件重叠和自相交写入 CSV 文件。用户可以为每个文件定义文件名,并根据设计研究的数量生成 CSV 文件。

study.checkGeometryForDefects
检查几何体缺陷中的面缺陷。
study.checkGeometryForOverlaps
检查几何体缺陷中的零件重叠。
study.checkGeometryForSelfIntersections
检查几何体缺陷中的零件自相交。
示例:
//open project
Project.open({ file: '<File_Directory>/example.ssp'});
var counter = 1;
Project.getDesignStudies().forEach(study => {
	study.checkGeometryForDefects({ file: '<File_Directory>/geometry_defects_test' + counter + '.csv' });
	study.checkGeometryForOverlaps({ file: '<File_Directory>/geometry_overlaps_test' + counter + '.csv' });
	study.checkGeometryForSelfIntersections({ file: '<File_Directory>/geometry_selfintersects_test' + counter + '.csv' });
	counter++;
});
Project.save({ file: ‘<File_Directory>/example_saved.ssp' });

导出结果功能

将已求解项目的最大位移和最大米塞斯等效应力结果导出为 CSV 文件。

目前支持结构线性和多载荷工况分析。CSV 文件可以输出这些坐标点的坐标值、位移、应力、应变和应变能密度。同时还会写入零件 ID、零件名称、分析名称和设计研究。

maxVonMises maxDisplacement
示例:
•Option 1 - export all analyses from all design studies to a single file
Project.exportToCSV({ file: <File_Directory> /All_analysis.csv', maxVonMises: true, maxDisplacement: true });
•Option 2 - export all analyses from all design studies into one file per design study
var counter = 1;
Project.getDesignStudies().forEach(study => {
study.exportToCSV({ file: ‘<File_Directory>/structural_output_per_ds' + (counter++) + '.csv', maxVonMises: true, maxDisplacement: true });
});
•Option 3 - export all analyses from all design studies into one file per analysis
var counter = 1;
Project.getDesignStudies().forEach(study => {
	study.getAnalyses().forEach(analysis => {
analysis.exportToCSV({ file: '<File_Directory>/sequential ' + (counter++) + '.csv', maxVonMises: true, maxDisplacement: true });
});
});

4.分析对象

分析对象是 SimSolid 脚本环境的重要组成部分,主要负责管理项目级设置和创建设计研究。

函数

setDefaultUnits
  • 设置整个项目中使用的默认单位
  • 该功能有助于保持一致性,避免每次操作都要重复指定单位。
参数:
11.
名称 类型 接受的值 默认值
长度 字符串 ‘mm’, ‘cm’, ‘m’, ‘in’ ‘mm’
角度 字符串 ‘deg’, ‘rad’ ‘deg’
字符串 'N', 'lbf', 'kip' 'N'
力矩 字符串 'N*m', 'N*mm', 'lbf*in' 'N*m'
示例:
// Set default units for the project 
Project.setDefaultUnits
({ 
length: 'cm', 
force: 'N' 
});
addDesignStudy
  • 通过从 CAD 文件导入几何体来创建新的设计研究并返回设计研究对象
参数:
12.
名称 类型 接受的值 默认值
文件 字符串 CAD 文件的路径
阅读器 字符串 ‘parasolid’, ‘ct’, 'parasolid+spatial' ‘parasolid’
分辨率 字符串 ‘standard’, ‘enhanced’, ‘fine’, ‘custom’ ‘standard’
geomType 字符串 'solids+skins', 'solids', 'skins' 'solids+skins'
angularDev 数值 15.0, deg
chordalDev 数值 0.635, mm
单位 对象 setDefaultUnits 相同 取自项目级别
示例:
// create a new design study
var study = Project.addDesignStudy({
		file: 'D:/Batch mode/Modal_Analysis/Pullup bar V1.step',
		reader: ‘ct’, //ct is the SimSolid legacy reader
		resolution: 'custom',
		angularDev: 12,
		chordalDev: 0.5,
		units: { length: 'cm', force: ‘N’ } });
保存
  • 将项目保存为 .ssp,如果失败则返回 false。
参数:
13.
名称 类型 接受的值 默认值
文件 字符串 .ssp 文件的路径。 不适用
语法:
Project.save ({ file: ‘<File_path>/<File_name>.ssp' });