Skip to main content

Groovy Script - Push Models to 3D Repo and Add 3D Repo's Model Viewing URL as a Custom Attribute

Updated over a week ago

Description: Sample groovy script for pushing models to 3D Repo and 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="Asite_internal";

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

String apikey="XXXXXXXXX";

String modelType="Architectural";

String modelUnit="mm";

String customAttributeNameForUrl="uploadUrl";

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(".DGN") || fileType.equalsIgnoreCase(".FBX") || fileType.equalsIgnoreCase(".OBJ") || fileType.equalsIgnoreCase(".SPM")){

///// CREATE PROJECT IN 3D REPO IF NOT EXIST THERE

String projectId= getProjectData(docVo,apiUrl,teamSpace,apikey);

///// CREATE MODEL IN 3D REPO IF NOT EXIST THERE

String modelId = getModelData( projectId, docVo, apiUrl, teamSpace, apikey,modelType,modelUnit);

/////// CHECK MODEL FILE IS EXIST IN 3D REPO

boolean uploadflag=true;

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

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

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

JsonObject jsonObjectLatestResvisionForModel= new Gson().fromJson(apiResponseGetLatestResvisionForModel.get(IGroovyConstant.DATA), JsonObject.class);

JsonArray jsonArray= (JsonArray) jsonObjectLatestResvisionForModel.get("revisions");

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 MODEL FILE IN 3D REPO AND UPDATE VIEW FILE LINK IN CUSTOM ATTRIBUTES.

if(uploadflag){

Map<String, String> apiResponseUploadFile = uploadFile(projectId,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 + "/" + projectId + "/" + 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));

}

}

}

}

// GET MODEL DETAILS

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

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

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

String apiURL = hostUrl + "/" +"teamspaces"+"/"+ teamSpace + "/projects/"+ projectId + "/containers?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(String projectId,DocumentVO docVo, String hostUrl,String teamSpace, String key,String type,String unit) {

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

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

String apiRequestType = "POST";

// boolean isSync = true;

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

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

String body = "{ \"desc\": \""+docVo.getDocTitle()+"\" ,\"name\": \""+docVo.getDocRef()+"\", \"type\": \""+type+"\" , \"unit\": \""+unit+"\" }";

/********** 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 projectId,String modelId, String hostUrl, String teamSpace,String key) {

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

String apiURL = hostUrl + "/" +"teamspaces"+"/"+ teamSpace + "/projects/"+ projectId + "/containers/"+modelId+"/revisions?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(String projectId,DocumentVO docVo, String modelId, String hostUrl, String teamSpace,String key) {

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

String apiURL = hostUrl + "/" +"teamspaces"+"/"+ teamSpace + "/projects/"+ projectId + "/containers/"+modelId+"/revisions?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 + "/" +"teamspaces"+"/"+ teamSpace + "/projects" + "?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 + "/" +"teamspaces"+"/"+ 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");

}

// GET PROJECT DATA IF ALREADY PROJECT CREATED

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

public String getProjectData(DocumentVO docVo, String apiUrl, String teamSpace, String apikey) {

String projectId="";

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

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

boolean foundProject= false;

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

if(jsonObjectGetProject.has("projects")&& ((JsonArray) jsonObjectGetProject.get("projects")).size() > 0) {

JsonArray projectList = (JsonArray) jsonObjectGetProject.get("projects");

for(int i=0;i<projectList.size();i++) {

JsonObject projectData = (JsonObject)projectList.get(i);

if(projectData.get("name").getAsString().equals(docVo.getProjectName())) {

foundProject= true;

projectId=projectData.get("_id").getAsString();

break;

};

}

}

if(!foundProject){

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

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

if(apiResponseCreateProject.get(IGroovyConstant.STATUS).equals("400")){

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

if(jsonObjectCreateProject.get("message").getAsString().equals("Project with the same name already exists")){

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

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

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

if(jsonObject.has("projects")&& ((JsonArray) jsonObject.get("projects")).size() > 0) {

JsonArray projectList = (JsonArray) jsonObject.get("projects");

for(int i=0;i<projectList.size();i++) {

JsonObject projectData = (JsonObject)projectList.get(i);

if(projectData.get("name").getAsString().equals(docVo.getProjectName())) {

foundProject= true;

projectId=projectData.get("_id").getAsString();

break;

};

}

}

}else{

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

}

}else{

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

}

}else{

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

}

}else{

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

projectId=jsonObjectCreateProject.get("_id").getAsString();

}

}

}else{

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

}

return projectId;

}

// CREATE MODEL DATA IF ALREADY MODEL AVAILABLE THEN GET THE MODEL DATA

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

public String CreateModelData(String projectId,DocumentVO docVo, String apiUrl, String teamSpace, String apikey,String modelType,String modelUnit) {

String modelId="";

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

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

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

modelId = jsonObjectCreateProj.get("_id").getAsString();

}else{

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

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

if(jsonObjectCreateProj.get("message").getAsString().equals("Name is already used within the project")){

Map<String, String> apiResponseGetModelList = getModel(projectId,apiUrl,teamSpace,apikey);

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

JsonObject jsonObjecGetModelListretry= new Gson().fromJson(apiResponseGetModelList.get(IGroovyConstant.DATA), JsonObject.class);

JsonArray jsonArrayContainers= (JsonArray) jsonObjecGetModelListretry.get("containers");

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

for(JsonObject jsonObject:jsonArrayContainers){

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

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

break;

}

}

}

}else{

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

}

}else{

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

}

}else{

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

}

}

return modelId;

}

// GET MODEL DATA , IF NOT AVAILABLE THEN CREATE MODEL

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

public String getModelData(String projectId,DocumentVO docVo, String apiUrl, String teamSpace, String apikey,String modelType,String modelUnit) {

String modelId="";

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

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

JsonObject jsonObjecGetModelList= new Gson().fromJson(apiResponseGetModel.get(IGroovyConstant.DATA), JsonObject.class);

JsonArray jsonArray= (JsonArray) jsonObjecGetModelList.get("containers");

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

for(JsonObject jsonObject:jsonArray){

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

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

break;

}

}

if(modelId.equals("")){

modelId = CreateModelData(projectId, docVo, apiUrl, teamSpace, apikey,modelType,modelUnit);

}

}else{

modelId = CreateModelData(projectId, docVo, apiUrl, teamSpace, apikey,modelType,modelUnit);

}

}else{

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

}

return modelId;

}

Teamspace

Replace {Teamspace} with the name of the Teamspace where the 3D 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.

Access to Asite's platform through an API must be requested before implementation. This can be achieved by sending a request to support@asite.com with the user’s email address (the email address must be registered with 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?