Here’s something else that may be of interest to people. As I was working towards the solution shown in the last post – before Albert told me about the ucsToWorld() function (thanks, Albert :-) – I ended up extending the .NET code we saw in the previous post to include a TransformToWcs() method (marshaled by a transToWcs() function on the JavaScript side of things). What’s interesting about this function – and has caused me to show it here, despite its existence being rendered redundant by ucsToWorld() – is that it returns a value to the caller by serializing point data into JSON (once again using Json.NET to save some effort).
Here’s the C# code:
[JavaScriptCallback("NetTransformToUcs")]
public string TransformToUcs(string jsonArgs)
{
// Get the matrix of the current UCS
var ed =
Application.DocumentManager.MdiActiveDocument.Editor;
var ucs = ed.CurrentUserCoordinateSystem;
// Unpack our JSON-encoded parameters using Json.NET
var o = JObject.Parse(jsonArgs);
var x = (double)o["functionParams"]["point"]["x"];
var y = (double)o["functionParams"]["point"]["y"];
var z = (double)o["functionParams"]["point"]["z"];
// Get and return our transformed point
var point = new Point3d(x, y, z).TransformBy(ucs);
return
"{\"retCode\":0, \"result\":\"OK\", \"point\":" +
JsonConvert.SerializeObject(point) + "}";
}
And here’s the JavaScript shaping code:
function transToWcs(pt) {
var jsonResponse = exec(
JSON.stringify({
functionName: 'NetTransformToUcs',
invokeAsCommand: false,
functionParams: {
point: pt
}
})
);
var jsonObj = JSON.parse(jsonResponse);
if (jsonObj.retCode !== Acad.ErrorStatus.eJsOk) {
throw Error(jsonObj.retErrorString);
}
var ret =
new Acad.Point3d(
parseFloat(jsonObj.point.X),
parseFloat(jsonObj.point.Y),
parseFloat(jsonObj.point.Z)
);
return ret;
}
That’s it for today: I just wanted to publish a quick one, as it’s a holiday for Autodesk here in Neuchatel (a bridging day after Ascension). Tomorrow I’ll be flying out to Singapore, but I’ll do my best to find the time to publish a few posts, next week.