@parametricos/bcf-js
Version:
BCF.js is a BIM Collaboration Format (BCF) reader & parser.
123 lines • 3.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const fflate_1 = require("fflate");
const fast_xml_parser_1 = require("fast-xml-parser");
class BcfWriter {
version;
project;
markups = [];
files;
helpers;
constructor(version, helpers) {
this.version = version;
this.helpers = helpers;
this.files = [];
}
addEntry = (file) => {
if (file.path.endsWith(".version"))
return;
this.files.push(file);
};
write = async (project) => {
try {
this.project = project;
createEntries(this, project.markups);
return await exportZip(this.files);
}
catch (e) {
console.log("Error in writing BCF archive. The error below was thrown.");
console.error(e);
}
};
}
exports.default = BcfWriter;
function createEntries(writer, markups) {
if (!writer.project)
return;
writer.addEntry(bcfversion(writer.version));
writer.addEntry(projectbcfp(writer.project.project_id, writer.project.name));
writer.addEntry(extensionssxd(writer));
for (const markup of markups) {
const formattedMarkup = writer.helpers.MarkupToXmlNotation(markup);
writer.markups.push(formattedMarkup);
let xml = new fast_xml_parser_1.XMLBuilder(writer.helpers.XmlBuilderOptions).build(formattedMarkup);
xml = `<?xml version="1.0" encoding="utf-8"?>${xml}`;
if (markup.topic) {
const guid = markup.topic.guid;
const newEntry = {
path: `${guid}/markup.bcf`,
content: xml,
};
writer.addEntry(newEntry);
}
}
}
async function exportZip(files) {
const fileMap = {};
for (const file of files) {
// Convert string content to Uint8Array for fflate
fileMap[file.path] = new TextEncoder().encode(file.content);
}
return new Promise((resolve, reject) => {
(0, fflate_1.zip)(fileMap, (err, data) => {
if (err) {
reject(err);
}
else {
resolve(Buffer.from(data));
}
});
});
}
function bcfversion(version) {
return {
path: "bcf.version",
content: `<?xml version="1.0" encoding="utf-8"?>
<Version xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" VersionId="${version}">
<DetailedVersion>${version}</DetailedVersion>
</Version>`,
};
}
function projectbcfp(projectId, projectName) {
return {
path: "project.bcfp",
content: `<?xml version="1.0" encoding="utf-8"?>
<ProjectExtension xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Project ProjectId="${projectId}">
<Name>${projectName}</Name>
</Project>
<ExtensionSchema>extensions.xsd</ExtensionSchema>
</ProjectExtension>`,
};
}
function extensionssxd(writer) {
const attributes = [
"version",
"encoding",
"standalone",
"xmlns",
"schemaLocation",
"name",
"base",
"value",
];
const options = {
additional_attributes: attributes,
firstletter_uppercase: false,
plural_to_singular: false,
};
let helpers = writer.helpers;
if (writer.version == "3.0") {
const v21 = require("./2.1/index");
const helpersV21 = new v21.BcfWriter();
helpers = helpersV21.helpers;
}
const formattedXml = helpers.RenameJsonKeys(writer.project?.extension_schema, options);
let xml = new fast_xml_parser_1.XMLBuilder(helpers.XmlBuilderOptions).build(formattedXml);
return {
path: "extensions.xsd",
content: xml,
};
}
//# sourceMappingURL=BcfWriter.js.map