在《C#开发BIMFACE系列12 服务端API之文件转换》中详细介绍了7种文件转换的方法。发起源文件/模型转换后,转换过程可能成功也可能失败。那么在这种情况下就需要查询源文件/模型的转换状态。一共有三种方式可以知道转换是否成功。
第一种:调用文件转换接口 https://api.bimface.com/translate,该接口的返回结果中包含转换结果信息。
第二种:调用文件转换接口 https://api.bimface.com/translate,由于转换不能立即完成,BIMFace支持在文件转换完成以后,通过Callback机制通知应用,在Callbak返回结果中包含了转换结果信息。
第三种就是本篇要介绍通过接口查询转换状态。
请求地址:GET https://api.bimface.com/translate
说明:应用发起转换以后,可以通过该接口查询转换状态
参数:
请求 path(示例):https://api.bimface.com/translate?fileId=857482189666208
请求 header(示例):"Authorization: Bearer dc671840-bacc-4dc5-a134-97c1918d664b"
HTTP响应示例(200):
{ "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 }
文章来源: 博客园
- 还没有人评论,欢迎说说您的想法!