mdf prototype / extrude exampl

This commit is contained in:
Val Erastov 2021-08-09 01:18:58 -07:00
parent 4883b44895
commit f0e9543fdc
5 changed files with 164 additions and 3 deletions

View file

@ -129,9 +129,10 @@ export interface OperationDescriptor<R> {
actionParams?: any;
run: (request: R, opContext: CoreContext) => OperationResult | Promise<OperationResult>;
paramsInfo: (params: R) => string,
previewGeomProvider: (params: R) => OperationGeometryProvider,
previewGeomProvider?: (params: R) => OperationGeometryProvider,
form: () => React.ReactNode,
schema: any
schema: any,
onParamsUpdate?: (params, name, value) => void
}
export interface OperationService {

View file

@ -0,0 +1,31 @@
import React from 'react';
import Entity from '../craft/wizard/components/form/Entity';
import { CheckboxField, NumberField } from '../craft/wizard/components/form/Fields';
import { Group } from '../craft/wizard/components/form/Form';
import { OperationSchema, SchemaField } from './mdf';
export function generateForm(schema: OperationSchema) {
return function MDForm() {
return <Group>
{Object.keys(schema).map(key => {
const fieldDef: SchemaField = schema[key];
const label = fieldDef.label || key;
if (fieldDef.type === 'number') {
return <NumberField name={key} defaultValue={fieldDef.defaultValue} label={label} />
} else if (['face', 'edge', 'sketchObject', 'datumAxis'].includes(fieldDef.type)) {
return <Entity name={key} label={label} />;
} else if (fieldDef.type === 'boolean') {
return <CheckboxField name={key} label={label} />;
} else {
return "I don't know";
}
})}
</Group>;
};
}

61
web/app/cad/mdf/mdf.ts Normal file
View file

@ -0,0 +1,61 @@
import { CoreContext } from "context";
import { IconType } from "react-icons";
import { OperationResult } from "../craft/craftPlugin";
import { OperationDescriptor } from "../craft/operationPlugin";
import { generateForm } from "./generateForm";
interface MDFCommand<R> {
id: string;
label: string;
info: string;
icon: IconType | string;
run: (request: R, opContext: CoreContext) => OperationResult | Promise<OperationResult>;
paramsInfo: (params: R) => string,
schema: OperationSchema,
mutualExclusiveFields?: string[]
}
export type Coercable = any;
export type OperationSchema = {
[key: string]: SchemaField
};
export interface SchemaField {
type: 'number' | 'face' | 'datumAxis' | 'edge' | 'sketchObject' | 'boolean'
defaultValue: Coercable,
optional: boolean,
label?: string
}
export function loadMDFCommand<R>(mdfCommand: MDFCommand<R>): OperationDescriptor<R> {
return {
id: mdfCommand.id,
label: mdfCommand.label,
icon: mdfCommand.icon,
info: mdfCommand.info,
paramsInfo: mdfCommand.paramsInfo,
onParamsUpdate: (params, name, value) => {
if (mdfCommand.mutualExclusiveFields) {
handleMutualExclusiveFields(mdfCommand.mutualExclusiveFields, params, name, value);
}
},
run: mdfCommand.run,
// actionParams: {
// ...requiresFaceSelection(1)
// },
form: generateForm(mdfCommand.schema),
schema: mdfCommand.schema
}
}
export function handleMutualExclusiveFields(mutualExclusiveFields, params, name, value) {
if (mutualExclusiveFields.includes(name)) {
mutualExclusiveFields.forEach(param => {
if (param !== name) {
delete params[param];
}
})
}
}

View file

@ -0,0 +1,65 @@
import { roundValueForPresentation as r } from '../craft/operationHelper';
export const MDF_EXTRUDE_EXAMPLE = {
id: 'EXTRUDE',
label: 'Extrude',
icon: 'img/cad/extrude',
info: 'extrudes 2D sketch',
paramsInfo: ({ value }) => `(${r(value)})`,
mutualExclusiveFields: ['datumAxisVector', 'edgeVector', 'sketchSegmentVector'],
run: (params, ctx) => {
return ctx.craftEngine.cutExtrude(false, params);
},
schema: {
value: {
type: 'number',
defaultValue: 50,
label: 'height'
},
wierdField: {
type: 'number',
defaultValue: 50,
label: 'weird field'
},
prism: {
type: 'number',
min: 0,
defaultValue: 1
},
angle: {
type: 'number',
defaultValue: 0
},
rotation: {
type: 'number',
defaultValue: 0
},
face: {
type: 'face',
initializeBySelection: 0
},
datumAxisVector: {
type: 'datumAxis',
optional: true,
label: 'datum axis'
},
edgeVector: {
type: 'edge',
optional: true,
label: 'edge',
accept: edge => edge.brepEdge.curve.degree === 1
},
sketchSegmentVector: {
type: 'sketchObject',
optional: true,
label: 'sketch segment',
accept: obj => obj.isSegment
},
flip: {
type: 'boolean',
defaultValue: false,
}
}
}

View file

@ -15,13 +15,16 @@ import coneOperation from '../craft/primitives/cone/coneOperation';
import spatialCurveOperation from '../craft/spatialCurve/spatialCurveOperation';
import loftOperation from '../craft/loft/loftOperation';
import {intersectionOperation, subtractOperation, unionOperation} from '../craft/boolean/booleanOperation';
import { loadMDFCommand } from '../mdf/mdf';
import { MDF_EXTRUDE_EXAMPLE } from '../mdf/mdfExtrudeExample';
export function activate({services}) {
services.operation.registerOperations([
planeOperation,
boxOperation,
extrudeOperation,
// extrudeOperation,
loadMDFCommand(MDF_EXTRUDE_EXAMPLE),
cutOperation,
revolveOperation,
filletOperation,