• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    .NET

    Reply
    Contributor
    texas1992
    Posts: 18
    Registered: ‎09-15-2011

    Export layer data from AutoCAD to Excel

    2395 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!

    Please use plain text.
    *Expert Elite*
    Posts: 6,641
    Registered: ‎06-29-2007

    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
    -------------------------------------------------------------------------
    Please use plain text.
    Valued Mentor
    Posts: 494
    Registered: ‎03-18-2008

    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

    HPxw4600 Workstation
    Core 2 Duo 2.33ghz, 8gb RAM
    nVidia Quadro FX1700
    Win7 64bit
    Please use plain text.
    Contributor
    texas1992
    Posts: 18
    Registered: ‎09-15-2011

    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.

    Please use plain text.
    *Expert Elite*
    Posts: 6,641
    Registered: ‎06-29-2007

    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
    -------------------------------------------------------------------------
    Please use plain text.
    Valued Mentor
    Posts: 494
    Registered: ‎03-18-2008

    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

    HPxw4600 Workstation
    Core 2 Duo 2.33ghz, 8gb RAM
    nVidia Quadro FX1700
    Win7 64bit
    Please use plain text.
    Mentor
    Posts: 248
    Registered: ‎05-12-2009

    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
    Please use plain text.
    Contributor
    texas1992
    Posts: 18
    Registered: ‎09-15-2011

    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.

    Please use plain text.
    Contributor
    texas1992
    Posts: 18
    Registered: ‎09-15-2011

    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.

    Please use plain text.
    *Expert Elite*
    Posts: 6,641
    Registered: ‎06-29-2007

    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
    -------------------------------------------------------------------------
    Please use plain text.