.NET

Reply
Contributor
ranjith0326
Posts: 24
Registered: ‎04-16-2011
Message 1 of 7 (406 Views)

Reading data from autocad table

406 Views, 6 Replies
10-06-2011 04:48 AM

Greetings to all,

 

Here i have one question...

Is that possible to read the Autocad Table data , without opening the Autocad from VB.NET.

 

I tried with opening one autocad then loading the source dll. it was working perfectly.

but i need to without opening a application its possible or not?

 

Thanks in advance.

Regards,

R.Ranjith
ADN Support Specialist
Posts: 349
Registered: ‎05-22-2006
Message 2 of 7 (395 Views)

Re: Reading data from autocad table

10-06-2011 09:01 AM in reply to: ranjith0326

You could do this if you licensed RealDWG (www.autodesk.com/realdwg). But the public (free) AutoCAD APIs require you have AutoCAD installed on your computer (and running).

Cheers,

Stephen Preston
Autodesk Developer Network
*Expert Elite*
chiefbraincloud
Posts: 750
Registered: ‎02-13-2008
Message 3 of 7 (385 Views)

Re: Reading data from autocad table

10-06-2011 11:13 AM in reply to: ranjith0326

In addition to Stephens comment, you could use a COM application which would require you to open AutoCAD, but you could open it Invisibly, so the user doesn't see it open.  That's as close as you can get, without using RealDWG.

Dave O.                                                                  Sig-Logos32.png
Contributor
ranjith0326
Posts: 24
Registered: ‎04-16-2011
Message 4 of 7 (369 Views)

Re: Reading data from autocad table

10-07-2011 12:49 AM in reply to: ranjith0326

Thank u so much,

 

Cheers,

Ranjith

Regards,

R.Ranjith
Mentor
amitk_189
Posts: 223
Registered: ‎12-15-2011
Message 5 of 7 (287 Views)

Re: Reading data from autocad table

07-24-2012 10:56 PM in reply to: ranjith0326

Hey Ranjith, did you get any success in doing so. Because i am also looking for some similar kind of solution. Can you post it here if you have any

ADN Support Specialist
Virupaksha.aithal
Posts: 85
Registered: ‎06-15-2011
Message 6 of 7 (248 Views)

Re: Reading data from autocad table

08-01-2012 12:38 AM in reply to: amitk_189

Hi amit,

 

What kind of solution you are looking for? Can you please explain?

 

If you want to start AutoCAD from a external application using ActiveX API, then refer http://through-the-interface.typepad.com/through_the_interface/2007/12/launching-autoc.html & http://through-the-interface.typepad.com/through_the_interface/2010/02/handling-com-calls-rejected-b...



Virupaksha Aithal KM
Developer Technical Services
Autodesk Developer Network

*Expert Elite*
Hallex
Posts: 1,558
Registered: ‎10-08-2008
Message 7 of 7 (243 Views)

Re: Reading data from autocad table

08-01-2012 03:52 AM in reply to: amitk_189

If you're still using COM, so try this code,

not tested seriously, just the same logic

as I'm posted before,

change file name you'll want to open

 

    Public Shared Sub ReadTableFromDwg()
        Dim acadver As String = "18" '//AutoCAD Version
        ' set your file name to read data here
        Dim fname As String = "C:\Test\mtext.dwg"
        ' define csv file name variable
        Dim csvname As String = String.Empty

        Dim listData As New List(Of Object)
        ' get current culture
        Dim oldCult As System.Globalization.CultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture
        ' create new culture
        Dim thisCult As System.Globalization.CultureInfo = New System.Globalization.CultureInfo("en-US")
        ' set current culture to newly created
        System.Threading.Thread.CurrentThread.CurrentCulture = thisCult

        MsgBox("Wait...")

        Dim appProgID As String = "Autocad.Application" + "." + acadver

        ' get reference on interface IDispatch
        Dim AcadType As Type = Type.GetTypeFromProgID(appProgID)

        ' launch AutoCAD
        Dim AcadApp As Object = Activator.CreateInstance(AcadType)

        Dim visargs() As Object = New Object(0) {}

        ' set visibility mode to false
        visargs(0) = True

        ' Make application visible
        AcadApp.GetType().InvokeMember("Visible", BindingFlags.SetProperty, Nothing, AcadApp, visargs, Nothing)

        ' Maximize window
        ''AcadApp.GetType().InvokeMember("WindowState", BindingFlags.SetProperty, Nothing, AcadApp, New Object() {1}, Nothing)

        Dim AcadDocs As Object = AcadApp.GetType().InvokeMember("Documents", BindingFlags.GetProperty, Nothing, AcadApp, Nothing)

        ' define arguments to open file
        Dim args() As Object = New Object(1) {}

        args(0) = fname
        ' set read only mode to false
        args(1) = False
        ' try open document
        Dim AcDoc As Object = AcadDocs.GetType().InvokeMember("Open", BindingFlags.InvokeMethod, Nothing, AcadDocs, args, Nothing)

        Dim AcUtil As Object = New Object

        Try

            ' get reference on active document
            AcDoc = AcadApp.GetType().InvokeMember("ActiveDocument", BindingFlags.GetProperty, Nothing, AcadApp, Nothing, Nothing)
            ' get reference on ModelSpace
            Dim AcSpace As Object = AcDoc.GetType().InvokeMember("ModelSpace", BindingFlags.GetProperty, Nothing, AcDoc, Nothing)
            ' get reference of  AcadUtility
            AcUtil = AcDoc.GetType().InvokeMember("Utility", BindingFlags.GetProperty Or BindingFlags.Public, Nothing, AcDoc, Nothing)

            Dim acTable As New Object

            '        '_____________________________________________

            '        ' based on example written by Tony Tanzillo
            '        '_____________________________________________

            ' The array of arguments that will be passed
            ' to the AcadUtil.GetEntity() method:
            Dim entargs As Object() = New Object(2) {}

            ' In order to get VT_BYREF  arguments to be
            ' filled in by the callee, we must use ParameterModifier
            ' to retrieve the arguments:

            ' The callee will replace the ParameterModifier
            ' in the args[] argument array with the results.

            entargs(0) = New Object
            entargs(1) = New Object
            entargs(2) = vbLf + "! Select Table !"
            ' We also need to tell the marshaler that
            ' both parameters in the COM method call are
            ' 'out' or 'byref' parameters:
            ' 2 = the total number of arguments passed:
            Dim pm As New ParameterModifier(3)

            pm(0) = True
            ' first argument is ByRef
            pm(1) = True
            ' second argument is ByRef
            pm(1) = False
            ' third argument is ByVal
            Dim modifiers As ParameterModifier() = New ParameterModifier() {pm}

            ' Invoke the method, passing the arguments, and
            ' the parameter modifers

            AcUtil.GetType().InvokeMember("GetEntity", BindingFlags.InvokeMethod, Nothing, AcUtil, entargs, modifiers, Nothing, Nothing)

            ' The results are now in the entargs[] array:
            Dim entity As Object = DirectCast(entargs(0), Object)
            Dim pickpt As Object = DirectCast(entargs(1), Object)
         


            Dim objName As String = entity.GetType.InvokeMember("ObjectName", BindingFlags.GetProperty Or BindingFlags.IgnoreCase, Nothing, entity, Nothing).ToString()
            ' make sure the table object selected
            If objName <> "AcDbTable" Then

                MessageBox.Show("Selectet is not type of table, exit program ...")

                Return

            End If

            acTable = entity

            Dim tableRows As Object = acTable.GetType().InvokeMember("Rows", BindingFlags.GetProperty Or BindingFlags.Public, Nothing, acTable, Nothing)

            Dim tableColumns As Object = acTable.GetType().InvokeMember("Columns", BindingFlags.GetProperty Or BindingFlags.Public, Nothing, acTable, Nothing)


            Dim tableContent As New List(Of List(Of String))

            For i As Integer = 0 To tableRows - 1

                Dim tableLine As New List(Of String)

                For j As Integer = 0 To tableColumns - 1

                    Dim cellText As Object = acTable.GetType().InvokeMember("GetText", BindingFlags.InvokeMethod Or BindingFlags.Public, Nothing, acTable, New Object() {i, j})

                    tableLine.Add(cellText.ToString())

                Next

                tableContent.Add(tableLine)

            Next
            ''-------------------------------------------------''
            '' for debug only:

            'Dim sb As New StringBuilder
            'For Each line As List(Of String) In tableContent
            '    Dim textline As String = ""
            '    For Each st As String In line
            '        textline = textline + st + vbTab
            '    Next
            '    sb.AppendLine(textline.TrimEnd(vbTab))
            'Next
            'MessageBox.Show(sb.ToString())

            ''-------------------------------------------------''

            Dim initDir As String = AcDoc.GetType.InvokeMember("Path", BindingFlags.GetProperty, Nothing, AcDoc, Nothing, Nothing).ToString()

            Dim saveFileDialog As New System.Windows.Forms.SaveFileDialog

            saveFileDialog.Title = "Enter a file name to write data: "

            saveFileDialog.InitialDirectory = initDir

            saveFileDialog.RestoreDirectory = True

            saveFileDialog.Filter = "CSV files | *.csv"

            saveFileDialog.FileName = "File name without extension"

            Dim result As System.Windows.Forms.DialogResult = saveFileDialog.ShowDialog()

            If result <> DialogResult.OK Then
                csvname = "Not saved"
                Return
            End If

            csvname = saveFileDialog.FileName

            ' Write data to CSV
            Using sw As New StreamWriter(csvname, True, Encoding.ASCII)

                For Each line As List(Of String) In tableContent
                    Dim textline As String = ""
                    For Each st As String In line
                        textline = textline + st + vbTab
                    Next
                    sw.WriteLine(textline.TrimEnd(vbTab))
                Next
            End Using
            ''----------------------------------------------------------------------------------------------------''
            'save document
            Dim closeargs() As Object = New Object(1) {}
            closeargs(0) = True
            ' with the same name
            closeargs(1) = fname
            ' Try close document
            ' simplified syntax
            AcDoc.GetType().InvokeMember("Close", BindingFlags.InvokeMethod, Nothing, AcDoc, closeargs)

        Catch ex As System.Exception

            MessageBox.Show("Error: " & ex.Message & vbLf & "Trace: " & ex.StackTrace)

        Finally

            ' Try quit application
            AcadApp.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, Nothing, AcadApp, Nothing)
            ' clean up the memory
            '--------------------'
            ' release Document.
            releaseObject(AcDoc)
            ' release Documents.
            releaseObject(AcadDocs)
            ' release Application.
            releaseObject(AcadApp)
            ' call garbage cleaner immediatelly
            GC.WaitForPendingFinalizers()
            GC.GetTotalMemory(True)
            GC.WaitForPendingFinalizers()
            GC.GetTotalMemory(True)
            ' restore current culture
            System.Threading.Thread.CurrentThread.CurrentUICulture = oldCult
            'Display result
            MessageBox.Show("Csv file saved as:" + vbLf + csvname)
        End Try

    End Sub

 

~'J'~

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919

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
Welcome to the new Autodesk Community!
If this is your first visit, click here to get started and make the most of the Community. Let us know what you think of the new experience in the Community Feedback Forum.

Need installation help?

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

Ask the Community