added support to import brp and brep files

This commit is contained in:
Mike Molinari 2022-07-03 02:06:24 +00:00 committed by Val Erastov
parent f72631bcfb
commit 0c4aeb7b05
2 changed files with 11 additions and 92 deletions

View file

@ -7,9 +7,6 @@ import { param } from 'cypress/types/jquery';
import { MObject } from 'cad/model/mobject';
import { LocalFile } from "ui/components/controls/FileControl";
import CadError from "../../../../../web/app/utils/errors";
import { parseStringPromise } from 'xml2js';
import * as jszip from "jszip";
interface ImportModelParams {
file: LocalFile;
@ -18,90 +15,28 @@ interface ImportModelParams {
export const ImportModelOpperation: OperationDescriptor<ImportModelParams> = {
id: 'IMPORT_MODEL',
label: 'Import',
icon: 'img/cad/import',
info: 'Imports BREP, STEP, IGES or FCStd file',
icon: 'img/cad/intersection',
info: 'Imports STEP, BREP 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: [] };
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);
const FileName = params.file.fileName.toUpperCase()
if (FileName.endsWith("BRP") || FileName.endsWith("BREP")) {
//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")) {
fileToRead = `DBRep_DrawableShape\n` + fileToRead;
alert("Importing BREP file");
if (!params.file.content.startsWith("DBRep_DrawableShape")) {
params.file.content = `DBRep_DrawableShape\n` + params.file.content;
}
FS.writeFile("newBREPobject", (fileToRead));
oci.readbrep("newBREPobject", "newBREPobject");
returnObject.created.push(occ.io.getShell("newBREPobject"));
} else if (FileName.endsWith("FCSTD")) {
var JSZip = require("jszip");
const zipContents = await (await JSZip.loadAsync(btoa(fileToRead), { base64: true })).files;
var xmlFreeCADData = await zipContents["Document.xml"].async("string");
let DecodedXmlFreeCADData = (JSON.parse(JSON.stringify(await parseStringPromise(xmlFreeCADData)))).Document.ObjectData[0].Object;
//console.log(DecodedXmlFreeCADData);
for (const itemToLookAt in DecodedXmlFreeCADData) {
const flattenedObject = flattenJSON(DecodedXmlFreeCADData[itemToLookAt]);
let importBrepFlag = true;
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] == "false") importBrepFlag = false;
}
}
if (importBrepFlag == true) {
try {
await FS.writeFile(importBrepShapeName, `DBRep_DrawableShape\n` + await zipContents[importBrepShapeName].async("string"));
await oci.readbrep(importBrepShapeName, importBrepShapeName);
returnObject.created.push(occ.io.getShell(importBrepShapeName));
} catch (e) {
console.log(e)
}
}
}
// 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")) {
//step Import
FS.writeFile("newStepObject", (params.file.content));
oci.stepread("newStepObject", "newStepObject");
await FS.writeFile("newStepObject", await (params.file.content));
oci.readbrep("newStepObject", "newStepObject");
returnObject.created.push(occ.io.getShell("newStepObject"));
} else if (FileName.endsWith("IGES") || FileName.endsWith("IGS")) {
//IGES import
FS.writeFile("newIgesObject", (params.file.content));
oci.igesread("newIgesObject", "newIgesObject");
returnObject.created.push(occ.io.getShell("newIgesObject"));
} else {
throw new CadError({
kind: CadError.KIND.INVALID_INPUT,
@ -124,14 +59,3 @@ 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;
};

View file

@ -19,7 +19,6 @@ import { MirrorBodyOperation} from "./features/mirrorBody/mirrorBody.operation";
import { PatternLinearOperation } from "./features/patternLinear/patternLinear.operation";
import { PatternRadialOperation } from "./features/patternRadial/patternRadial.operation";
import { ImportModelOpperation } from "./features/importModel/importModel.operation";
import { DeleteBodyOperation } from "./features/deleteBody/deleteBody.operation";
//imports of action type commands
import { GetVolume } from './actions/getVolume/getVolume.action';
import {GiCubes} from "react-icons/gi";
@ -47,10 +46,6 @@ export const ModelerWorkspace: WorkbenchConfig = {
PatternLinearOperation,
PatternRadialOperation,
ImportModelOpperation,
DeleteBodyOperation,
],
actions: [
GetVolume,
],
ui: {
toolbar: [
@ -60,7 +55,7 @@ export const ModelerWorkspace: WorkbenchConfig = {
"SHELL_TOOL", "FILLET_TOOL", "SCALE_BODY","-",
"MIRROR_BODY", "PATTERN_LINEAR", "PATTERN_RADIAL", "-",
"CYLINDER", "BOX", "CONE", "SPHERE", "TORUS", "-",
"HOLE_TOOL", "-", 'GET_VOLUME', "IMPORT_MODEL","DELETE_BODY",
"HOLE_TOOL", "-", 'GET_VOLUME', "IMPORT_MODEL"
]
},
icon: GiCubes