api examples

40
Example 1 Remarks This example is written for Visual Basic for Applications (VBA). It can be run from a program such as Microsoft Excel. It is based on the SAP2000 verification problem Example 1-001. This example creates the example verification problem from scratch, runs the analysis, extracts results, and compares the results with hand-calculated values. Example Sub VerificationExample1001() 'dimension variables Dim SapObject As SAP2000.SapObject Dim SapModel As cSapModel Dim ret As Long Dim i As Long Dim ModValue() As Double Dim PointLoadValue() As Double Dim Restraint() As Boolean Dim FrameName() As String Dim PointName() As String Dim NumberResults As Long Dim Obj() As String Dim Elm() As String Dim LoadCase() As String Dim StepType() As String Dim StepNum() As Double Dim U1() As Double Dim U2() As Double Dim U3() As Double Dim R1() As Double Dim R2() As Double Dim R3() As Double Dim SapResult() As Double Dim IndResult() As Double Dim PercentDiff() As Double Dim SapResultString() As String Dim IndResultString() As String Dim PercentDiffString() As String Dim msg As String 'create Sap2000 object Set SapObject = New SAP2000.SapObject 'start Sap2000 application SapObject.ApplicationStart Page 1 of 40 Example 1 (VBA) 12-04-2011 file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Upload: christian-audet

Post on 26-Dec-2014

1.805 views

Category:

Documents


31 download

TRANSCRIPT

Page 1: API Examples

Example 1

Remarks

This example is written for Visual Basic for Applications (VBA). It can be run from a program such as Microsoft Excel. It is based on the SAP2000 verification problem Example 1-001.

This example creates the example verification problem from scratch, runs the analysis, extracts results, and compares the results with hand-calculated values.

Example

Sub VerificationExample1001() 'dimension variables Dim SapObject As SAP2000.SapObject Dim SapModel As cSapModel Dim ret As Long Dim i As Long Dim ModValue() As Double Dim PointLoadValue() As Double Dim Restraint() As Boolean Dim FrameName() As String Dim PointName() As String Dim NumberResults As Long Dim Obj() As String Dim Elm() As String Dim LoadCase() As String Dim StepType() As String Dim StepNum() As Double Dim U1() As Double Dim U2() As Double Dim U3() As Double Dim R1() As Double Dim R2() As Double Dim R3() As Double Dim SapResult() As Double Dim IndResult() As Double Dim PercentDiff() As Double Dim SapResultString() As String Dim IndResultString() As String Dim PercentDiffString() As String Dim msg As String 'create Sap2000 object Set SapObject = New SAP2000.SapObject 'start Sap2000 application SapObject.ApplicationStart

Page 1 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 2: API Examples

'create SapModel object Set SapModel = SapObject.SapModel 'initialize model ret = SapModel.InitializeNewModel 'create new blank model ret = SapModel.File.NewBlank 'define material property ret = SapModel.PropMaterial.SetMaterial("CONC", MATERIAL_CONCRETE) 'assign isotropic mechanical properties to material ret = SapModel.PropMaterial.SetMPIsotropic("CONC", 3600, 0.2, 0.0000055) 'define rectangular frame section property ret = SapModel.PropFrame.SetRectangle("R1", "CONC", 12, 12) 'define frame section property modifiers ReDim ModValue(7) For i = 0 To 7 ModValue(i) = 1 Next i ModValue(0) = 1000 ModValue(1) = 0 ModValue(2) = 0 ret = SapModel.PropFrame.SetModifiers("R1", ModValue) 'switch to k-ft units ret = SapModel.SetPresentUnits(kip_ft_F) 'add frame object by coordinates ReDim FrameName(2) ret = SapModel.FrameObj.AddByCoord(0, 0, 0, 0, 0, 10, FrameName(0), "R1", "1") ret = SapModel.FrameObj.AddByCoord(0, 0, 10, 8, 0, 16, FrameName(1), "R1", "2") ret = SapModel.FrameObj.AddByCoord(-4, 0, 10, 0, 0, 10, FrameName(2), "R1", "3") 'assign point object restraint at base ReDim PointName(1) ReDim Restraint(5) For i = 0 To 3 Restraint(i) = True Next i For i = 4 To 5 Restraint(i) = False Next i ret = SapModel.FrameObj.GetPoints(FrameName(0), PointName(0), PointName(1)) ret = SapModel.PointObj.SetRestraint(PointName(0), Restraint) 'assign point object restraint at top For i = 0 To 1

Page 2 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 3: API Examples

Restraint(i) = True Next i For i = 2 To 5 Restraint(i) = False Next i ret = SapModel.FrameObj.GetPoints(FrameName(1), PointName(0), PointName(1)) ret = SapModel.PointObj.SetRestraint(PointName(1), Restraint) 'refresh view, update (initialize) zoom ret = SapModel.View.RefreshView(0, False) 'add load patterns ret = SapModel.LoadPatterns.Add("1", LTYPE_OTHER, 1) ret = SapModel.LoadPatterns.Add("2", LTYPE_OTHER) ret = SapModel.LoadPatterns.Add("3", LTYPE_OTHER) ret = SapModel.LoadPatterns.Add("4", LTYPE_OTHER) ret = SapModel.LoadPatterns.Add("5", LTYPE_OTHER) ret = SapModel.LoadPatterns.Add("6", LTYPE_OTHER) ret = SapModel.LoadPatterns.Add("7", LTYPE_OTHER) 'assign loading for load pattern 2 ret = SapModel.FrameObj.GetPoints(FrameName(2), PointName(0), PointName(1)) ReDim PointLoadValue(5) PointLoadValue(2) = -10 ret = SapModel.PointObj.SetLoadForce(PointName(0), "2", PointLoadValue) ret = SapModel.FrameObj.SetLoadDistributed(FrameName(2), "2", 1, 10, 0, 1, 1.8, 1.8) 'assign loading for load pattern 3 ret = SapModel.FrameObj.GetPoints(FrameName(2), PointName(0), PointName(1)) ReDim PointLoadValue(5) PointLoadValue(2) = -17.2 PointLoadValue(4) = -54.4 ret = SapModel.PointObj.SetLoadForce(PointName(1), "3", PointLoadValue) 'assign loading for load pattern 4 ret = SapModel.FrameObj.SetLoadDistributed(FrameName(1), "4", 1, 11, 0, 1, 2, 2) 'assign loading for load pattern 5 ret = SapModel.FrameObj.SetLoadDistributed(FrameName(0), "5", 1, 2, 0, 1, 2, 2, "Local") ret = SapModel.FrameObj.SetLoadDistributed(FrameName(1), "5", 1, 2, 0, 1, -2, -2, "Local") 'assign loading for load pattern 6 ret = SapModel.FrameObj.SetLoadDistributed(FrameName(0), "6", 1, 2, 0, 1, 0.9984, 0.3744, "Local") ret = SapModel.FrameObj.SetLoadDistributed(FrameName(1), "6", 1, 2, 0, 1, -0.3744, 0, "Local") 'assign loading for load pattern 7 ret = SapModel.FrameObj.SetLoadPoint(FrameName(1), "7", 1, 2, 0.5, -15, "Local") 'switch to k-in units ret = SapModel.SetPresentUnits(kip_in_F)

Page 3 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 4: API Examples

'save model 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 ReDim SapResult(6) ret = SapModel.FrameObj.GetPoints(FrameName(1), PointName(0), PointName(1)) 'get Sap2000 results for load patterns 1 through 7 For i = 0 To 6 ret = SapModel.Results.Setup.DeselectAllCasesAndCombosForOutput ret = SapModel.Results.Setup.SetCaseSelectedForOutput(Format(i + 1)) If i <= 3 Then ret = SapModel.Results.JointDispl(PointName(1), ObjectElm, NumberResults, Obj, Elm, LoadCase, StepType, StepNum, U1, U2, U3, R1, R2, R3) SapResult(i) = U3(0) Else ret = SapModel.Results.JointDispl(PointName(0), ObjectElm, NumberResults, Obj, Elm, LoadCase, StepType, StepNum, U1, U2, U3, R1, R2, R3) SapResult(i) = U1(0) End If Next i 'close Sap2000 SapObject.ApplicationExit False Set SapModel = Nothing Set SapObject = Nothing 'fill Sap2000 result strings ReDim SapResultString(6) For i = 0 To 6 SapResultString(i) = Format(SapResult(i), "0.00000") If Left(SapResultString(i), 1) <> "-" Then SapResultString(i) = " " & SapResultString(i) End If Next i 'fill independent results (hand calculated) ReDim IndResult(6) ReDim IndResultString(6) 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 To 6

Page 4 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 5: API Examples

IndResultString(i) = Format(IndResult(i), "0.00000") If Left(IndResultString(i), 1) <> "-" Then IndResultString(i) = " " & IndResultString(i) End If Next i 'fill percent difference ReDim PercentDiff(6) ReDim PercentDiffString(6) For i = 0 To 6 PercentDiff(i) = (SapResult(i) / IndResult(i)) - 1 PercentDiffString(i) = Format(PercentDiff(i), "0%") If Left(PercentDiffString(i), 1) <> "-" Then PercentDiffString(i) = " " & PercentDiffString(i) End If Next i 'display message box comparing results msg = "" msg = msg & "LC Sap2000 Independent %Diff" & vbCr & vbLf For i = 0 To 5 msg = msg & Format(i + 1) & " " & SapResultString(i) & " " & IndResultString(i) & " " & PercentDiffString(i) & vbCr & vbLf Next i msg = msg & Format(i + 1) & " " & SapResultString(i) & " " & IndResultString(i) & " " & PercentDiffString(i) MsgBox msg End Sub

Release Notes

Initial release in version 11.03.

Changed nomenclature from Load Cases, Analysis Cases and Response Combinations to Load Patterns, Load Cases and Load Combinations, respectively, in version 12.00.

Example 2 (Visual Basic 2005)

Remarks

This example is written for Visual Basic 2005. It is based on the SAP2000 verification problem Example 1-001.

This example creates the example verification problem from scratch, runs the analysis, extracts results, and compares the results with hand-calculated values.

Example

Page 5 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 6: API Examples

1. Create a new Visual Basic 2005 project.

2. Add a reference (COM) in the project to the 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.

'dimension variables Dim SapObject As SAP2000.SapObject Dim SapModel As SAP2000.cSapModel Dim ret As Long Dim i As Long Dim ModValue() As Double Dim PointLoadValue() As Double Dim Restraint() As Boolean Dim FrameName() As String Dim PointName() As String Dim NumberResults As Long Dim Obj() As String Dim Elm() As String Dim LoadCase() As String Dim StepType() As String Dim StepNum() As Double Dim U1() As Double Dim U2() As Double Dim U3() As Double Dim R1() As Double Dim R2() As Double Dim R3() As Double Dim SapResult() As Double Dim IndResult() As Double Dim PercentDiff() As Double Dim SapResultString() As String Dim IndResultString() As String Dim PercentDiffString() As String Dim msg As String 'create Sap2000 object SapObject = New SAP2000.SapObject 'start Sap2000 application SapObject.ApplicationStart() 'create SapModel object SapModel = SapObject.SapModel 'initialize model ret = SapModel.InitializeNewModel

Page 6 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 7: API Examples

'create new blank model ret = SapModel.File.NewBlank 'define material property ret = SapModel.PropMaterial.SetMaterial("CONC", SAP2000.eMatType.MATERIAL_CONCRETE) 'assign isotropic mechanical properties to material ret = SapModel.PropMaterial.SetMPIsotropic("CONC", 3600, 0.2, 0.0000055) 'define rectangular frame section property ret = SapModel.PropFrame.SetRectangle("R1", "CONC", 12, 12) 'define frame section property modifiers ReDim ModValue(7) For i = 0 To 7 ModValue(i) = 1 Next i ModValue(0) = 1000 ModValue(1) = 0 ModValue(2) = 0 ret = SapModel.PropFrame.SetModifiers("R1", ModValue) 'switch to k-ft units ret = SapModel.SetPresentUnits(SAP2000.eUnits.kip_ft_F) 'add frame object by coordinates ReDim FrameName(2) ret = SapModel.FrameObj.AddByCoord(0, 0, 0, 0, 0, 10, FrameName(0), "R1", "1") ret = SapModel.FrameObj.AddByCoord(0, 0, 10, 8, 0, 16, FrameName(1), "R1", "2") ret = SapModel.FrameObj.AddByCoord(-4, 0, 10, 0, 0, 10, FrameName(2), "R1", "3") 'assign point object restraint at base ReDim PointName(1) ReDim Restraint(5) For i = 0 To 3 Restraint(i) = True Next i For i = 4 To 5 Restraint(i) = False Next i ret = SapModel.FrameObj.GetPoints(FrameName(0), PointName(0), PointName(1)) ret = SapModel.PointObj.SetRestraint(PointName(0), Restraint) 'assign point object restraint at top For i = 0 To 1 Restraint(i) = True Next i For i = 2 To 5 Restraint(i) = False Next i ret = SapModel.FrameObj.GetPoints(FrameName(1), PointName(0), PointName(1))

Page 7 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 8: API Examples

ret = SapModel.PointObj.SetRestraint(PointName(1), Restraint) 'refresh view, update (initialize) zoom ret = SapModel.View.RefreshView(0, False) 'add load patterns ret = SapModel.LoadPatterns.Add("1", SAP2000.eLoadPatternType.LTYPE_OTHER, 1) ret = SapModel.LoadPatterns.Add("2", SAP2000.eLoadPatternType.LTYPE_OTHER) ret = SapModel.LoadPatterns.Add("3", SAP2000.eLoadPatternType.LTYPE_OTHER) ret = SapModel.LoadPatterns.Add("4", SAP2000.eLoadPatternType.LTYPE_OTHER) ret = SapModel.LoadPatterns.Add("5", SAP2000.eLoadPatternType.LTYPE_OTHER) ret = SapModel.LoadPatterns.Add("6", SAP2000.eLoadPatternType.LTYPE_OTHER) ret = SapModel.LoadPatterns.Add("7", SAP2000.eLoadPatternType.LTYPE_OTHER) 'assign loading for load pattern 2 ret = SapModel.FrameObj.GetPoints(FrameName(2), PointName(0), PointName(1)) ReDim PointLoadValue(5) PointLoadValue(2) = -10 ret = SapModel.PointObj.SetLoadForce(PointName(0), "2", PointLoadValue) ret = SapModel.FrameObj.SetLoadDistributed(FrameName(2), "2", 1, 10, 0, 1, 1.8, 1.8) 'assign loading for load pattern 3 ret = SapModel.FrameObj.GetPoints(FrameName(2), PointName(0), PointName(1)) ReDim PointLoadValue(5) PointLoadValue(2) = -17.2 PointLoadValue(4) = -54.4 ret = SapModel.PointObj.SetLoadForce(PointName(1), "3", PointLoadValue) 'assign loading for load pattern 4 ret = SapModel.FrameObj.SetLoadDistributed(FrameName(1), "4", 1, 11, 0, 1, 2, 2) 'assign loading for load pattern 5 ret = SapModel.FrameObj.SetLoadDistributed(FrameName(0), "5", 1, 2, 0, 1, 2, 2, "Local") ret = SapModel.FrameObj.SetLoadDistributed(FrameName(1), "5", 1, 2, 0, 1, -2, -2, "Local") 'assign loading for load pattern 6 ret = SapModel.FrameObj.SetLoadDistributed(FrameName(0), "6", 1, 2, 0, 1, 0.9984, 0.3744, "Local") ret = SapModel.FrameObj.SetLoadDistributed(FrameName(1), "6", 1, 2, 0, 1, -0.3744, 0, "Local") 'assign loading for load pattern 7 ret = SapModel.FrameObj.SetLoadPoint(FrameName(1), "7", 1, 2, 0.5, -15, "Local") 'switch to k-in units ret = SapModel.SetPresentUnits(SAP2000.eUnits.kip_in_F) 'save model ret = SapModel.File.Save("C:\SapAPI\API_1-001.sdb") 'run model (this will create the analysis model) ret = SapModel.Analyze.RunAnalysis

Page 8 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 9: API Examples

'initialize for Sap2000 results ReDim SapResult(6) ret = SapModel.FrameObj.GetPoints(FrameName(1), PointName(0), PointName(1)) 'get Sap2000 results for load patterns 1 through 7 ReDim Obj(0) ReDim Elm(0) ReDim LoadCase(0) ReDim StepType(0) ReDim StepNum(0) ReDim U1(0) ReDim U2(0) ReDim U3(0) ReDim R1(0) ReDim R2(0) ReDim R3(0) For i = 0 To 6 ret = SapModel.Results.Setup.DeselectAllCasesAndCombosForOutput ret = SapModel.Results.Setup.SetCaseSelectedForOutput(Format(i + 1)) If i <= 3 Then ret = SapModel.Results.JointDispl(PointName(1), SAP2000.eItemTypeElm.ObjectElm, NumberResults, Obj, Elm, LoadCase, StepType, StepNum, U1, U2, U3, R1, R2, R3) SapResult(i) = U3(0) Else ret = SapModel.Results.JointDispl(PointName(0), SAP2000.eItemTypeElm.ObjectElm, NumberResults, Obj, Elm, LoadCase, StepType, StepNum, U1, U2, U3, R1, R2, R3) SapResult(i) = U1(0) End If Next i 'close Sap2000 SapObject.ApplicationExit(False) SapModel = Nothing SapObject = Nothing 'fill Sap2000 result strings ReDim SapResultString(6) For i = 0 To 6 SapResultString(i) = Format(SapResult(i), "0.00000") If Microsoft.VisualBasic.Left(SapResultString(i), 1) <> "-" Then SapResultString(i) = " " & SapResultString(i) End If Next i 'fill independent results (hand calculated) ReDim IndResult(6) ReDim IndResultString(6) IndResult(0) = -0.02639 IndResult(1) = 0.06296 IndResult(2) = 0.06296

Page 9 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 10: API Examples

IndResult(3) = -0.2963 IndResult(4) = 0.3125 IndResult(5) = 0.11556 IndResult(6) = 0.00651 For i = 0 To 6 IndResultString(i) = Format(IndResult(i), "0.00000") If Microsoft.VisualBasic.Left(IndResultString(i), 1) <> "-" Then IndResultString(i) = " " & IndResultString(i) End If Next i 'fill percent difference ReDim PercentDiff(6) ReDim PercentDiffString(6) For i = 0 To 6 PercentDiff(i) = (SapResult(i) / IndResult(i)) - 1 PercentDiffString(i) = Format(PercentDiff(i), "0%") If Microsoft.VisualBasic.Left(PercentDiffString(i), 1) <> "-" Then PercentDiffString(i) = " " & PercentDiffString(i) End If Next i 'display message box comparing results msg = "" msg = msg & "LC Sap2000 Independent %Diff" & vbCr & vbLf For i = 0 To 5 msg = msg & Format(i + 1) & " " & SapResultString(i) & " " & IndResultString(i) & " " & PercentDiffString(i) & vbCr & vbLf Next i msg = msg & Format(i + 1) & " " & SapResultString(i) & " " & IndResultString(i) & " " & PercentDiffString(i) MsgBox(msg)

Release Notes

Initial release in version 11.03.

Changed nomenclature from Load Cases, Analysis Cases and Response Combinations to Load Patterns, Load Cases and Load Combinations, respectively, in version 12.00.

Example 3 (Visual C# 2005)

Remarks

This example is written for Visual C# 2005. It is based on the Sap2000 verification problem Example 1-001.

This example creates the example verification problem from scratch, runs the analysis, extracts results, and compares the results with hand calculated values.

Page 10 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 11: API Examples

Example

1. Create a new Visual C# 2005 project.

2. Add a reference (COM) in the project to the 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.

//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 SapObject = new SAP2000.SapObject(); //start Sap2000 application temp_bool = true; SapObject.ApplicationStart(SAP2000.eUnits.kip_in_F, temp_bool, "");

Page 11 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 12: API Examples

//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 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++) {

Page 12 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 13: API Examples

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, ref temp_bool); //add load patterns temp_bool = true; ret = SapModel.LoadPatterns.Add("1", SAP2000.eLoadPatternType.LTYPE_OTHER, 1, ref temp_bool); ret = SapModel.LoadPatterns.Add("2", SAP2000.eLoadPatternType.LTYPE_OTHER, 0, ref temp_bool); ret = SapModel.LoadPatterns.Add("3", SAP2000.eLoadPatternType.LTYPE_OTHER, 0, ref temp_bool); ret = SapModel.LoadPatterns.Add("4", SAP2000.eLoadPatternType.LTYPE_OTHER, 0, ref temp_bool); ret = SapModel.LoadPatterns.Add("5", SAP2000.eLoadPatternType.LTYPE_OTHER, 0, ref temp_bool); ret = SapModel.LoadPatterns.Add("6", SAP2000.eLoadPatternType.LTYPE_OTHER, 0, ref temp_bool); ret = SapModel.LoadPatterns.Add("7", SAP2000.eLoadPatternType.LTYPE_OTHER, 0, ref temp_bool); //assign loading for load pattern 2 ret = SapModel.FrameObj.GetPoints(FrameName[2], ref temp_string1, ref temp_string2); PointName[0] = temp_string1;

Page 13 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 14: API Examples

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 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 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 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 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 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 ret = SapModel.SetPresentUnits(SAP2000.eUnits.kip_in_F); //save model 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 SapResult = new double[7]; ret = SapModel.FrameObj.GetPoints(FrameName[1], ref temp_string1, ref temp_string2); PointName[0] = temp_string1;

Page 14 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 15: API Examples

PointName[1] = temp_string2; //get Sap2000 results for load patterns 1 through 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;

Page 15 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 16: API Examples

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 SapObject.ApplicationExit(false); SapModel = null; SapObject = null; //fill Sap2000 result strings 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];

Page 16 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 17: API Examples

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);

Release Notes

Initial release in version 11.03.

Changed nomenclature from Load Cases, Analysis Cases and Response Combinations to Load Patterns, Load Cases and Load Combinations, respectively, in version 12.00.

Example 4 (Intel Visual Fortran v9)

Remarks

This example is written for Intel Visual Fortran v9 with Microsoft Visual Studio Integration. It is based on the SAP2000 verification problem Example 1-001.

This example creates the example verification problem from scratch, runs the analysis, extracts results, and compares the results with hand-calculated values.

Example

1. Create an empty Intel Fortran project.

2. Using the Intel Fortran Module Wizard (under Tools menu), browse for Sap2000.exe, and select all members to generate Sap2000.f90. The generated module contains explicit interfaces to all objects and member functions of the SAP2000 API.

Page 17 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 18: API Examples

3. Add an empty source file by right clicking on the “Source Files” folder in Solution Explorer and by selecting Add/New Item/Source.

4. Open the newly added file by double clicking on it and paste in the following code:

program APIExample use Sap2000 ! gives access to SAP2000 API calls implicit none !parameters integer(kind=4), parameter :: nDimCON = 1 ! array dimension !local variables integer(kind=4) :: pSapObject ! pointer to a Sap object integer(kind=4) :: pSapModel ! pointer to a model object integer(kind=4) :: pFile ! pointer to a file object integer(kind=4) :: pPropMaterial ! pointer to a material property object integer(kind=4) :: pPropFrame ! pointer to a model object integer(kind=4) :: pFrameObj ! pointer to a frame object integer(kind=4) :: pView ! pointer to a view object integer(kind=4) :: pPointObj ! pointer to a point object integer(kind=4) :: pLoadPatterns ! pointer to a load patterns object integer(kind=4) :: pAnalyze ! pointer to an analyze object integer(kind=4) :: pAnalysisResults ! pointer to a analysis resutls object integer(kind=4) :: pAnalysisResultsSetup ! pointer to a setup object integer(kind=4) :: iStatus ! error code returned from COM subsystem integer(kind=4) :: iRet ! error code returned from SAP2000 API calls integer(kind=4) :: iCol integer(kind=4) :: iUnits integer(kind=4) :: iColor integer(kind=4) :: iWindow integer(kind=4) :: iItemType integer(kind=4) :: iTypleLoadPat integer(kind=4) :: iMyType integer(kind=4) :: iDir integer(kind=4) :: iNumberResults real(kind=8) :: dE real(kind=8) :: dU real(kind=8) :: dA real(kind=8) :: dTemp real(kind=8) :: dT3 real(kind=8) :: dT2 real(kind=8) :: dModValue real(kind=8) :: dXi

Page 18 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 19: API Examples

real(kind=8) :: dYi real(kind=8) :: dZi real(kind=8) :: dXj real(kind=8) :: dYj real(kind=8) :: dZj real(kind=8) :: dSelfWTMultiplier real(kind=8) :: dDist1 real(kind=8) :: dDist2 real(kind=8) :: dVal1 real(kind=8) :: dVal2 real(kind=8) :: dSapResultsRA1(7) real(kind=8) :: dIndResultsRA1(7) logical(kind=2) :: bVisible logical(kind=2) :: bFileSave logical(kind=2) :: bZoom logical(kind=2) :: bRestrained logical(kind=2) :: bAddLoadCase logical(kind=2) :: bReplace logical(kind=2) :: bRelDist logical(kind=2) :: bSelected character(len=256) :: cFileName character(len=256) :: cNotes character(len=256) :: cGUID character(len=256) :: cCsys character(len=256) :: cLoadPat character(len=256) :: cFrameName(3) character(len=256) :: cPointName(2) ! pointers to SafeArrays type(SA_BOUNDS) :: saBounds ! bounds object defining for SafeArray integer(kind=4) :: pModValueSA ! property modifiers integer(kind=4) :: pRestraintSA ! restraints integer(kind=4) :: pPointLoadValueSA ! load values integer(kind=4) :: pObjSA ! object names integer(kind=4) :: pElmSA ! element names integer(kind=4) :: pLCaseSA ! load case names integer(kind=4) :: pStepTypeSA ! step type names integer(kind=4) :: pStepNumSA ! step numbers integer(kind=4) :: pU1SA ! displacements along u1 integer(kind=4) :: pU2SA ! displacements along u2 integer(kind=4) :: pU3SA ! displacements along u3 integer(kind=4) :: pR1SA ! displacements along r1 integer(kind=4) :: pR2SA ! displacements along r2 integer(kind=4) :: pR3SA ! displacements along r3 !initialize COM call COMInitialize(iStatus)

Page 19 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 20: API Examples

!create Sap2000 object call COMCreateObjectByGUID(CLSID_SapObject, CLSCTX_ALL, IID__SapObject, pSapObject, iStatus) !start Sap2000 application iUnits = kip_in_F bVisible = .TRUE. cFileName = '' iStatus = $$SapObject_ApplicationStart(pSapObject, iUnits, bVisible, cFileName, iRet) !create SapModel object iStatus = $$SapObject_GetSapModel(pSapObject, pSapModel) !initialize model iStatus = $$cSapModel_InitializeNewModel(pSapModel, iUnits, iRet) !create new blank model iStatus = $$cSapModel_GetFile(pSapModel, pFile) iStatus = $$cFile_NewBlank(pFile, iRet) !define material property iStatus = $$cSapModel_GetPropMaterial(pSapModel, pPropMaterial) iColor = -1 cNotes = '' cGUID = '' iStatus = $$cPropMaterial_SetMaterial(pPropMaterial, 'CONC', MATERIAL_CONCRETE, iColor, cNotes, cGUID, iRet) !assign isotropic mechanical properties to material dE = 3600. dU = 0.2 dA = 0.0000055 dTemp = 0. iStatus = $$cPropMaterial_SetMPIsotropic(pPropMaterial, 'CONC', dE, dU, dA, dTemp, iRet) !define rectangular frame section property iStatus = $$cSapModel_GetPropFrame(pSapModel, pPropFrame) dT3 = 12. dT2 = 12. iColor = -1 cNotes = '' cGUID = '' iStatus = $$cPropFrame_SetRectangle(pPropFrame, 'R1', 'CONC', dT3, dT2, iColor, cNotes, cGUID, iRet) !define frame section property modifiers saBounds%lbound = 0 saBounds%extent = 8

Page 20 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 21: API Examples

pModValueSA = SafeArrayCreate(VT_R8, nDimCON, saBounds) dModValue = 1. do iCol = 0, 7 iRet = SafeArrayPutElement(pModValueSA, iCol, loc(dModValue)) end do dModValue = 1000. iRet = SafeArrayPutElement(pModValueSA, 0, loc(dModValue)) dModValue = 0. iRet = SafeArrayPutElement(pModValueSA, 1, loc(dModValue)) iRet = SafeArrayPutElement(pModValueSA, 2, loc(dModValue)) iStatus = $$cPropFrame_SetModifiers(pPropFrame, 'R1', pModValueSA, iRet) iRet = SafeArrayDestroy(pModValueSA) !switch to k-ft units iStatus = $$cSapModel_SetPresentUnits(pSapModel, kip_ft_F, iRet) !add frame object by coordinates cCsys = 'Global' dXi = 0.; dYi = 0.; dZi = 0.; dXj = 0.; dYj = 0.; dZj = 10. iStatus = $$cSapModel_GetFrameObj(pSapModel, pFrameObj) iStatus = $$cFrameObj_AddByCoord(pFrameObj, dXi, dYi, dZi, dXj, dYj, dZj, cFrameName(1), 'R1', '1', cCsys, iRet) dXi = 0.; dYi = 0.; dZi = 10.; dXj = 8.; dYj = 0.; dZj = 16. iStatus = $$cFrameObj_AddByCoord(pFrameObj, dXi, dYi, dZi, dXj, dYj, dZj, cFrameName(2), 'R1', '2', cCsys, iRet) dXi = -4.; dYi = 0.; dZi = 10.; dXj = 0.; dYj = 0.; dZj = 10. iStatus = $$cFrameObj_AddByCoord(pFrameObj, dXi, dYi, dZi, dXj, dYj, dZj, cFrameName(3), 'R1', '3', cCsys, iRet) !assign point object restraint at base saBounds%lbound = 0 saBounds%extent = 6 pRestraintSA = SafeArrayCreate(VT_BOOL, nDimCON, saBounds) bRestrained = .TRUE. do iCol = 0, 3 iRet = SafeArrayPutElement(pRestraintSA, iCol, loc(bRestrained)) end do bRestrained = .FALSE. do iCol = 4, 5 iRet = SafeArrayPutElement(pRestraintSA, iCol, loc(bRestrained)) end do iStatus = $$cFrameObj_GetPoints(pFrameObj, cFrameName(1), cPointName(1), cPointName(2), iRet) iStatus = $$cSapModel_GetPointObj(pSapModel, pPointObj) iItemType = Object iStatus = $$cPointObj_SetRestraint(pPointObj, cPointName(1), pRestraintSA, iItemType, iRet)

Page 21 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 22: API Examples

!assign point object restraint at top bRestrained = .TRUE. do iCol = 0, 1 iRet = SafeArrayPutElement(pRestraintSA, iCol, loc(bRestrained)) end do bRestrained = .FALSE. do iCol = 2, 5 iRet = SafeArrayPutElement(pRestraintSA, iCol, loc(bRestrained)) end do iStatus = $$cFrameObj_GetPoints(pFrameObj, cFrameName(2), cPointName(1), cPointName(2), iRet) iItemType = Object iStatus = $$cPointObj_SetRestraint(pPointObj, cPointName(2), pRestraintSA, iItemType, iRet) iRet = SafeArrayDestroy(pRestraintSA) !refresh view, update (initialize) zoom iStatus = $$cSapModel_GetView(pSapModel, pView) bZoom = .FALSE. iWindow = 0 iStatus = $$cView_RefreshView(pView, iWindow, bZoom, iRet) !add load patterns iStatus = $$cSapModel_GetLoadPatterns(pSapModel, pLoadPatterns) iTypleLoadPat = LTYPE_OTHER dSelfWTMultiplier = 1. bAddLoadCase = .TRUE. iStatus = $$cLoadPatterns_Add(pLoadPatterns, '1', iTypleLoadPat, dSelfWTMultiplier, bAddLoadCase, iRet) dSelfWTMultiplier = 0. iStatus = $$cLoadPatterns_Add(pLoadPatterns, '2', iTypleLoadPat, dSelfWTMultiplier, bAddLoadCase, iRet) iStatus = $$cLoadPatterns_Add(pLoadPatterns, '3', iTypleLoadPat, dSelfWTMultiplier, bAddLoadCase, iRet) iStatus = $$cLoadPatterns_Add(pLoadPatterns, '4', iTypleLoadPat, dSelfWTMultiplier, bAddLoadCase, iRet) iStatus = $$cLoadPatterns_Add(pLoadPatterns, '5', iTypleLoadPat, dSelfWTMultiplier, bAddLoadCase, iRet) iStatus = $$cLoadPatterns_Add(pLoadPatterns, '6', iTypleLoadPat, dSelfWTMultiplier, bAddLoadCase, iRet) iStatus = $$cLoadPatterns_Add(pLoadPatterns, '7', iTypleLoadPat, dSelfWTMultiplier, bAddLoadCase, iRet) !assign loading for load pattern 2 iStatus = $$cSapModel_GetFrameObj(pSapModel, pFrameObj) iStatus = $$cFrameObj_GetPoints(pFrameObj, cFrameName(3), cPointName(1), cPointName(2), iRet) saBounds%lbound = 0

Page 22 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 23: API Examples

saBounds%extent = 6 pPointLoadValueSA = SafeArrayCreate(VT_R8, nDimCON, saBounds) dModValue = 0. do iCol = saBounds%lbound, saBounds%extent iRet = SafeArrayPutElement(pPointLoadValueSA, iCol, loc(dModValue)) end do dModValue = -10. iRet = SafeArrayPutElement(pPointLoadValueSA, 2, loc(dModValue)) bReplace = .FALSE. cCsys = 'Global' iItemType = Object iStatus = $$cPointObj_SetLoadForce(pPointObj, cPointName(1), '2', pPointLoadValueSA, bReplace, cCsys, iItemType, iRet) iMyType = 1 iDir = 10 dDist1 = 0. dDist2 = 1. dVal1 = 1.8 dVal2 = 1.8 cCsys = 'Global' bRelDist = .TRUE. bReplace = .TRUE. iItemType = Object iStatus = $$cFrameObj_SetLoadDistributed(pFrameObj, cFrameName(3), '2', iMyType, iDir, dDist1, dDist2, dVal1, dVal2, cCsys, bRelDist, bReplace, iItemType, iRet) !assign loading for load pattern 3 iStatus = $$cFrameObj_GetPoints(pFrameObj, cFrameName(3), cPointName(1), cPointName(2), iRet) dModValue = 0. do iCol = saBounds%lbound, saBounds%extent iRet = SafeArrayPutElement(pPointLoadValueSA, iCol, loc(dModValue)) end do dModValue = -17.2 iRet = SafeArrayPutElement(pPointLoadValueSA, 2, loc(dModValue)) dModValue = -54.4 iRet = SafeArrayPutElement(pPointLoadValueSA, 4, loc(dModValue)) bReplace = .FALSE. cCsys = 'Global' iItemType = Object iStatus = $$cPointObj_SetLoadForce(pPointObj, cPointName(2), '3', pPointLoadValueSA, bReplace, cCsys, iItemType, iRet) iRet = SafeArrayDestroy(pPointLoadValueSA) !assign loading for load pattern 4 iMyType = 1 iDir = 11

Page 23 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 24: API Examples

dDist1 = 0. dDist2 = 1. dVal1 = 2. dVal2 = 2. cCsys = 'Global' bRelDist = .TRUE. bReplace = .TRUE. iItemType = Object iStatus = $$cFrameObj_SetLoadDistributed(pFrameObj, cFrameName(2), '4', iMyType, iDir, dDist1, dDist2, dVal1, dVal2, cCsys, bRelDist, bReplace, iItemType, iRet) !assign loading for load pattern 5 iMyType = 1 iDir = 2 dDist1 = 0. dDist2 = 1. dVal1 = 2. dVal2 = 2. cCsys = 'Local' bRelDist = .TRUE. bReplace = .TRUE. iItemType = Object iStatus = $$cFrameObj_SetLoadDistributed(pFrameObj, cFrameName(1), '5', iMyType, iDir, dDist1, dDist2, dVal1, dVal2, cCsys, bRelDist, bReplace, iItemType, iRet) dVal1 = -2. dVal2 = -2. iStatus = $$cFrameObj_SetLoadDistributed(pFrameObj, cFrameName(2), '5', iMyType, iDir, dDist1, dDist2, dVal1, dVal2, cCsys, bRelDist, bReplace, iItemType, iRet) !assign loading for load pattern 6 iMyType = 1 iDir = 2 dDist1 = 0. dDist2 = 1. dVal1 = 0.9984 dVal2 = 0.3744 cCsys = 'Local' bRelDist = .TRUE. bReplace = .TRUE. iItemType = Object iStatus = $$cFrameObj_SetLoadDistributed(pFrameObj, cFrameName(1), '6', iMyType, iDir, dDist1, dDist2, dVal1, dVal2, cCsys, bRelDist, bReplace, iItemType, iRet) dVal1 = -0.3744 dVal2 = 0. iStatus = $$cFrameObj_SetLoadDistributed(pFrameObj, cFrameName(2), '6', iMyType, iDir, dDist1, dDist2, dVal1, dVal2, cCsys, bRelDist, bReplace, iItemType, iRet) !assign loading for load pattern 7 iMyType = 1

Page 24 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 25: API Examples

iDir = 2 dDist1 = 0.5 dVal1 = -15. cCsys = 'Local' bRelDist = .TRUE. bReplace = .TRUE. iItemType = Object iStatus = $$cFrameObj_SetLoadPoint(pFrameObj, cFrameName(2), '7', iMyType, iDir, dDist1, dVal1, cCsys, bRelDist, bReplace, iItemType, iRet) !switch to k-in units iStatus = $$cSapModel_SetPresentUnits(pSapModel, kip_in_F, iRet) !save model iStatus = $$cFile_Save(pFile, "c:\API_1-001.sdb", iRet) !run model (this will create the analysis model) iStatus = $$cSapModel_GetAnalyze(pSapModel, pAnalyze) iStatus = $$cAnalyze_RunAnalysis(pAnalyze, iRet) !initialize for Sap2000 results iStatus = $$cFrameObj_GetPoints(pFrameObj, cFrameName(2), cPointName(1), cPointName(2), iRet) !get Sap2000 results for load patterns 1 through 7 saBounds%lbound = 0 saBounds%extent = 0 pObjSA = SafeArrayCreate(VT_BSTR, nDimCON, saBounds) pElmSA = SafeArrayCreate(VT_BSTR, nDimCON, saBounds) pLCaseSA = SafeArrayCreate(VT_BSTR, nDimCON, saBounds) pStepTypeSA = SafeArrayCreate(VT_BSTR, nDimCON, saBounds) pStepNumSA = SafeArrayCreate(VT_R8, nDimCON, saBounds) pU1SA = SafeArrayCreate(VT_R8, nDimCON, saBounds) pU2SA = SafeArrayCreate(VT_R8, nDimCON, saBounds) pU3SA = SafeArrayCreate(VT_R8, nDimCON, saBounds) pR1SA = SafeArrayCreate(VT_R8, nDimCON, saBounds) pR2SA = SafeArrayCreate(VT_R8, nDimCON, saBounds) pR3SA = SafeArrayCreate(VT_R8, nDimCON, saBounds) dSapResultsRA1(:) = 0.

iItemType = ObjectElm bSelected = .TRUE. iStatus = $$cSapModel_GetResults(pSapModel, pAnalysisResults) do iCol = 0, 6 iStatus = $$cAnalysisResults_GetSetup(pAnalysisResults, pAnalysisResultsSetup) write (cLoadPat, '(I1)') iCol+1 iStatus = $$cAnalysisResultsSetup_DeselectAllCasesAndCombosForOutput(pAnalysisResultsSetup, iRet)

Page 25 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 26: API Examples

iStatus = $$cAnalysisResultsSetup_SetCaseSelectedForOutput(pAnalysisResultsSetup, cLoadPat, bSelected, iRet) if (iCol <= 3) then iStatus = $$cAnalysisResults_JointDispl(pAnalysisResults, cPointName(2), iItemType, iNumberResults, pObjSA, pElmSA, pLCaseSA, pStepTypeSA, pStepNumSA, pU1SA, pU2SA, pU3SA, pR1SA, pR2SA, pR3SA, iRet) iRet = SafeArrayGetElement(pU3SA, 0, loc(dModValue)) else iStatus = $$cAnalysisResults_JointDispl(pAnalysisResults, cPointName(1), iItemType, iNumberResults, pObjSA, pElmSA, pLCaseSA, pStepTypeSA, pStepNumSA, pU1SA, pU2SA, pU3SA, pR1SA, pR2SA, pR3SA, iRet) iRet = SafeArrayGetElement(pU1SA, 0, loc(dModValue)) end if dSapResultsRA1(iCol+1) = dModValue end do !close Sap2000 application bFileSave = .FALSE. iRet = $$SapObject_ApplicationExit(pSapObject, bFileSave, iStatus) !release Sap2000 object iStatus = COMReleaseObject(pSapObject) !uninitialize COM call COMUninitialize() !deallocate SafeArrays iRet = SafeArrayDestroy(pObjSA) iRet = SafeArrayDestroy(pElmSA) iRet = SafeArrayDestroy(pLCaseSA) iRet = SafeArrayDestroy(pStepTypeSA) iRet = SafeArrayDestroy(pStepNumSA) iRet = SafeArrayDestroy(pU1SA) iRet = SafeArrayDestroy(pU3SA) iRet = SafeArrayDestroy(pR1SA) iRet = SafeArrayDestroy(pR2SA) iRet = SafeArrayDestroy(pR3SA) !fill independent results dIndResultsRA1(1) = -0.02639 dIndResultsRA1(2) = 0.06296 dIndResultsRA1(3) = 0.06296 dIndResultsRA1(4) = -0.29630 dIndResultsRA1(5) = 0.31250 dIndResultsRA1(6) = 0.11556 dIndResultsRA1(7) = 0.00651 !display results print *, 'LC Sap2000 Independent %Diff'

Page 26 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 27: API Examples

print *, '-- -------- ----------- -----' do iCol = 1, 7 print 1, iCol, dSapResultsRA1(iCol), dIndResultsRA1(iCol), (dSapResultsRA1(iCol) / dIndResultsRA1(iCol)) - 1. end do 1format ( i3, f10.5, f13.5, f7.2) pause end program APIExample

Release Notes

Initial release in version 11.03.

Changed nomenclature from Load Cases, Analysis Cases and Response Combinations to Load Patterns, Load Cases and Load Combinations, respectively, in version 12.00.

Example 5 (Microsoft Visual C++ 2005)

Remarks

This example is written for Microsoft Visual C++ 2005. It is based on the Sap2000 verification problem Example 1-001.

This example creates the example verification problem from scratch, runs the analysis, extracts results, and compares the results with hand calculated values.

Example

1. Create a Visual C++ Win32 project using the template “Win32 Console Application”.

2. In Win32 Application Wizard, add common header files for ATL under Application Settings tab.

3. Create a folder named “tlb” under the project folder and copy “Sap2000.tlb” from the installation folder to the “tlb” folder.

4. Create a folder named “C:\API” if it does not exist.

5. Open the .cpp file generated by the wizard by double clicking on it and paste in the following code:

#include "stdafx.h" #include <sstream>

Page 27 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 28: API Examples

#include <iomanip> #include <math.h> #import "..\tlb\Sap2000.tlb" high_property_prefixes("Get_","Put_","PutRef_") no_smart_pointers no_namespace raw_native_types rename("min", "sap2000v12_min") rename("SetProp", "sap2000v12_SetProp") rename("GetProp", "sap2000v12_GetProp") rename("Yield", "sap2000v12_Yield") using namespace std; bool CheckHRESULT(HRESULT hRes, const wchar_t *msg) { if(FAILED(hRes)) { MessageBox(0, msg, L"ERROR!", MB_SETFOREGROUND); return (false);

} return (true); } int _tmain(int argc, _TCHAR* argv[]) { HRESULT hRes = 0; int res = 0; // initialize COM hRes = CoInitialize(NULL); if(!CheckHRESULT(hRes, L"Error initializing COM subsystem!")) return (hRes); // create Sap2000 object // CComQIPtr is a smart pointer class for managing COM interface pointers. CComQIPtr<_SapObject> pSapObject; hRes = pSapObject.CoCreateInstance( __uuidof(SapObject), NULL, CLSCTX_LOCAL_SERVER); if(!CheckHRESULT(hRes, L"Cannot instanciate SapObject!")) return (hRes); try { // start Sap2000 application // CComBSTR is a wrapper for BSTRs. CComBSTR bstrEmpty(""); res = pSapObject->ApplicationStart(kip_in_F, VARIANT_TRUE, bstrEmpty); // create SapModel object CComQIPtr<_cSapModel> pSapModel; pSapModel = pSapObject->Get_SapModel(); // initialize model res = pSapModel->InitializeNewModel(kip_in_F); // create new blank model CComQIPtr<_cFile> pFile;

Page 28 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 29: API Examples

pFile = pSapModel->Get_File(); res = pFile->NewBlank(); // define material property CComQIPtr<_cPropMaterial> pPropMaterial; CComBSTR bstrPropMaterial("CONC"); pPropMaterial = pSapModel->Get_PropMaterial(); res = pPropMaterial->SetMaterial(bstrPropMaterial, MATERIAL_CONCRETE, -1, bstrEmpty, bstrEmpty); // assign isotropic mechanical properties to material res = pPropMaterial->SetMPIsotropic(bstrPropMaterial, 3600, 0.2, 0.0000055, 0.0); // define rectangular frame section property CComQIPtr<_cPropFrame> pPropFrame; pPropFrame = pSapModel->Get_PropFrame(); CComBSTR bstrPropFrame("R1"); res = pPropFrame->SetRectangle(bstrPropFrame, bstrPropMaterial, 12, 12, -1, bstrEmpty, bstrEmpty); // define frame section property modifiers // CComSafeArray is a wrapper for the SAFEARRAY structure. CComSafeArray<double> saMod(8); for (int i = 0; i < 8; i++) saMod[i] = 1.; saMod[0] = 1000.; saMod[1] = 0.; saMod[2] = 0.; res = pPropFrame->SetModifiers(bstrPropFrame, &(saMod.m_psa)); // switch to k-ft units res = pSapModel->SetPresentUnits(kip_ft_F); // add frame object by coordinates CComBSTR name1(" "); CComBSTR name2(" "); CComBSTR name3(" "); CComBSTR FrameName0("1"); CComBSTR FrameName1("2"); CComBSTR FrameName2("3"); CComBSTR bstrCoordSys("Global"); CComQIPtr<_cFrameObj> pFrameObj; pFrameObj = pSapModel->Get_FrameObj(); res = pFrameObj->AddByCoord(0, 0, 0, 0, 0, 10, &name1, bstrPropFrame, FrameName0, bstrCoordSys); res = pFrameObj->AddByCoord(0, 0, 10, 8, 0, 16, &name2, bstrPropFrame, FrameName1, bstrCoordSys); res = pFrameObj->AddByCoord(-4, 0, 10, 0, 0, 10, &name3, bstrPropFrame, FrameName2, bstrCoordSys);

Page 29 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 30: API Examples

// assign point object restraint at base CComBSTR PointName(""); CComBSTR PointName0(""); CComBSTR PointName1(""); CComSafeArray<VARIANT_BOOL> saRest(6); for (int i = 0; i < 4; i++) saRest[i] = VARIANT_TRUE; for (int i = 4; i < 6; i++) saRest[i] = VARIANT_FALSE; res = pFrameObj->GetPoints(FrameName0, &PointName0, &PointName1); CComQIPtr<_cPointObj> pPointObj; pPointObj = pSapModel->Get_PointObj(); res = pPointObj->SetRestraint(PointName0, &(saRest.m_psa), Object); // assign point object restraint at top for (int i = 0; i < 2; i++) saRest[i] = VARIANT_TRUE; for (int i = 2; i < 6; i++) saRest[i] = VARIANT_FALSE; res = pFrameObj->GetPoints(FrameName1, &PointName0, &PointName1); res = pPointObj->SetRestraint(PointName1, &(saRest.m_psa), Object); // refresh view, update (initialize) zoom long window = 0; VARIANT_BOOL zoom = VARIANT_FALSE; CComQIPtr<_cView> pView; pView = pSapModel->Get_View(); res = pView->RefreshView(window, zoom); // add load patterns double SelfWTMultiplier = 1.0; VARIANT_BOOL AddLoadCase = VARIANT_TRUE; CComQIPtr<_cLoadPatterns> pLoadPatterns; pLoadPatterns = pSapModel->Get_LoadPatterns(); CComBSTR bstrLoadPattern("1"); res = pLoadPatterns->Add(bstrLoadPattern, LTYPE_OTHER, SelfWTMultiplier, AddLoadCase); SelfWTMultiplier = 0.; bstrLoadPattern = "2"; res = pLoadPatterns->Add(bstrLoadPattern, LTYPE_OTHER, SelfWTMultiplier, AddLoadCase); bstrLoadPattern = "3"; res = pLoadPatterns->Add(bstrLoadPattern, LTYPE_OTHER, SelfWTMultiplier, AddLoadCase); bstrLoadPattern = "4"; res = pLoadPatterns->Add(bstrLoadPattern, LTYPE_OTHER, SelfWTMultiplier,

Page 30 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 31: API Examples

AddLoadCase); bstrLoadPattern = "5"; res = pLoadPatterns->Add(bstrLoadPattern, LTYPE_OTHER, SelfWTMultiplier, AddLoadCase); bstrLoadPattern = "6"; res = pLoadPatterns->Add(bstrLoadPattern, LTYPE_OTHER, SelfWTMultiplier, AddLoadCase); bstrLoadPattern = "7"; res = pLoadPatterns->Add(bstrLoadPattern, LTYPE_OTHER, SelfWTMultiplier, AddLoadCase); // assign loading for load pattern 2 res = pFrameObj->GetPoints(FrameName2, &PointName0, &PointName1); CComSafeArray<double> saPLoad(6); for (int i = 0; i < 6; i++) saPLoad[i] = 0.; saPLoad[2] = -10.; bstrLoadPattern = "2"; bstrCoordSys = "Global"; res = pPointObj->SetLoadForce(PointName0, bstrLoadPattern, &(saPLoad.m_psa), VARIANT_FALSE, bstrCoordSys, Object); res = pFrameObj->SetLoadDistributed(FrameName2, bstrLoadPattern, 1, 10, 0., 1., 1.8, 1.8, bstrCoordSys, VARIANT_TRUE, VARIANT_TRUE, Object); // assign loading for load pattern 3 res = pFrameObj->GetPoints(FrameName2, &PointName0, &PointName1); for (int i = 0; i < 6; i++) saPLoad[i] = 0.; saPLoad[2] = -17.2; saPLoad[4] = -54.4; bstrLoadPattern = "3"; res = pPointObj->SetLoadForce(PointName1, bstrLoadPattern, &(saPLoad.m_psa), VARIANT_FALSE, bstrCoordSys, Object); // assign loading for load pattern 4 bstrLoadPattern = "4"; res = pFrameObj->SetLoadDistributed(FrameName1, bstrLoadPattern, 1, 10, 0, 1, 1.8, 1.8, bstrCoordSys, VARIANT_TRUE, VARIANT_TRUE, Object); // assign loading for load pattern 5 bstrCoordSys = "Local"; bstrLoadPattern = "5"; res = pFrameObj->SetLoadDistributed(FrameName0, bstrLoadPattern, 1, 2, 0, 1, 2, 2, bstrCoordSys, VARIANT_TRUE, VARIANT_TRUE, Object); res = pFrameObj->SetLoadDistributed(FrameName1, bstrLoadPattern, 1, 2, 0, 1, -2, -2,

Page 31 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 32: API Examples

bstrCoordSys, VARIANT_TRUE, VARIANT_TRUE, Object); // assign loading for load pattern 6 bstrLoadPattern = "6"; res = pFrameObj->SetLoadDistributed(FrameName0, bstrLoadPattern, 1, 2, 0, 1, 0.9984, 0.3744, bstrCoordSys, VARIANT_TRUE, VARIANT_TRUE, Object); res = pFrameObj->SetLoadDistributed(FrameName1, bstrLoadPattern, 1, 2, 0, 1, -0.3744, 0, bstrCoordSys, VARIANT_TRUE, VARIANT_TRUE, Object); // assign loading for load pattern 7 bstrLoadPattern = "7"; res = pFrameObj->SetLoadPoint(FrameName1,bstrLoadPattern, 1, 2, 0.5, -15, bstrCoordSys, VARIANT_TRUE, VARIANT_TRUE, Object); // switch to k-in units res = pSapModel->SetPresentUnits(kip_in_F); // save model CComBSTR bstrFileName("C:\\SapAPI\\API_1-001.sdb"); res = pFile->Save(bstrFileName); // run model (this will create the analysis model) CComQIPtr<_cAnalyze> pAnalyze; pAnalyze = pSapModel->Get_Analyze(); res = pAnalyze->RunAnalysis(); // initialize for Sap2000 results long NumberResults = 0; double SapResult[7]; for (int i = 0; i < 7; i++) SapResult[i] = 0.; // use CComSafeArray class to simplify SAFEARRAY creation CComSafeArray<BSTR> saResObj(1); saResObj[0] = ::SysAllocString(L""); CComSafeArray<BSTR> saResElm(1); saResElm[0] = ::SysAllocString(L""); CComSafeArray<BSTR> saResLoadCase(1); saResLoadCase[0] = ::SysAllocString(L""); CComSafeArray<BSTR> saResStepType(1); saResStepType[0] = ::SysAllocString(L""); CComSafeArray<double> saResStepNum(1); CComSafeArray<double> saResU1(1); CComSafeArray<double> saResU2(1); CComSafeArray<double> saResU3(1); CComSafeArray<double> saResR1(1); CComSafeArray<double> saResR2(1); CComSafeArray<double> saResR3(1); res = pFrameObj->GetPoints(FrameName1, &PointName0, &PointName1);

Page 32 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 33: API Examples

// get Sap2000 results for load cases 1 through 7 CComQIPtr<_cAnalysisResults> pResults; CComQIPtr<_cAnalysisResultsSetup> pResultsSetup; pResults = pSapModel->Get_Results(); pResultsSetup = pResults->Get_Setup(); for (int i = 0; i < 7; i++) { res = pResultsSetup->DeselectAllCasesAndCombosForOutput(); if (i==0) { bstrLoadPattern = "1"; res = pResultsSetup->SetCaseSelectedForOutput(bstrLoadPattern, VARIANT_TRUE); } else if (i==1) { bstrLoadPattern = "2"; res = pResultsSetup->SetCaseSelectedForOutput(bstrLoadPattern, VARIANT_TRUE); } else if (i==2) { bstrLoadPattern = "3"; res = pResultsSetup->SetCaseSelectedForOutput(bstrLoadPattern, VARIANT_TRUE); } else if (i==3) { bstrLoadPattern = "4"; res = pResultsSetup->SetCaseSelectedForOutput(bstrLoadPattern, VARIANT_TRUE); } else if (i==4) { bstrLoadPattern = "5"; res = pResultsSetup->SetCaseSelectedForOutput(bstrLoadPattern, VARIANT_TRUE); } else if (i==5) { bstrLoadPattern = "6"; res = pResultsSetup->SetCaseSelectedForOutput(bstrLoadPattern, VARIANT_TRUE); } else if (i==6) { bstrLoadPattern = "7"; res = pResultsSetup->SetCaseSelectedForOutput(bstrLoadPattern, VARIANT_TRUE); } // SAFEARRAY pointers must be detached before being passed // if they are to be redimensioned in the SAP2000 API method LPSAFEARRAY psaResObj = saResObj.Detach(); LPSAFEARRAY psaResElm = saResElm.Detach(); LPSAFEARRAY psaResLoadCase = saResLoadCase.Detach(); LPSAFEARRAY psaResStepType = saResStepType.Detach(); LPSAFEARRAY psaResStepNum = saResStepNum.Detach(); LPSAFEARRAY psaResU1 = saResU1.Detach(); LPSAFEARRAY psaResU2 = saResU2.Detach(); LPSAFEARRAY psaResU3 = saResU3.Detach(); LPSAFEARRAY psaResR1 = saResR1.Detach(); LPSAFEARRAY psaResR2 = saResR2.Detach(); LPSAFEARRAY psaResR3 = saResR3.Detach(); if (i <= 3) PointName = PointName1; else PointName = PointName0;

Page 33 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 34: API Examples

res = pResults->JointDispl(PointName, ObjectElm, &NumberResults, &psaResObj, &psaResElm, &psaResLoadCase, &psaResStepType, &psaResStepNum, &psaResU1, &psaResU2, &psaResU3, &psaResR1, &psaResR2, &psaResR3); // reattach SAFEARRAY pointers saResObj.Attach(psaResObj); saResElm.Attach(psaResElm); saResLoadCase.Attach(psaResLoadCase); saResStepType.Attach(psaResStepType); saResStepNum.Attach(psaResStepNum); saResU1.Attach(psaResU1); saResU2.Attach(psaResU2); saResU3.Attach(psaResU3); saResR1.Attach(psaResR1); saResR2.Attach(psaResR2); saResR3.Attach(psaResR3); if (i <= 3) SapResult[i] = saResU3[0]; else SapResult[i] = saResU1[0]; } // close Sap2000 application res = pSapObject->ApplicationExit(VARIANT_FALSE); // fill independent results (hand calculated) double IndResult[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; // fill percent difference double PercentDiff[7]; for (int i = 0; i < 7; i++) PercentDiff[i] = fabs((SapResult[i] / IndResult[i]) - 1); // print results stringstream sMsg; sMsg << fixed << setfill(' '); sMsg << "LC Sap2000 Independent %Diff" << endl; for (int i = 0; i < 7; i++) sMsg << setprecision(5) << showpoint << setiosflags(ios::left) << setw(2) << i + 1 << " " << setw(11) << IndResult[i] << " "

Page 34 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 35: API Examples

<< setw(11) << SapResult[i] << " " << setprecision(0) << noshowpoint << setiosflags(ios::right) << setw(5) << PercentDiff[i] << '%' << endl; MessageBoxA(0, sMsg.str().c_str(), " Results", MB_SETFOREGROUND); // clean up objects pFile.Release(); pPropMaterial.Release(); pPropFrame.Release(); pFrameObj.Release(); pPointObj.Release(); pView.Release(); pLoadPatterns.Release(); pAnalyze.Release(); pResults.Release(); pResultsSetup.Release(); pSapModel.Release(); pSapObject.Release(); // uninitialize COM CoUninitialize(); // we're done! return (res); } catch( _com_error& ex ) { CheckHRESULT(ex.Error(), ex.ErrorMessage()); // close Sap2000 application res = pSapObject->ApplicationExit(VARIANT_FALSE); // uninitialize COM CoUninitialize(); return (-1); } }

Release Notes

Initial release in version 12.0.1

Example 6 (MATLAB R2008a)

Remarks

This example is written for MATLAB R2008a. It is based on the Sap2000 verification problem Example 1-001.

Page 35 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 36: API Examples

This example creates the example verification problem from scratch, runs the analysis, extracts results, and compares the results with hand calculated values.

Example

1. Create a folder named “C:\API” if it does not exist.

2. Create a MATLAB .m file using the built-in or any text editor and paste in the following code:

%% clean-up the workspace & command window clear; clc; %% pass data to Sap2000 as one-dimensional arrays feature('COM_SafeArraySingleDim', 1); %% pass non-scalar arrays to Sap2000 API by reference feature('COM_PassSafeArrayByRef', 1); %% create Sap2000 object SapObject = actxserver('sap2000.SapObject'); %% start Sap2000 application SapObject.ApplicationStart; %% create SapModel object SapModel = SapObject.SapModel; %% initialize model ret = SapModel.InitializeNewModel; %% create new blank model ret = SapModel.File.NewBlank; %% define material property MATERIAL_CONCRETE = 2; ret = SapModel.PropMaterial.SetMaterial('CONC', MATERIAL_CONCRETE); %% assign isotropic mechanical properties to material ret = SapModel.PropMaterial.SetMPIsotropic('CONC', 3600, 0.2, 0.0000055); %% define rectangular frame section property ret = SapModel.PropFrame.SetRectangle('R1', 'CONC', 12, 12); %% define frame section property modifiers ModValue = zeros(8,1,'double'); for i = 1 : 8 ModValue(i,1) = 1;

Page 36 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 37: API Examples

end ModValue(1,1) = 1000; ModValue(2,1) = 0; ModValue(3,1) = 0; ret = SapModel.PropFrame.SetModifiers('R1', ModValue); %% switch to k-ft units kip_ft_F = 4; ret = SapModel.SetPresentUnits(kip_ft_F); %% add frame object by coordinates FrameName1 = ' ';

FrameName2 = ' ';

FrameName3 = ' ';

[ret, FrameName1] = SapModel.FrameObj.AddByCoord(0, 0, 0, 0, 0, 10, FrameName1, 'R1', '1', Global'); [ret, FrameName2] = SapModel.FrameObj.AddByCoord(0, 0, 10, 8, 0, 16, FrameName2, 'R1', '2', Global'); [ret, FrameName3] = SapModel.FrameObj.AddByCoord(-4, 0, 10, 0, 0, 10, FrameName3, 'R1', '3', Global'); %% assign point object restraint at base PointName1 = ' ';

PointName2 = ' ';

Restraint = logical(zeros(6,1)); for i = 1 : 4 Restraint(i,1) = true(); end for i = 5 : 6 Restraint(i,1) = false(); end [ret, PointName2] = SapModel.FrameObj.GetPoints(FrameName1, PointName1, PointName2); ret = SapModel.PointObj.SetRestraint(PointName1, Restraint); %% assign point object restraint at top for i = 1 : 2 Restraint(i,1) = true(); end for i = 3 : 6 Restraint(i,1) = false(); end

Page 37 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 38: API Examples

[ret, PointName1, PointName2] = SapModel.FrameObj.GetPoints(FrameName2, PointName1, PointName2); ret = SapModel.PointObj.SetRestraint(PointName2, Restraint); %% refresh view, update (initialize) zoom ret = SapModel.View.RefreshView(0, false()); %% add load patterns LTYPE_OTHER = 8; ret = SapModel.LoadPatterns.Add('1', LTYPE_OTHER, 1, true()); ret = SapModel.LoadPatterns.Add('2', LTYPE_OTHER, 0, true()); ret = SapModel.LoadPatterns.Add('3', LTYPE_OTHER, 0, true()); ret = SapModel.LoadPatterns.Add('4', LTYPE_OTHER, 0, true()); ret = SapModel.LoadPatterns.Add('5', LTYPE_OTHER, 0, true()); ret = SapModel.LoadPatterns.Add('6', LTYPE_OTHER, 0, true()); ret = SapModel.LoadPatterns.Add('7', LTYPE_OTHER, 0, true()); %% assign loading for load pattern 2 [ret, PointName1, PointName2] = SapModel.FrameObj.GetPoints(FrameName3, PointName1, PointName2); PointLoadValue(6,1) = 0; PointLoadValue(3,1) = -10; ret = SapModel.PointObj.SetLoadForce(PointName1, '2', PointLoadValue); ret = SapModel.FrameObj.SetLoadDistributed(FrameName3, '2', 1, 10, 0, 1, 1.8, 1.8); %% assign loading for load pattern 3 [ret, PointName1, PointName2] = SapModel.FrameObj.GetPoints(FrameName3, PointName1, PointName2); PointLoadValue(6,1) = 0; PointLoadValue(3,1) = -17.2; PointLoadValue(5,1) = -54.4; ret = SapModel.PointObj.SetLoadForce(PointName2, '3', PointLoadValue); %% assign loading for load pattern 4 ret = SapModel.FrameObj.SetLoadDistributed(FrameName2, '4', 1, 11, 0, 1, 2, 2); %% assign loading for load pattern 5 ret = SapModel.FrameObj.SetLoadDistributed(FrameName1, '5', 1, 2, 0, 1, 2, 2, 'Local'); ret = SapModel.FrameObj.SetLoadDistributed(FrameName2, '5', 1, 2, 0, 1, -2, -2, 'Local'); %% assign loading for load pattern 6 ret = SapModel.FrameObj.SetLoadDistributed(FrameName1, '6', 1, 2, 0, 1, 0.9984, 0.3744, 'Local'); ret = SapModel.FrameObj.SetLoadDistributed(FrameName2, '6', 1, 2, 0, 1, -0.3744, 0, 'Local'); %% assign loading for load pattern 7 ret = SapModel.FrameObj.SetLoadPoint(FrameName2, '7', 1, 2, 0.5, -15, 'Local'); %% switch to k-in units kip_in_F = 3; ret = SapModel.SetPresentUnits(kip_in_F);

Page 38 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 39: API Examples

%% save model ret = SapModel.File.Save('C:\API\API_1-001.sdb'); %% run model (this will create the analysis model) ret = SapModel.Analyze.RunAnalysis(); %% initialize for Sap2000 results SapResult= zeros(7,1,'double'); [ret, PointName1, PointName2] = SapModel.FrameObj.GetPoints(FrameName2, PointName1, PointName2); %% get Sap2000 results for load cases 1 through 7 for i = 1 : 7 NumberResults = 0; Obj = cellstr(' '); Elm = cellstr(' '); ACase = cellstr(' '); StepType = cellstr(' '); StepNum = zeros(1,1,'double'); U1 = zeros(1,1,'double'); U2 = zeros(1,1,'double'); U3 = zeros(1,1,'double'); R1 = zeros(1,1,'double'); R2 = zeros(1,1,'double'); R3 = zeros(1,1,'double'); ObjectElm = 0; ret = SapModel.Results.Setup.DeselectAllCasesAndCombosForOutput; ret = SapModel.Results.Setup.SetCaseSelectedForOutput(int2str(i)); if i <= 4 [ret, NumberResults, Obj, Elm, ACase, StepType, StepNum, U1, U2, U3, R1, R2, R3] = SapModel.Results.JointDispl(PointName2, ObjectElm, NumberResults, Obj, Elm, ACase, StepType, StepNum, U1, U2, U3, R1, R2, R3); SapResult(i) = U3(1); else [ret, NumberResults, Obj, Elm, ACase, StepType, StepNum, U1, U2, U3, R1, R2, R3] = SapModel.Results.JointDispl(PointName1, ObjectElm, NumberResults, Obj, Elm, ACase, StepType, StepNum, U1, U2, U3, R1, R2, R3); SapResult(i) = U1(1); end end %% close Sap2000 ret = SapObject.ApplicationExit(false()); SapModel = 0; SapObject = 0; %% fill independent results IndResult= zeros(7,1,'double'); IndResult(1) = -0.02639;

Page 39 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm

Page 40: API Examples

IndResult(2) = 0.06296; IndResult(3) = 0.06296; IndResult(4) = -0.2963; IndResult(5) = 0.3125; IndResult(6) = 0.11556; IndResult(7) = 0.00651; %% fill percent difference PercentDiff = zeros(7,1,'double'); for i = 1 : 7 PercentDiff(i) = (SapResult(i) / IndResult(i)) - 1; end %% display results SapResult IndResult PercentDiff

Release Notes

Initial release in version 12.0.1

Page 40 of 40Example 1 (VBA)

12-04-2011file://C:\Documents and Settings\chraud\Local Settings\Temp\~hhCCE8.htm