1. Grasshopper调用SAP2000 API教程
1.1. SAP2000 API
1.1.1. SAP2000 API概述
1.1.1.1. API基本关联文件:
python/C#:C:Program FilesComputers and StructuresSAP2000 22SAP2000v1.dllVBA:C:Program FilesComputers and StructuresSAP2000 22SAP2000v1.tlb
API说明文件:C:Program FilesComputers and StructuresSAP2000 22CSI_OAPI_Documentation.chm
注:此处的SAP2000v1.dll及SAP2000v1.tlb文件需要根据不同的SAP2000版本单独配置
1.1.1.2. 开发环境配置—-Python
安装comtypes库
pipinstallcomtypes
Python调用SAP2000的两种方式:
(1)获取当前正在运行的SAP2000对象
importosimportsysimportcomtypes.clientSapObject=comtypes.client.GetActiveObject("CSI.SAP2000.API.SapObject")SapModel=SapObject.SapModel
(2)新建SAP2000对象
importosimportsysimportcomtypes.clientimportcomtypeshelper=comtypes.client.CreateObject('CSiAPIv1.Helper')mySapObject=helper.CreateObject(r"C:ProgramFilesComputersandStructuresSAP200022SAP2000.exe")#mySapObject=helper.CreateObjectProgID("CSI.SAP2000.API.SapObject")mySapObject.ApplicationStart()SapModel=mySapObject.SapModel
注:progid的获取,regedit进入注册表,HKEY_LOCAL_MACHINESOFTWAREClasses下查找
1.1.1.3. 开发环境配置—-C#
(1)获取当前正在运行的SAP2000对象
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSAP2000v1;namespaceSAP2000{classProgram{staticvoidMain(string[]args){cOAPImySapObject=null;mySapObject=(cOAPI)System.Runtime.InteropServices.Marshal.GetActiveObject("CSI.SAP2000.API.SapObject");cSapModelsapModel=mySapObject.SapModel;}}}
(2)新建SAP2000对象
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSAP2000v1;namespaceSAP2000{classProgram{staticvoidMain(string[]args){cHelpermyHelper;myHelper=newHelper();cOAPImySapObject=null;mySapObject=myHelper.CreateObject(@"C:ProgramFilesComputersandStructuresSAP200022SAP2000.exe");//或者//mySapObject=myHelper.CreateObjectProgID("CSI.SAP2000.API.SapObject");mySapObject.ApplicationStart();cSapModelmySapModel;mySapModel=mySapObject.SapModel;mySapModel.InitializeNewModel((eUnits.kip_in_F));}}}
1.1.2. SAP2000 API简单案例
运用python调用SAP2000分析一根跨度6m的梁
importosimportsysimportcomtypes.clientdefCaculation(SapModel,depth,force_z):#新建SAP2000模型SapModel.InitializeNewModel()ret=SapModel.File.NewBlank()#定义材料SapModel.PropMaterial.AddMaterial("C40",2,"China","GB","GB50010C40")#定义截面SapModel.PropFrame.SetRectangle("Frame","C40",depth,400)#建立模型SapModel.FrameObj.AddByCoord(0,0,0,3000,0,0,"element1","Frame","element1")SapModel.FrameObj.AddByCoord(3000,0,0,6000,0,0,"element2","Frame","element2")#添加约束s_Point1=""e_Point1=""[s_Point1,e_Point1,ret]=SapModel.FrameObj.GetPoints("element1",s_Point1,e_Point1)s_Point2=""e_Point2=""[s_Point2,e_Point2,ret]=SapModel.FrameObj.GetPoints("element2",s_Point2,e_Point2)R=[True,True,True,True,True,True]SapModel.PointObj.setRestraint(s_Point1,R)SapModel.PointObj.setRestraint(e_Point2,R)#添加荷载SapModel.LoadPatterns.Add("Live",3)Load=[0,0,force_z,0,0,0]SapModel.PointObj.SetLoadForce(e_Point1,"Live",Load)#保存模型path=r'D:2021study 7-grasshopperTutorialexample_01test01.sdb'SapModel.File.Save(path)SapModel.Analyze.RunAnalysis()#提取结果SapModel.Results.Setup.DeselectAllCasesAndCombosForOutput()ret=SapModel.Results.Setup.SetCaseSelectedForOutput("Live")NumberResults=0Obj,Elm,LoadCase,StepType,StepNum,F1,F2,F3,M1,M2,M3=[],[],[],[],[],[],[],[],[],[],[][NumberResults,Obj,Elm,LoadCase,StepType,StepNum,F1,F2,F3,M1,M2,M3,ret]=SapModel.Results.JointReact(s_Point1,0,NumberResults,Obj,Elm,LoadCase,StepType,StepNum,F1,F2,F3,M1,M2,M3)print(F1)print(F2)print(F3)#获取SAP2000对象mySapObject=comtypes.client.GetActiveObject("CSI.SAP2000.API.SapObject")SapModel=mySapObject.SapModeldepths=[500,600,700,800]force_zs=[1000,2000,3000,4000]fori,depthinenumerate(depths):force_z=force_zs[i]Caculation(SapModel,depth,force_z)
我们非解构一直关注建筑艺术与结构技术的有机融合。我们在做好设计的同时,一直关注数字化、智能化等前沿技术在建筑设计行业中的运用,这些年一直在坚持探索和实践。
非常欢迎优秀的你来加入我们,一起来跨界,做一名推动行业发展的斜杠青年。
这几年,对参数化设计感兴趣的朋友越来越多,我们的参数化设计交流群也已经发展到了5群,欢迎更多的朋友加入,相互交流学习。
添加我们“转自:非解构-公众号”微信,
加入参数化设计交流群。
不了解我们的可以来补课了