.NET

Reply
*Expert Elite*
Alfred.NESWADBA
Posts: 9,607
Registered: ‎06-29-2007
Message 11 of 16 (252 Views)

Re: Extract Bill of Materials

07-16-2012 09:20 AM in reply to: amitk_189

Hi,

 

OK, now I understand (at least I hope so).

I did show you in my demo-project how you can access entities in modelspace using COM, so please try to get the text-objects by yourself based on what I showed you on how to loop through the modelspace, just don't use an ObjectID.

 

- alfred -

 

-------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at
-------------------------------------------------------------------------
Mentor
amitk_189
Posts: 242
Registered: ‎12-15-2011
Message 12 of 16 (251 Views)

Re: Extract Bill of Materials

07-16-2012 09:35 AM in reply to: Alfred.NESWADBA

Ok Thanks for Guiding me through out this project. I will try to get text objects and avoid objectID.

 

Probably if you find any ideas of solutions keep me posted on this.

 

 

With Warm Regards

Amit

Software Engineer

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 13 of 16 (242 Views)

Re: Extract Bill of Materials

07-16-2012 11:41 AM in reply to: amitk_189

This will get you started

       <System.Security.SuppressUnmanagedCodeSecurity()> _
        Public Shared Sub ReadTexts()
            ' Set application version ( I use 17 for A2009, 18 is for A2010)
            Dim acadver As String = "17"
            ' Set your file name to read  data here
            Dim fname As String = "C:\Test\tags.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 one
            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)

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

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

            ' Set visibility mode to true
            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() {3}, 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 true
            args(1) = False
            ' Try open document
            Dim AcDoc As Object = AcadDocs.GetType.InvokeMember("Open", BindingFlags.InvokeMethod, Nothing, AcadDocs, args, Nothing)
            ''--------------------------------------------------''
            'Try
            '    '' Set Try Catch block to avoid crash application
            '    '' if any items does not exists :
            '    Dim AcSuminfo As Object = AcDoc.GetType.InvokeMember("SummaryInfo", BindingFlags.GetProperty, Nothing, AcDoc, Nothing)
            '    Dim Author As Object = AcSuminfo.GetType.InvokeMember("Author", BindingFlags.GetProperty, Nothing, AcSuminfo, Nothing)
            '    Dim Comments As Object = AcSuminfo.GetType.InvokeMember("Comments", BindingFlags.GetProperty, Nothing, AcSuminfo, Nothing)
            '    Dim HyperlinkBase As Object = AcSuminfo.GetType.InvokeMember("HyperlinkBase", BindingFlags.GetProperty, Nothing, AcSuminfo, Nothing)
            '    Dim Keywords As Object = AcSuminfo.GetType.InvokeMember("Keywords", BindingFlags.GetProperty, Nothing, AcSuminfo, Nothing)
            '    Dim LastSavedBy As Object = AcSuminfo.GetType.InvokeMember("LastSavedBy", BindingFlags.GetProperty, Nothing, AcSuminfo, Nothing)
            '    Dim RevisionNumber As Object = AcSuminfo.GetType.InvokeMember("RevisionNumber", BindingFlags.GetProperty, Nothing, AcSuminfo, Nothing)
            '    Dim Subject As Object = AcSuminfo.GetType.InvokeMember("Subject", BindingFlags.GetProperty, Nothing, AcSuminfo, Nothing)
            '    Dim Title As Object = AcSuminfo.GetType.InvokeMember("Title", BindingFlags.GetProperty, Nothing, AcSuminfo, Nothing)
            '    ' Add and display standard properties

            '    MsgBox("The standard drawing properties are " & vbLf & _
            '           "Author = " & Author.ToString() & vbLf & _
            '           "Comments = " & Comments.ToString() & vbLf & _
            '           "HyperlinkBase = " & HyperlinkBase.ToString() & vbLf & _
            '           "Keywords = " & Keywords.ToString() & vbLf & _
            '           "LastSavedBy = " & LastSavedBy.ToString() & vbLf & _
            '           "RevisionNumber = " & RevisionNumber.ToString() & vbLf & _
            '           "Subject = " & Subject.ToString() & vbLf & _
            '           "Title = " & Title.ToString())
            'Catch

            'End Try
            ''--------------------------------------------------''

            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 on Utility 
                AcUtil = AcDoc.GetType.InvokeMember("Utility", BindingFlags.GetProperty, Nothing, AcDoc, Nothing, Nothing)
                ' get reference on SelectionSets
                Dim AcSsets As Object = AcDoc.GetType.InvokeMember("SelectionSets", BindingFlags.GetProperty, Nothing, AcDoc, Nothing, Nothing)
                ' Perform zooming
                AcadApp.GetType.InvokeMember("ZoomExtents", BindingFlags.InvokeMethod, Nothing, AcadApp, Nothing, Nothing)
                ' Perform zooming
                AcadApp.GetType.InvokeMember("ZoomScaled", BindingFlags.InvokeMethod, Nothing, AcadApp, New Object() {0.8, 1})
                ' Add prompt to the command line
                AcUtil.GetType.InvokeMember("Prompt", BindingFlags.InvokeMethod, Nothing, AcUtil, New Object() {vbLf & "  >>  SELECT TEXTS ON SCREEN >> "})
                ' Add SelectionSet, named "$SelectionTest$"
                AcSsets.GetType.InvokeMember("Add", BindingFlags.InvokeMethod, Nothing, AcSsets, New Object() {"$SelectionTest$"})
                ' Get reference on newly created Selection Set
                Dim AcSset As Object = AcSsets.GetType.InvokeMember("Item", BindingFlags.InvokeMethod Or BindingFlags.GetProperty, Nothing, AcSsets, New Object() {"$SelectionTest$"})
                ' Create filter to use attributed blocks
                Dim selargs As Object() = New Object(1) {}

                Dim ftype As Short() = New Short() {0}

                Dim fdata As Object() = New Object() {"*TEXT"}

                selargs(0) = DirectCast(ftype, Object)

                selargs(1) = DirectCast(fdata, Object)

                ' Perform selection on screen
                AcSset.GetType.InvokeMember("SelectOnScreen", BindingFlags.InvokeMethod, Nothing, AcSset, selargs)
                ' Get number of selected items
                Dim cnt As Integer = CInt(AcSset.GetType.InvokeMember("Count", BindingFlags.GetProperty, Nothing, AcSset, Nothing))
                ' Add prompt to the command line
                AcUtil.GetType.InvokeMember("Prompt", BindingFlags.InvokeMethod, Nothing, AcUtil, New Object() {vbLf & "--> Selected Mtexts -->" & vbTab & cnt.ToString()})

                ' Loop through selected objects
                For n As Integer = 0 To cnt - 1
                    ' Get single item from selection
                    Dim AcSelItem As Object = AcSset.GetType.InvokeMember("Item", BindingFlags.InvokeMethod, Nothing, AcSset, New Object() {n})
                    ' Get Object name
                    Dim objName As String = AcSelItem.GetType.InvokeMember("ObjectName", BindingFlags.GetProperty, Nothing, AcSelItem, Nothing).ToString()
                    Dim textStr As String = String.Empty
                    If objName = "AcDbText" Then
                        textStr = AcSelItem.GetType.InvokeMember("TextString", BindingFlags.GetProperty, Nothing, AcSelItem, Nothing).ToString()
                    ElseIf objName = "AcDbMText" Then
                        textStr = AcSelItem.GetType.InvokeMember("TextString", BindingFlags.GetProperty, Nothing, AcSelItem, Nothing).ToString()
                    End If

                    listData.Add(textStr)
                Next

                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
                Using sw As New StreamWriter(csvname, True, Encoding.ASCII)

                    For Each strline As String In listData
                        sw.WriteLine(strline)
                    Next

                    sw.Flush()

                    sw.Close()

                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)


                ' Try quit application
                AcadApp.GetType().InvokeMember("Quit", BindingFlags.InvokeMethod, Nothing, AcadApp, Nothing)

            Catch ex As System.Exception

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

            Finally
                ' clean up the memory
                '--------------------'
                ' release Utility.
                releaseObject(AcUtil)
                ' 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
                MsgBox("Csv file saved as : " + csvname)
            End Try

        End Sub


        Public Shared Sub releaseObject(ByVal obj As Object)
            Try
                System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj)
                obj = Nothing
            Catch ex As System.Exception
                obj = Nothing
            Finally
                GC.Collect()
            End Try
        End Sub

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Mentor
amitk_189
Posts: 242
Registered: ‎12-15-2011
Message 14 of 16 (230 Views)

Re: Extract Bill of Materials

07-16-2012 07:05 PM in reply to: Alfred.NESWADBA

Yes, It Worked. Thanks a Lot Hallex. Thankyou very Much Hallex for Supporting Me. Thanks a Lot.

Thanks Alfred Neswadba for Guiding me. Thanks a Lot

 

Thank You

 

With Warm Regards

Amit

Software Engineer

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 15 of 16 (221 Views)

Re: Extract Bill of Materials

07-16-2012 09:17 PM in reply to: amitk_189

Glad to help

cheers  :smileyhappy:

 

~'J'~

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Mentor
amitk_189
Posts: 242
Registered: ‎12-15-2011
Message 16 of 16 (192 Views)

Re: Extract Bill of Materials

07-24-2012 03:21 AM in reply to: Hallex

Is it possible to use select All option instead of selecting/prompting user to select on screen but hide autocad (donot display)

 

Post to the Community

Have questions about Autodesk products? Ask the community.

New Post
Announcements
Do you have 60 seconds to spare? The Autodesk Community Team is revamping our site ranking system and we want your feedback! Please click here to launch the 5 question survey. As always your input is greatly appreciated.