Command Development
About 1 min
Command Development
Development and registration of custom commands, including command structure, preview drawing, state machines, and other advanced usage.
Online Examples
| Example | Description | Link |
|---|---|---|
| Simple Command | Basic command structure example | Online Demo{target="_blank"} |
| Register Command | CommandRegistry usage | Online Demo{target="_blank"} |
| Command with Preview | Preview drawing example | Online Demo{target="_blank"} |
| State Machine Command | Multi-step command example | Online Demo{target="_blank"} |
| Execute Script | EXECUTESTR execute CAD command script example | Online Demo{target="_blank"} |
| Execute JS Code | EXECUTEJS execute JavaScript code example | Online Demo{target="_blank"} |
Core API
Basic Command Structure
const { CommandDefinition, CommandOptions, CommandRegistry } = vjcad;
// 1. Define command class
class MyCommand {
async main() {
writeMessage("<br/>Command executing...");
// Command logic
}
}
// 2. Create command options
const options = new CommandOptions();
options.useAutoComplete = true;
// 3. Create command definition
const cmdDef = new CommandDefinition(
'MYCMD', // Command name (uppercase)
'Command description', // Description
MyCommand, // Command class
options // Options
);
// 4. Register command
CommandRegistry.regist(cmdDef);
// 5. Execute command
await Engine.editor.executerWithOp('MYCMD');Command with Preview
class DrawCircleCommand {
async main() {
const { getPoint, drawPreviewEntity } = vjcad;
// Get center point
const centerResult = await getPoint({ prompt: "Specify center:" });
if (centerResult.status !== InputStatusEnum.OK) return;
const center = centerResult.value;
// Get radius point (with preview)
const radiusResult = await getPoint({
prompt: "Specify radius:",
basePoint: center,
onMouseMove: (currentPoint) => {
const radius = distance(center, currentPoint);
const previewCircle = new CircleEnt(center, radius);
drawPreviewEntity(previewCircle);
}
});
if (radiusResult.status !== InputStatusEnum.OK) return;
const radius = distance(center, radiusResult.value);
// Create entity
const circle = new CircleEnt(center, radius);
circle.setDefaults();
Engine.addEntities(circle);
}
}State Machine Command
class MultiStepCommand {
state = 'START';
async main() {
while (this.state !== 'END') {
switch (this.state) {
case 'START':
await this.handleStart();
break;
case 'SELECT':
await this.handleSelect();
break;
case 'CONFIRM':
await this.handleConfirm();
break;
}
}
}
async handleStart() {
// Initialization logic
this.state = 'SELECT';
}
// ...
}