Skip to main content
All CollectionsSystem TasksWorking with Groovy ScriptsGroovy Script Library
Groovy Script - Push federated models to 3D Repo and add 3D Repo's model viewing URL as a custom attribute
Groovy Script - Push federated models to 3D Repo and add 3D Repo's model viewing URL as a custom attribute
Updated over a month ago

Description: Sample groovy script for pushing federated models to 3D Repo and for adding 3D Repo's model viewing URL as a custom attribute.

Context: Document

Remark: Accessible in 'Post' task mode only

Script:

def execute() {

/********** Configurable Param Section ****************/

String teamSpace="3DRepo_Teamspace_Name"; // Add your Team Space name

String apiUrl="https://www.3drepo.io/api";

String apikey="Enter_API_Key_here"; // Add Admin user's API key

String modelType="Enter_Model_Type_here";

String modelUnit="mm";

String customAttributeNameForUrl="Link to 3D Repo"; // Add Asite Custom Attribute name to add 3D repo view file link

String documentViewUrl="https://www.3drepo.io/v5/viewer";

/********** Configurable Param Section ****************/

def DocumentVO docVo = documentService.getDocumentDetails();

String fileType = docVo.getFileName().substring(docVo.getFileName().lastIndexOf("."),docVo.getFileName().length());

if(fileType.equalsIgnoreCase(".ifc")|| fileType.equalsIgnoreCase(".RVT")|| fileType.equalsIgnoreCase(".NWD")|| fileType.equalsIgnoreCase(".NWC")|| fileType.equalsIgnoreCase(".DGN")|| fileType.equalsIgnoreCase(".SPM")|| fileType.equalsIgnoreCase(".DWG")|| fileType.equalsIgnoreCase(".PDF")|| fileType.equalsIgnoreCase(".BIM")){

///// GET PROJECT OR ASSET DATA AND IF NOT GET CREATE THE PROJECT OR ASSET

String ThreeDRepoProjectId = "";

Map<String, String> apiResponse = getProject(docVo.getProjectName(),apiUrl,teamSpace,apikey);

if(!apiResponse.get(IGroovyConstant.STATUS).equals("200")){ // If project NOT FOUND

JsonObject jsonObject= new Gson().fromJson(apiResponse.get(IGroovyConstant.DATA), JsonObject.class);

if(jsonObject!=null && jsonObject.has("code")&& jsonObject.get("code").getAsString().equalsIgnoreCase("PROJECT_NOT_FOUND") ){ //create only when project not found

Map<String, String> apiResponseCreateProject = createProject(docVo.getProjectName(),apiUrl,teamSpace,apikey);

if(!apiResponseCreateProject.get(IGroovyConstant.STATUS).equals("200")){

throw new Exception("Not able to create project :: " + apiResponseCreateProject.get(IGroovyConstant.DATA));

}else{

JsonObject projJsonObject = new Gson().fromJson(apiResponseCreateProject.get(IGroovyConstant.DATA), JsonObject.class);

if(!projJsonObject.get("_id").getAsString().equals("")){

ThreeDRepoProjectId = projJsonObject.get("_id").getAsString();

}else{

throw new Exception("Not able to get project ID " + apiResponse.get(IGroovyConstant.DATA));

}

}

}else{

throw new Exception("Not able to get project :: " + apiResponse.get(IGroovyConstant.DATA));

}

}else{ //If project FOUND

JsonObject jsonObject = new Gson().fromJson(apiResponse.get(IGroovyConstant.DATA), JsonObject.class);

if(!jsonObject.get("_id").getAsString().equals("")){

ThreeDRepoProjectId = jsonObject.get("_id").getAsString();

}else{

throw new Exception("Not able to get project ID " + apiResponse.get(IGroovyConstant.DATA));

}

}

if(ThreeDRepoProjectId.equals("")){

throw new Exception("Not able to get project ID :: " + apiResponse.get(IGroovyConstant.DATA));

}

////// GET MODEL AND CREATE MODEL

String modelId="MODEL ID HERE";

Map<String, String> apiResponseGetModel = getModel(docVo,apiUrl,teamSpace,apikey);

if(apiResponseGetModel.get(IGroovyConstant.STATUS).equals("200")){

JsonArray jsonArray= new Gson().fromJson(apiResponseGetModel.get(IGroovyConstant.DATA), JsonArray.class);

if(jsonArray !=null && jsonArray.size() > 0){

for(JsonObject jsonObject:jsonArray){

if(jsonObject.get("name").getAsString().equals(docVo.getDocRef())){

modelId = jsonObject.get("model").getAsString();

break;

}

}

if(modelId.equals("")){

Map<String, String> apiResponseCreateModel = CreateModel(docVo,apiUrl,teamSpace,apikey,modelType,modelUnit); //CREATE MODEL

if(apiResponseCreateModel.get(IGroovyConstant.STATUS).equals("200")){

JsonObject jsonObject= new Gson().fromJson(apiResponseCreateModel.get(IGroovyConstant.DATA), JsonObject.class);

modelId = jsonObject.get("model").getAsString();

}else{

throw new Exception("Not able to create Model :: " + apiResponseCreateModel.get(IGroovyConstant.DATA));

}

}

System.out.println("modelId::---------------->"+modelId);

}else{

Map<String, String> apiResponseCreateModel = CreateModel(docVo,apiUrl,teamSpace,apikey,modelType,modelUnit); //CREATE MODEL

if(apiResponseCreateModel.get(IGroovyConstant.STATUS).equals("200")){

JsonObject jsonObject= new Gson().fromJson(apiResponseCreateModel.get(IGroovyConstant.DATA), JsonObject.class);

modelId = jsonObject.get("model").getAsString();

}else{

throw new Exception("Not able to create Model :: " + apiResponseCreateModel.get(IGroovyConstant.DATA));

}

}

}else{

throw new Exception("Not able to Get Model :: " + apiResponseGetModel.get(IGroovyConstant.DATA));

}

/////// GET LATEST REVISIOIN AND CHECK TAG NUMBER FOR UPLOAD FILE

boolean uploadflag=true;

String revisionno = docVo.getRevision() + "_" + docVo.getVer();

Map<String, String> apiResponseGetLatestResvisionForModel = getLatestResvisionForModel(modelId,apiUrl,teamSpace,apikey);

if(apiResponseGetLatestResvisionForModel.get(IGroovyConstant.STATUS).equals("200")){

JsonArray jsonArray= new Gson().fromJson(apiResponseGetLatestResvisionForModel.get(IGroovyConstant.DATA), JsonArray.class);

if(jsonArray !=null && jsonArray.size() > 0) {

JsonObject latestrevisiondetail = (JsonObject)jsonArray.get(0); //for latest revision

if(revisionno.equals(latestrevisiondetail.get("tag").getAsString())){

uploadflag=false;

}

}

}else{

throw new Exception("Not able to Get Latest Model :: " + apiResponseGetLatestResvisionForModel.get(IGroovyConstant.DATA));

}

///////UPLOAD FILE AND UPDATE STATUS IN CUSTOM ATTRIBUTES.

if(uploadflag){

Map<String, String> apiResponseUploadFile = uploadFile(docVo,modelId,apiUrl,teamSpace,apikey);

if(apiResponseUploadFile.get(IGroovyConstant.STATUS).equals("200")){

ArrayList<DocumentVO> documentList= new ArrayList<DocumentVO>();

Map<String, List<String>> customAttributeMap = new HashMap<String, List<String>>();

List<String> textBoxValue = new ArrayList<String>();

textBoxValue.add(documentViewUrl + "/" + teamSpace + "/" +ThreeDRepoProjectId + "/"+ modelId + "/" + revisionno);

customAttributeMap.put(customAttributeNameForUrl, textBoxValue);

docVo.setCustomAttribute(customAttributeMap);

documentList.add(docVo);

documentService.updateDocuments(documentList);

}else{

throw new Exception("Not able to upload file :: " + apiResponseUploadFile.get(IGroovyConstant.DATA));

}

}

}else{

System.out.println("File type is not IFC, RVT, DGN, FBX, OBJ, SPM");

}

}

// GET MODEL DETAILS

//*************

public Map<String, String> getModel(DocumentVO docVo, String hostUrl, String teamSpace, String key) {

/********** Configurable Param Section ****************/

String projectName = replaceData(docVo.getProjectName());

String modelName = replaceData(docVo.getDocRef() );

String apiURL = hostUrl + "/" + teamSpace + "/projects/" + projectName + "/models?name="+ modelName + "&key=" + key;

String apiRequestType = "GET";

Map<String, String> headerParameters = new HashMap<>();

headerParameters.put("Content-Type", "application/json");

String body = "";

/********** Configurable Param Section ****************/

Map<String, String> apiResponse =executeMethod(apiURL,apiRequestType,headerParameters,body);

return apiResponse;

}

// CREATE MODEL

//*************

public Map<String, String> CreateModel(DocumentVO docVo, String hostUrl,String teamSpace, String key,String type,String unit) {

/********** Configurable Param Section ****************/

String apiURL = hostUrl + "/" + teamSpace + "/model?key=" + key;

System.out.println("apiURL::-------------------------->"+apiURL);

String apiRequestType = "POST";

// boolean isSync = true;

Map<String, String> headerParameters = new HashMap<>();

headerParameters.put("Content-Type", "application/json");

String body = "{\"code\": \"\" , \"desc\": \"\" ,\"modelName\": \""+docVo.getDocRef()+"\", \"project\": \""+docVo.getProjectName()+"\" , \"type\": \""+type+"\" , \"unit\": \""+unit+"\" }";

System.out.println("body::-------------------------->"+body);

/********** Configurable Param Section ****************/

Map<String, String> apiResponse =executeMethod(apiURL,apiRequestType,headerParameters,body);

return apiResponse;

}

// GET LATEST REVISION DETAIL OF MODEL

//*************

public Map<String, String> getLatestResvisionForModel(String modelId, String hostUrl, String teamSpace,String key) {

/********** Configurable Param Section ****************/

String apiURL = hostUrl + "/" + teamSpace + "/" + modelId + "/revisions.json?key=" + key;

String apiRequestType = "GET";

Map<String, String> headerParameters = new HashMap<>();

headerParameters.put("Content-Type", "application/json");

String body = "";

/********** Configurable Param Section ****************/

Map<String, String> apiResponse =executeMethod(apiURL,apiRequestType,headerParameters,body);

return apiResponse;

}

//UPLOAD FILE

//*************

public Map<String, String> uploadFile(DocumentVO docVo, String modelId, String hostUrl, String teamSpace,String key) {

/********** Configurable Param Section ****************/

String apiURL = hostUrl + "/" + teamSpace + "/" + modelId + "/upload?key=" + key;

String apiRequestType = "POST";

boolean sendCurrentFile = true;

String uploadFileParamName = "file";

Map<String, String> formParamMap = new HashMap<>();

formParamMap.put("tag", (docVo.getRevision() + "_" + docVo.getVer()));

formParamMap.put("desc", docVo.getDocTitle());

/********** Configurable Param Section ****************/

HashMap<Object, Object> apiCallParameters = new HashMap<>();

apiCallParameters.put(IGroovyConstant.API_URL, apiURL);

apiCallParameters.put(IGroovyConstant.API_REQUEST_TYPE, apiRequestType);

apiCallParameters.put(IGroovyConstant.UPLOAD_FILE_PARAM_NAME, uploadFileParamName);

apiCallParameters.put(IGroovyConstant.FORM_PARAM_MAP, formParamMap);

apiCallParameters.put(IGroovyConstant.SEND_CURRENT_FILE, sendCurrentFile);

Map<String, String> apiResponse = documentService.callExternalAPI(apiCallParameters);

return apiResponse;

}

// GET PROJECT

//*************

public Map<String, String> getProject(String projectName, String hostUrl, String teamSpace, String key) {

/********** Configurable Param Section ****************/

projectName = replaceData(projectName);

String apiURL = hostUrl + "/" + teamSpace + "/projects/" + projectName + "?key=" + key;

String apiRequestType = "GET";

Map<String, String> headerParameters = new HashMap<>();

headerParameters.put("Content-Type", "application/json");

String body = "";

/********** Configurable Param Section ****************/

Map<String, String> apiResponse =executeMethod(apiURL,apiRequestType,headerParameters,body);

return apiResponse;

}

// CREATE PROJECT

//*************

public Map<String, String> createProject(String projectName, String hostUrl, String teamSpace, String key) {

/********** Configurable Param Section ****************/

String apiURL = hostUrl + "/" + teamSpace + "/projects?key=" + key;

String apiRequestType = "POST";

Map<String, String> headerParameters = new HashMap<>();

headerParameters.put("Content-Type", "application/json");

String body = "{\"name\": \"" + projectName + "\"}";

/********** Configurable Param Section ****************/

Map<String, String> apiResponse =executeMethod(apiURL,apiRequestType,headerParameters,body);

return apiResponse;

}

public Map<String, String> executeMethod(String apiURL, String apiRequestType, Map<String, String> headerParameters , String body) {

HashMap<Object, Object> apiCallParameters = new HashMap<>();

apiCallParameters.put(IGroovyConstant.API_URL, apiURL);

apiCallParameters.put(IGroovyConstant.API_REQUEST_TYPE, apiRequestType);

apiCallParameters.put(IGroovyConstant.HEADER_PARAM_MAP, headerParameters);

apiCallParameters.put(IGroovyConstant.BODY, body);

Map<String, String> apiResponse = documentService.callExternalAPI(apiCallParameters);

return apiResponse;

}

//Replace method

public String replaceData(String str) {

return str.replaceAll(" ", "%20");

}

Teamspace

Replace {Teamspace} with the name of the Teamspace where the 3D federated model needs to be pushed to in 3D Repo, e.g. Sandbox. In 3D Repo, this is available in the Teamspaces listing view.

API Key

Replace {API Key} with the unique identifier assigned to the 3D Repo user’s account. In 3D Repo, this is available under “Profile” in the Teamspaces listing view.

Permission to access Asite’s platform via an API must be requested ahead of the implementation. This can be done by sending a request to support@asite.com with the user’s email address (email address must be registered to Asite). Similarly, to request permission to access 3D Repo’s platform via an API, please get in touch directly with 3D Repo.



Did this answer your question?