.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Imports Schmimports.....Arrrrrgh!

13 REPLIES 13
Reply
Message 1 of 14
btillman
1040 Views, 13 Replies

Imports Schmimports.....Arrrrrgh!

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????

 

 

13 REPLIES 13
Message 2 of 14
arcticad
in reply to: btillman

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

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



(defun botsbuildbots() (botsbuildbots))
Message 3 of 14
chiefbraincloud
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
Message 4 of 14
btillman
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.

Message 5 of 14
chiefbraincloud
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
Message 6 of 14
btillman
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.....Smiley Wink

 

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

 

 

Message 7 of 14
norman.yuan
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

Message 8 of 14
arcticad
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))
Message 9 of 14
chiefbraincloud
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
Message 10 of 14
btillman
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.

Message 11 of 14
arcticad
in reply to: btillman

The question think we need answered is what exactly do you want:

 

1. An archive button to archive the current drawing.

       a. Automatically archive when saved

       b. Only when prompted to do so.

       c. Manual archive only

 

2. Archive an entire project at a time.

       a. Archive entire project

       b. Archive only drawing files.

 

Are you going to allow the user to do this or will someone else.

 

Do you need the program to run in AutoCAD? How are going to handle files in use?

 

If your going to Queue the files you can store all this info into the systems registry / text file / database

and then build and export the files in the queue and a predetermined time.


You need to be very clear about the steps you expect the program to do.

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



(defun botsbuildbots() (botsbuildbots))
Message 12 of 14
chiefbraincloud
in reply to: btillman

If I've got my head wrapped around this correctly, you are not going to be able to have AutoCAD save the Drawing to the final Archive location, because AutoCAD will not be running under the proper identity to perform the save, so AutoCAD will have to save the drawing in the working location, then your separate program will be fired off under the different identity, and copy (or move) the file to the Archive location.

 

All this is doable.  You might not even need a .NET program to run in AutoCAD.  You can put a save and a shell execute into the button code, but I'm not sure how you are planning on getting the .EXE running under different credentials.  I believe there is a way to do it in .NET code, but I had one chance to look into a couple of years ago, and about one day to play around with it, and while my code executed, it did not execute under the admin credentials like I was trying to make it, so we used a different solution.

 

This migh tgive me an excuse to look into it again. 😉

Dave O.                                                                  Sig-Logos32.png
Message 13 of 14
btillman
in reply to: chiefbraincloud

Yes, that's the idea. I plan on having the user click a menu choice which will startapp a separate .NET exe. And you're also right the AutoCAD will not be running with the proper credentials to handle the file save. Thus the need for a stand-alone EXE.

 

For example, I have a custom menu in all the users' setup now and one of the choices is to enter a new drawing into an Access database. The users enjoy this very much because it controls the environment and they can't hack up the database. It launches a stand-alone exe which is a form they use and it allows the get the next drawing sequence number as well as enter data about their new drawing. And they never have to run Access at all. It works slick.

 

I'm attempting the same thing here. Again on the surface it looks whack-o but there really is a method to the madness. I will post back later when I get to my home office. Thanks again, I really appreciate all the tips and advice.

 

Message 14 of 14
andrewpuller3811
in reply to: btillman

Check out the Autodesk developer Network page here http://usa.autodesk.com/adsk/servlet/index?siteID=123112&id=1911627

 

It has links to the 2013 dot net training labs and other 2013 specific documentation.



If a post provides a fix for your issue, click on "Accept as Solution" to help other users find solutions to problems they might have that are similar to yours.

Andrew Puller
Maitland, NSW, Australia
Windows 10 Enterprise 64bit
Intel core i7 11800 @ 2.30 GHz with 32GB Ram
Civil 3d 2021

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report

”Boost