DEMO示例

服务场景描述

应用IQCAD图纸解析引擎,通过调用API,可提取图纸中构件,空间、文本表格图纸属性信息。

可以通过接口调用实现以下场景

  • 获取建筑专业平面图中的构件、空间信息
  • 获取图纸中的文本表格信息
  • 对于图纸中的信息解析后,经过数据加工后需要可视化到图纸的dwg和json的相互转化

接口调用流程

image.png

服务调用流程

1. 获取Access Token

首先需要获取token。
token有两个,一个是AECORE Access Token,获取方式参考文档获取AccessToken
另一个是CAD识别服务Access Token,获取方式参考文档获取AccessToken

2. 上传文件

第二步需要对需要进行识别的图纸进行上传,目前支持的图纸格式为dwg。
上传时可以直接上传单张dwg图纸,也可以将多张图纸打包成zip上传。
具体上传方法请参照:
上传文件

3. 查询文件上传状态

上传后可以通过调用文件查询接口,查询文件是否已上传完毕。
具体方法参照:
查询文件上传状态

4. 发起识别任务

文件上传完毕后,可以正式发起识别任务。
具体方法参照:
发起识别任务

5. 查询识别任务状态

如果在发起识别任务时,填写了回调函数,则可以在发起识别任务后,等待回调函数通知。
如果没有填写,则需要主动查询任务识别状态。
具体方法参照:
查询识别任务状态

6. 获取识别结果

如果识别任务已经完整,则可以下载识别结果数据。
具体方法参照:
获取识别结果

演示代码

step1:maven依赖

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.10.0</version>
</dependency>

step2:OkHttpUtil工具类

package com.utils;

import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.Set;

public class OkHttpUtil {

    private static Logger logger = LoggerFactory
            .getLogger(OkHttpUtil.class);

    private OkHttpClient client = new OkHttpClient();

    private Request.Builder createBuilder(String url,Map<String, String> requestProperty) {
        Request.Builder builder = new Request.Builder()
                .url(url);
        if (requestProperty != null) {
            Set<String> keys = requestProperty.keySet();
            for (String key : keys) {
                builder.addHeader(key, requestProperty.get(key));
            }
        }
        return builder;
    }

    private String loadResponseStr(Request.Builder builder) {
        Request request = builder.build();
        try {
            Response response = client.newCall(request).execute();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        } catch (Exception e) {
            logger.error("LoadResponseStr:", e);
        }
        return null;
    }

    private RequestBody createRequestBody(Map<String, String> params){
        FormBody.Builder formBody = new FormBody.Builder();
        if (params != null) {
            Set<String> keys = params.keySet();
            for (String key : keys) {
                formBody.add(key, params.get(key));
            }
        }
        return formBody.build();
    }

    /**
     * 采用Get方式
     *
     * @param urlStr
     * @param param
     * @param requestProperty 请求头
     * @return
     */
    public String doGet(String urlStr, String param,
                        Map<String, String> requestProperty) {
        String url = StringUtils.isBlank(param) ? urlStr : urlStr + "?"
                + param;
        return loadResponseStr(createBuilder(url,requestProperty));
    }

    /**
     * 采用Post方式,模拟form提交
     *
     * @param urlStr
     * @param params
     * @param requestProperty 请求头
     * @return
     */
    public String doPost(String urlStr, Map<String, String> params,
                         Map<String, String> requestProperty) {
        return loadResponseStr(createBuilder(urlStr,requestProperty).post(createRequestBody(params)));
    }


    /**
     * 采用Post方式,提交json
     *
     * @param urlStr
     * @param jsonBody
     * @param requestProperty 请求头
     * @return
     */
    public String doPostJson(String urlStr, String jsonBody,
                             Map<String, String> requestProperty) {
        //数据类型为json格式
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, jsonBody);
        return loadResponseStr(createBuilder(urlStr,requestProperty).post(body));
    }


    /**
     * 采用Put方式,提交json
     *
     * @param urlStr
     * @param jsonBody
     * @param requestProperty 请求头
     * @return
     */
    public String doPutJson(String urlStr, String jsonBody,
                            Map<String, String> requestProperty) {
       //数据类型为json格式
        if(StringUtils.isNotBlank(jsonBody)) {
            MediaType JSON = MediaType.parse("application/json; charset=utf-8");
            RequestBody body = RequestBody.create(JSON, jsonBody);
            return loadResponseStr(createBuilder(urlStr,requestProperty).put(body));
        } else {
            RequestBody body =RequestBody.create(null, new byte[0]);
            return loadResponseStr(createBuilder(urlStr,requestProperty).put(body));
        }
    }
} 

step3:获取token

public void getToken() throws UnsupportedEncodingException {
    OkHttpUtil client = new OkHttpUtil();
    String appKey = "aaa"; //isv申请的appKey
    String appSecret = "aaaa"; //isv申请的appSecret
    String creds = String.format("%s:%s", appKey, appSecret);
    String credential = Base64.encode(creds.getBytes("UTF-8"));
    String urlStr = "https://apigate.glodon.com/gcad/oauth2/token";
    Map<String, String> params = new HashMap();
    params.put("grant_type","client_credentials");
    Map<String, String> requestProperty = new HashMap();
    requestProperty.put("Authorization","Basic "+credential);
    String res = client.doPost(urlStr,params,requestProperty);
    System.out.println(res);
}

step4:上传图纸

public void upload(){
    OkHttpUtil client = new OkHttpUtil();
    String access_token = "cn-e1br0908-09dd-4039-9040-9fd583";
    String fileName = "xx.dwg";
    String fileUrl = "https://glodon-cad-drawing.oss-cn-beijing.aliyuncs.com/xx.dwg";
    String urlStr = "https://apigate.glodon.com/gcad/api/v1/file/upload?name="+URLEncoder.encode(fileName,"UTF-8")+"&url="+ URLEncoder.encode(fileUrl, "UTF-8" );
    Map<String, String> requestProperty = new HashMap();
    requestProperty.put("Authorization","Bearer "+access_token);
    String res = client.doPutJson(urlStr, null, requestProperty);
    // 得到fileId
    System.out.println(res);
}

step5:查询上传图纸状态

public void uploadStatus(){
    String access_token = "cn-e1br0908-09dd-4039-9040-9fd583";
    OkHttpUtil client = new OkHttpUtil();
    // fileId是从step4结果获取的
    String urlStr = "https://apigate.glodon.com/gcad/api/v1/file/files/"+fileId+"/uploadStatus";
    Map<String, String> requestProperty = new HashMap();
    requestProperty.put("Authorization","Bearer "+access_token);
    String res = client.doGet(urlStr, null,requestProperty);
    System.out.println(res);
}

step6:发起识别

public void translate(){
    String access_token = "cn-e1br0908-09dd-4039-9040-9fd583";
    OkHttpUtil client = new OkHttpUtil();
    // fileId是从step4结果获取的
    String urlStr = "https://apigate.glodon.com/gcad/api/v1/job?access_token_ex="+access_token;
    Map<String, String> requestProperty = new HashMap();
    requestProperty.put("Authorization","Bearer "+access_token);
    String requestBody = "{\"source\":{\"fileId\":xxx,\"compressed\":false, \"rootName\":\"xxx.dwg\"},\"config\":{\"split\":true}}";
    String res = client.doPutJson(urlStr, requestBody, requestProperty);
    //得到databagId
    System.out.println(res);
}

step7:查询识别任务状态

public void translateStatus(){
    String access_token = "cn-e1br0908-09dd-4039-9040-9fd583";
    OkHttpUtil client = new OkHttpUtil();
    // fileId是从step4结果获取的
    String urlStr = "https://apigate.glodon.com/gcad/api/v1/job?access_token_ex="+access_token;
    Map<String, String> requestProperty = new HashMap();
    requestProperty.put("Authorization","Bearer "+access_token);
    String res = client.doGet(urlStr, null,requestProperty);
    System.out.println(res);
}

step8:查询识别结果

public void extractFeatures(){
    String access_token = "cn-e1br0908-09dd-4039-9040-9fd583";
    OkHttpUtil client = new OkHttpUtil();
    // fileId是从step4结果获取的,databagId是从step6结果获取的
    String urlStr = "https://apigate.glodon.com/gcad/api/v1/resource/"+databagId+"/"+fileId;
    Map<String, String> requestProperty = new HashMap();
    requestProperty.put("Authorization","Bearer "+access_token);
    String requestBody = "{\"config\":{\"features\":[\"sheetFrame\",\"floorTable\"]}}";
    String res = client.doPutJson(urlStr, requestBody, requestProperty);
    System.out.println(res);
}