在《C#開發BIMFACE系列12 服務端API之文件轉換》中詳細介紹了7種文件轉換的方法。發起源文件/模型轉換後,轉換過程可能成功也可能失敗。那麼在這種狀況下就須要查詢源文件/模型的轉換狀態。一共有三種方式能夠知道轉換是否成功。html
第一種:調用文件轉換接口 https://api.bimface.com/translate,該接口的返回結果中包含轉換結果信息。api
第二種:調用文件轉換接口 https://api.bimface.com/translate,因爲轉換不能當即完成,BIMFace支持在文件轉換完成之後,經過Callback機制通知應用,在Callbak返回結果中包含了轉換結果信息。ide
第三種就是本篇要介紹經過接口查詢轉換狀態。測試
請求地址:GET https://api.bimface.com/translateui
說明:應用發起轉換之後,能夠經過該接口查詢轉換狀態
url
參數:
spa
請求 path(示例):https://api.bimface.com/translate?fileId=857482189666208code
請求 header(示例):"Authorization: Bearer dc671840-bacc-4dc5-a134-97c1918d664b"orm
HTTP響應示例(200):htm
{ "code" : "success", "data" : { "createTime" : "2017-12-25 17:23:46", "databagId" : "9b711803a43b92d871cde346b63e5019", "fileId" : 1248789071339712, "name" : "bimface_2018.rvt", "priority" : 2, "reason" : "reason", "status" : "success", "thumbnail" : [ "https://m.bimface.com/9b711803a43b92d871cde346b63e5019/thumbnail/96.png", "https://m.bimface.com/9b711803a43b92d871cde346b63e5019/thumbnail/256.png" ] }, "message" : "" }
C#實現方法:
1 /// <summary> 2 /// 獲取轉換狀態(應用發起轉換之後,能夠經過該接口查詢轉換狀態) 3 /// </summary> 4 /// <param name="accessToken">令牌</param> 5 /// <param name="fileId"></param> 6 /// <returns></returns> 7 public virtual FileTranslateResponse GetFileTranslateStatus(string accessToken, long fileId) 8 { 9 //GET https://api.bimface.com/translate 10 string url = string.Format(BimfaceConstants.API_HOST + "/translate?fileId={0}", fileId); 11 12 BimFaceHttpHeaders headers = new BimFaceHttpHeaders(); 13 headers.AddOAuth2Header(accessToken); 14 15 try 16 { 17 FileTranslateResponse response; 18 19 HttpManager httpManager = new HttpManager(headers); 20 HttpResult httpResult = httpManager.Get(url); 21 if (httpResult.Status == HttpResult.STATUS_SUCCESS) 22 { 23 response = httpResult.Text.DeserializeJsonToObject<FileTranslateResponse>(); 24 } 25 else 26 { 27 response = new FileTranslateResponse 28 { 29 Message = httpResult.RefText 30 }; 31 } 32 33 return response; 34 } 35 catch (Exception ex) 36 { 37 throw new Exception("[獲取轉換狀態]發生異常!", ex); 38 } 39 }
其中調用到的 httpManager.Get() 方法,請參考《C# HTTP系列》
以「rac_advanced_sample_project-三維視圖 - From Parking Area.dwg」爲例,查詢其轉換的狀態信息
1 // 獲取轉換狀態 2 protected void btnGetFileTranslateStatus_Click(object sender, EventArgs e) 3 { 4 long fileId = txtFileID.Text.Trim().ToLong(); 5 FileConvertApi api = new FileConvertApi(); 6 FileTranslateResponse response = api.GetFileTranslateStatus(txtAccessToken.Text, fileId); 7 8 txtResult.Text = response.Code.ToString2() 9 + Environment.NewLine 10 + response.Message.ToString2() 11 + Environment.NewLine 12 + response.Data.ToString2(); 13 }
返回的轉換結果類FileTranslateResponse以下:
1 /// <summary> 2 /// 文件轉換返回的結果類 3 /// </summary> 4 [Serializable] 5 public class FileTranslateResponse : GeneralResponse<FileTranslateEntity> 6 { 7 8 } 9 10 [Serializable] 11 public class FileTranslateEntity 12 { 13 /// <summary> 14 /// 文件轉換完成的時間 15 /// </summary> 16 [JsonProperty("createTime")] 17 public DateTime? CreateTime { get; set; } 18 19 [JsonProperty("databagId")] 20 public string DatabagId { get; set; } 21 22 [JsonProperty("fileId")] 23 public long? FileId { get; set; } 24 25 /// <summary> 26 /// 文件的名稱,包括後綴 27 /// </summary> 28 [JsonProperty("name")] 29 public string Name { get; set; } 30 31 /// <summary> 32 /// 優先級,數字越大,優先級越低。1, 2, 3 33 /// </summary> 34 [JsonProperty("priority")] 35 public int? Priority { get; set; } 36 37 /// <summary> 38 /// 若轉換失敗,則返回失敗緣由 39 /// </summary> 40 [JsonProperty("reason")] 41 public string Reason { get; set; } 42 43 /// <summary> 44 /// 轉換的狀態 45 /// </summary> 46 [JsonProperty("status")] 47 public string Status { get; set; } 48 49 /// <summary> 50 /// 縮略圖 51 /// </summary> 52 [JsonProperty("thumbnail")] 53 public string[] Thumbnails { get; set; } 54 55 56 /// <summary>返回表示當前對象的字符串。</summary> 57 /// <returns>表示當前對象的字符串。</returns> 58 public override string ToString() 59 { 60 StringBuilder sb = new StringBuilder(); 61 if(Thumbnails != null && Thumbnails.Length > 0) 62 { 63 foreach(string thumbnail in Thumbnails) 64 { 65 sb.AppendLine(thumbnail); 66 } 67 } 68 69 return string.Format("FileTranslateEntity [createTime={0}, fileId={1}, name={2}, priority={3},reason={4},status={5},thumbnail={6}]", 70 CreateTime, FileId, Name, Priority, Reason, Status, sb.ToString()); 71 } 72 }