mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-06 08:25:19 +01:00
Fully functional FreeCAD importer tool
This commit is contained in:
parent
d66214f21a
commit
214bffb4d5
1 changed files with 46 additions and 17 deletions
|
|
@ -22,20 +22,19 @@ export const ImportModelOpperation: OperationDescriptor<ImportModelParams> = {
|
||||||
info: 'Imports BREP, STEP, IGES or FCStd file',
|
info: 'Imports BREP, STEP, IGES or FCStd file',
|
||||||
paramsInfo: ({ }) => `()`,
|
paramsInfo: ({ }) => `()`,
|
||||||
run: async (params: ImportModelParams, ctx: ApplicationContext) => {
|
run: async (params: ImportModelParams, ctx: ApplicationContext) => {
|
||||||
console.log(params);
|
//console.log(params);
|
||||||
let occ = ctx.occService;
|
let occ = ctx.occService;
|
||||||
const oci = occ.commandInterface;
|
const oci = occ.commandInterface;
|
||||||
|
|
||||||
let returnObject = { created: [], consumed: [] };
|
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")) {
|
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
|
//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
|
//as part of the brp files stored in the .FCStf file archive format
|
||||||
if (!fileToRead.startsWith("DBRep_DrawableShape")) {
|
if (!fileToRead.startsWith("DBRep_DrawableShape")) {
|
||||||
|
|
@ -46,23 +45,43 @@ export const ImportModelOpperation: OperationDescriptor<ImportModelParams> = {
|
||||||
oci.readbrep("newBREPobject", "newBREPobject");
|
oci.readbrep("newBREPobject", "newBREPobject");
|
||||||
returnObject.created.push(occ.io.getShell("newBREPobject"));
|
returnObject.created.push(occ.io.getShell("newBREPobject"));
|
||||||
} else if (FileName.endsWith("FCSTD")) {
|
} else if (FileName.endsWith("FCSTD")) {
|
||||||
const archiveData = params.file.content.slice(params.file.content.indexOf(',') + 1);
|
|
||||||
var JSZip = require("jszip");
|
var JSZip = require("jszip");
|
||||||
|
|
||||||
const zipContents = await (await JSZip.loadAsync(archiveData, { base64: true })).files;
|
const zipContents = await (await JSZip.loadAsync(btoa(fileToRead), { base64: true })).files;
|
||||||
var xmlFreeCADData = await zipContents["GuiDocument.xml"].async("string");
|
var xmlFreeCADData = await zipContents["Document.xml"].async("string");
|
||||||
|
|
||||||
let DecodedXmlFreeCADData = await parseStringPromise(xmlFreeCADData);
|
let DecodedXmlFreeCADData = (JSON.parse(JSON.stringify(await parseStringPromise(xmlFreeCADData)))).Document.ObjectData[0].Object;
|
||||||
DecodedXmlFreeCADData = (JSON.parse(JSON.stringify(DecodedXmlFreeCADData)));
|
//console.log(DecodedXmlFreeCADData);
|
||||||
console.log(DecodedXmlFreeCADData);
|
|
||||||
|
|
||||||
for (const property in zipContents) {
|
|
||||||
if (property.endsWith("brp")){
|
for (const itemToLookAt in DecodedXmlFreeCADData) {
|
||||||
FS.writeFile(property, `DBRep_DrawableShape\n` + await zipContents[property].async("string"));
|
const flattenedObject = flattenJSON(DecodedXmlFreeCADData[itemToLookAt]);
|
||||||
oci.readbrep(property, property);
|
let importBrepFlag = false;
|
||||||
returnObject.created.push(occ.io.getShell(property));
|
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);
|
//console.log(zipContents);
|
||||||
} else if (FileName.endsWith("STEP") || FileName.endsWith("STP")) {
|
} 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;
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue