Sap2000官方API文档中的C#示例(已修改原有错误并加中文注释)
1. Create a new Visual C# 2010 project. 新建Visual C# 2010工程
2. Add a reference (COM) in the project to the Sap2000.exe. 添加Sap2000.exe引用到工程
3. Add a new button to a form in the project. 在窗体上添加一个新按钮
4. Double click on the new button to bring up the Click procedure for the button and paste in the following code. 双击新建的按钮添加如下代码
PS:记得添加 using System.Windows.Forms; using Sap2000;
//dimension variables 定义变量
Sap2000.SapObject SapObject;
Sap2000.cSapModel SapModel;
int ret;
int i;
double[] ModValue;
double[] PointLoadValue;
bool[] Restraint;
string[] FrameName;
string[] PointName;
int NumberResults;
string[] Obj;
string[] Elm;
string[] LoadCase;
string[] StepType;
double[] StepNum;
double[] U1;
double[] U2;
double[] U3;
double[] R1;
double[] R2;
double[] R3;
double[] SapResult;
double[] IndResult;
double[] PercentDiff;
string[] SapResultString;
string[] IndResultString;
string[] PercentDiffString;
string msg;
string temp_string1;
string temp_string2;
bool temp_bool;
//create Sap2000 object 创建Sap2000对象
SapObject = new Sap2000.SapObject();
//start Sap2000 application 运行Sap2000程序
temp_bool = true;
SapObject.ApplicationStart(Sap2000.eUnits.kip_in_F, temp_bool, "");
//create SapModel object 创建模型对象
SapModel = SapObject.SapModel;
//initialize model 初始化模型
ret = SapModel.InitializeNewModel((Sap2000.eUnits.kip_in_F));
//create new blank model 新建空白模型
ret = SapModel.File.NewBlank();
//define material property 定义材料属性
ret = SapModel.PropMaterial.SetMaterial("CONC", Sap2000.eMatType.MATERIAL_CONCRETE, -1, "", "");
//assign isotropic mechanical properties to material 添加材料各向同性力学属性
ret = SapModel.PropMaterial.SetMPIsotropic("CONC", 3600, 0.2, 0.0000055, 0);
//define rectangular frame section property 定义矩形框架截面属性
ret = SapModel.PropFrame.SetRectangle("R1", "CONC", 12, 12, -1, "", "");
//define frame section property modifiers 定义矩形框架截面属性修改器
ModValue = new double[8];
for (i = 0; i <= 7; i++)
{
ModValue[i] = 1;
}
ModValue[0] = 1000;
ModValue[1] = 0;
ModValue[2] = 0;
System.Array temp_SystemArray = ModValue;
ret = SapModel.PropFrame.SetModifiers("R1", ref temp_SystemArray);
//switch to k-ft units 切换至kip_ft_F单位
ret = SapModel.SetPresentUnits(Sap2000.eUnits.kip_ft_F);
//add frame object by coordinates 通过坐标系添加框架对象
FrameName = new string[3];
temp_string1 = FrameName[0];
temp_string2 = FrameName[0];
ret = SapModel.FrameObj.AddByCoord(0, 0, 0, 0, 0, 10, ref temp_string1, "R1", "1", "Global");
FrameName[0] = temp_string1;
ret = SapModel.FrameObj.AddByCoord(0, 0, 10, 8, 0, 16, ref temp_string1, "R1", "2", "Global");
FrameName[1] = temp_string1;
ret = SapModel.FrameObj.AddByCoord(-4, 0, 10, 0, 0, 10, ref temp_string1, "R1", "3", "Global");
FrameName[2] = temp_string1;
//assign point object restraint at base 在底部添加节点约束
PointName = new string[2];
Restraint = new bool[6];
for (i = 0; i <= 3; i++)
{
Restraint[i] = true;
}
for (i = 4; i <= 5; i++)
{
Restraint[i] = false;
}
ret = SapModel.FrameObj.GetPoints(FrameName[0], ref temp_string1, ref temp_string2);
PointName[0] = temp_string1;
PointName[1] = temp_string2;
System.Array temp_SystemArray1 = Restraint;
ret = SapModel.PointObj.SetRestraint(PointName[0], ref temp_SystemArray1, 0);
//assign point object restraint at top 在顶部添加节点约束
for (i = 0; i <= 1; i++)
{
Restraint[i] = true;
}
for (i = 2; i <= 5; i++)
{
Restraint[i] = false;
}
ret = SapModel.FrameObj.GetPoints(FrameName[1], ref temp_string1, ref temp_string2);
PointName[0] = temp_string1;
PointName[1] = temp_string2;
temp_SystemArray1 = Restraint;
ret = SapModel.PointObj.SetRestraint(PointName[1], ref temp_SystemArray1, 0);
//refresh view, update (initialize) zoom 刷新视图,更新图像
temp_bool = false;
ret = SapModel.View.RefreshView(0, temp_bool);
//add load patterns 添加荷载类型
temp_bool = true;
ret = SapModel.LoadPatterns.Add("1", Sap2000.eLoadPatternType.LTYPE_OTHER, 1, temp_bool);
ret = SapModel.LoadPatterns.Add("2", Sap2000.eLoadPatternType.LTYPE_OTHER, 0, temp_bool);
ret = SapModel.LoadPatterns.Add("3", Sap2000.eLoadPatternType.LTYPE_OTHER, 0, temp_bool);
ret = SapModel.LoadPatterns.Add("4", Sap2000.eLoadPatternType.LTYPE_OTHER, 0, temp_bool);
ret = SapModel.LoadPatterns.Add("5", Sap2000.eLoadPatternType.LTYPE_OTHER, 0, temp_bool);
ret = SapModel.LoadPatterns.Add("6", Sap2000.eLoadPatternType.LTYPE_OTHER, 0, temp_bool);
ret = SapModel.LoadPatterns.Add("7", Sap2000.eLoadPatternType.LTYPE_OTHER, 0, temp_bool);
//assign loading for load pattern 2 为荷载类型2添加荷载
ret = SapModel.FrameObj.GetPoints(FrameName[2], ref temp_string1, ref temp_string2);
PointName[0] = temp_string1;
PointName[1] = temp_string2;
PointLoadValue = new double[6];
PointLoadValue[2] = -10;
temp_SystemArray1 = PointLoadValue;
ret = SapModel.PointObj.SetLoadForce(PointName[0], "2", ref temp_SystemArray1, false, "Global", 0);
ret = SapModel.FrameObj.SetLoadDistributed(FrameName[2], "2", 1, 10, 0, 1, 1.8, 1.8, "Global", System.Convert.ToBoolean(-1), System.Convert.ToBoolean(-1), 0);
//assign loading for load pattern 3 为荷载类型3添加荷载
ret = SapModel.FrameObj.GetPoints(FrameName[2], ref temp_string1, ref temp_string2);
PointName[0] = temp_string1;
PointName[1] = temp_string2;
PointLoadValue = new double[6];
PointLoadValue[2] = -17.2;
PointLoadValue[4] = -54.4;
temp_SystemArray1 = PointLoadValue;
ret = SapModel.PointObj.SetLoadForce(PointName[1], "3", ref temp_SystemArray1, false, "Global", 0);
//assign loading for load pattern 4 为荷载类型4添加荷载
ret = SapModel.FrameObj.SetLoadDistributed(FrameName[1], "4", 1, 11, 0, 1, 2, 2, "Global", System.Convert.ToBoolean(-1), System.Convert.ToBoolean(-1), 0);
//assign loading for load pattern 5 为荷载类型5添加荷载
ret = SapModel.FrameObj.SetLoadDistributed(FrameName[0], "5", 1, 2, 0, 1, 2, 2, "Local", System.Convert.ToBoolean(-1), System.Convert.ToBoolean(-1), 0);
ret = SapModel.FrameObj.SetLoadDistributed(FrameName[1], "5", 1, 2, 0, 1, -2, -2, "Local", System.Convert.ToBoolean(-1), System.Convert.ToBoolean(-1), 0);
//assign loading for load pattern 6 为荷载类型6添加荷载
ret = SapModel.FrameObj.SetLoadDistributed(FrameName[0], "6", 1, 2, 0, 1, 0.9984, 0.3744, "Local", System.Convert.ToBoolean(-1), System.Convert.ToBoolean(-1), 0);
ret = SapModel.FrameObj.SetLoadDistributed(FrameName[1], "6", 1, 2, 0, 1, -0.3744, 0, "Local", System.Convert.ToBoolean(-1), System.Convert.ToBoolean(-1), 0);
//assign loading for load pattern 7 为荷载类型7添加荷载
ret = SapModel.FrameObj.SetLoadPoint(FrameName[1], "7", 1, 2, 0.5, -15, "Local", System.Convert.ToBoolean(-1), System.Convert.ToBoolean(-1), 0);
//switch to k-in units 切换至kip_in_F单位
ret = SapModel.SetPresentUnits(Sap2000.eUnits.kip_in_F);
//save model 保存模型(官方原代码未创建文件夹导致计算不出结果)
Directory.CreateDirectory("C:\\SapAPI");
ret = SapModel.File.Save("C:\\SapAPI\\API_1-001.sdb");
//run model (this will create the analysis model) 运行分析模型(该命令将自动创建分析模型)
ret = SapModel.Analyze.RunAnalysis();
//initialize for Sap2000 results 初始化Sap2000分析结果
SapResult = new double[7];
ret = SapModel.FrameObj.GetPoints(FrameName[1], ref temp_string1, ref temp_string2);
PointName[0] = temp_string1;
PointName[1] = temp_string2;
//get Sap2000 results for load patterns 1 through 7 获取荷载类型1~7的分析结果
NumberResults = 0;
Obj = new string[1];
Elm = new string[1];
LoadCase = new string[1];
StepType = new string[1];
StepNum = new double[1];
U1 = new double[1];
U2 = new double[1];
U3 = new double[1];
R1 = new double[1];
R2 = new double[1];
R3 = new double[1];
for (i = 0; i <= 6; i++)
{
ret = SapModel.Results.Setup.DeselectAllCasesAndCombosForOutput();
ret = SapModel.Results.Setup.SetCaseSelectedForOutput(System.Convert.ToString(i + 1), System.Convert.ToBoolean(-1));
if (i <= 3)
{
System.Array temp_SystemArray2 = Obj;
System.Array temp_SystemArray3 = Elm;
System.Array temp_SystemArray4 = LoadCase;
System.Array temp_SystemArray5 = StepType;
System.Array temp_SystemArray6 = StepNum;
System.Array temp_SystemArray7 = U1;
System.Array temp_SystemArray8 = U2;
System.Array temp_SystemArray9 = U3;
System.Array temp_SystemArray10 = R1;
System.Array temp_SystemArray11 = R2;
System.Array temp_SystemArray12 = R3;
ret = SapModel.Results.JointDispl(PointName[1], Sap2000.eItemTypeElm.ObjectElm, ref NumberResults, ref temp_SystemArray2, ref temp_SystemArray3, ref temp_SystemArray4, ref temp_SystemArray5, ref temp_SystemArray6, ref temp_SystemArray7, ref temp_SystemArray8, ref temp_SystemArray9, ref temp_SystemArray10, ref temp_SystemArray11, ref temp_SystemArray12);
temp_SystemArray9.CopyTo(U3, 0);
SapResult[i] = U3[0];
}
else
{
System.Array temp_SystemArray2 = Obj;
System.Array temp_SystemArray3 = Elm;
System.Array temp_SystemArray4 = LoadCase;
System.Array temp_SystemArray5 = StepType;
System.Array temp_SystemArray6 = StepNum;
System.Array temp_SystemArray7 = U1;
System.Array temp_SystemArray8 = U2;
System.Array temp_SystemArray9 = U3;
System.Array temp_SystemArray10 = R1;
System.Array temp_SystemArray11 = R2;
System.Array temp_SystemArray12 = R3;
ret = SapModel.Results.JointDispl(PointName[0], Sap2000.eItemTypeElm.ObjectElm, ref NumberResults, ref temp_SystemArray2, ref temp_SystemArray3, ref temp_SystemArray4, ref temp_SystemArray5, ref temp_SystemArray6, ref temp_SystemArray7, ref temp_SystemArray8, ref temp_SystemArray9, ref temp_SystemArray10, ref temp_SystemArray11, ref temp_SystemArray12);
temp_SystemArray7.CopyTo(U1, 0);
SapResult[i] = U1[0];
}
}
//close Sap2000 关闭Sap2000程序
SapObject.ApplicationExit(false);
SapModel = null;
SapObject = null;
//fill Sap2000 result strings 格式化Sap2000分析结果
SapResultString = new string[7];
for (i = 0; i <= 6; i++)
{
SapResultString[i] = string.Format("{0:0.00000}", SapResult[i]);
ret = (string.Compare(SapResultString[i], 1, "-", 1, 1, true));
if (ret != 0)
{
SapResultString[i] = " " + SapResultString[i];
}
}
//fill independent results 格式化独立数据
IndResult = new double[7];
IndResultString = new string[7];
IndResult[0] = -0.02639;
IndResult[1] = 0.06296;
IndResult[2] = 0.06296;
IndResult[3] = -0.2963;
IndResult[4] = 0.3125;
IndResult[5] = 0.11556;
IndResult[6] = 0.00651;
for (i = 0; i <= 6; i++)
{
IndResultString[i] = string.Format("{0:0.00000}", IndResult[i]);
ret = (string.Compare(IndResultString[i], 1, "-", 1, 1, true));
if (ret != 0)
{
IndResultString[i] = " " + IndResultString[i];
}
}
//fill percent difference 格式化百分比
PercentDiff = new double[7];
PercentDiffString = new string[7];
for (i = 0; i <= 6; i++)
{
PercentDiff[i] = (SapResult[i] / IndResult[i]) - 1;
PercentDiffString[i] = string.Format("{0:0%}", PercentDiff[i]);
ret = (string.Compare(PercentDiffString[i], 1, "-", 1, 1, true));
if (ret != 0)
{
PercentDiffString[i] = " " + PercentDiffString[i];
}
}
//display message box comparing results 通过消息框显示输出比较结果
msg = "";
msg = msg + "LC Sap2000 Independent %Diff\r\n";
for (i = 0; i <= 5; i++)
{
msg = msg + string.Format("{0:0}", i + 1) + " " + SapResultString[i] + " " + IndResultString[i] + " " + PercentDiffString[i] + "\r\n";
}
msg = msg + string.Format("{0:0}", i + 1) + " " + SapResultString[i] + " " + IndResultString[i] + " " + PercentDiffString[i];
MessageBox.Show(msg);

浙公网安备 33010602011771号