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

Export layer data from AutoCAD to Excel

17 REPLIES 17
Reply
Message 1 of 18
texas1992
18953 Views, 17 Replies

Export layer data from AutoCAD to Excel

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!

17 REPLIES 17
Message 2 of 18
Alfred.NESWADBA
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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 3 of 18

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
Message 4 of 18

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.

Message 5 of 18
Alfred.NESWADBA
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 😉 ) 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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 6 of 18

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
Message 7 of 18
jeff
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
Message 8 of 18
texas1992
in reply to: Alfred.NESWADBA

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

Message 9 of 18
texas1992
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.

Message 10 of 18
Alfred.NESWADBA
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 ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 11 of 18
texas1992
in reply to: Alfred.NESWADBA

Thanks! It was so obvious but I just couldn't see it.

Message 12 of 18

 
Message 13 of 18

Hi,

 

...is that a question or an answer or how should we understand your emtpy message?

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 14 of 18

How would I do this (export layer data) in AutoCAD Mech 2014?  So far I have been unable to extract/export this information.  I was able to do it precisely as you have discussed (on earlier versions) but for some reason it will not work in 2014.

 

Any help you can provide would be greatly appreciated.

 

 

Message 15 of 18

Hi,

 

>> I was able to do it precisely as you have discussed (on earlier versions) but for some reason it will not work in 2014.

Show your code, at least these line where you get errors, the code which worked in 2014 and now creates errors.

 

Also let us know which exception you get or what you mean in detail with "I have been unable to extract/export this information"

 

- alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 16 of 18

The first part of your reply: No code, no error messages.

Before I simply selected all of the layer information from the Layer table that appears when executing the "LA" shortcut.  I would then "Ctrl V" into a blank excel spreadsheet.

 

The second part of your reply: This time Nothing happens.  Nothing appears in the spread sheet.  The information I have been trying to extract is the layer information from the layer box that appears on the screen when I execute the "LA" shortcut.

 

I hope I have clarified the issue.  And again thanks for any help you can provide.

Message 17 of 18

Hi,

 

>> No code, no error messages.

Then you landed in the wrong forum, here we discuss development of AutoCAD applications and tools using .NET based languages.

You should then ask in the AutoCAD forum (>>>click<<<) or Mechanical forum (>>>cllick<<<).

 

Good luck, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)
Message 18 of 18
aritradey27
in reply to: texas1992

Hi did u get macro for the above problem. as i am still in search


@texas1992 wrote:

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!


 

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