fileAttributesImport
The fileAttributesImport function uploads a set of attributions that are used to add properties to file uploads, such as design files.
File attributes file selection and parsing#
The file attributes file selection code differs for a one-click orchestrator setup and a VS code extension setup:
Orchestrator file selection#
The script downloads the scripts directory:
//extracts the zip link for the scripts folder from the passed parametersconst { zipLink } = params.actualParams;//downloads the project setup zip fileconst response = await fetch(zipLink);//uses response.unZippedFilePath for the directory pathconst directoryPath = response.unZippedFilePath;//constructs the path to the scripts folderconst filePath = `${directoryPath}/scripts`;With the scripts folder downloaded, the readFilesRecursively function reads the content of each script in the folder and its subfolders to find the Kingspan_FileAttributes.xlsx file:
let xlsxFile;await readFilesRecursively(filePath);
async function readFilesRecursively(dir) { //uses fs.readdirSync to read the directory's contents const files = await fs.readdirSync(dir); for (const file of files) { const filePath = `${dir}/${file}`; //if the item does not have a file extension, //it is a folder and the readFilesRecursively //function is recursively called to read its files if (!file.includes(".")) { await readFilesRecursively(filePath); // Await the recursive call } else { try { if (fileAttributesFile === file) { const fileData = await fs.readFileSync(filePath); xlsxFile = fileData; } } catch (error) { throw error; } } }}Next, using the DataXlsx library from CoreUtils, the script reads the spreadsheet:
const { DataXlsx } = CoreUtils;const workbook = await DataXlsx.read(xlsxFile);Then converts it to JSON and parses it as data objects:
const wbJSON = await DataXlsx.workbookToJSON(workbook);let iaf_dt_grid_data = wbJSON["Document Attributes"];let iaf_dt_grid_as_objects = await CoreUtils.Util.parseGridData({ gridData: iaf_dt_grid_data,});VS code file selection#
The script uses UiUtils.IafLocalFile.selectFiles to prompt the user to select an xlsx file:
const xlsxFiles = await UiUtils.IafLocalFile.selectFiles({ multiple: false, accept: ".xlsx" })UiUtils.IafDataPlugin.readXLSXFiles reads the spreadsheet file:
const typeWorkbook = await UiUtils.IafDataPlugin.readXLSXFiles(xlsxFiles)Then converts it to JSON with IafDataPlugin.workbookToJSON and parses it as data objects using IafDataPlugin.parseGridData:
const wbJSON = UiUtils.IafDataPlugin.workbookToJSON(typeWorkbook[0])const iaf_dt_grid_data = wbJSON.Sheet1const iaf_dt_grid_as_objects = UiUtils.IafDataPlugin.parseGridData({ gridData: iaf_dt_grid_data })Creating a collection#
The script creates a NamedUserCollection for the file attributes:
let atm_defs_coll = await IafScriptEngine.createOrRecreateCollection( { _name: "Revit Type Map Collection", _shortName: "revit_typemap_defs", _namespaces: ctx._namespaces, _description: "Revit Element Type Map Collection", _userType: "iaf_ref_type_map_defs_coll", }, ctx);The script then adds the parsed data objects as RelatedItems to the file attributes collection and resolves:
let atm_defs_items_res = await IafScriptEngine.createItemsBulk( { _userItemId: atm_defs_coll._userItemId, _namespaces: ctx._namespaces, items: iaf_dt_grid_as_objects, }, ctx);console.log( "fileAttributesImport ==== atm_defs_items_res", atm_defs_items_res);
resolve(true);} catch (error) {reject(error);}