awv3
Version:
⚡ AWV3 embedded CAD
119 lines (105 loc) • 4.37 kB
JavaScript
// lowlevel commands are those that map 1-to-1 to ClassCad constructions
import * as THREE from 'three';
import Ccfuturef from '../ccfuturef';
import Ccref from '../ccref';
import * as ast from './ast';
export var GlobalObjId = //TODO: workaround until ccref can be created only for sketcher objects.
function GlobalObjId(val) {
this.id = Number(val);
if (isNaN(this.id)) {
var e = new TypeError('Value should be able to convert into Number');
e.value = val;
this.id = undefined;
throw e;
}
};
function toAst(x) {
if (x === null) return new ast.Pass();
switch (typeof x) {
case 'undefined':
case 'boolean':
return new ast.NameConstant(x);
case 'number':
return new ast.Num(x);
case 'string':
return new ast.Str(x);
}
if (x instanceof Array) return new ast.List(x.map(toAst));else if (x instanceof THREE.Vector3) return new ast.Vector(x.toArray().map(toAst));else if (x instanceof Ccref || x instanceof GlobalObjId) return RealToId(x.id);else if (x instanceof Ccfuturef) return x.command;else if (x instanceof ast.Ast) return x;
var e = new TypeError('Unsupported value to convert to ClassCad type');
e.value = x;
throw e;
}
export function Assign(target, value) {
return new ast.Assign(toAst(target), toAst(value));
}
export function Call(fn) {
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
return new ast.Call(toAst(fn), args.map(toAst));
}
export function Member(value, attr) {
return new ast.Attribute(toAst(value), attr);
}
export function Return(value) {
return new ast.Return(toAst(value));
} // return a Module AST node that can be directly executed
// commands can be a mix of literal values, expression and statement AST nodes
export function Sequence() {
for (var _len2 = arguments.length, commands = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
commands[_key2] = arguments[_key2];
}
if (commands[0] instanceof ast.Module) return commands[0];
return new ast.Module(commands.map(toAst).map(function (command, i) {
if (command instanceof ast.Stmt) return command;else if (i === commands.length - 1) return new ast.Return(command);else return new ast.Expression(command);
}));
}
var variableCounter = 0;
export function Variable(basename) {
if (basename === void 0) {
basename = 't';
}
return new ast.NameVariable(basename + String(variableCounter++));
}
function cc(name) {
return function () {
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
return new ast.Call(new ast.Name(name), args.map(toAst));
};
}
var interop = '_C.SketcherCloudInterop.';
var obj = '_O.OBJ_';
var cadh = 'CADH_';
export var AddPoint = cc(interop + 'AddPoint'),
AddLine = cc(interop + 'AddLine'),
AddArc = cc(interop + 'AddArc'),
AddCircle = cc(interop + 'AddCircle'),
AddConstraint = cc(interop + 'AddConstraint'),
RemovePoint = cc(interop + 'RemovePoint'),
RemoveLine = cc(interop + 'RemoveLine'),
RemoveArc = cc(interop + 'RemoveArc'),
RemoveCircle = cc(interop + 'RemoveCircle'),
RemoveConstraint = cc(interop + 'RemoveConstraint'),
UpdatePoint = cc(interop + 'UpdatePoint'),
UpdateLine = cc(interop + 'UpdateLine'),
UpdateArc = cc(interop + 'UpdateArc'),
UpdateCircle = cc(interop + 'UpdateCircle'),
UpdateDimensions = cc(interop + 'UpdateDimensions'),
SketchAppendNewObj = cc(interop + 'SketchAppendNewObj'),
ReplaceEntityInConstraints = cc(interop + 'ReplaceEntityInConstraints'),
CopyObjects = cc(interop + 'CopyObjects'),
GetOrigin = cc(interop + 'GetOrigin'),
MoveObjects = cc(interop + 'MoveObjects'),
SolveConstraints = cc(interop + 'SolveConstraints'),
SplitAllCurves = cc(interop + 'SplitAllCurves'),
SplitCurvesMergeBack = cc(interop + 'SplitCurvesMergeBack'),
CreateNewExtrusion = cc(interop + 'CreateNewExtrusion'),
CreateNewRevolve = cc(interop + 'CreateNewRevolve'),
UpdateSketchRegion = cc(interop + 'UpdateSketchRegion'),
UpdateWorkAxis = cc(interop + 'UpdateWorkAxis'),
GetLoop = cc(interop + 'GetLoop'),
IdToReal = cc(cadh + 'IdToReal'),
RealToId = cc(cadh + 'RealToId'),
Recalc = cc(obj + 'Recalc');