[C# SolidWorks API] シミュレーションを実行する
Simulationスタディを作成して,部品の材料特性や境界条件を設定したら,次はいよいよシミュレーションを実行しましょう.
早速,シミュレーションを実行して,応力の最小値と最大値を取得するプログラムを作成していきましょう.
実行例
シミュレーションを実行して,0番目の要素におけるミーゼス応力の最小値と最大値をダイアログに表示しています.
コード
全体のコードは以下のようになります.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // for SolidWorks API using SolidWorks.Interop.cosworks; using SolidWorks.Interop.sldworks; using SolidWorks.Interop.swconst; // for Debuging using System.Diagnostics; namespace Simulation { class Program { // SolidWorks APIにアクセスするためのメンバ SldWorks.ISldWorks swApp; // Simulation APIにアクセスするためのメンバ ICosmosWorks COSMOSWORKS = null; // 部品(Part),図面(Drawing),アセンブリ(Assembly)の各ドキュメントにアクセスするためのメンバ IModelDoc2 model = default(ModelDoc2); // Simlation スタディにアクセスするためのメンバ ICWStudy study = default(CWStudy); void startSW() { swApp = new SldWorks.SldWorks(); Debug.Assert(swApp != null); swApp.Visible = true; } void createCube(double length) { string partTemplate = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart); if ((partTemplate != null) && (partTemplate != "")) { model = (IModelDoc2)swApp.NewDocument( partTemplate, // テンプレートファイルの場所 (int)swDwgPaperSizes_e.swDwgPaperA2size, // ドキュメントのサイズ(A4やA2など) 0.0, 0.0); // 正方形を描く ISketchManager sketchMan = model.SketchManager; sketchMan.InsertSketch(true); sketchMan.CreateCornerRectangle(0, 0, 0, length, length, 0); // 正方形を押し出す IFeatureManager featMan = model.FeatureManager; featMan.FeatureExtrusion3( true, false, false, (int)swEndConditions_e.swEndCondBlind, (int)swEndConditions_e.swEndCondBlind, length, 0.0, false, false, false, false, 0.0, 0.0, false, false, false, false, true, false, false, (int)swStartConditions_e.swStartSketchPlane, 0.0, false ); } else { Console.WriteLine("There is no part template available. Please check your options and make sure there is a part template selected, or select a new part template."); } } void loadSimulationAddIn() { string path_to_cosworks_dll = swApp.GetExecutablePath() + @"\Simulation\cosworks.dll"; int errors = swApp.LoadAddIn(path_to_cosworks_dll); checkStatus("Load cosworks.dll: ", errors); string addInName = "SldWorks.Simulation"; CwAddincallback COSMOSObject = (CwAddincallback)swApp.GetAddInObject(addInName); Debug.Assert(COSMOSObject != null); COSMOSWORKS = (ICosmosWorks)COSMOSObject.CosmosWorks; Debug.Assert(COSMOSWORKS != null); } void createStudy(string name, int type) { ICWModelDoc actDoc = (ICWModelDoc)COSMOSWORKS.ActiveDoc; Debug.Assert(actDoc != null); ICWStudyManager studyMgr = (ICWStudyManager)actDoc.StudyManager; Debug.Assert(studyMgr != null); int errors = 0; study = (ICWStudy)studyMgr.CreateNewStudy3(name, type, 0, out errors); checkStatus("createStudy", errors); } void createStaticStudy(string name) { createStudy(name, (int)swsAnalysisStudyType_e.swsAnalysisStudyTypeStatic); } void applyMaterial(int componentNum, int bodyNum, string materialName) { string materialLibrary = swApp.GetExecutablePath() + @"\lang\english\sldmaterials\solidworks materials.sldmat"; ICWSolidManager SolidMgr = (ICWSolidManager)study.SolidManager; int errorCode = 0; ICWSolidComponent SolidComponent = SolidMgr.GetComponentAt(componentNum, out errorCode); if (errorCode == 0) { //string SolidComponentName = SolidComponent.ComponentName; ICWSolidBody SolidBody = (ICWSolidBody)SolidComponent.GetSolidBodyAt(bodyNum, out errorCode); //if (errorCode != 0) ErrorMsg("No solid body."); errorCode = (int)SolidBody.SetLibraryMaterial(materialLibrary, materialName); Debug.WriteLine("Mat: " + errorCode); } else { checkStatus("applyMaterial", errorCode); } } void applyFixedBC() { if (model == null) { model = (IModelDoc2)swApp.ActiveDoc; } // 固定する面を選択する ISelectionMgr selectionMgr = (ISelectionMgr)model.SelectionManager; bool isSelected = model.Extension.SelectByID2("", "FACE", 2.81519136727046E-02, 0, 4.24519591314118E-02, false, 0, null, 0); if (isSelected) { // 選択された面のオブジェクトを取得する object selectedFace = (object)selectionMgr.GetSelectedObject6(1, -1); object[] fixedFaces = { selectedFace }; // 境界条件を設定する ICWLoadsAndRestraintsManager BCMgr = (ICWLoadsAndRestraintsManager)study.LoadsAndRestraintsManager; int errorCode = 0; ICWRestraint restraint = (ICWRestraint)BCMgr.AddRestraint((int)swsRestraintType_e.swsRestraintTypeFixed, fixedFaces, null, out errorCode); checkStatus("Apply Fixed BC", errorCode); } model.ClearSelection2(true); } void applyDisplacementBC() { if (model == null) { model = (IModelDoc2)swApp.ActiveDoc; } // 変位させる面を選択する ISelectionMgr selectionMgr = (ISelectionMgr)model.SelectionManager; bool isSelected = model.Extension.SelectByID2("", "FACE", 5.89200124707077E-02, 9.99999999999659E-02, 5.97631804628236E-02, false, 0, null, 0); if (isSelected) { // 選択された面のオブジェクトを取得する object selectedFace = (object)selectionMgr.GetSelectedObject6(1, -1); object[] displacedFaces = { selectedFace }; // 境界条件を設定する ICWLoadsAndRestraintsManager BCMgr = (ICWLoadsAndRestraintsManager)study.LoadsAndRestraintsManager; int errorCode = 0; ICWRestraint restraint = (ICWRestraint)BCMgr.AddRestraint((int)swsRestraintType_e.swsRestraintTypeFlatFace, displacedFaces, null, out errorCode); checkStatus("Add Restraint", errorCode); restraint.RestraintBeginEdit(); restraint.SetTranslationComponentsValues(0, 0, 1, 0, 0, 10); restraint.Unit = (int)swsLinearUnit_e.swsLinearUnitMillimeters; errorCode = restraint.RestraintEndEdit(); checkStatus("Apply Displacement BC", errorCode); } model.ClearSelection2(true); } void createMesh() { double averageGlobalElementSize = 0; double tolerance = 0; ICWMesh mesh = (ICWMesh)study.Mesh; Debug.Assert(mesh != null); mesh.Quality = 1; mesh.GetDefaultElementSizeAndTolerance(0, out averageGlobalElementSize, out tolerance); int errorCode = study.CreateMesh(0, averageGlobalElementSize, tolerance); checkStatus("Mesh failed", errorCode); } void runAnalysis() { int errorCode = study.RunAnalysis(); checkStatus("runAnalysis", errorCode); } void showResult(int elementNum) { ICWResults result = (ICWResults)study.Results; int errorCode = 0; object[] stress = (object[])result.GetMinMaxStress((int)swsStressComponent_e.swsStressComponentVON, elementNum, 1, null, (int)swsStrengthUnit_e.swsStrengthUnitPascal, out errorCode); checkStatus("getMinMaxStress", errorCode); swApp.SendMsgToUser("Element Number :" + elementNum + "\n" + "Min Stress at " + Convert.ToString(stress[0]) + ": " + Convert.ToString(stress[1]) + "\n" + "Max Stress at " + Convert.ToString(stress[2]) + ": " + Convert.ToString(stress[3])); } // エラーが発生したときに,発生したメソッド名とエラーコードを表示するためのメソッド public static void checkStatus(string methodName, int code) { int success = 0; if (code != success) { Console.WriteLine(methodName + ": " + code); } } static void Main(string[] args) { var p = new Program(); Console.WriteLine("Starting SolidWorks"); p.startSW(); Console.WriteLine("Creating a Cube"); double length = 0.1; p.createCube(length); Console.WriteLine("Loading Simulation AddIn"); p.loadSimulationAddIn(); Console.WriteLine("Creating Static Study"); string studyName = "StaticStudy"; p.createStaticStudy(studyName); Console.WriteLine("Applying Materials"); int componentNum = 0; int bodyNum = 0; string materialName = "Gray Cast Iron (SN)"; p.applyMaterial(componentNum, bodyNum, materialName); Console.WriteLine("Applying Fixed BC"); p.applyFixedBC(); p.applyDisplacementBC(); Console.WriteLine("Creating Mesh"); p.createMesh(); Console.WriteLine("Running Analysis"); p.runAnalysis(); Console.WriteLine("Getting MinMaxStresses"); int elementNum = 0; p.showResult(elementNum); } } } |
解説 : runAnalysis
runAnalysisは,シミュレーションを実行するためのメソッドです.
1 2 3 4 5 |
void runAnalysis() { int errorCode = study.RunAnalysis(); checkStatus("runAnalysis", errorCode); } |
3行目
Simulation スタディで設定したシミュレーションを実行しています.
RunAnalysis
戻り値でエラーの種類を判別することができます.
swsRunAnalysisError_e
解説 : showResult
showResultは,要素番号[elementNum]を指定して,応力の最大値と最少値を表示するメソッドです.節点番号も一緒に表示します.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
void showResult(int elementNum) { ICWResults result = (ICWResults)study.Results; int errorCode = 0; object[] stress = (object[])result.GetMinMaxStress((int)swsStressComponent_e.swsStressComponentVON, elementNum, 1, null, (int)swsStrengthUnit_e.swsStrengthUnitPascal, out errorCode); checkStatus("getMinMaxStress", errorCode); swApp.SendMsgToUser("Element Number :" + elementNum + "\n" + "Min Stress at " + Convert.ToString(stress[0]) + ": " + Convert.ToString(stress[1]) + "\n" + "Max Stress at " + Convert.ToString(stress[2]) + ": " + Convert.ToString(stress[3]) ); } |
3行目
Simulation結果にアクセスするためのオブジェクトを取得しています.
GetMinMaxStress
6行目
応力の種類,要素,解析のステップを指定して,指定した応力の最大値と最小値を取得しています.
System.object GetMinMaxStress (
System.int NComponent, // 応力の種類 (swsStressComponent_e)
System.int NElementNumber, // 要素番号
System.int NStepNum, // 解析のステップ(静解析の場合:1)
System.object DispPlane, // 方向を定義するための参照図形
System.int NUnits, // 単位 (swsStrengthUnit_e)
out System.int ErrorCode // エラー (swsResultsError_e)
)
今回は,応力の種類をミーゼス応力,単位をPaとしています.
戻り値はobject型となっていますが,実際の型はobjectの配列です.
配列の中身は,
0番目の要素:最小応力の節点番号
1番目の要素:最小応力の値
2番目の要素:最大応力の節点番号
3番目の要素:最大応力の値
となります.
13行目
指定された要素のミーゼス応力の最大値と最小値を表示しています.節点番号も表示しています.
SendMsgToUser
まとめ
Simulation スタディで解析を実行する方法を説明しました.
シミュレーション結果から,要素を指定してミーゼス応力の最小値と最大値を取得する方法を説明しました.
作成したプログラムの動作を確認しました.
Post Thumbnail by Los Alamos National Labratory