[C# SolidWorks API] 固定&変位境界条件を設定する
境界条件とは,シミュレーションを行うときに対象物(設計した部品や製品)に負荷する荷重や温度,変位などの条件のことです.
境界条件を設定する手順は次の通りです.
1. 条件を設定する対象を選択する
2. 条件を設定する
ここでは,固定条件と変位条件を設定してみます.
実行例
下図のような100 mm角の立方体に,下面を固定する,上面を10 mm下方へ押し込むという境界条件を設定します.
プログラムを実行すると,拘束に固定-1と平面上-1の条件が追加されます.
固定-1を選択して詳細を確認すると,下面に”固定ジオメトリ”が追加されていることがわかります.
平面上-1を選択して詳細を確認すると,上面に”平面上: 参照面に垂直 (mm)”が追加されていることがわかります.
コード
コード全体を確認しましょう.Mainメソッドを確認すると分かるように,SolidWorksを起動 > 立方体を造る > Simulation AddInを読み込む > Simulationスタディを作る > 境界条件を設定するという順番で実行されます.
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 |
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 BoundaryCondition { 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 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); } // エラーが発生したときに,発生したメソッド名とエラーコードを表示するためのメソッド 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 Fixed BC"); p.applyFixedBC(); p.applyDisplacementBC(); } } } |
境界条件を設定するために,applyFixedBCとapplyDisplacementBCの2つのメソッドを定義しています.以下では,その詳細を確認していきましょう.
解説 :applyFixedBC
applyFixedBCは,固定境界条件を与えるを与えるメソッドです.
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 |
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); } |
6,7行目
座標値をもとにして,境界条件を設定する面(下面)を選択しています.
ISelectionMgr
12行目
下面のオブジェクトを取得しています.
GetSelectedObject6
16行目
荷重および拘束条件を設定するためのオブジェクトを取得しています.
ICWLoadsAndRestraintsManager
18行目
固定境界条件を設定しています.
AddRestraint
CWRestraint AddRestraint(
System.int NRestraintType, // 拘束の種類:swsRestraintType_e
System.object DispArray, // 拘束を適用する対象の配列
System.object RefGeom, // 方向を指定するための参照形状
// (必要に応じて参照面など指定します)
out System.int ErrorCode // エラーコード:swsRestraintError_e
)
固定境界条件を指定するため,拘束の種類をswsRestraintTypeFixedとしています.
解説:applyDisplacementBC
applyDisplacementBCは,変位境界条件を与えるメソッドです.
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 |
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); } |
6,7行目
座標値をもとにして,境界条件を設定する面(上面)を選択しています.
ISelectionMgr
12行目
下面のオブジェクトを取得しています.
GetSelectedObject6
16行目
荷重および拘束条件を設定するためのオブジェクトを取得しています.
ICWLoadsAndRestraintsManager
18行目
変位境界条件を設定しています.
拘束の種類に,swsRestraintTypeFlatFaceを設定しています.
21行目
荷重および拘束条件を内容を編集できるようにしています.
RestraintBeginEdit
22行目
変位量を編集しています.
void SetTranslationComponentsValues(
System.int BVal1, // 基準面1に平行な方向に変位させる場合は1
// 変位させない場合は0
System.int BVal2, // 基準面2に平行な方向に変位させる場合は1
// 変位させない場合は0
System.int BVal3, // 参照面に垂直な方向に変位させる場合は1
// 変位させない場合は0
System.double DVal1, // 基準面1に平行な方向の変位量
System.double DVal2, // 基準面2に平行な方向の変位量
System.double DVal3 // 参照面に垂直な方向の変位量
)
今回の場合は,
基準面1に平行な平面 =z軸の負の方向
基準面2に平行な平面 = x軸の正の方向
となっています.
選択した面によって方向の定義が複雑になる場合があります.選択した方向の定義は,GUI上で確認しておくと間違いがないかと思います.下の図のようにして,方向を確認することができます.拘束で,①平面上を選択,②平面を選択,③方向を選択します.選択した面での基準面の方向が矢印で描画されます.
拘束の種類にswsRestraintTypeFlatFaceを設定すると,
参照面=選択した面
となります.どちらの方向が正の方向になるかについても,GUIで確認しておいた方が良いでしょう.
23行目
変位量の単位をmmに設定しています.
Unit
swsLinearUnit_e
24行目
拘束条件の編集を終了しています.
RestraintEndEdit
まとめ
固定境界条件を設定する方法を説明しました.
変位境界条件を設定する方法を説明しました.
APIで境界条件を設定するプログラムを作成し,動作を確認しました.