.NET

Reply
Active Contributor
btillman
Posts: 48
Registered: ‎07-25-2012
Message 1 of 14 (280 Views)

Imports Schmimports.....Arrrrrgh!

280 Views, 13 Replies
08-09-2012 01:17 PM

I have been searching, reading, experimenting, you name it and this deal with the namepsace is finally getting me to the breaking point. I would like to programatically save the current AutoCAD drawing.

 

I'm working in VB 2010 Express and I have made some code which gave me very high hopes that I was catching on to this namespace deal. So I'm looking in the AutoCAD .NET Developer's Guide on-line and not a single block of code in there showing me how to save a file will work.

 

I have AutoCAD 2013 running and I load the AutoCAD 2013 Type Library and the AutoCAD/ObjectDBX Common references before I begin coding.

 

That said, can someone please point me in the right direction here. Is there something I'm missing? Here is the code, straight from what appears to be the 2010 version of the Developer's Guide. I cannot put those namespaces into the IDE of VB 2010 Express on either of my machines, 2009 nor the 2013 one and when I try to use the earlier ones I was successful with the code below will not go in without errors.

 

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime

<CommandMethod("SaveActiveDrawing")> _

Public Sub SaveActiveDrawing()

  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim strDWGName As String = acDoc.Name

  Dim obj As Object = Application.GetSystemVariable("DWGTITLED")

'' Check to see if the drawing has been named If System.Convert.ToInt16(obj) = 0 Then '' If the drawing is using a default name (Drawing1, Drawing2, etc) '' then provide a new name strDWGName = "c:\MyDrawing.dwg" End If
'' Save the active drawing
acDoc.Database.SaveAs(strDWGName, True, DwgVersion.Current, _ acDoc.Database.SecurityParameters) End Sub

Does someone have a link to the latest copy of the .NET Developer's Guide????

 

 

*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 2 of 14 (271 Views)

Re: Imports Schmimports.....Arrrrrgh!

08-09-2012 02:25 PM in reply to: btillman

http://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=123112

---------------------------



(defun botsbuildbots() (botsbuildbots))
*Expert Elite*
chiefbraincloud
Posts: 752
Registered: ‎02-13-2008
Message 3 of 14 (265 Views)

Re: Imports Schmimports.....Arrrrrgh!

08-09-2012 03:27 PM in reply to: btillman

You have referenced the COM libraries into your project, and copied Managed Example code.

 

Take out the two references you added, and add references to AcMgd.dll and AcDbMgd.dll from the ObjectArx SDK.

There is also a way to save with COM code, using AcadDocument.SaveAs, but I don't have an example, and haven't worked with COM in so long, I wouldn't want to lead you astray.

Dave O.                                                                  Sig-Logos32.png
Active Contributor
btillman
Posts: 48
Registered: ‎07-25-2012
Message 4 of 14 (257 Views)

Re: Imports Schmimports.....Arrrrrgh!

08-09-2012 07:10 PM in reply to: btillman

I have one more pertinent question about this. I'm writing lots of VB.NET code these days, not just for AutoCAD automation. And for the life of me I cannot figure why people who post code and write docs about code leave out these very important points on name spaces and references which are required to make the code workable. It's like loaning your car to someone and not giving them the keys.

 

As I told my supervisor today when he asked why is it taking so long and I told him not only are there namespaces and references, but there are literally hundreds if not thousands of combinations of these very important chunks of code which are so blantantly left out of the picture. And now I find out that Autodesk doesn't even have anything near what one needs for 2013 as far as docs for .NET. Our reseller told me today that I can pay for $1,400 to join the ADN and I reminded him that we just paid more than 4 grand for 2013 and subscription. To which he replied that I'm doing more than the product was meant to do.....oh boy, I'm glad my boss who paid for this copy didn't hear that. If all we were going to do was 2D drawings we could have purchased a much cheaper alternative for far less than what we spent. We paid this premium because AutoCAD is supposed to do more.

 

Sorry for the rant but tomorrow is not going to be fun when I meet with the boss.

 

And for the record, I will search for those dll's but I read just the other day that the new .NET made them obselete and we should not be using them anymore....again, it wasn't my words, it was from someone who was supposed to know what they were talking about.

*Expert Elite*
chiefbraincloud
Posts: 752
Registered: ‎02-13-2008
Message 5 of 14 (246 Views)

Re: Imports Schmimports.....Arrrrrgh!

08-09-2012 09:54 PM in reply to: btillman

for the life of me I cannot figure why people who post code and write docs about code leave out these very important points on name spaces and references which are required to make the code workable.


Well, Posting code and writing docs are two different things in my opinion.  I won't deny, the current documentation is not very "current", or complete, and admitedly, I do make some assumptions (when I post code) about the posters ability to determine were some references come from.  I can give you a little tip, from experience, that the two type libraries you mentioned in your post (the COM libraries) have "Acad" in front of practically every type.  So in the Managed classes, you have Document, but in the COM classes, you have AcadDocument.  that is an easy way to tell them apart.

 

You should get friendly with your Object Browser in Visual Studio, and use the search tool... a lot.  And if you still don't find what you need, come to this forum, or whatever forum post you got the example code from, and ask the question.  I'm sure someone will help you out.  On This Forum in particular, I will certainly help with questions like that.

 

And now I find out that Autodesk doesn't even have anything near what one needs for 2013 as far as docs for .NET.

 

I already mentioned my agreement about the documentation.  That is a sore spot amongst all of us trying to code for AutoCAD products, that said, you will get a lot of help here on this group, and there are a couple of other stellar resources, the Through The Interface blog by Kean Walmsley which has a ****-ton of info about .NET programming for AutoCAD, but he has taken on a different role at Autodesk recently and therefore his blog does not contain a lot of info about 2013 AutoCAD.  There is also a fairly new DevBlog that has been created, with Stephen Preston, and, Oh, I can't think of all the other names that are contributing, but they are all of the ADN team.  And there is 'The Swamp', which I am not a member of, it seems to be based in Australia, but there are some good programmers, and good with AutoCAD programmers there.  I don't have a handy link for them either, but if you do enough searching/browsing of this forum, you will find one. (It's getting late, and I need to wrap this up, but I have a lot more to say, so I'll try to get you some links tommorrow)

 

Our reseller told me today that I can pay for $1,400 to join the ADN and I reminded him that we just paid more than 4 grand for 2013 and subscription. To which he replied that I'm doing more than the product was meant to do.....

 

I would say that I was able to convince my company to spend the money for the ADN membership, simply because with ADN membership you get access to Developer Licenses for any version of AutoCAD you want (for Developement purposes only, no production).  That argument may or may not apply to you.

 

Sorry for the rant but tomorrow is not going to be fun when I meet with the boss.

 

I hope you are able to read this post before you meet with the boss, so you can temper your frustration with a little optimism

 

And for the record, I will search for those dll's but I read just the other day that the new .NET made them obselete and we should not be using them anymore....again, it wasn't my words, it was from someone who was supposed to know what they were talking about.

 

"The new .NET" has nothing to do with those two references.  whoever said that , either did not not know what they were talking about, or were not talking about AutoCAD stuff.  With 2013 AutoCAD, there is one additional reference that needs to be added.  I just did a (reallyquick) search, and I can't seem to locate it at the moment, but it is something 'Mgd.Core'. I'll try and locate that tommorrow as well.

What you really need to know, beyond that, is that much of the code you find for 2012 and earlier will still work, as coded, except some objects have moved from a Namespace in ApplicationServices, or DatabaseServices, into ApplicationServices.Core, or DatabaseServices.Core

The ultimate point is, don't give up, don't tell your boss it can't be done, because chances are it can be done, and you will find your answers here or on the Swamp, or on the DevBlog, or on Through the Interface. 

I've got to start heading for bed now.  I'll try to get you those links in the morning.  (USA Mountain Time)

 

 

 

Dave O.                                                                  Sig-Logos32.png
Active Contributor
btillman
Posts: 48
Registered: ‎07-25-2012
Message 6 of 14 (238 Views)

Re: Imports Schmimports.....Arrrrrgh!

08-10-2012 06:31 AM in reply to: btillman

Thanks again for your comments and advice. I am back at the corporate grind today with the computer which is running AutoCAD 2013. I downloaded the ObjectARX 2013 docs and files and was able to get the dll's you referenced installed. But after getting everything into the compiler so error messages were no longer showing up the program failed with an InvalidProgramException, which by looking this up said this generally indicates a bug in the compiler which generated the code.

 

First, I'm shocked....shocked that Microsoft would release a product with bugs in it.....:smileywink:

 

I'm running VB 2010 Express....because the boss won't pay for VS just yet, until I prove we can do it. So can anyone who's running full VS see if you get the same problem with this:

 

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.ApplicationServices.Core

Module Module1

    Sub Main()
        SaveActiveDrawing()
    End Sub

    Public Sub SaveActiveDrawing()
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim strDWGName As String = acDoc.Name

        Dim obj As Object = Application.GetSystemVariable("DWGTITLED")

        If System.Convert.ToInt16(obj) = 0 Then
            strDWGName = "C:\MyFiles\MyDrawing.dwg"
        End If

        acDoc.Database.SaveAs(strDWGName, True, DwgVersion.Current, _
                              acDoc.Database.SecurityParameters)

    End Sub

End Module

 I added the following references for this as well:

 

    AcCoreMgd.dll

    AcDbMgd.dll

    AcMgd.dll

 

 

*Expert Elite*
norman.yuan
Posts: 946
Registered: ‎04-27-2009
Message 7 of 14 (229 Views)

Re: Imports Schmimports.....Arrrrrgh!

08-10-2012 07:25 AM in reply to: btillman

How do you run the code you showed here? If it is an EXE and starts the Main(), well, you cannot. That is probably where the InvalidProgramException comes from: it is not a bug from MS.

 

No offence here: programming AutoCAD does need tremandous knowledge on AutoCAD. A good .NET programmer still need to learn a lot before he/she can program AutoCAD. Autodesk's bad documentation does not make things easier. As starter (no matter how good you are at .NET programming), it is good to always begin with AutoCAD .NET API labs published by Autodesk.

 

Also, if you have pre-AutoCAD 2013 available, you might want to started with that version, because Acad 2013 introduced some managed assembly changes that break existing code, while most resources you found so far work with pre Acad 2013. So, with pre 2013 version, you would certainly be able to find more workable code samples. Once you get comfortable with coding AutoCAD, then move to Acad 2013

*Expert Elite*
arcticad
Posts: 1,285
Registered: ‎06-21-2004
Message 8 of 14 (227 Views)

Re: Imports Schmimports.....Arrrrrgh!

08-10-2012 07:45 AM in reply to: norman.yuan

The code needs to be run inside of autocad as a .dll file.

 

Imports Autodesk.AutoCAD.Runtime

Public Class clsSomeClassName

    <CommandMethod("runCommand")> _
    Public Sub main()
        ' do stuff 
    End Sub

End Class

 

---------------------------



(defun botsbuildbots() (botsbuildbots))
*Expert Elite*
chiefbraincloud
Posts: 752
Registered: ‎02-13-2008
Message 9 of 14 (218 Views)

Re: Imports Schmimports.....Arrrrrgh!

08-10-2012 08:41 AM in reply to: btillman

Here is the link to the DevBlog

http://adndevblog.typepad.com/autocad

 

and Here is The Swamp

 

Norman and arcticad have already noted that the code you have can not be run from an .EXE, can you elaborate a little bit on what your ultimate goals are?  I'm sure doing a SaveAs isn't all of it.

 

if you are particularly set on driving AutoCAD through an .EXE, then you must go back to using the COM references that you mentioned in your original post.  That provides access to a completely different set of Objects, and adds a layer of abstraction.  Most things can be accomplished that way, but any heavy interaction will be (really) slow by comparison to Managed code.  As has been said, to use the managed code, you must compile to .DLL, and use the Netload command (or a variety of Autoloading mechanisms) to load your code into AutoCAD.

 

Without more information, I would say the learning curve may be a bit steeper for the managed code as opposed to COM, but the benefits in the long run are great.  I suggest you stick it out with managed code.

Dave O.                                                                  Sig-Logos32.png
Active Contributor
btillman
Posts: 48
Registered: ‎07-25-2012
Message 10 of 14 (211 Views)

Re: Imports Schmimports.....Arrrrrgh!

08-10-2012 10:21 AM in reply to: chiefbraincloud

Yes, I visit theSwamp.org on occassion. There are some very good programmers there. I really do appreciate your advice and assistance. As you can see from my questions, I'm an old-time Qbasic programmer from long ago who got thrust into the role of developer for .NET or dotnet....! Anyway, I do love a challenge and this is certainly interesting work.

 

My goal is rather perversed and twisted but I'll try to explain it briefly. This company is going for it's ISO-9001 certification and as such they will eventually dump approved and completed drawing files into a folder on the main server which once in there can never be changed, revised deleted or edited in any way. We can of course make copies or use these files as templates but according to the ISO-9001 rules, we must archive a drawing and then it cannot be changed or deleted...ever. The IT guys have setup this directory which is going to and it has very limited rights for typical users. My task is to automate a method of saving files to this directory. In a nutshell, the users will use the new pull down menu I've created for these ISO-9001 related tasks. The archive file option will basically mean they click their mouse once and forget it. The click will activate a VB.NET program which will run under an identity on the network with full rights to this archive folder.

 

I know this is a confusing thing to get your arms around but that's really all I need the program to do. And again, it has to be automated. My supervisor says if it can't be done with just one mouse click he doesn't want to hear about it.

You are not logged in.

Log into access your profile, ask and answer questions, share ideas and more. Haven't signed up yet? Register

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.

Need installation help?

Start with some of our most frequented solutions to get help installing your software.

Ask the Community