.NET

Reply
Contributor
texas1992
Posts: 18
Registered: ‎09-15-2011
Message 1 of 13 (5,470 Views)

Export layer data from AutoCAD to Excel

5470 Views, 12 Replies
09-15-2011 11:36 AM

OK, I am completely new to AutoCAD so please be patient with me. I have been asked to write a module that will export layer data from AutoCAD and put it in  an Excel spreadsheet. The user wants to have this done through a macro. I am assuming that I need to create a dll that is called from inside AutoCAD but I am not sure.

 

They want to start with AutoCAD 2008 and once we get it working there we will convert it to 2011 or 2012.

 

First of all, I can not find the 2008 API and second of all I'm not sure where to start. I am very experienced at programming. We are going to use VB.NET. Any help would be greatly appreciated.

 

Thanks!

*Expert Elite*
Alfred.NESWADBA
Posts: 9,403
Registered: ‎06-29-2007
Message 2 of 13 (5,467 Views)

Betreff: Export layer data from AutoCAD to Excel

09-15-2011 11:46 AM in reply to: texas1992

Hi,

 

>> export layer data from AutoCAD and put it in  an Excel spreadsheet

Not to spent to much time, there is a way like copy&paste

  • start the layermanager
  • press <Ctrl>-<A> to select all layers
  • then <Ctrl>-<C> for copy
  • change to Excel
  • press <Ctrl>-<V> for paste)

I think that could not be done much faster.

Or is "wants to have this done through a macro" for you something like an exercise for learning?

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Distinguished Mentor
michael.robertson
Posts: 602
Registered: ‎03-18-2008
Message 3 of 13 (5,458 Views)

Betreff: Export layer data from AutoCAD to Excel

09-15-2011 12:44 PM in reply to: Alfred.NESWADBA

I agree, if you are just wanting to get the info into excel then use the copy/paste method.

 

If you want to do this as a programming experience lesson you have a number of decisions to make depending on what your current programming abilities are. Here are a few choices (maybe more but these I think are the most common)

 

ARX - C programming language

.NET - either VB or C-Sharp language

Lisp

VBA - with Autodesk saying this feature is going away I wouldn't spend time on this option

 

As far as programmatically creating excel files you have a few options:

Use COM communicating with Excel (user must have Excel installed on machine, versions can be a pain) - I know you can do this with VBA & .NET fairly easy, not sure about ARX and Lisp

 

If using .NET you can use the Microsoft OpenXML SDK to read/write Excel 2007 & later files (xlsx) directly without needing Excel installed on the machine.

 

Mike Robertson
FL. Dept. of Transportation
CADD Applications Developer
Contributor
texas1992
Posts: 18
Registered: ‎09-15-2011
Message 4 of 13 (5,454 Views)

Betreff: Export layer data from AutoCAD to Excel

09-15-2011 12:51 PM in reply to: michael.robertson

Thanks for your replies. The user wants to be able to open the file and then click a custom macro that will export the selected data to an excel spreadsheet. I want to do this using VB.NET and probably an API but I'm not sure where to get the API since I need the 2008 version.

 

Also, are there any examples around that show how all of this is done? I assume that I will extracting from a database but what query language does it use to do that?

 

Again, thanks for your help.

*Expert Elite*
Alfred.NESWADBA
Posts: 9,403
Registered: ‎06-29-2007
Message 5 of 13 (5,444 Views)

Betreff: Export layer data from AutoCAD to Excel

09-15-2011 01:09 PM in reply to: texas1992

Hi,

 

there are many tutorials showing how to start VB.NET with AutoCAD, the >>>help<<< also has some useful info.

Seeing now most tutorials for current versions of AutoCAD you should not be to much afraid having 2008 (for this function), you have to know that the internal version for libraries is 17.1 for 2008 (17.2 for 2009, 18.0 for 2010, 18.1 for 2011 and 18.2 for 2012). So if you find some version applied to statements you now know which one to use for 2008.

One good site to start is the ObjectARX-kit (free-of-charge, just to register), to find >>>here<<<, here you find the most complete documentation (sorry, don't know if "complete" is the right word :smileywink: ) and a lot of samples for dotNET also.

 

>> I assume that I will extracting from a database but what query language does it use to do that?

Don't think of DWG is compareble to database-tables, no, it's not.

To scan through the layer you have to go the following steps:

a) get the LayerTable from the DWG

b) you can then iterate through the LayerTable and get ObjectID's from Layer

c) from the ObjectID get the LayerTableRecord

d) cache or export the details for this LayerTableRecord

e) next ObjectID in LayerTable

 

HTH, - alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Distinguished Mentor
michael.robertson
Posts: 602
Registered: ‎03-18-2008
Message 6 of 13 (5,442 Views)

Betreff: Export layer data from AutoCAD to Excel

09-15-2011 01:11 PM in reply to: texas1992

First place to start is the Autocad Developer Center at

 

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

 

It contains the SDKs, training material, docs, etc.

 

Mike Robertson
FL. Dept. of Transportation
CADD Applications Developer
Valued Mentor
jeff
Posts: 337
Registered: ‎05-12-2009
Message 7 of 13 (5,440 Views)

Betreff: Export layer data from AutoCAD to Excel

09-15-2011 01:27 PM in reply to: texas1992

What you are doing is very easy and no reason to build for 2008 unless you really have to, but if so you can just reference the acdbmgd.dll & acmgd.dll in the installation folder of AutoCAD 2008.

 

 

Since you are a expirenced programer then should no problem taking the properties from a LayerTableRecord to work with a 'Interface' to output in whatever format in Excel.

 

This will get you going and just use the properties you want

 

        <CommandMethod("ExtractLayerData")> _
        Public Sub ExtractLayerData()

            Dim doc As Document = Application.DocumentManager.MdiActiveDocument
            Dim db As Database = doc.Database
            Dim ed As Editor = doc.Editor

            Using trx As Transaction = db.TransactionManager.StartTransaction()
                Dim lyrTbl As LayerTable = db.LayerTableId.GetObject(OpenMode.ForRead)

                For Each lyrId As ObjectId In lyrTbl

                    Dim lyrTblRec As LayerTableRecord = trx.GetObject(lyrId, OpenMode.ForRead)
                    ed.WriteMessage("{0}Name: {1,-20} LineWeight: {2,-15:G}",
                                    Environment.NewLine, lyrTblRec.Name, lyrTblRec.LineWeight.ToString())
                Next

                trx.Commit()
            End Using
        End Sub

 

 

 

 

 

You can also find your answers @ TheSwamp
Contributor
texas1992
Posts: 18
Registered: ‎09-15-2011
Message 8 of 13 (5,433 Views)

Betreff: Export layer data from AutoCAD to Excel

09-15-2011 01:41 PM in reply to: Alfred.NESWADBA

Thanks! I will get working on this and see if I have any more questions.

Contributor
texas1992
Posts: 18
Registered: ‎09-15-2011
Message 9 of 13 (5,373 Views)

Betreff: Export layer data from AutoCAD to Excel

09-20-2011 01:26 PM in reply to: jeff

Jeff,

 

Thank you for your code sample. I have learned a lot about how to work with AutoCAD and am now trying to work with your example code. I am having a problem with the declaration of the variable Doc as in the following line:

 

Dim doc As Document = Application.DocumentManager.MdiActiveDocument

 

Document is not a defined type. What are you including to define the type Document?

 

Here is what I have for imports:

 

Imports Autodesk.AutoCAD
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices

 

Thanks for your help.

*Expert Elite*
Alfred.NESWADBA
Posts: 9,403
Registered: ‎06-29-2007
Message 10 of 13 (5,371 Views)

Betreff: Export layer data from AutoCAD to Excel

09-20-2011 01:43 PM in reply to: texas1992

Hi,

 

>> Document is not a defined type. What are you including to define the type Document?

Imports Autodesk.AutoCAD.ApplicationServices

 

- alfred -

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Are You Going To Be @ AU 2014? Feel free to drop by our AU topic post and share your plans, plug a class that you're teaching, or simply check out who else from the community might be in attendance. Ohh and don't forgot to stop by the Autodesk Help | Learn | Collaborate booths in the Exhibit Hall and meet our community team if you get a chance!