Skip to main content
Version: v5.0

createCollections

The createCollections script creates search indexes for the collections and then upload the type map.

createIndex​

const createIndex = async () => {
//gets the composite collection for the project's BIM model
let compositeCollection = await IafScriptEngine.getCompositeCollection(
{
query: {
_userType: "bim_model_version",
_namespaces: {
$in: ctx._namespaces,
},
_itemClass: "NamedCompositeItem",
},
},
ctx,
{ getLatestVersion: true }
);
console.log(
"Started createIndex.",
"Create index ==== getCompositeCollection.",
compositeCollection
);

// gets the rvt_type_elements collection from the composite collection
let _userItemId = compositeCollection._userItemId;
let collectionInComposite = await IafScriptEngine.getCollectionInComposite(
_userItemId,
{ _userType: "rvt_type_elements" },
ctx
);
console.log(
"Create index ==== getCollectionInComposite.",
collectionInComposite
);

// Revit
let indexRes = await IafScriptEngine.createOrRecreateIndex(
{
// model type elememt colletcion
_id: collectionInComposite._userItemId,
indexDefs: [
{
key: { "Element Category": 1 },
options: {
name: "model_els_coll_id",
default_language: "english",
},
},
],
},
ctx
);
console.log(
"Completed createIndex.",
"Create index ==== Create Or Recreate Index Index response.",
indexRes
);
};

addTypeMap​

The type map is used later in the process to map the Revit types to the model elements.

Type map file selection​

The type map 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:

const typeMapFile = "TypeMap.xlsx";
//extracts the zip link for the scripts folder from the passed parameters
const { zipLink } = params.actualParams;
//imports DataXlsx to extract spreadsheet data
const { DataXlsx } = CoreUtils;
//placeholder for processed spreadsheet data
let xlsxFile;

//downloads the project setup zip file
const response = await fetch(zipLink);
//uses response.unZippedFilePath for the directory path
const directoryPath = response.unZippedFilePath;
//constructs the path to the scripts folder
const 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 type map file:

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 the file name equals TypeMap.xlsx
if (typeMapFile === file) {
//read the file synchronously and assign it to the variable xlsxFile
fileData = await fs.readFileSync(filePath);
xlsxFile = fileData;
}
} catch (error) {
null;
}
}
}
}

VS code type map file selection​

The script requests that the user selects a type map spreadsheet file:

async uploadTypeMap(input, libraries, ctx) {
const { PlatformApi, UiUtils, IafScriptEngine } = libraries
const proj = await PlatformApi.IafProj.getCurrent(ctx)

const xlsxFiles = await UiUtils.IafLocalFile.selectFiles({ multiple: false, accept: ".xlsx" })

The script uses IafDataPlugin.readXLSXFiles from UiUtils to read the spreadsheet, then uses IafDataPlugin.workbookToJSON to transform the data to JSON data, which it then parses as objects using IafDataPlugin.parseGridData:

const typeWorkbook = await UiUtils.IafDataPlugin.readXLSXFiles(xlsxFiles)
const wbJSON = UiUtils.IafDataPlugin.workbookToJSON(typeWorkbook[0])
const iaf_dt_grid_data = wbJSON.Sheet1
const iaf_dt_grid_as_objects = UiUtils.IafDataPlugin.parseGridData({ gridData: iaf_dt_grid_data })

Creating the type map collection​

The script creates a NamedUserCollection for the type map objects:

const atm_defs_coll = await IafScriptEngine.createOrRecreateCollection(
{
_name: 'Type Map Def Collection',
_shortName: 'typemap_defs',
_namespaces: proj._namespaces,
_description: 'Revit Element Type Map Collection',
_userType: 'iaf_ref_type_map_defs_coll'
},
ctx
)

Finally, the parsed JSON objects are added to the Item Service and related to the type map collection:

const atm_defs_items_res = await IafScriptEngine.createItemsBulk({
_userItemId: atm_defs_coll._userItemId,
_namespaces: ctx._namespaces,
items: iaf_dt_grid_as_objects
}, ctx)

return atm_defs_items_res
}