在《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" : ,
"name" : "bimface_2018.rvt",
"priority" : ,
"reason" : "reason",
"status" : "success",
"thumbnail" : [ "https://m.bimface.com/9b711803a43b92d871cde346b63e5019/thumbnail/96.png", "https://m.bimface.com/9b711803a43b92d871cde346b63e5019/thumbnail/256.png" ]
},
"message" : ""
}
C#实现方法:
/// <summary>
/// 获取转换状态(应用发起转换以后,可以通过该接口查询转换状态)
/// </summary>
/// <param name="accessToken">令牌</param>
/// <param name="fileId"></param>
/// <returns></returns>
public virtual FileTranslateResponse GetFileTranslateStatus(string accessToken, long fileId)
{
//GET https://api.bimface.com/translate
string url = string.Format(BimfaceConstants.API_HOST + "/translate?fileId={0}", fileId); BimFaceHttpHeaders headers = new BimFaceHttpHeaders();
headers.AddOAuth2Header(accessToken); try
{
FileTranslateResponse response; HttpManager httpManager = new HttpManager(headers);
HttpResult httpResult = httpManager.Get(url);
if (httpResult.Status == HttpResult.STATUS_SUCCESS)
{
response = httpResult.Text.DeserializeJsonToObject<FileTranslateResponse>();
}
else
{
response = new FileTranslateResponse
{
Message = httpResult.RefText
};
} return response;
}
catch (Exception ex)
{
throw new Exception("[获取转换状态]发生异常!", ex);
}
}
其中调用到的 httpManager.Get() 方法,请参考《C# HTTP系列》
以“rac_advanced_sample_project-三维视图 - From Parking Area.dwg”为例,查询其转换的状态信息
// 获取转换状态
protected void btnGetFileTranslateStatus_Click(object sender, EventArgs e)
{
long fileId = txtFileID.Text.Trim().ToLong();
FileConvertApi api = new FileConvertApi();
FileTranslateResponse response = api.GetFileTranslateStatus(txtAccessToken.Text, fileId); txtResult.Text = response.Code.ToString2()
+ Environment.NewLine
+ response.Message.ToString2()
+ Environment.NewLine
+ response.Data.ToString2();
}
返回的转换结果类FileTranslateResponse如下:
/// <summary>
/// 文件转换返回的结果类
/// </summary>
[Serializable]
public class FileTranslateResponse : GeneralResponse<FileTranslateEntity>
{ } [Serializable]
public class FileTranslateEntity
{
/// <summary>
/// 文件转换完成的时间
/// </summary>
[JsonProperty("createTime")]
public DateTime? CreateTime { get; set; } [JsonProperty("databagId")]
public string DatabagId { get; set; } [JsonProperty("fileId")]
public long? FileId { get; set; } /// <summary>
/// 文件的名称,包括后缀
/// </summary>
[JsonProperty("name")]
public string Name { get; set; } /// <summary>
/// 优先级,数字越大,优先级越低。1, 2, 3
/// </summary>
[JsonProperty("priority")]
public int? Priority { get; set; } /// <summary>
/// 若转换失败,则返回失败原因
/// </summary>
[JsonProperty("reason")]
public string Reason { get; set; } /// <summary>
/// 转换的状态
/// </summary>
[JsonProperty("status")]
public string Status { get; set; } /// <summary>
/// 缩略图
/// </summary>
[JsonProperty("thumbnail")]
public string[] Thumbnails { get; set; } /// <summary>返回表示当前对象的字符串。</summary>
/// <returns>表示当前对象的字符串。</returns>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if(Thumbnails != null && Thumbnails.Length > )
{
foreach(string thumbnail in Thumbnails)
{
sb.AppendLine(thumbnail);
}
} return string.Format("FileTranslateEntity [createTime={0}, fileId={1}, name={2}, priority={3},reason={4},status={5},thumbnail={6}]",
CreateTime, FileId, Name, Priority, Reason, Status, sb.ToString());
}
}