I decided to take the second route you suggested, with mixed success. I have fully working functions for horizontal and aligned dims, however I’m still having trouble creating functions for vertical and angular dims. I will post what success I have had so far.
My horizontal dim function, which works correctly; it receives the co-ordinates as lists and convert them to RVectors internally (I had to tweak the getOperation function because it tries to read the toolbar value for Scale, which causes an error if it isn’t present):
function drawHorizontalDim(extensionPoint1, extensionPoint2, definitionPoint)
{
include("scripts/Draw/Dimension/DimRotated/DimRotated.js");
var di = getDocumentInterface();
var doc = this.getDocument();
// create and initialize tool:
var a = new DimRotated();
// Amend getOperation for absence of scale value in toolbar
a.getOperation = function(preview) {
if (!this.data.isValid()) {
return undefined;
}
var doc = this.getDocument();
var scale = 1;
var scaled_data = this.data;
scaled_data.setLinearFactor(1/scale);
var entity = new RDimRotatedEntity(doc, scaled_data);
if (!isEntity(entity)) {
return undefined;
}
return new RAddObjectOperation(entity, this.getToolTitle());
};
a.setDocumentInterface(di);
a.data.setExtensionPoint1(new RVector(extensionPoint1));
a.data.setExtensionPoint2(new RVector(extensionPoint2));
a.data.setDefinitionPoint(new RVector(definitionPoint));
// Locks the rotation of the measurement at 0
a.data.setRotation(0.0);
// run operation on current document:
var op = a.getOperation(false);
di.applyOperation(op);
}
My aligned dim function, which works correctly in much the same way:
function drawAlignedDim(extensionPoint1, extensionPoint2, definitionPoint)
{
include("scripts/Draw/Dimension/DimAligned/DimAligned.js");
var di = getDocumentInterface();
var doc = this.getDocument();
// create and initialize tool:
var a = new DimAligned();
// Amend getOperation for absence of scale value in toolbar
a.getOperation = function(preview) {
if (!this.data.isValid()) {
return undefined;
}
var doc = this.getDocument();
var scale = 1;
var scaled_data = this.data;
scaled_data.setLinearFactor(1/scale);
var entity = new RDimAlignedEntity(doc, scaled_data);
if (!isEntity(entity)) {
return undefined;
}
return new RAddObjectOperation(entity, this.getToolTitle());
};
a.setDocumentInterface(di);
a.data.setExtensionPoint1(new RVector(extensionPoint1));
a.data.setExtensionPoint2(new RVector(extensionPoint2));
a.data.setDefinitionPoint(new RVector(definitionPoint));
// run operation on current document:
var op = a.getOperation(false);
di.applyOperation(op);
}
My vertical dim function; this almost works but the definitionPoint isn’t used correctly. Unlike the horizontal dim function, which aligns the label to the absolute y value of definitionPoint, regardless of the x value, the vertical one seems to be stuck either exactly between extensionPoint 1 and 2, or aligned with extensionPoint1, and nothing else (I have tried multiple x and y values and these are the only results I can achieve):
function drawVerticalDim(extensionPoint1, extensionPoint2, definitionPoint)
{
include("scripts/Draw/Dimension/DimRotated/DimRotated.js");
var di = getDocumentInterface();
var doc = this.getDocument();
// create and initialize tool:
var a = new DimRotated();
// Amend getOperation for absence of scale value in toolbar
a.getOperation = function(preview) {
if (!this.data.isValid()) {
return undefined;
}
var doc = this.getDocument();
var scale = 1;
var scaled_data = this.data;
scaled_data.setLinearFactor(1/scale);
var entity = new RDimRotatedEntity(doc, scaled_data);
if (!isEntity(entity)) {
return undefined;
}
return new RAddObjectOperation(entity, this.getToolTitle());
};
a.setDocumentInterface(di);
a.data.setExtensionPoint1(new RVector(extensionPoint1));
a.data.setExtensionPoint2(new RVector(extensionPoint2));
a.data.setDefinitionPoint(new RVector(definitionPoint));
a.data.setRotation(Math.PI / 2);
// run operation on current document:
var op = a.getOperation(false);
di.applyOperation(op);
}
And finally, my angular dim function; this doesn’t work at all yet.
function drawAngularDim(firstEntity, secondEntity, dimArcPosition)
{
include("scripts/Draw/Dimension/DimAngular/DimAngular.js");
var di = getDocumentInterface();
var doc = this.getDocument();
// create and initialize tool:
var a = new DimAngular();
a.setDocumentInterface(di);
a.firstEntity = firstEntity;
a.secondEntity = secondEntity;
a.dimArcPosition = dimArcPosition;
// run operation on current document:
var op = a.getOperation(false);
di.applyOperation(op);
}