mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-15 21:05:22 +01:00
define offset constant for offset constraints
This commit is contained in:
parent
d9e61e273e
commit
b0a086d9dd
2 changed files with 42 additions and 15 deletions
|
|
@ -23,8 +23,8 @@ function ParametricManager(viewer) {
|
|||
this.listeners = [];
|
||||
this.constantTable = {};
|
||||
|
||||
this.viewer.params.define("constantDefinition", null);
|
||||
this.viewer.params.subscribe("constantDefinition", "parametricManager", this.rebuildConstantTable, this)();
|
||||
this.viewer.params.define('constantDefinition', null);
|
||||
this.viewer.params.subscribe('constantDefinition', 'parametricManager', this.onConstantsExternalChange, this)();
|
||||
this.constantResolver = this.createConstantResolver();
|
||||
}
|
||||
|
||||
|
|
@ -61,15 +61,31 @@ ParametricManager.prototype.rebuildConstantTable = function(constantDefinition)
|
|||
try {
|
||||
var value = eval(prefix + "return " + m[2] + "; \n})()");
|
||||
this.constantTable[constant] = value;
|
||||
prefix += constant + " = " + value + ";\n"
|
||||
prefix += "const " + constant + " = " + value + ";\n"
|
||||
} catch(e) {
|
||||
console.log(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
ParametricManager.prototype.onConstantsExternalChange = function(constantDefinition) {
|
||||
this.rebuildConstantTable(constantDefinition);
|
||||
this.refresh();
|
||||
};
|
||||
|
||||
ParametricManager.prototype.defineNewConstant = function(name, value) {
|
||||
let constantDefinition = this.viewer.params.constantDefinition;
|
||||
let constantText = name + ' = ' + value;
|
||||
if (constantDefinition) {
|
||||
constantDefinition += '\n' + constantText;
|
||||
} else {
|
||||
constantDefinition = constantText;
|
||||
}
|
||||
this.rebuildConstantTable(constantDefinition);
|
||||
//disabling onConstantsExternalChange since we don't need re-solve
|
||||
this.viewer.params.set('constantDefinition', constantDefinition, 'parametricManager');
|
||||
};
|
||||
|
||||
ParametricManager.prototype.findComponents = function(constr) {
|
||||
if (this.subSystems.length === 0) {
|
||||
|
|
@ -500,7 +516,7 @@ ParametricManager.prototype._prepare = function(locked, subSystems, extraConstra
|
|||
for (var i = 0; i < subSystems.length; i++) {
|
||||
solvers.push(this.prepareForSubSystem(locked, subSystems[i].constraints, extraConstraints, disabledObjects));
|
||||
}
|
||||
if (subSystems.length == 0 && locked.length != 0) {
|
||||
if (subSystems.length == 0 && locked && locked.length != 0) {
|
||||
solvers.push(this.prepareForSubSystem(locked, [], extraConstraints, disabledObjects));
|
||||
}
|
||||
return {
|
||||
|
|
@ -896,9 +912,9 @@ Constraints.RadiusOffset = function(arc1, arc2, offset) {
|
|||
Constraints.RadiusOffset.prototype.NAME = 'RadiusOffset';
|
||||
Constraints.RadiusOffset.prototype.UI_NAME = 'Radius Offset';
|
||||
|
||||
Constraints.RadiusOffset.prototype.getSolveData = function() {
|
||||
Constraints.RadiusOffset.prototype.getSolveData = function(resolver) {
|
||||
return [
|
||||
['Diff', [this.arc1.r, this.arc2.r], [this.offset]]
|
||||
['Diff', [this.arc1.r, this.arc2.r], [resolver(this.offset)]]
|
||||
];
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,6 @@ export class OffsetTool extends LoopPickTool {
|
|||
if (isNaN(delta)) {
|
||||
return;
|
||||
}
|
||||
const absDelta = Math.abs(delta);
|
||||
|
||||
const edges = [];
|
||||
const startPoint = findLowestPoint(loopPoints);
|
||||
|
|
@ -45,7 +44,9 @@ export class OffsetTool extends LoopPickTool {
|
|||
if (inverse) {
|
||||
delta *= -1;
|
||||
}
|
||||
|
||||
|
||||
const pm = this.viewer.parametricManager;
|
||||
const offsetConstant = createOffsetConstant(pm, delta);
|
||||
for (let i = 0; i < length; ++i) {
|
||||
let a = loopPoints[pos(i)];
|
||||
let b = loopPoints[pos(i + 1)];
|
||||
|
|
@ -59,8 +60,8 @@ export class OffsetTool extends LoopPickTool {
|
|||
if (origEdge._class == 'TCAD.TWO.Segment') {
|
||||
const segment = this.viewer.addSegment(aOffX, aOffY,
|
||||
bOffX, bOffY, this.viewer.activeLayer);
|
||||
this.viewer.parametricManager._add(new Constraints.Parallel(origEdge, segment));
|
||||
this.viewer.parametricManager._add(new Constraints.P2LDistanceSigned(a, segment.b, segment.a, delta));
|
||||
pm._add(new Constraints.Parallel(origEdge, segment));
|
||||
pm._add(new Constraints.P2LDistanceSigned(a, segment.b, segment.a, offsetConstant));
|
||||
edges.push(segment);
|
||||
} else if (origEdge._class == 'TCAD.TWO.Arc') {
|
||||
const connectionEdge = new SimpleEdge(new EndPoint(aOffX, aOffY), new EndPoint(bOffX, bOffY));
|
||||
|
|
@ -72,17 +73,17 @@ export class OffsetTool extends LoopPickTool {
|
|||
new EndPoint(origEdge.c.x + offVector.x, origEdge.c.y + offVector.y)
|
||||
);
|
||||
arc.stabilize(this.viewer);
|
||||
this.viewer.parametricManager._linkObjects([arc.c, origEdge.c]);
|
||||
this.viewer.parametricManager._add(new Constraints.RadiusOffset(inverse?arc:origEdge, inverse?origEdge:arc, delta));
|
||||
pm._linkObjects([arc.c, origEdge.c]);
|
||||
pm._add(new Constraints.RadiusOffset(inverse?arc:origEdge, inverse?origEdge:arc, offsetConstant));
|
||||
this.viewer.activeLayer.add(arc);
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < edges.length; i++) {
|
||||
this.viewer.parametricManager._linkObjects([edges[i].b, edges[(i + 1) % edges.length].a]);
|
||||
pm._linkObjects([edges[i].b, edges[(i + 1) % edges.length].a]);
|
||||
}
|
||||
this.viewer.parametricManager.solve(undefined, undefined, loopEdges);
|
||||
this.viewer.parametricManager.refresh();
|
||||
pm.solve(undefined, undefined, loopEdges);
|
||||
pm.refresh();
|
||||
this.viewer.toolManager.releaseControl();
|
||||
}
|
||||
|
||||
|
|
@ -124,4 +125,14 @@ function findLowestPoint(poly) {
|
|||
}
|
||||
}
|
||||
return hero;
|
||||
}
|
||||
|
||||
function createOffsetConstant(pm, value) {
|
||||
let constant;
|
||||
let i = 0;
|
||||
do {
|
||||
constant = 'OFFSET' + i++;
|
||||
} while (pm.constantTable[constant]);
|
||||
pm.defineNewConstant(constant, value);
|
||||
return constant;
|
||||
}
|
||||
Loading…
Reference in a new issue