Created
May 23, 2024 12:15
-
-
Save chuongmep/da53ddb53953a19fc6945b97b04ecc1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ModelData2 { | |
constructor(viewer) { | |
this._modelData = {}; | |
this._viewer = viewer; | |
} | |
init(callback) { | |
let _this = this; | |
_this.getAllLeafComponents(function (dbIds) { | |
let count = dbIds.length; | |
dbIds.forEach(function (dbId) { | |
NOP_VIEWER.getProperties(dbId, function (props) { | |
props.properties.forEach(function (prop) { | |
if (!isNaN(prop.displayValue)) return; // let's not categorize properties that store numbers | |
// some adjustments for revit: | |
prop.displayValue = prop.displayValue.replace('Revit ', ''); // remove this Revit prefix | |
if (prop.displayValue.indexOf('<') == 0) return; // skip categories that start with < | |
// ok, now let's organize the data into this hash table | |
if (_this._modelData[prop.displayName] == null) _this._modelData[prop.displayName] = {}; | |
if (_this._modelData[prop.displayName][prop.displayValue] == null) _this._modelData[prop.displayName][prop.displayValue] = []; | |
_this._modelData[prop.displayName][prop.displayValue].push(dbId); | |
}) | |
if ((--count) == 0) callback(); | |
}); | |
}) | |
}) | |
} | |
getAllLeafComponents(callback) { | |
var instanceTree = NOP_VIEWER.model.getData().instanceTree; | |
var allLeafComponents = []; | |
instanceTree.enumNodeChildren(instanceTree.getRootId(), function (dbId) { | |
if (instanceTree.getChildCount(dbId) === 0) { | |
allLeafComponents.push(dbId); | |
} | |
}, true); | |
callback(allLeafComponents); | |
} | |
} | |
async function getProperties(model, dbid) { | |
return new Promise(function(resolve, reject) { | |
model.getProperties(dbid, function (props) { | |
resolve(props); | |
}); | |
}); | |
} | |
async function getAllProperties(idsOnCategory) { | |
return new Promise(function(resolve, reject) { | |
let promises = []; | |
idsOnCategory.forEach(function (dbid) { | |
promises.push(getProperties(NOP_VIEWER.model, dbid)); | |
}); | |
resolve(Promise.all(promises)); | |
}); | |
} | |
function formatRows(rows) { | |
// Implement this function to format rows into CSV format | |
// For simplicity, I'll assume each row is an array of strings | |
return rows.map(row => Object.values(row).join(',')).join('\n'); | |
} | |
function jsonToCsv(jsonData) { | |
// Extract unique display names from all properties | |
const uniqueDisplayNames = Array.from( | |
new Set(jsonData.flatMap(obj => | |
obj.properties.map(prop => prop.displayName) | |
)) | |
); | |
// Construct headers with dbId and unique display names | |
const headers = ['dbId', ...uniqueDisplayNames]; | |
// Format rows | |
const rows = jsonData.map(obj => { | |
const rowData = [obj.dbId]; | |
uniqueDisplayNames.forEach(displayName => { | |
const property = obj.properties.find(prop => prop.displayName === displayName); | |
rowData.push(property ? property.displayValue : ''); | |
}); | |
return rowData.join(','); | |
}); | |
// Join headers and rows with newlines | |
return [headers.join(','), ...rows].join('\n'); | |
} | |
function downloadCsv(csvData, fileName) { | |
const blob = new Blob([csvData], { type: 'text/csv' }); | |
const url = URL.createObjectURL(blob); | |
const downloadLink = document.createElement('a'); | |
downloadLink.href = url; | |
downloadLink.download = fileName; | |
document.body.appendChild(downloadLink); | |
downloadLink.click(); | |
document.body.removeChild(downloadLink); | |
URL.revokeObjectURL(url); | |
} | |
let data = new ModelData2(NOP_VIEWER); | |
data.init(async function () { | |
let hierarchy = data._modelData.Category; | |
let csvData = ''; | |
for (let key in hierarchy) { | |
if (hierarchy.hasOwnProperty(key)) { | |
console.log(key) | |
let idsOnCategory = hierarchy[key]; | |
let rows = await getAllProperties(idsOnCategory); | |
const csvData = jsonToCsv(rows); | |
const fileName = `${key}.csv`; | |
downloadCsv(csvData, fileName); | |
// just test for one category, in case you want to download all categories, you can remove the break; | |
break; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment