@parametricos/bcf-js
Version:
BCF.js is a BIM Collaboration Format (BCF) reader & parser.
222 lines • 7.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChangeToUppercase = exports.ParsePoint = exports.ObjectToArray = exports.FixArraysNodes = exports.XmlToJsonNotation = exports.GetViewpoint = exports.GetVisInfoComponent = exports.attributes = exports.arrayProps = exports.XmlBuilderOptions = exports.XmlParserOptions = void 0;
const fast_xml_parser_1 = require("fast-xml-parser");
exports.XmlParserOptions = {
attributeNamePrefix: "@_",
ignoreAttributes: false,
ignoreNameSpace: true,
allowBooleanAttributes: true,
trimValues: true,
};
exports.XmlBuilderOptions = {
attributeNamePrefix: "@_",
ignoreAttributes: false,
ignoreNameSpace: true,
suppressBooleanAttributes: false,
parseNodeValue: true,
parseAttributeValue: true,
trimValues: true,
suppressEmptyNode: true
};
exports.arrayProps = [
"ReferenceLinks",
"Labels",
"DocumentReferences",
"RelatedTopics",
"Comments",
"Viewpoints",
"Files"
];
exports.attributes = [
"Guid",
"SnippetType",
"IsExternal",
"IfcProject",
"IfcSpatialStructureElement",
"ServerAssignedId",
"TopicType",
"TopicStatus"
];
function GetVisInfoComponent(xmlData) {
return {
ifc_guid: xmlData["@_IfcGuid"]
};
}
exports.GetVisInfoComponent = GetVisInfoComponent;
function GetViewpoint(xmlString) {
const { VisualizationInfo } = new fast_xml_parser_1.XMLParser(exports.XmlParserOptions).parse(xmlString);
const Vis = VisualizationInfo;
//Camera
const orthogonal_camera = Vis["OrthogonalCamera"];
const perspective_camera = Vis["PerspectiveCamera"];
//Extras
//NOTE: Keep this?
const lines = Vis["Lines"];
const clipping_planes = Vis["ClippingPlanes"];
const GetComponents = () => {
if (!Vis["Components"])
return undefined;
const components = Vis["Components"];
const GetViewSetupHints = () => {
if (!components["ViewSetupHints"])
return undefined;
const view_setup_hints = components["ViewSetupHints"];
return {
spaces_visible: view_setup_hints["@_SpacesVisible"],
spaces_boundaries_visible: view_setup_hints["@_SpacesBoundariesVisible"],
openings_visible: view_setup_hints["@_OpeningsVisible"]
};
};
const GetVisibility = () => {
if (!components["Visibility"])
throw new Error("Visibility not found.");
const visibility = components["Visibility"];
return {
default_visibility: visibility["@_DefaultVisibility"],
exceptions: visibility["Exceptions"] &&
visibility["Exceptions"]["Component"] &&
ObjectToArray(visibility["Exceptions"]["Component"])?.map((exception) => {
return GetVisInfoComponent(exception);
})
};
};
const GetSelection = () => {
if (!components["Selection"])
return undefined;
const selection = components["Selection"];
const arr = ObjectToArray(selection["Component"]);
return arr?.map((exception) => {
return GetVisInfoComponent(exception);
});
};
const GetColoring = () => {
if (!components["Coloring"])
return undefined;
const coloring = components["Coloring"];
const colors = coloring["Color"];
if (!colors)
return undefined;
return ObjectToArray(colors).map((color) => ({
color: color["@_Color"],
components: ObjectToArray(color["Component"])
.map((exception) => {
return GetVisInfoComponent(exception);
})
}));
};
return {
view_setup_hints: GetViewSetupHints(),
visibility: GetVisibility(),
selection: GetSelection(),
coloring: GetColoring()
};
};
return {
guid: Vis["@_Guid"],
components: GetComponents(),
orthogonal_camera: orthogonal_camera && {
camera_view_point: ParsePoint(orthogonal_camera["CameraViewPoint"]),
camera_direction: ParsePoint(orthogonal_camera["CameraDirection"]),
camera_up_vector: ParsePoint(orthogonal_camera["CameraUpVector"]),
view_to_world_scale: orthogonal_camera["ViewToWorldScale"]
},
perspective_camera: perspective_camera && {
camera_view_point: ParsePoint(perspective_camera["CameraViewPoint"]),
camera_direction: ParsePoint(perspective_camera["CameraDirection"]),
camera_up_vector: ParsePoint(perspective_camera["CameraUpVector"]),
field_of_view: perspective_camera["FieldOfView"]
}
};
}
exports.GetViewpoint = GetViewpoint;
function XmlToJsonNotation(node) {
if (!node)
return;
const isArray = Array.isArray(node);
let outputNode = isArray ? [] : {};
if (typeof node === 'string')
return node;
for (const child in node) {
let value = node[child];
if (!value)
continue;
let key = child
.replace(/^@_/g, "")
.replace(/((?<=^@_).|^[A-Z])/g, match => match.toLowerCase())
.replaceAll(/[A-Z]/g, match => "_" + match.toLowerCase());
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean')
outputNode[key] = value;
else
outputNode[key] = XmlToJsonNotation(FixArraysNodes(value));
}
return outputNode;
}
exports.XmlToJsonNotation = XmlToJsonNotation;
function FixArraysNodes(value) {
let result = {};
if (typeof value === 'string' || typeof value === 'number' || Array.isArray(value))
return value;
for (const child in value) {
if (!exports.arrayProps.includes(child)) {
result[child] = value[child];
continue;
}
result[child] = [];
const val = Object.values(value[child])[0];
if (Array.isArray(val))
result[child] = val;
else
result[child].push(val);
}
return result;
}
exports.FixArraysNodes = FixArraysNodes;
/**
* Returns an object as an array
* Can also accept array and returns new array if type is unknown
*
* @return data as an array
* @param data
*/
function ObjectToArray(data) {
return Array.isArray(data) ? data : [data];
}
exports.ObjectToArray = ObjectToArray;
function ParsePoint(point) {
return {
x: point.X,
y: point.Y,
z: point.Z
};
}
exports.ParsePoint = ParsePoint;
function ChangeToUppercase(key, options = {}) {
const opt_firstletter_uppercase = options.firstletter_uppercase !== undefined ? options.firstletter_uppercase : true;
let newKey = [];
let chars = key.split('');
for (let i = 0; i < chars.length; i++) {
if (chars[i] === '_') {
newKey.push(chars[i + 1].toUpperCase());
i++;
}
else if (i == 0 && opt_firstletter_uppercase)
newKey.push(chars[i].toUpperCase());
else
newKey.push(chars[i]);
}
let output = newKey.join("");
if (isAttribute(output, options))
output = `@_${output}`;
return output;
}
exports.ChangeToUppercase = ChangeToUppercase;
function isAttribute(output, options) {
const opt_additional_attributes = options.additional_attributes || [];
if (output.startsWith('?xml') || output.startsWith('xs:'))
return false;
if (exports.attributes.includes(output) || opt_additional_attributes.includes(output))
return true;
return false;
}
//# sourceMappingURL=SharedHelpers.js.map