Files
cadSet/cad-agent-studio/src/lib/cad-edit-tools.ts
T
2026-07-27 17:24:04 +08:00

219 lines
7.0 KiB
TypeScript

export type AiSelectionMode = "none" | "point" | "lasso";
export type CadEditTool = {
id: string;
label: string;
pickKinds: Array<"surface_point" | "edge">;
parameters: Record<string, string | number>;
parameterFields: Array<{
name: string;
label: string;
type: "number" | "select" | "string";
unit?: string;
min?: number;
step?: number;
options?: string[];
placeholder?: string;
}>;
};
export const CAD_EDIT_TOOLS: CadEditTool[] = [
{
id: "add_hole",
label: "打孔",
pickKinds: ["surface_point"],
parameters: { holeDiameter: 2, depth: "through", direction: "face_normal" },
parameterFields: [
{ name: "holeDiameter", label: "孔径", type: "number", unit: "mm", min: 0.01, step: 0.1 },
{ name: "depth", label: "深度", type: "select", options: ["through", "blind"] },
],
},
{
id: "add_counterbore",
label: "沉孔",
pickKinds: ["surface_point"],
parameters: {
holeDiameter: 3,
counterboreDiameter: 6,
counterboreDepth: 2,
depth: "through",
direction: "face_normal",
},
parameterFields: [
{ name: "holeDiameter", label: "底孔直径", type: "number", unit: "mm", min: 0.01, step: 0.1 },
{ name: "counterboreDiameter", label: "沉孔直径", type: "number", unit: "mm", min: 0.01, step: 0.1 },
{ name: "counterboreDepth", label: "沉孔深度", type: "number", unit: "mm", min: 0.01, step: 0.1 },
{ name: "depth", label: "深度", type: "select", options: ["through", "blind"] },
],
},
{
id: "add_countersink",
label: "锥沉孔",
pickKinds: ["surface_point"],
parameters: {
holeDiameter: 3,
countersinkDiameter: 6,
countersinkAngleDeg: 90,
depth: "through",
direction: "face_normal",
},
parameterFields: [
{ name: "holeDiameter", label: "底孔直径", type: "number", unit: "mm", min: 0.01, step: 0.1 },
{ name: "countersinkDiameter", label: "锥口直径", type: "number", unit: "mm", min: 0.01, step: 0.1 },
{ name: "countersinkAngleDeg", label: "锥角", type: "number", unit: "deg", min: 1, step: 1 },
{ name: "depth", label: "深度", type: "select", options: ["through", "blind"] },
],
},
{
id: "add_slot",
label: "开槽",
pickKinds: ["surface_point", "surface_point"],
parameters: { slotWidth: 2, depth: "through", direction: "face_normal" },
parameterFields: [
{ name: "slotWidth", label: "槽宽", type: "number", unit: "mm", min: 0.01, step: 0.1 },
{ name: "depth", label: "深度", type: "select", options: ["through", "blind"] },
],
},
{
id: "add_pocket",
label: "铣型腔",
pickKinds: ["surface_point"],
parameters: { shape: "rectangle", width: 10, height: 6, depth: 1, direction: "face_normal", orientation: "surface_xy" },
parameterFields: [
{ name: "width", label: "宽度", type: "number", unit: "mm", min: 0.01, step: 0.1 },
{ name: "height", label: "高度", type: "number", unit: "mm", min: 0.01, step: 0.1 },
{ name: "depth", label: "深度", type: "number", unit: "mm", min: 0.01, step: 0.1 },
],
},
{
id: "add_circular_pocket",
label: "圆形型腔",
pickKinds: ["surface_point"],
parameters: { diameter: 8, depth: 1, direction: "face_normal" },
parameterFields: [
{ name: "diameter", label: "直径", type: "number", unit: "mm", min: 0.01, step: 0.1 },
{ name: "depth", label: "深度", type: "number", unit: "mm", min: 0.01, step: 0.1 },
],
},
{
id: "add_hole_pattern",
label: "阵列孔",
pickKinds: ["surface_point"],
parameters: {
holeDiameter: 2,
rows: 2,
columns: 2,
pitchX: 8,
pitchY: 8,
depth: "through",
direction: "face_normal",
orientation: "surface_xy",
},
parameterFields: [
{ name: "holeDiameter", label: "孔径", type: "number", unit: "mm", min: 0.01, step: 0.1 },
{ name: "rows", label: "行数", type: "number", min: 1, step: 1 },
{ name: "columns", label: "列数", type: "number", min: 1, step: 1 },
{ name: "pitchX", label: "X 间距", type: "number", unit: "mm", min: 0.01, step: 0.1 },
{ name: "pitchY", label: "Y 间距", type: "number", unit: "mm", min: 0.01, step: 0.1 },
{ name: "depth", label: "深度", type: "select", options: ["through", "blind"] },
],
},
{
id: "add_chamfer",
label: "倒角",
pickKinds: ["edge"],
parameters: { distance: 1 },
parameterFields: [
{ name: "distance", label: "倒角距离", type: "number", unit: "mm", min: 0.01, step: 0.1 },
],
},
{
id: "add_fillet",
label: "圆角",
pickKinds: ["edge"],
parameters: { radius: 1 },
parameterFields: [
{ name: "radius", label: "圆角半径", type: "number", unit: "mm", min: 0.01, step: 0.1 },
],
},
];
export function cadEditToolForOperation(operation: string) {
return CAD_EDIT_TOOLS.find((tool) => tool.id === operation) || CAD_EDIT_TOOLS[0];
}
export function cadEditToolPickKind(operation: string) {
return cadEditToolForOperation(operation).pickKinds[0] || "surface_point";
}
export function cadEditToolNextPickKind(operation: string, pickCount: number) {
const pickKinds = cadEditToolForOperation(operation).pickKinds;
return pickKinds[Math.min(Math.max(Math.floor(pickCount), 0), pickKinds.length - 1)] || "surface_point";
}
export function cadEditToolPickComplete(operation: string, picks: unknown[]) {
return picks.length >= cadEditToolForOperation(operation).pickKinds.length;
}
export function defaultCadEditParameters(operation: string) {
return { ...cadEditToolForOperation(operation).parameters };
}
export function buildEmbeddedCadEditIntent({
operation,
pick,
parameters,
artifactPath,
taskId,
}: {
operation: string;
pick: Record<string, unknown>;
parameters?: Record<string, unknown>;
artifactPath: string;
taskId: string;
}) {
const tool = cadEditToolForOperation(operation);
return {
schema: "cad-edit-intent.v1",
operation: tool.id,
source: {
entryFile: artifactPath,
stepPath: artifactPath,
sourcePath: "",
backend: "agent-studio",
units: "mm",
taskId,
},
selection: pick,
parameters: {
...defaultCadEditParameters(tool.id),
...(parameters || {}),
},
};
}
export function buildEmbeddedGeometrySelectionIntent({
selection,
artifactPath,
taskId,
}: {
selection: Record<string, unknown>;
artifactPath: string;
taskId: string;
}) {
return {
schema: "cad-ai-geometry-selection.v1",
intent: "modify_selected_geometry",
source: {
entryFile: artifactPath,
stepPath: artifactPath,
sourcePath: "",
backend: "agent-studio",
units: "mm",
taskId,
},
selection,
instruction: "用户通过 CAD Agent Studio 内置预览指定了要修改的几何区域。请结合 selection 中的屏幕坐标、近似模型坐标和当前视角理解位置,再修改可编辑源文件并重新生成 STEP 与预览资产。",
};
}