Returning data from .NET to JavaScript inside AutoCAD

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.

10 responses to “Returning data from .NET to JavaScript inside AutoCAD”

  1. georgeendrulat Avatar
    georgeendrulat

    I've been reading through anything js + autocad related, and it appears a lot of people have a tool called ADNJSDemo, if you've heard of it, any idea where I can find it?

    1. Kean Walmsley Avatar

      I'm pretty sure it's posted on the ADN web-site under Events/Developer Days 2014. But you should really ping the ADN team - best via the AutoCAD DevBlog - to see if they can help provide it.

      Kean

      1. georgeendrulat Avatar
        georgeendrulat

        Got it, I really appreciate it.

        For those looking:

        You need to compile the AcHTML c# project in the autocad_2014_getting_started_with_javascript_ap --> Samples folder.

        To get it to work with acad 2015 I had to compile it against the .net framework 4.5, it works.

        1. Jose Antonio Mateos Avatar
          Jose Antonio Mateos

          I have Built the project and get a folder in /bin/release/ with a librery called ACHtml.dll. The question is where should I put such dll file in AutoCAD?

          1. First try NETLOADing it from where it is, to see if it works.

            Kean

            1. Jose Antonio Mateos Avatar
              Jose Antonio Mateos

              Yes.... it works now. Thanks.

  2. Hi Kean,
    I am confused that where are the "exec" and "execAsync" functions defined? Obviously, it is not from global of DOM.

  3. Add in the 2019 version of Autocad, should using "retValue" as result.

  4. Hi Kean,
    I am confused that where are the "exec" and "execAsync" functions defined? Obviously, it is not from global of DOM.

    1. They're extensions to the object model exposed from the Chromium Embedded Framework.

      Kean

Leave a Reply

Your email address will not be published. Required fields are marked *