Getting Started with JavaScript API on AutoCAD 2014

By Philippe Leefsma

As you may have seen already, AutoCAD 2014 is coming with a brand new JavaScript API that will allow a new wave of programmers to interact with it.

Whether you are an experienced web developer or a complete newbie in that area, this DevTv will take you through the steps required to get started programming AutoCAD in JavaScript. A previous experience with one of the existing AutoCAD API, such as ObjectARX or .Net is however a plus.

Here are the topics I cover during the presentation:

I. Introduction to JavaScript

II. JavaScript API in AutoCAD

III. Extending AutoCAD JavaScript API

You will also find the samples that I am demo-ing as part of the attached material below. Note: Firefox or Chrome are recommended browsers if you are playing the video using the html link, IE may behave strangely.

Download AutoCAD_2014_Getting_started_with_JavaScript_API


Comments

27 responses to “Getting Started with JavaScript API on AutoCAD 2014”

  1. Jeff Evans Avatar
    Jeff Evans

    Hi, the video is very interesting. As for the API document mentioned in the video, I took a look at http://www.autocadws.com/jsapi/v1/docs/Acad_Application.html. An very interesting thing is that classes in the document have no details, just a constructor, such as “Acad.Vector2d”.
    Without member methods, how could we use them? Could you explain this?

  2. Philippe Avatar
    Philippe

    Hi Jeff,
    Thanks for your interest in the JavaScript API. Make sure you scroll down the page, so the documentation link you mentioned does have a description of the properties for each object. Talking about Acad.Vector2d, here is what I can see:
    Name: x
    Type: Number
    Description: Returns the X property value
    Condition: Read-write
    Name: y
    Type: Number
    Description: Returns the Y property value
    Condition: Read-write
    I hope it helps,
    Philippe.

  3. Jeff Evans Avatar
    Jeff Evans

    Thank you very much, Philippe.
    Take the Vector2d as the example, I know that there are X and Y properties. However, there is no methods, like rotation, dotProduct, crossProduct etc, which are available in ARX’s AcGeVector2d documentation.
    Thanks.

  4. The 2014 version of the JavaScript API has to be considered as a “tech-preview”. The real v1.0 should be released in the next version of AutoCAD.
    It is however in no point the equivalent or a replacement for the powerful ARX API.
    Acad.Vector2d has no methods, it’s simply a structure holding X,Y coordinates. If needed, methods should be implemented on your own.
    To get an idea about what is doable at the moment using JS, take a look at the samples coming along, they are pretty exhaustive in term of functionality. You can also extend the JS API using .Net or ARX also as illustrated in that material.
    I hope it clarifies thing a bit.

  5. Jeff Evans Avatar
    Jeff Evans

    Thanks!

  6. Hi,
    In your demo video you use “adnjsdemo” to load the html files.
    Is this a separate app that we can download from somewhere? Or is there an alternative way to load the html file?
    If I understand the help files correctly, webload loads only JavaScript files.

  7. Philippe Avatar
    Philippe

    Hi, everything is included in the package. ADNJSDEMO is a custom command created by a .Net plug-in. You need to use custom code to load html files, you are correct webload loads only js.

  8. Thank you for your clarification.
    I found a file called JavascriptLoader.dll in the package and I assumed it is something similar to yout ADNJSDEMO command, but NETLOAD returns error on load: “Cannot load assembly. Error details: System.BadImageFormatException….”
    Is there any way to create a HTML5 palette without having to learn and compile C# code?

  9. Philippe Avatar
    Philippe

    You are getting that error most likely because the dll is compiled against another version of the SDK than the AutoCAD version you are using. Unfortunately the only way at the moment to display a custom html page in a palette is to load it using custom .Net code (C# or VB.Net). We have resources that can help you getting started with AutoCAD.Net programming:
    http://usa.autodesk.com/adsk/servlet/index?id=18162650&siteID=123112

  10. Does anyone have a compiled ADNJSDEMO, or whatever lib contains it, or just anything that can point me in the right direction to obtaining it?

  11. Hi George, the complete source code is provided in the link, what is holding you back to build it yourself?

  12. Hi, the article is very interesting.
    I’m a designer (AutoCAD user) i can programming on JS, but i dont know ObjectARX C# .NET VB.net C++ etc.
    Could you briefly describe how can I create a command as ADNJSdemo?
    I think it will be very interesting and very useful for people like me.
    We hope for your help… Thanks!

  13. Hello,
    The ADNJSDemo is available in the sample code and you can compile it using Visual Studio. Express edition of Visual Studio will also do.
    This tutorial will help you get started with the AutoCAD .Net API :
    http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=18162650
    Regards,
    Balaji

  14. Hello Balaji.
    Thank you very much!
    Now everything clicked into place.

  15. In addition,
    The following article is very useful for create a command as ADNJSdemo:
    http://through-the-interface.typepad.com/through_the_interface/2013/03/implementing-an-autocad-palette-using-html5-and-javascript.html

  16. Jose Antonio Mateos Avatar
    Jose Antonio Mateos

    Is there any function I could use to get the points from a polyline?
    I thought that once your select one Entity Id (like a Polyline) you could get info like its vertex and so on.
    I use your ADNJSDEMO script to load my HTML and I only need to be able to click on every Entity in the DWG and store the vertex of each polyline into the database.
    Is there any example I could use?

  17. I don’t think you can do this from the JavaScript API, you would need to use the C++ or .Net API inside AutoCAD to achieve that. Getting the vertices from .Net is fairly straightforward: http://through-the-interface.typepad.com/through_the_interface/2007/04/iterating_throu.html
    You could load a custom .Net dll in AutoCAD that expose a toolkit of features and invoke that from js. There is an example in that material that illustrates calling .net from js.
    Hope that helps
    Philippe.

  18. Rudolffo Lemos Avatar
    Rudolffo Lemos

    Hi Philippe,
    I note on my application that the acwebbrowser.exe process is left running after i remove it from palletset. I run your example ADNJSDEMO and the same problem happened when i call it more than one time. Its a Bug ? Do you know how can i fix it?
    Regards,
    Rudolffo

  19. Here is the feedback from a colleague about that:
    “This issue can happen with older versions of the AcWebBrowser when it gets in a hung state, or if the UI frame is still active. Is the host palette set really shutdown, or just hidden? Did you dispose it yet?”
    Hope that helps
    Philippe.

  20. Rudolffo Lemos Avatar
    Rudolffo Lemos

    Thanks Philippe for yours feedback. Some aditional information.
    Product name Autodesk Browser Components
    Company name Autodesk
    File description Chromium host executable
    Internal name acwebbrowser.exe
    Original filename acwebbrowser.exe
    Legal copyright Copyright (C) 2013, Autodesk
    Product version 2.0.0.0
    File version 2.0.0.0
    Basically the code is:
    _ps = new PaletteSet(“”, new Guid(“229E43DB-E76F-48F9-849A-CC8D726DF257”));
    _ps.Style = …

    _ps.Visible = false;
    _ps.Add(objLabel, uri); // process created.
    _ps.Remove(0);
    _ps.Visible = true;
    I re-use this palette and show an other URI. I do this because i want to keep the palette position.

  21. Athanasius Avatar
    Athanasius

    Hi,
    I’m having some issues to get AcHTML running. For now I’ve created the ACHTML.dll file with Visual Studio 2012, 4.5 .NET. Running it with AutoCAD 2016.
    I can load the .dll into AutoCAD and I can also open the html file with the adnjsdemo command, as i.e. the DBReactors.html. From there on the window opens in autocad, but I cannot Add Listeners or anything else. Also tried all the other html examples. Do I still have to add a javascript loader or anything else? It seems, that the html file cannot call the .js files.
    Many thanks in advance,
    Athanasius

  22. Athanasius Avatar
    Athanasius

    Maybe this helps to come closer to the solution. Starting AutoCAD I get the following errors:
    AutoCAD menu utilities loaded.Cancel
    Command: Cancel
    _RIBBON
    *Cancel*
    COMMANDLINE
    Layer
    *Cancel*
    JavaScript Unknown command “JAVASCRIPT”. Press F1 for help.
    Demo
    Unknown command “DEMO”. Press F1 for help.

  23. Athanasius Avatar
    Athanasius

    Slowly answering my own questions… I had to change the Autodesk.AutoCAD.js to
    http://www.autocadws.com/jsapi/v2/Autodesk.AutoCAD.js
    Probably that’s due to AutoCAD 2016. However I think I still need to change parts in the .js File, because I cannot get the interaction right between the AutoCad objects and the .html file. Any ideas about that?

  24. Athanasius Avatar
    Athanasius

    src=”http://www.autocadws.com/jsapi/v2/Autodesk.AutoCAD.js”

  25. I think they have suspended support. It was not a successful attempt to introduce JS to the Autocad

  26. E com nodejs, não seria interessante criar uma npm e integrar ao autocad?

  27. Thanks, this was lot to me and it will help me in my work again thanks check this also https://www.speraspect.com

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading