1. 程式人生 > >C#開發BIMFACE系列18 服務端API之獲取模型資料3:獲取構件屬性

C#開發BIMFACE系列18 服務端API之獲取模型資料3:獲取構件屬性

系列目錄     【已更新最新開發文章,點選檢視詳細】

本篇主要介紹如何獲取單檔案/模型下單個構建的屬性資訊。

請求地址:GET https://api.bimface.com/data/v2/files/{fileId}/elements/{elementId}

說明:支援查詢模型屬性重寫後構件的屬性,需要設定請求引數includeOverrides的值為true

引數:

請求 path(示例):https://api.bimface.com/data/v2/files/1211223382064960/elements/1109329

                            若需查詢重寫後的構件屬性,請求示例為

                            https://api.bimface.com/data/v2/files/1211223382064960/elements/1109329?includeOverrides=true

請求 header(示例):"Authorization: Bearer dc671840-bacc-4dc5-a134-97c1918d664b"

HTTP響應示例(200):

{
  "code" : "success",
  "data" : {
    "boundingBox" : {
      "max" : {
        "x" : -4938.068482562385,
        "y" : -3201.59397858169,
        "z" : 0.0
      },
      "min" : {
        "x" : -4938.068482562385,
        "y" : -3201.59397858169,
        "z" : 0.0
      }
    },
    "elementId" : "313052",
    "familyGuid" : "000222",
    "guid" : "79d547c1-5dbf-4e6a-811d-951cf37b29da-0004c6dc",
    "name" : "norm - 150mm",
    "properties" : [ {
      "group" : "dimension",
      "items" : [ {
        "code" : "perimeter",
        "extension" : "object",
        "key" : "perimeter",
        "orderNumber" : 0,
        "unit" : "mm",
        "value" : 17200,
        "valueType" : 2
      } ]
    } ]
  },
  "message" : ""
}

C#實現方法:

 1 /// <summary>
 2 ///  獲取構件屬性
 3 /// </summary>
 4 /// <param name="accessToken">令牌</param>
 5 /// <param name="fileId">【必填】代表該單模型的檔案ID</param>
 6 /// <param name="elementId">【必填】代表該單模型的構件ID</param>
 7 /// <param name="includeOverrides">【非必填】是否查詢修改的屬性</param>
 8 /// <returns></returns>
 9 public virtual SingleModelSingleElementProperty GetSingleModelSingleElementProperty(string accessToken, long fileId, string elementId, bool? includeOverrides = null)
10 {
11     // GET https://api.bimface.com/data/v2/files/{fileId}/elements/{elementId}
12     string url = string.Format(BimfaceConstants.API_HOST + "/data/v2/files/{0}/elements/{1}", fileId, elementId);
13     if (includeOverrides != null)
14     {
15         url = url + "?includeOverrides=" + includeOverrides;
16     }
17 
18     BimFaceHttpHeaders headers = new BimFaceHttpHeaders();
19     headers.AddOAuth2Header(accessToken);
20 
21     try
22     {
23         SingleModelSingleElementProperty response;
24 
25         HttpManager httpManager = new HttpManager(headers);
26         HttpResult httpResult = httpManager.Get(url);
27         if (httpResult.Status == HttpResult.STATUS_SUCCESS)
28         {
29             response = httpResult.Text.DeserializeJsonToObject<SingleModelSingleElementProperty>();
30         }
31         else
32         {
33             response = new SingleModelSingleElementProperty
34             {
35                 Message = httpResult.RefText
36             };
37         }
38 
39         return response;
40     }
41     catch (Exception ex)
42     {
43         throw new Exception("[獲取構件屬性]發生異常!", ex);
44     }
45 }

其中呼叫到的 httpManager.Get() 方法,請參考《C# HTTP系列》

 響應結果對應的實體類 SingleModelSingleElementProperty ,封裝如下:

/// <summary>
/// 獲取單個模型單個構件的屬性返回的結果類
/// </summary>
[Serializable]
public class SingleModelSingleElementProperty : GeneralResponse<SingleModelSingleElementEntity>
{

}
 1 [Serializable]
 2 public class SingleModelSingleElementEntity
 3 {
 4     [JsonProperty("boundingBox")]
 5     public BoundingBox BoundingBox { get; set; }
 6 
 7     [JsonProperty("elementId")]
 8     public string ElementId { get; set; }
 9 
10     [JsonProperty("familyGuid")]
11     public string FamilyGuid { get; set; }
12 
13     [JsonProperty("guid")]
14     public string Guid { get; set; }
15 
16     [JsonProperty("name")]
17     public string Name { get; set; }
18 
19     [JsonProperty("properties")]
20     public PropertyGroup[] Properties { get; set; }
21 
22     /// <summary>返回表示當前物件的字串。</summary>
23     /// <returns>表示當前物件的字串。</returns>
24     public override string ToString()
25     {
26         StringBuilder sb = new StringBuilder();
27         if (Properties != null && Properties.Length > 0)
28         {
29             foreach (var pro in Properties)
30             {
31                 sb.AppendLine(pro.ToString());
32             }
33         }
34 
35         return string.Format("SingleModelSingleElementEntity: [boundingBox={0}, elementId={1}, familyGuid={2}, Guid={3}, name={4}, Properties={5}]",
36                              BoundingBox, ElementId, FamilyGuid, Guid,Name, sb);
37     }
38 }
 1 [Serializable]
 2 public class BoundingBox
 3 {
 4     [JsonProperty("max")]
 5     public Max Max { get; set; }
 6 
 7     [JsonProperty("min")]
 8     public Min Min { get; set; }
 9 
10     /// <summary>返回表示當前物件的字串。</summary>
11     /// <returns>表示當前物件的字串。</returns>
12     public override string ToString()
13     {
14         return string.Format("[max={0}, min={1}]", Max.ToString(), Min.ToString());
15     }
16 }
1 [Serializable]
2 public class Max : ThreeDimensionalCoordinates
3 {
4 }
[Serializable]
public class Min : ThreeDimensionalCoordinates
{
}
/// <summary>
///  三維座標系
/// </summary>
public class ThreeDimensionalCoordinates
{
    [JsonProperty("x")]
    public double X { get; set; }

    [JsonProperty("y")]
    public double Y { get; set; }

    [JsonProperty("z")]
    public double Z { get; set; }

    /// <summary>返回表示當前物件的字串。</summary>
    /// <returns>表示當前物件的字串。</returns>
    public override string ToString()
    {
        return string.Format("[x={0}, y={1}, z={2}]", X, Y, Z);
    }
}

其中 PropertyGroup 類,請參考《C#開發BIMFACE系列17 服務端API之獲取模型資料2:獲取構件材質列表》中的 響應結果類中的 PropertyGroup 類。

測試

在BIMFACE的控制檯中可以看到我們上傳的檔案列表,共計2個檔案。模型狀態均為轉換成功。

以“01_BIMFACE示例檔案-Revit模型.rvt”為例來測試。

  完整的屬性為:
success
elementId:300067

SingleModelSingleElementEntity: 
[boundingBox=[max=[x=5838.783, y=4387.716, z=6000], 
              min=[x=5788.783, y=4237.716, z=5475]
             ], 
 elementId=300067, 
 familyGuid=2d99307a-66cd-4439-b173-5c88d252416e-00002122, 
 Guid=89c90034-40ae-423a-8935-8f5624db86c1-00049423, 
 name=50 x 150 mm, 
 Properties=
    [group=基本屬性, 
     items= [code=, extension=, key=specialty, orderNumber=,unit=,value=,valueType=]
            [code=, extension=, key=floor, orderNumber=,unit=,value=F2,valueType=]
            [code=, extension=, key=categoryId, orderNumber=,unit=,value=-2000171,valueType=]
            [code=, extension=, key=categoryName, orderNumber=,unit=,value=幕牆豎梃,valueType=]
            [code=, extension=, key=family, orderNumber=,unit=,value=矩形豎梃,valueType=]
            [code=, extension=, key=familyId, orderNumber=,unit=,value=,valueType=]
            [code=, extension=, key=familyType, orderNumber=,unit=,value=50 x 150 mm,valueType=]
            [code=, extension=, key=familyTypeId, orderNumber=,unit=,value=8486,valueType=]
            [code=, extension=, key=systemType, orderNumber=,unit=,value=,valueType=]
            [code=, extension=, key=building, orderNumber=,unit=,value=,valueType=]
    ]
    [group=尺寸標註, 
     items=[code=, extension=, key=邊 1 上的寬度, orderNumber=,unit=mm,value=25,valueType=2]
           [code=, extension=, key=邊 2 上的寬度, orderNumber=,unit=mm,value=25,valueType=2]
           [code=, extension=, key=長度, orderNumber=,unit=mm,value=525,valueType=2]
    ]
    [group=材質和裝飾, 
     items=[code=, extension=, key=材質, orderNumber=,unit=,value=金屬 - 鋁,valueType=4]
    ]
    [group=構造, 
     items=[code=, extension=, key=位置, orderNumber=,unit=,value=垂直於面,valueType=4]
           [code=, extension=, key=厚度, orderNumber=,unit=mm,value=150,valueType=2]
           [code=, extension=, key=角豎梃, orderNumber=,unit=,value=False,valueType=1]
           [code=, extension=, key=輪廓, orderNumber=,unit=,value=預設,valueType=4]
    ]
    [group=標識資料, 
     items=[code=, extension=, key=OmniClass 標題, orderNumber=,unit=,value=,valueType=3]
           [code=, extension=, key=OmniClass 編號, orderNumber=,unit=,value=,valueType=3]
           [code=, extension=, key=URL, orderNumber=,unit=,value=,valueType=3]
           [code=, extension=, key=程式碼名稱, orderNumber=,unit=,value=,valueType=3]
           [code=, extension=, key=製造商, orderNumber=,unit=,value=,valueType=3]
           [code=, extension=, key=影象, orderNumber=,unit=,value=,valueType=4]
           [code=, extension=, key=型號, orderNumber=,unit=,value=,valueType=3]
           [code=, extension=, key=成本, orderNumber=,unit=,value=,valueType=2]
           [code=, extension=, key=標記, orderNumber=,unit=,value=,valueType=3]
           [code=, extension=, key=註釋, orderNumber=,unit=,value=,valueType=3]
           [code=, extension=, key=註釋記號, orderNumber=,unit=,value=,valueType=3]
           [code=, extension=, key=型別影象, orderNumber=,unit=,value=,valueType=4]
           [code=, extension=, key=型別標記, orderNumber=,unit=,value=,valueType=3]
           [code=, extension=, key=型別註釋, orderNumber=,unit=,value=,valueType=3]
           [code=, extension=, key=說明, orderNumber=,unit=,value=,valueType=3]
           [code=, extension=, key=部件程式碼, orderNumber=,unit=,value=,valueType=3]
           [code=, extension=, key=部件說明, orderNumber=,unit=,value=,valueType=3]
    ]
    [group=階段化, 
     items=[code=, extension=, key=建立的階段, orderNumber=,unit=,value=新構造,valueType=4]
           [code=, extension=, key=拆除的階段, orderNumber=,unit=,value=無,valueType=4]
    ]
    [group=限制條件, 
     items=[code=, extension=, key=偏移量, orderNumber=,unit=mm,value=0,valueType=2]
           [code=, extension=, key=角度, orderNumber=,unit=°,value=0.00,valueType=2]
    ]
]

測試程式碼如下:

 1 // 獲取構件屬性
 2 protected void btnGetSingleModelSingleElementProperty_Click(object sender, EventArgs e)
 3 {
 4     FileConvertApi api = new FileConvertApi();
 5 
 6     string elementId = ddlSingleModelElements.SelectedValue;
 7 
 8     SingleModelSingleElementProperty response = api.GetSingleModelSingleElementProperty(txtAccessToken.Text, txtFileID.Text.ToLong(), elementId, chkIncludeOverrides.Checked);
 9 
10     txtResult.Text = response.Code.ToString2()
11                    + Environment.NewLine
12                    + "elementId:" + elementId
13                    + Environment.NewLine
14                    + response.Message.ToString2()
15                    + Environment.NewLine
16                    + response.Data.ToString();
17 }
系列目錄     【已更新最新開發文章,點選檢視詳細】 &nbs