import comtypes.client
from ctypes import byref, c_int, c_double, create_string_buffer, cast, POINTER, c_char_p
try:
SapObject = comtypes.client.GetActiveObject("CSI.SAP2000.API.SapObject")
except (OSError, comtypes.COMError):
SapObject = comtypes.client.CreateObject("CSI.SAP2000.API.SapObject")
SapObject.ApplicationStart()
SapModel = SapObject.SapModel
ret = SapModel.SelectObj.GetSelected()
if ret[0] > 0:
frame_name = ret[1][0]
NumberResults = c_int()
NumResults = 100
** # Define array types
Obj = (create_string_buffer(32) * NumResults)()
Elm = (create_string_buffer(32) * NumResults)()
LoadCase = (create_string_buffer(32) * NumResults)()
StepType = (create_string_buffer(32) * NumResults)()
StepNum = (c_double * NumResults)()
P = (c_double * NumResults)()
V2 = (c_double * NumResults)()
V3 = (c_double * NumResults)()
T = (c_double * NumResults)()
M2 = (c_double * NumResults)()
M3 = (c_double * NumResults)()**
Obj = cast(Obj, POINTER(c_char_p))
Elm = cast(Elm, POINTER(c_char_p))
LoadCase = cast(LoadCase, POINTER(c_char_p))
StepType = cast(StepType, POINTER(c_char_p))
ret = SapModel.Results.FrameForce(
frame_name,
byref(NumberResults),
Obj,
Elm,
LoadCase,
StepType,
StepNum,
P,
V2,
V3,
T,
M2,
M3
)
if ret == 0:
print("Selected Frame Name:", frame_name)
print("Number of Results:", NumberResults.value)
for i in range(NumberResults.value):
print("Load Case:", LoadCase[i].value.decode())
print("Step Type:", StepType[i].value.decode())
print("Step Num:", StepNum[i])
print("P (axial force):", P[i])
print("V2 (shear force):", V2[i])
print("V3 (shear force):", V3[i])
print("T (torsion):", T[i])
print("M2 (moment):", M2[i])
print("M3 (moment):", M3[i])
else:
print("Error in retrieving frame forces")
else:
print("No frame elements are selected in SAP2000.")
technically this code is supposed to get the analysis results from a selected element in CSI SAP2000. However, I am having an issue with creating an array for the values. Problematic block is bold.
I tried using Obj = (c_char_p * NumResults)() instead, but nothing. Can not convert the c_string_buffer(32) into an integer anyway.
New contributor
Joey Sedik is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.