【C#】【SAP2000】读取SAP2000中单元截面信息和几何信息到Grasshopper中


    if (build == true)
    {
      // 声明变量
      int ret;
      int Numit = 0;
      int[] ObjType = new int[0];
      string[] ObjName = new string[0];
      string sauto = "";
      string propname = "";
      string point1 = "";
      string point2 = "";
      double x1 = 0;
      double y1 = 0;
      double z1 = 0;
      double x2 = 0;
      double y2 = 0;
      double z2 = 0;
      List<Point3d> _pnt1 = new List<Point3d>();
      List<Point3d> _pnt2 = new List<Point3d>();
      List<string> _prof = new List<string>();

      // 连接到正在运行的 SAP2000
      cOAPI mySapObject = null;
      mySapObject = (cOAPI) System.Runtime.InteropServices.Marshal.GetActiveObject("CSI.SAP2000.API.SapObject");
      cSapModel mySapModel;
      mySapModel = mySapObject.SapModel;

      // 获取选中的对象
      ret = mySapModel.SelectObj.GetSelected(ref Numit, ref ObjType, ref ObjName);

      // 遍历选中的对象
      for (int i = 0; i < ObjName.Length; i++)
      {
        // 获取框架对象的起点和终点
        ret = mySapModel.FrameObj.GetPoints(ObjName[i], ref point1, ref point2);

        // 获取起点的坐标
        ret = mySapModel.PointObj.GetCoordCartesian(point1, ref x1, ref y1, ref z1);

        // 获取终点的坐标
        ret = mySapModel.PointObj.GetCoordCartesian(point2, ref x2, ref y2, ref z2);

        // 创建起点和终点的 Point3d 对象
        Point3d pt1 = new Point3d(x1, y1, z1);
        Point3d pt2 = new Point3d(x2, y2, z2);

        // 添加起点和终点到相应的列表
        _pnt1.Add(pt1);
        _pnt2.Add(pt2);

        // 将起点、终点和截面名称列表赋值给输出参数
        p1 = _pnt1;
        p2 = _pnt2;

        // 获取框架对象的截面名称
        ret = mySapModel.FrameObj.GetSection(ObjName[i], ref propname, ref sauto);

        // 添加截面名称到列表
        _prof.Add(propname);
        Profile = _prof;
      }
    }

调整代码编写,让变量在定义参数的时候才声明,并在附近编写。更容易理解。

if (build == true)
{
    // 连接到正在运行的 SAP2000
    cOAPI mySapObject = (cOAPI)System.Runtime.InteropServices.Marshal.GetActiveObject("CSI.SAP2000.API.SapObject");
    cSapModel mySapModel = mySapObject.SapModel;

    // 获取选中的对象
    int Numit = 0;
    int[] ObjType = new int[0];
    string[] ObjName = new string[0];
    int ret = mySapModel.SelectObj.GetSelected(ref Numit, ref ObjType, ref ObjName);

    // 声明点和截面名称列表
    List<Point3d> _pnt1 = new List<Point3d>();
    List<Point3d> _pnt2 = new List<Point3d>();
    List<string> _prof = new List<string>();

    // 遍历选中的对象
    for (int i = 0; i < ObjName.Length; i++)
    {
        // 获取框架对象的起点和终点
        string point1 = "";
        string point2 = "";
        ret = mySapModel.FrameObj.GetPoints(ObjName[i], ref point1, ref point2);

        // 获取起点的坐标
        double x1 = 0;
        double y1 = 0;
        double z1 = 0;
        ret = mySapModel.PointObj.GetCoordCartesian(point1, ref x1, ref y1, ref z1);

        // 获取终点的坐标
        double x2 = 0;
        double y2 = 0;
        double z2 = 0;
        ret = mySapModel.PointObj.GetCoordCartesian(point2, ref x2, ref y2, ref z2);

        // 创建起点和终点的 Point3d 对象
        Point3d pt1 = new Point3d(x1, y1, z1);
        Point3d pt2 = new Point3d(x2, y2, z2);

        // 添加起点和终点到相应的列表
        _pnt1.Add(pt1);
        _pnt2.Add(pt2);

        // 获取框架对象的截面名称
        string propname = "";
        string sauto = "";
        ret = mySapModel.FrameObj.GetSection(ObjName[i], ref propname, ref sauto);

        // 添加截面名称到列表
        _prof.Add(propname);
    }

    // 将起点、终点和截面名称列表赋值给输出参数
    p1 = _pnt1;
    p2 = _pnt2;
    Profile = _prof;
}

做了以下改变:

  1. 将变量声明移动到了它们第一次被使用的位置附近,使代码更加紧凑和易于理解。

  2. 将点和截面名称列表的声明移动到了循环之前,使它们的作用域更加清晰。

  3. 将输出参数的赋值移动到了循环之后,使代码的逻辑更加清晰。

这样调整后的代码结构更加清晰,变量的声明和使用更加紧密,提高了代码的可读性和可维护性。

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hmywillstronger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值