Fully functional FreeCAD importer tool

This commit is contained in:
Mike Molinari 2022-07-10 09:40:28 +00:00 committed by Val Erastov
parent d66214f21a
commit 214bffb4d5

View file

@ -22,20 +22,19 @@ export const ImportModelOpperation: OperationDescriptor<ImportModelParams> = {
info: 'Imports BREP, STEP, IGES or FCStd file',
paramsInfo: ({ }) => `()`,
run: async (params: ImportModelParams, ctx: ApplicationContext) => {
console.log(params);
//console.log(params);
let occ = ctx.occService;
const oci = occ.commandInterface;
let returnObject = { created: [], consumed: [] };
console.log(params.file.content);
const FileName = params.file.fileName.toUpperCase()
const FileName = params.file.fileName.toUpperCase();
let fileToRead = await atob(await params.file.content.slice(await params.file.content.indexOf(',') + 1));
//console.log(params.file.content);
//console.log(fileToRead);
if (FileName.endsWith("BRP") || FileName.endsWith("BREP")) {
let fileToRead = atob(params.file.content.slice(params.file.content.indexOf(',') + 1));
console.log(fileToRead);
//FreeCAD some times omits this text from the top of BRP files
//as part of the brp files stored in the .FCStf file archive format
if (!fileToRead.startsWith("DBRep_DrawableShape")) {
@ -46,23 +45,43 @@ export const ImportModelOpperation: OperationDescriptor<ImportModelParams> = {
oci.readbrep("newBREPobject", "newBREPobject");
returnObject.created.push(occ.io.getShell("newBREPobject"));
} else if (FileName.endsWith("FCSTD")) {
const archiveData = params.file.content.slice(params.file.content.indexOf(',') + 1);
var JSZip = require("jszip");
const zipContents = await (await JSZip.loadAsync(archiveData, { base64: true })).files;
var xmlFreeCADData = await zipContents["GuiDocument.xml"].async("string");
const zipContents = await (await JSZip.loadAsync(btoa(fileToRead), { base64: true })).files;
var xmlFreeCADData = await zipContents["Document.xml"].async("string");
let DecodedXmlFreeCADData = await parseStringPromise(xmlFreeCADData);
DecodedXmlFreeCADData = (JSON.parse(JSON.stringify(DecodedXmlFreeCADData)));
console.log(DecodedXmlFreeCADData);
let DecodedXmlFreeCADData = (JSON.parse(JSON.stringify(await parseStringPromise(xmlFreeCADData)))).Document.ObjectData[0].Object;
//console.log(DecodedXmlFreeCADData);
for (const property in zipContents) {
if (property.endsWith("brp")){
FS.writeFile(property, `DBRep_DrawableShape\n` + await zipContents[property].async("string"));
oci.readbrep(property, property);
returnObject.created.push(occ.io.getShell(property));
for (const itemToLookAt in DecodedXmlFreeCADData) {
const flattenedObject = flattenJSON(DecodedXmlFreeCADData[itemToLookAt]);
let importBrepFlag = false;
let importBrepShapeName = "";
let visiblePropertyName = "";
for (const propertyToLookAt in flattenedObject) {
console.log(propertyToLookAt + " = " + flattenedObject[propertyToLookAt]);
if (propertyToLookAt.includes("Part.0.$.file")) importBrepShapeName = flattenedObject[propertyToLookAt];
if (propertyToLookAt.includes("$.name") && flattenedObject[propertyToLookAt] == "Visibility") {
let propToCheck = propertyToLookAt.replace(".$.name", ".Bool.0.$.value");
if (flattenedObject[propToCheck] == "true") importBrepFlag = true;
}
}
if (importBrepFlag == true) {
FS.writeFile(importBrepShapeName, `DBRep_DrawableShape\n` + await zipContents[importBrepShapeName].async("string"));
oci.readbrep(importBrepShapeName, importBrepShapeName);
returnObject.created.push(occ.io.getShell(importBrepShapeName));
}
}
// for (const property in zipContents) {
// if (property.endsWith("brp")) {
// FS.writeFile(property, `DBRep_DrawableShape\n` + await zipContents[property].async("string"));
// oci.readbrep(property, property);
// returnObject.created.push(occ.io.getShell(property));
// }
// }
//console.log(zipContents);
} else if (FileName.endsWith("STEP") || FileName.endsWith("STP")) {
@ -101,3 +120,13 @@ export const ImportModelOpperation: OperationDescriptor<ImportModelParams> = {
],
}
const flattenJSON = (obj = {}, res = {}, extraKey = '') => {
for (key in obj) {
if (typeof obj[key] !== 'object') {
res[extraKey + key] = obj[key];
} else {
flattenJSON(obj[key], res, `${extraKey}${key}.`);
};
};
return res;
};