Kean Walmsley

July 2009

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Twitter Updates

    follow me on Twitter



    « Civil 3D 2007 Service Pack 1A Released | Main | Getting the ObjectARX Wizard to work with Visual Studio Express editions »

    July 03, 2006

    Getting started with AutoCAD and .NET

    To get started with writing a .NET app for AutoCAD, download the ObjectARX SDK for AutoCAD 2007. Contained within the samples/dotNet folder of the SDK are a number of helpful samples showing how to use various features of the managed API to AutoCAD.

    Incidentally, the project files etc. are generally saved in the version of Visual Studio that is recommended to build ObjectARX (C++) apps for that version of AutoCAD. So the projects in the ObjectARX 2006 SDK will be for Visual Studio .NET 2002, and in ObjectARX 2007 they will be for Visual Studio 2005. These specific Visual Studio versions are not strictly necessary to use the managed APIs for the respective versions of AutoCAD (that's one of the beauties of .NET, in that it helps decouple you from needing a specific compiler version), but for consistency and our own testing we maintain the parity with the version needed to build ObjectARX/C++ applications to work with AutoCAD.

    The simplest sample to get started with is the classically named "Hello World" sample, which in this case is a VB.NET sample. I won't talk in depth about any of the samples at this stage; I'm going to focus more on how to use the ObjectARX Wizard to create a VB.NET application.

    In the utils\ObjARXWiz folder of the ObjectARX SDK, you'll find the installer for the ObjectARX Wizards (ArxWizards.msi). I'm using the Wizard provided with the ObjectARX SDK for AutoCAD 2007.

    Once installed, you can, of course, create new ObjectARX/C++ projects; we use this tool all the time in DevTech to help generate new SDK samples as well as diagnose API issues reported to us. A relatively new feature is the AppWizard for VB.NET and C#. This is visible when you ask Visual Studio 2005 to create a new project:

    Vb_appwizard

    Once you select "OK", you will be shown a single page to configure your project settings - all very simple stuff:

    Vb_appwizard_2

    Selecting "Finish" will set up the required project settings and generate the basic code needed for your application to define a single command called "Asdkcmd1".

    Before we look into the code, what has the Wizard done? It has created a Class Library project, adding a couple of references to the DLLs defining the managed API to AutoCAD. If you select "Add Reference" on the created project, you can see them in the "Recent" list:

    Vb_project_references

    There are two AutoCAD-centric references listed here: acdbmgd.dll, which exposes the internal AcDb and supporting classes (common to both AutoCAD and RealDWG), and acmgd.dll, which exposes classes that are specific to the AutoCAD application.

    So now let's look at the code. It's really very straighforward - it imports a namespace (which saves us from prefixing certain keywords such as CommandMethod with "Autodesk.AutoCAD.Runtime.") and then defines a class to represent our application module. This class (AdskClass) defines callbacks that can be declared as commands. This is enough to tell AutoCAD that the Asdkcmd1 method needs to be registered as a command and should be executed when someone types that string at the command-line.

        Imports Autodesk.AutoCAD.Runtime

        Public Class AdskClass

            ' Define command 'Asdkcmd1'

            <CommandMethod("Asdkcmd1")> _

            Public Sub Asdkcmd1()

                ' Type your code here

            End Sub

        End Class

    And that's really all there is to it. To see it working, add a function call to the command function, such as MsgBox("Hello!"), build the app, and use AutoCAD's NETLOAD command to load the resultant DLL. When you type in ASDKCMD1 at the command line, your custom command defined by VB.NET should be called.

    Time for some quick credits: a number of the DevTech team have been involved over the years in developing the ObjectARX Wizard (including the recent versions that support .NET) but the chief architect of the tool is Cyrille Fauvel, who is part of the DevTech EMEA team and is based in France.

    TrackBack

    TrackBack URL for this entry:
    http://www.typepad.com/services/trackback/6a00d83452464869e200d835046e1269e2

    Listed below are links to weblogs that reference Getting started with AutoCAD and .NET:

    » Getting started with AutoCAD and .NET from CadKicks.com
    You've been kicked (a good thing) - Trackback from CadKicks.com [Read More]

    Comments

    ... thank you for getting me started in Object ARX! I've used AutoLISP since '93, VB/VBA since 2000, and stated using .NET in 2002. I've always want to use ARX and can't wait to get going -- thanks again!

    My pleasure, Lanny! :-)

    Kean

    Hi Kean,
    Great post, I'm new in developing programs in Visual Studio for using in Autocad and I would like to know the other way around, i.e, if it's possible to programme in .NET in the AutoCad environment...until now we had had always to use VBA or LISP, but I don't know if we can use VB.NET and use all the power of the .NET Framework in Autocad.
    Best Regards
    Juan Carlos

    Hi Juan,

    You can absolutely use Visual Studio - even one of the Express Editions - to write managed assemblies that load and run inside AutoCAD.

    There currently isn't an Integrated Development Environment (IDE) inside AutoCAD that supports .NET development (such as VSTA - Microsoft's version of Visual Studio that can be embedded by applications), but that shouldn't stop you from writing and debugging .NET code inside AutoCAD.

    Best regards,

    Kean

    Hi Kean,

    Can i create dockable forms this way also?
    In the past i used delphi to develop activex components and accont16.arx to run them.
    Now i have to pay for accont17.arx so i'm looking for something else.

    Marcel

    Hi Marcel,

    Check out the DockingPalette sample on the ObjectARX SDK - this will show you how to create a dockable window through .NET. I believe the .NET Labs on the AutoCAD Developer Center (http://www.autodesk.com/developautocad) also covers this topic.

    Regards,

    Kean

    Hi Kean,

    Since from 3 years i am using Lisp, VBA. Now i would like to create AutoCAD Customize Programs through VB.Net, Is there any Complete reference for Beginners? Because i gone threw your post's its very use full for experienced .Net Programmers.

    Please give me an idea ASAP.

    Thanks,
    Senthil Prabu B R
    Banglore.

    Gr8 tut.... I did worked on this tech during 2000. Easily recollect with this stuff...

    ThanX...
    Sunilkumar S.
    Mumbai-India

    how can getting start autocad.....?

    Can you rephrase the question?

    Kean

    You've been kicked (a good thing) - Trackback from CadKicks.com
    http://www.cadkicks.com/adkautocad/Getting_started_with_AutoCAD_and_NET

    I just stated learning VB.net the past couple weeks after just learning some AutoLisp a few months ago. I liked the article and hope to read more like it to help me along the way. It was very easy to read and started with the basic which I need.

    Hi Kean!

    I am working in the C#.net applications and we need to know how to import the autocad files like DWG files and also we want to load the dwg files into the .net applications..

    Any ideas?

    Thanks in Advance!

    There are two ways to "import" DWG files to be accessible in a .NET application: one is to load them in the editor (use the DocumentManager for that) the other is to create a Database object and use ReadDWG file to load it in. This second approach creates an "in-memory" or "side" database - the DWG is loaded and accessible but not open in the editor. With either approach you can then open the various objects in the DWG file - most commonly by getting the block table and then the model-/paper-space block table records.

    I hope this helps,

    Kean

    Kean, just getting started. Installed Visual Studio 2008, downloaded ObjectARX 2008 SDK. Ran the "ArxWizards.msi" file, it said it was looking for Visual Studio 2005 only.

    Can I use VS2008 for Vb.NET development?

    You certainly can, but the Wizard is primarily for ObjectARX development and so only installs on Visual Studio 2005. You could probably fool the installer using the technique in this post, and then just use the VB.NET project template (the current .NET portion of the Wizard is very basic - just project setup, which is already straightforward in .NET).

    I probably wouldn't bother, on balance - there's not much to gain for all that effort. This post may actually be of more use to you.

    Kean

    Is there a way to dynamically create commandsmethods at runtime in VB.Net?

    There are two ways I know of to do this:

    1. P/Invoke the C++ API.
    2. Use the unsupported Autodesk.AutoCAD.Internals.Utils.Add/RemoveCommand (from acdbmgdinternal.dll).

    Kean

    Hi Kean,
    Love the blogs, I have spent a lot of time reading through them lately.
    This topic covers opening .net from AutoCAD and creating a new command. I want to do the opposite, open AutoCAD 2008 from a .net (3.5)/VB Express form and issue existing AutoCAD commands to create and populate a 2-3 page .dwg based on user input from the form. I am relatively new to both AutoCAD and VB, any idea where I can find information or tutorials that would help me along this path? I already have Jerry Winters book and have covered about the first 7 chapters, but it seems to also work from the AutoCAD side. Or am I missing something?
    Thanks.

    Hi Joey,

    This post should be of help.

    Kean

    Examples in C#: http://devdump.wordpress.com/2009/03/04/autocad-and-net/

    Hi Kean,

    I want to use parameters for this function. How i can declare parameters in the CommandMethod instruction ?

    [Autodesk.AutoCAD.Runtime.CommandMethod("SetCustom")]

    public void SetCustom(string varName, string varValue)
    {
    Database db = Application.DocumentManager.MdiActiveDocument.Database;

    DatabaseSummaryInfoBuilder infoBuilder = new DatabaseSummaryInfoBuilder(db.SummaryInfo);

    infoBuilder.CustomPropertyTable.Add(varName, varValue);

    db.SummaryInfo = infoBuilder.ToDatabaseSummaryInfo();
    }

    Thank you.

    Hi Marcel,

    This question comes up from time to time (which makes me think it deserves a dedicated post).

    You have a choice: you can use the LispFunction attribute to create a LISP-callable function - which means you would need to call it using (setcustom "name" "value") - or otherwise you can use the Get* functions (as described in this comment) to let you pass the arguments using SendStringToExecute() or SendCommand().

    Regards,

    Kean

    I am starting on a project in autocad and have try VB & C++ helloworld in dotNet and can't get them to work I am using 2008 AutoCAD & DotNet. Is there anything in these version that is keeping these from working? Could you send me a completed program that works in C++.

    There's nothing that should be a problem, assuming you're creating a Class Library (a .DLL for .NET and a .ARX for C++) and are loading them via NETLOAD or ARXLOAD (there are other ways, but I won't go into them here).

    I suggest working through the resources provided on the AutoCAD Developer Center, especially the ObjectARX and AutoCAD .NET Labs and all the samples that ship with the ObjectARX SDK.

    Kean

    Verify your Comment

    Previewing your Comment

    This is only a preview. Your comment has not yet been posted.

    Working...
    Your comment could not be posted. Error type:
    Your comment has been posted. Post another comment

    The letters and numbers you entered did not match the image. Please try again.

    As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

    Having trouble reading this image? View an alternate.

    Working...

    Post a comment

    Feed & Share

    Search