@parametricos/bcf-js
Version:
BCF.js is a BIM Collaboration Format (BCF) reader & parser.
146 lines • 5.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Markup = void 0;
const unzipit_1 = require("unzipit");
const fast_xml_parser_1 = require("fast-xml-parser");
class BcfReader {
version;
bcf_archive;
project;
markups = [];
helpers;
constructor(version, helpers) {
this.version = version;
this.helpers = helpers;
}
read = async (src) => {
try {
const markups = [];
this.bcf_archive = await (0, unzipit_1.unzip)(src);
const { entries } = this.bcf_archive;
let projectId = '';
let projectName = '';
let projectVersion = '';
let extension_schema = undefined;
for (const [name, entry] of Object.entries(entries)) {
if (name.endsWith('.bcf')) {
markups.push(entry);
}
else if (name.endsWith('.version')) {
const parsedEntry = new fast_xml_parser_1.XMLParser(this.helpers.XmlParserOptions).parse(await entry.text());
projectVersion = parsedEntry.Version.DetailedVersion;
}
else if (name.endsWith('.bcfp')) {
const parsedEntry = new fast_xml_parser_1.XMLParser(this.helpers.XmlParserOptions).parse(await entry.text());
if (!parsedEntry.ProjectExtension || !parsedEntry.ProjectExtension.Project)
continue; //NOTE: Throw an error here?
projectId = parsedEntry.ProjectExtension.Project["@_ProjectId"] || ''; //NOTE: Throw an error here?
projectName = parsedEntry.ProjectExtension.Project.Name || '';
}
else if (name.endsWith('extensions.xsd')) {
const parsedEntry = new fast_xml_parser_1.XMLParser(this.helpers.XmlParserOptions).parse(await entry.text());
extension_schema = this.helpers.XmlToJsonNotation(parsedEntry);
}
}
const purged_markups = [];
for (let i = 0; i < markups.length; i++) {
const t = markups[i];
const markup = new Markup(this, t);
await markup.read();
this.markups.push(markup);
const purged_markup = { header: markup.header, topic: markup.topic, project: this.project, viewpoints: markup.viewpoints };
purged_markups.push(purged_markup);
}
this.project = {
project_id: projectId,
name: projectName,
version: projectVersion,
markups: undefined,
reader: this,
extension_schema: extension_schema
};
this.project.markups = purged_markups.map(mkp => { return { ...mkp, project: this.project }; });
}
catch (e) {
console.log("Error in loading BCF archive. The error below was thrown.");
console.error(e);
}
};
getEntry = (name) => {
return this.bcf_archive?.entries[name];
};
}
exports.default = BcfReader;
class Markup {
reader;
markup_file;
header;
topic;
viewpoints = [];
constructor(reader, markup) {
this.reader = reader;
this.markup_file = markup;
}
read = async () => {
await this.parseMarkup();
await this.parseViewpoints();
};
parseMarkup = async () => {
const markup = this.reader.helpers.GetMarkup(await this.markup_file.text());
this.topic = markup.topic;
this.header = markup.header;
};
parseViewpoints = async () => {
if (!this.topic)
return;
if (this.topic.viewpoints) {
const topic_viewpoints = this.topic.viewpoints;
for (let i = 0; i < topic_viewpoints.length; i++) {
const entry = topic_viewpoints[i];
const key = this.topic.guid + "/" + entry.viewpoint;
const file = this.reader.getEntry(key);
if (!file)
throw new Error("Missing Visualization Info");
const viewpoint = this.reader.helpers.GetViewpoint(await file.text());
viewpoint.snapshot = entry.snapshot;
viewpoint.getSnapshot = async () => { if (entry.snapshot)
return await this.getSnapshot(entry.snapshot); };
this.viewpoints.push(viewpoint);
}
}
};
/**
* Parses the png snapshot.
*
* @returns {string} The image in base64String format.
*
* @deprecated This function is deprecated and will be removed in the next version.<br>
* Please use viewpoint.getSnapshot() instead.<br>
*
*/
getViewpointSnapshot = async (viewpoint) => {
if (!viewpoint || !this.topic)
return;
const entry = this.reader.getEntry(`${this.topic.guid}/${viewpoint.snapshot}`);
if (entry) {
const arrayBuffer = await entry.arrayBuffer();
return btoa(String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)));
}
};
/**
* Parses the png snapshot.
*
* @returns {string} The image in base64String format.
*/
getSnapshot = async (guid) => {
if (!guid || !this.topic)
return;
const entry = this.reader.getEntry(`${this.topic.guid}/${guid}`);
if (entry) {
const arrayBuffer = await entry.arrayBuffer();
return btoa(String.fromCharCode.apply(null, new Uint8Array(arrayBuffer)));
}
};
}
exports.Markup = Markup;
//# sourceMappingURL=BcfReader.js.map