Reading data from autocad table

Reading data from autocad table

Anonymous
Not applicable
2,744 Views
10 Replies
Message 1 of 11

Reading data from autocad table

Anonymous
Not applicable

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.

0 Likes
2,745 Views
10 Replies
Replies (10)
Message 2 of 11

StephenPreston
Alumni
Alumni

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
0 Likes
Message 3 of 11

chiefbraincloud
Collaborator
Collaborator

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
0 Likes
Message 4 of 11

Anonymous
Not applicable

Thank u so much,

 

Cheers,

Ranjith

0 Likes
Message 5 of 11

amitnkukanur
Collaborator
Collaborator

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

Senior Software Engineer
0 Likes
Message 6 of 11

Virupaksha_aithal
Autodesk Support
Autodesk Support

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

0 Likes
Message 7 of 11

Hallex
Advisor
Advisor

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
Message 8 of 11

dudde.giridhar
Enthusiast
Enthusiast

Hi,

I have seen your posts so for.

 

I am facing a problem in reading the table from autocad document.

Can you share your code.

 

That could be helpful.

 

Thanks and regards,

D.Giridhar

0 Likes
Message 9 of 11

amitnkukanur
Collaborator
Collaborator

Hi,

 

I don't have the code and going by concept.

Reading table is straight forward approach 

Senior Software Engineer
0 Likes
Message 11 of 11

dudde.giridhar
Enthusiast
Enthusiast

Dear  hosneyalaa,

Thanks for the replay.

I will go through that and come back.

 

Thanks,

D.Giridhar.

0 Likes