复杂形态钢结构设计培训班

首页 非解构-公众号 Grasshopper调用SAP2000 API教程文档(2)

Grasshopper调用SAP2000 API教程文档(2)

Grasshopper调用SAP2000 API教程

grasshopper中包含C#,VB和GHPython三个脚本电池,同时,food4rhino(https://www.food4rhino.com/)上也包含一些可以编写脚本的电池。

1.2.1. C# Scripts

1.2.1.1. 开发环境配置

1.配置SAP2000V1.dll文件

2.基本语法与Visual Studio中的C#环境一致 (1)获取当前正在运行的SAP2000对象

cOAPImySapObject=null;mySapObject=(cOAPI)System.Runtime.InteropServices.Marshal.GetActiveObject("CSI.SAP2000.API.SapObject");cSapModelsapModel=mySapObject.SapModel;}

(2)新建SAP2000对象

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.2.1.2. 与几何对象的交互

Rhino的API文档地址:https://developer.rhino3d.com/api/RhinoCommon/html/R_Project_RhinoCommon.htm#! 几何对象的属性说明主要在Rhino.Geometry Namespace下

(1)点

cOAPImySapObject=null;mySapObject=(cOAPI)System.Runtime.InteropServices.Marshal.GetActiveObject("CSI.SAP2000.API.SapObject");cSapModelsapModel=mySapObject.SapModel;if(!build){return;}for(inti=0;i<point.Count;i++){stringName="Point"+Convert.ToString(i);sapModel.PointObj.AddCartesian(point[i].X,point[i].Y,point[i].Z,refName);}

(2)线

cOAPImySapObject=null;mySapObject=(cOAPI)System.Runtime.InteropServices.Marshal.GetActiveObject("CSI.SAP2000.API.SapObject");cSapModelsapModel=mySapObject.SapModel;if(!build){return;}stringmat="C30";sapModel.PropMaterial.AddMaterial(refmat,eMatType.Concrete,"China","GB","GB50010C30");stringsec="Frame";sapModel.PropFrame.SetRectangle(sec,"C30",800,300);for(inti=0;i<curve.Count;i++){stringName="Curve"+Convert.ToString(i);Point3dstartPoint=curve[i].PointAtStart;Point3dendPoint=curve[i].PointAtEnd;sapModel.FrameObj.AddByCoord(startPoint.X,startPoint.Y,startPoint.Z,endPoint.X,endPoint.Y,endPoint.Z,refName,sec);}

(3)面

cOAPImySapObject=null;mySapObject=(cOAPI)System.Runtime.InteropServices.Marshal.GetActiveObject("CSI.SAP2000.API.SapObject");cSapModelsapModel=mySapObject.SapModel;if(!build){return;}stringmat="C30";sapModel.PropMaterial.AddMaterial(refmat,eMatType.Concrete,"China","GB","GB50010C30");stringsec="Area";sapModel.PropArea.SetShell(sec,1,mat,0,120,120);for(inti=0;i<surface.Count;i++){stringName="surface"+Convert.ToString(i);BrepsurfaceItem=surface[i];BrepVertexListcorners=surfaceItem.Vertices;double[]corner_X=newdouble[3];double[]corner_Y=newdouble[3];double[]corner_Z=newdouble[3];for(intj=0;j<corners.Count;j++){BrepVertexcornerItem=corners[j];corner_X[j]=cornerItem.Location.X;corner_Y[j]=cornerItem.Location.Y;corner_Z[j]=cornerItem.Location.Z;}sapModel.AreaObj.AddByCoord(3,refcorner_X,refcorner_Y,refcorner_Z,refName,sec);}

1.2.2. GHPython Scripts

1.2.2.1. 开发环境配置

importmathimportrhinoscriptsyntaxasrsimportsysimportclrimportSystemsapDllPath=r'C:ProgramFilesComputersandStructuresSAP200022'ProgramPath=r'C:ProgramFilesComputersandStructuresSAP200022SAP2000.exe'sapDllName='SAP2000v1.dll'sys.path.append(sapDllPath)clr.AddReference(sapDllName)importSAP2000v1fromSAP2000v1import*mhelper=SAP2000v1.Helper()##获取已有的SAP2000对象mySapObject=mhelper.GetObject("CSI.SAP2000.API.SapObject")#新建SAP2000对象#mySapObject=mhelper.CreateObject(r"G:ProgramFilesComputersandStructuresSAP200022SAP2000.exe")#mySapObject=mhelper.CreateObjectProgID("CSI.SAP2000.API.SapObject")#mySapObject.ApplicationStart()SapModel=mySapObject.SapModelSapModel.InitializeNewModel()ret=SapModel.File.NewBlank()

1.1.2.2. 与几何对象的交互

RhinoScriptSyntax开发文档:https://developer.rhino3d.com/api/RhinoScriptSyntax/

grasshopper中的GHPython是IronPython,与.Net框架交互,Python版本为2.7,无法直接引用内存环境中的库,因此与原生Python环境的配置以及代码的书写有很大的不同。

(1)点

importmathimportrhinoscriptsyntaxasrsimportsysimportclrimportSystemsapDllPath=r'G:ProgramFilesComputersandStructuresSAP200022'ProgramPath=r'G:ProgramFilesComputersandStructuresSAP200022SAP2000.exe'sapDllName='SAP2000v1.dll'sys.path.append(sapDllPath)clr.AddReference(sapDllName)importSAP2000v1fromSAP2000v1import*mhelper=SAP2000v1.Helper()mySapObject=mhelper.GetObject("CSI.SAP2000.API.SapObject")SapModel=mySapObject.SapModelifbuild:fori,pointCoorinenumerate(point):name=str(i)name=clr.Reference[System.String]()SapModel.PointObj.AddCartesian(pointCoor[0],pointCoor[1],pointCoor[2],name)

(2)线

importmathimportrhinoscriptsyntaxasrsimportsysimportclrimportSystemsapDllPath=r'G:ProgramFilesComputersandStructuresSAP200022'ProgramPath=r'G:ProgramFilesComputersandStructuresSAP200022SAP2000.exe'sapDllName='SAP2000v1.dll'sys.path.append(sapDllPath)clr.AddReference(sapDllName)importSAP2000v1fromSAP2000v1import*mhelper=SAP2000v1.Helper()mySapObject=mhelper.GetObject("CSI.SAP2000.API.SapObject")SapModel=mySapObject.SapModelifbuild:mat="C30";mat=clr.Reference[System.String]()SapModel.PropMaterial.AddMaterial(mat,eMatType.Concrete,"China","GB","GB50010C30");sec="Frame";SapModel.PropFrame.SetRectangle(sec,"C30",800,300);fori,curveIteminenumerate(curve):name=str(i)name=clr.Reference[System.String]()sp=rs.CurveStartPoint(curveItem)ep=rs.CurveEndPoint(curveItem)SapModel.FrameObj.AddByCoord(sp[0],sp[1],sp[2],ep[0],ep[1],ep[2],name,sec);

(3)面

importmathimportrhinoscriptsyntaxasrsimportsysimportclrimportSystemsapDllPath=r'G:ProgramFilesComputersandStructuresSAP200022'ProgramPath=r'G:ProgramFilesComputersandStructuresSAP200022SAP2000.exe'sapDllName='SAP2000v1.dll'sys.path.append(sapDllPath)clr.AddReference(sapDllName)importSAP2000v1fromSAP2000v1import*mhelper=SAP2000v1.Helper()mySapObject=mhelper.GetObject("CSI.SAP2000.API.SapObject")SapModel=mySapObject.SapModel#ifbuild:mat="C30";mat=clr.Reference[System.String]()SapModel.PropMaterial.AddMaterial(mat,eMatType.Concrete,"China","GB","GB50010C30");sec="Area";SapModel.PropArea.SetShell(sec,1,"C30",0,120,120);fori,surfaceIteminenumerate(surface):name=str(i)name=clr.Reference[System.String]()points=rs.SurfacePoints(surfaceItem)pointNumber=len(points)corner_X=System.Array.CreateInstance(float,pointNumber)corner_Y=System.Array.CreateInstance(float,pointNumber)corner_Z=System.Array.CreateInstance(float,pointNumber)fori,pointIteminenumerate(points):corner_X[i]=pointItem[0]corner_Y[i]=pointItem[1]corner_Z[i]=pointItem[2]corner_X_n=clr.StrongBox[System.Array[float]](corner_X)corner_Y_n=clr.StrongBox[System.Array[float]](corner_Y)corner_Z_n=clr.StrongBox[System.Array[float]](corner_Z)SapModel.AreaObj.AddByCoord(pointNumber,corner_X_n,corner_Y_n,corner_Z_n,name,sec);

此处很多朋友都来问说面不能成功显示出来。

主要有两个问题:

①确保面的角点坐标和数量均正确

②角点逆时针排序

(4)Python除了使用RhinoScriptSyntax外,也可以直接引用Rhino的库

importrhinoscriptsyntaxasrsfromRhino.Geometryimport*a=Point3d(10,10,10)

1.2.3. 脚本案例

1.2.3.1. 制作一个建立梁单元的电池,并建立参数化结构分析模型

添加相关参数

cOAPImySapObject=null;mySapObject=(cOAPI)System.Runtime.InteropServices.Marshal.GetActiveObject("CSI.SAP2000.API.SapObject");cSapModelsapModel=mySapObject.SapModel;if(!build){return;}stringmat="C30";sapModel.PropMaterial.AddMaterial(refmat,eMatType.Concrete,"China","GB","GB50010C30");stringsec="Frame";sapModel.PropFrame.SetRectangle(sec,"C30",height,width);stringName="Beam";Point3dstartPoint=curve.PointAtStart;Point3dendPoint=curve.PointAtEnd;sapModel.FrameObj.AddByCoord(startPoint.X,startPoint.Y,startPoint.Z,endPoint.X,endPoint.Y,endPoint.Z,refName,sec);

1.2.3.2. 通过Galapagos进行最优化分析

新建模型

cOAPImySapObject=null;mySapObject=(cOAPI)System.Runtime.InteropServices.Marshal.GetActiveObject("CSI.SAP2000.API.SapObject");cSapModelsapModel=mySapObject.SapModel;if(!build){return;}sapModel.InitializeNewModel();sapModel.File.NewBlank();A=true;

运行分析并提取结果

cOAPImySapObject=null;mySapObject=(cOAPI)System.Runtime.InteropServices.Marshal.GetActiveObject("CSI.SAP2000.API.SapObject");cSapModelsapModel=mySapObject.SapModel;if(!build){return;}sapModel.SetModelIsLocked(false);sapModel.SelectObj.CoordinateRange(-100000,100000,-100000,100000,-100,100,false,"Global",false,true,false);bool[]Res=newbool[6]{true,true,true,false,false,false};sapModel.PointObj.SetRestraint("1",refRes,eItemType.SelectedObjects);sapModel.File.Save(@"D:2021study7-grasshopperTutorialexample_01test02.sdb");sapModel.Analyze.RunAnalysis();sapModel.Results.Setup.DeselectAllCasesAndCombosForOutput();sapModel.Results.Setup.SetCaseSelectedForOutput("DEAD");intNumberResults=0;string[]Obj=newstring[1];string[]Elm=newstring[1];string[]LoadCase=newstring[1];string[]StepType=newstring[1];double[]StepNum=newdouble[1];double[]U1=newdouble[1];double[]U2=newdouble[1];double[]U3=newdouble[1];double[]R1=newdouble[1];double[]R2=newdouble[1];double[]R3=newdouble[1];sapModel.Results.JointDispl("77",eItemTypeElm.ObjectElm,refNumberResults,refObj,refElm,refLoadCase,refStepType,refStepNum,refU1,refU2,refU3,refR1,refR2,refR3);A=U3[0];

我们非解构一直关注建筑艺术与结构技术的有机融合。我们在做好设计的同时,一直关注数字化、智能化等前沿技术在建筑设计行业中的运用,这些年一直在坚持探索和实践。

非常欢迎优秀的你来加入我们,一起来跨界,做一名推动行业发展的斜杠青年。

非解构 | 跨界建筑师招募

非解构 | 跨界结构工程师招募

非解构 | 算法工程师招募

结构跨界实习生招募

这几年,对参数化设计感兴趣的朋友越来越多,我们的参数化设计交流群也已经发展到了5群,欢迎更多的朋友加入,相互交流学习。

添加我们“转自:非解构-公众号”微信,

加入参数化设计交流群。

不了解我们的可以来补课了

非解构 | 数字化技术助力探索结构设计新空间

非解构 | 参数化建筑设计技术路径探讨

非解构 | 对BIM工作流的深度思考

当结构设计遇到遗传算法

当建筑师甩给我一个Rhino模型(一)

当建筑师甩给我一个rhino模型(二)

盈建科,二次开发

PKPM, 二次开发

本文来自网络,不代表钢构人的立场,转载请注明出处。搜索工程类文章,就用钢构人网站。 https://www.ganggouren.com/2021/08/751b08d2dc/

钢结构地图

上一篇
下一篇

作者: ganggouren

为您推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

联系我们

联系我们

17717621528

在线咨询: QQ交谈

邮箱: 1356745727@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息
关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部