A simplified .NET API for accessing AutoCAD parameters and constraints

In the 2010 release, we introduced 2D geometric constraints to AutoCAD. The feature was implemented using AutoCAD's low-level – and very powerful – Associative Framework, which has subsequently been used to implement features such as parametric surfaces in AutoCAD 2011 (more information on that here). Our initial API implementation for creating and accessing geometric constraints (which you can find out more about via the DevTV session linked to from this previous post) in many ways reflects the low-level nature of the underlying framework: it was unfortunately just a bit too complicated for most mortals to get to grips with.

Philippe Leefsma, from DevTech EMEA, and Gopinath Taget, from DevTech Americas, have worked with our Engineering team to develop a "high-level" API for constraints inside AutoCAD, which does a great job of abstracting away much of this complexity.

The API distribution includes a single VS 2008 solution – as the API has been implemented using ObjectARX, which currently requires the use of VS 2008 – containing projects for the C++ API, the mixed-mode layer exposing this to .NET and a test project for each of these managed and unmanaged APIs.

For people such as myself using VS 2010, the best thing to do is to load this solution – converting the various projects – and then unload the projects you don't need: AdnAssocConstraintAPI, AdnAssocConstraintAPIMgd and ArxConstraintAPITest. You will then need to set a "Reference Path" in the remaining project (MgdConstraintAPITest), pointing it to the compiled AdnAssocConstraintAPIMgd.dll file in the "bin resource" folder, as well as to the usual AcDbMgd.dll and AcMgd.dll files.

Beyond that, the implementation should be relatively straightforward: I suggest trying out the various commands implemented in the test project to get a feel for what's possible, as well as taking a look at the compiled help-file provided in the Help folder.

Here are a list of these commands with a brief description of what they do:

  • MGHLCOIN – creates a coincident constraint
  • MGHLPAR – creates a parallel constraint
  • MGHLPERP – creates a perpendicular constraint
  • MGHLCONC – creates a concentric constraint
  • MGHLCOLIN – creates a co-linear constraint
  • MGHLHOR – creates a horizontal constraint
  • MGHLVERT – creates a vertical constraint
  • MGHLFIX – creates a fix constraint
  • MGHLTAN – creates a tangent constraint
  • MGHLG2SM – creates a smooth constraint
  • MGHLSYM – creates a symmetric constraint
  • MGHLEQLN – creates a equal length constraint
  • MGHLEQRAD – creates a equal radius constraint
  • MGHLDIMALIGN – creates an aligned dimensional constraint
  • MGHLPARAM – illustrates several functionalities of AssocUtil Parameter API (query, creation, modification, renaming)

Thanks to Philippe and Gopi for their hard work on this very helpful API!

17 responses to “A simplified .NET API for accessing AutoCAD parameters and constraints”

  1. Kean, do you know if an x64 compatable version of the this API is available?

    Thanks,

    Chris Wardell

  2. Chris,

    I pinged Philippe about this, and he very quickly created and tested an x64-compatible library. I've updated the link in the post.

    Please take a look and let us know if you have any feedback.

    Regards,

    Kean

  3. Thanks Kean, I'll give it a try.

    C.W.

  4. Kean,
    I am trying to use the library in VS2010, and followed your instructions. The application compiles properly, but on the first call to the library, I get:

    {"Could not load file or assembly 'AdskAdnAssocConstraintAPIMgd.dll' or one of its dependencies. The specified module could not be found.":"AdskAdnAssocConstraintAPIMgd.dll"}

    I pointed to the DLL using the Add Reference. The dll was in the adnassocconstraintapi\bin release\x64 folder. I have also tried moving it and the library file to my solution folder.

    Can you please help as to why the file cannot be found? What is the ":" in the message above?

    Thank you

  5. By the way, I also added the reference path to the same folder.

  6. OK, I know what the problem is.

    I had to NETLOAD the DLL. Fine. But I am using it with Autocad 2013 and am getting a message that it is incompatible.

    Is there an AutoCAD 2013 compatible version of the library?

    Thanks

  7. Hi David,

    As far as I recall the version posted there was compiled against 2012 SDK, so you need to migrate the settings for all projects in order to build against the 2013 SDK, compile the arx part using patform toolset V100, .Net framework 4.0...
    I've tested that lib on my side under 2013, so I can assure you it works.
    I hope it helps,
    Philippe.

  8. Could you please post or send me the DLL you created that is 2013 compatible?

  9. Matthias Mueller Avatar
    Matthias Mueller

    Any chance to get the code in C#? The Utils are amazing but lack a bit when working with polylines. E.g. try to apply a coincident constraint between two polylines (start point of "polyline A" to end point of "polyline B") -> it fails with a runtime exception.

  10. Philippe Leefsma Avatar
    Philippe Leefsma

    Hi Matthias, I would need to take a look in details why it fails. But the full source code is provided along with the download, the lib is written in C++, but if you run it from the debugger, you will be able to break inside the managed and unmanaged code to see where the problem is...

  11. Philippe Leefsma Avatar
    Philippe Leefsma

    Hi Matthias, I was fixing the issue. Please give a try at the new distribution. Thanks for your feedback and feel free to report further problem you may find with the lib.

  12. Matthias Mueller Avatar
    Matthias Mueller

    I tried to fix this issue with a colleague of mine (he's more into c++). We were almost at the point to give up, when we saw your reply.
    Thank you so much! The updated Util now also handles coincident constraints between polylines and nested entities within block references. If we find further problems, where do we communicate? Here? ADN Forum?
    Again, thanks!

  13. Philippe Leefsma Avatar
    Philippe Leefsma

    Hi Matthias, the best I could think of was to create my own post, so programmers can directly report issues to me and we won't bother Kean anymore;) but let's hope you will find no more;) ... Here is the dedicated post on ADN blog:

    adndevblog.typepad.com/autocad/2013/01/a-simplified-net-api-for-accessing-autocad-parameters-and-constraints.html

  14. I used MGHLDIMALIGN command .Result not like result of DCALIGNED command with Point & Line option in Autocad .Please explain to me ?

    1. Kean Walmsley Avatar

      Please address your questions on this to the ADN DevBlog via the post linked to in the comment before yours.

      Thanks,

      Kean

  15. Can you please update this for AutoCAD 2020 and 2021?

    1. Kean Walmsley Avatar

      Please submit your request via this post:

      adndevblog.typepad....

      Kean

Leave a Reply to Philippe Cancel reply

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