importModel
Calling importModel from orchestrator step#
The process of importing the model composite from the Bimpk file requires an orchestrator which runs the importModel script in the iaf_import_model Object Model API script.
User interface one-click orchestrator via ProjectSetup component#
For the user interface one-click project setup, the SetupProject component already runs an orchestrator that includes all steps, including the model import step:
async function runOrchestrator() { let orchestratorConfig = await IafScriptEngine.addDatasource({ _name: "Setup Project", _description: "Run all scripts needed for setup", _namespaces: ctx._namespaces, _userType: "setup_runner", _params: { tasks: [ //tasks before { _sequenceno: 4, _name: "Importing Bimpk", _orchcomp: "default_script_target", _actualparams: { userType: "iaf_import_model", _scriptName: "importModel", // the named script to run in the file above }, }, //tasks after ] } })}VS code extension orchestrator setup#
For the VS code extension setup, the importModelFromBimpk script creates a single-step orchestrator.
The script calls IafScriptEngine.addDatasource to create a new orchestrator that calls the importModel function from the iaf_import_model Object Model API script:
let bimpkDatasourceResult = await IafScriptEngine.addDatasource( { _name: "BIMPK Import", _description: "BIMPK Import", _namespaces: ctx._namespaces, _userType: "bimpk_import", _schemaversion: "2.0", _params: { tasks: [ { name: "default_script_target", _actualparams: { userType: "iaf_import_model", //OMAPI script _scriptName: "importModel", //script function }, _sequenceno: 1, }, ], }, }, ctx);The script runs the orchestrator with IafDataSource.runOrchestrator:
if (bimpkDatasourceResult) { const params = { orchestratorId: bimpkDatasourceResult.id } const orchResult = await PlatformApi.IafDataSource.runOrchestrator( bimpkDatasourceResult.id, params, ctx );
The script uses IafDataSource.getOrchRunStatus to get the status of the orchestrator run:
let orchRunResult = await PlatformApi.IafDataSource.getOrchRunStatus(orchResult.id, ctx); console.log(`orchRunResult`, orchRunResult[0].orchrunsteps)
let orchStepRunStatus = orchRunResult[0].orchrunsteps;It sets an interval of 10000 milliseconds to poll the orchestrator run for a status update until the run finishes:
//poll based on in run id until finished let interval = setInterval(async () => { let errStatus = _.filter(orchStepRunStatus, run_status => { return run_status._status === "ERROR"; }); let queuedStatus = _.filter(orchStepRunStatus, run_status => { return run_status._status === "QUEUED"; }); let runningStatus = _.filter(orchStepRunStatus, run_status => { return run_status._status === "RUNNING"; });
console.log(`errStatus`, errStatus) console.log(`queuedStatus`, queuedStatus) console.log(`runningStatus`, runningStatus)
if (!_.isEmpty(errStatus) || (_.isEmpty(queuedStatus) && _.isEmpty(runningStatus))) { if (_.isEmpty(errStatus)) { orchStepRunStatus.forEach((step) => step.status = 'COMPLETED'); } //kills the polling when import is complete clearInterval(interval); } orchRunResult = await PlatformApi.IafDataSource.getOrchRunStatus(orchResult.id, ctx); orchStepRunStatus = orchRunResult[0].orchrunsteps; }, 10000);
console.log(`orchRunResult`, orchStepRunStatus) } }importModel#
First, the script establishes if the bimpk _fileId and _fileVersionId are passed and if not, it fetches them with the local getBimpkFile function:
async function importModel(params, libraries, ctx) { return new Promise(async (resolve, reject) => { try { //checks if the bimpk `_fileId` and `_fileVersionId` are passed if ( !params?.actualParams?._fileId && !params?.actualParams?._fileVersionId ) { //if there are no _fileId and _fileVersionId values passed, //the getBimpkFile function gets the file object and sets the values const fileObj = await getBimpkFile(params, libraries, ctx); if (fileObj) { params.actualParams._fileId = fileObj.file_id; params.actualParams._fileVersionId = fileObj.fileVersion_id; } }The getBimpkFile searches for a file with "bimpk" in the _name property and then gets the latest version of that file, returning the file id and version id:
const getBimpkFile = async (params, libraries, ctx) => { const { PlatformApi } = libraries; //search criteria used in the request let criteria = { _name: ".*bimpk" }; let fileItem = await PlatformApi.IafFileSvc.getFiles(criteria, ctx); if (!fileItem) return; console.log("fileItem", fileItem); let file_id = fileItem._list[0]._id; //uses the file id of the found bimpk file to get its versions let fileVersion = await PlatformApi.IafFile.getFileVerisons(file_id, ctx); //takes the latest version const fileVersion_id = fileVersion._list[0]._id; console.log("fileVersion", fileVersion); console.log( "Completed bimpkUploadAndImport.", "onSuccess ==== fileVersion.", JSON.stringify(fileVersion) ); return { file_id, fileVersion_id, };};Returning to the importModel function, the script gets the file name and file extension
//Helper gets the file's metadataconst helper = new Helper(params, libraries);const { filename, ext } = await helper.getFileMetaData( params?.actualParams?._fileId);
params.filename = filename;params.ext = ext?.toLowerCase();Next, the script validates that the model's _fileId, _fileVersionId, and file extension:
//validates const validateInput = new InputValidation(params, libraries, ctx);await validateInput.validate();console.timeEnd(`${params.orchRunId}: validation`);
let result = {};If it is a valid bimpk file, the script creates a new BimpkImport class object and initializes the import–otherwise, it creates a new SgpkImport class object and initializes the import:
if (params.ext == "bimpk") { console.time(`${params.orchRunId}: BimpkImport`); const bimpkImport = new BimpkImport(params, libraries, ctx);
result = await bimpkImport.initialize(); console.timeEnd(`${params.orchRunId}: BimpkImport`);} else { const sgpkImport = new SgpkImport(params, libraries, ctx); result = await sgpkImport.initialize();}
params.result = result;console.log(result);Next, the script initializes a Datasource target for Structural Coordination Zones:
console.time(`${params.orchRunId}: SczImport`); const sczImport = new SczTarget(params, libraries, ctx); await sczImport.intialize(); console.timeEnd(`${params.orchRunId}: SczImport`);
const res = { filecolid: result.filecolid, viewcolid: result.viewcolid, compositeitemid: result.compositeitemid, }; if (result.myCollections) { res.myCollections = result.myCollections; } console.log(`Model import is complete, ${params.orchRunId}`);
resolve(res);} catch (error) { reject(error);}