.NET

.NET

Reply
Contributor
tdunn3
Posts: 18
Registered: ‎09-13-2007
Message 1 of 11 (429 Views)
Accepted Solution

Crashing when exporting large amount of points to csv vb.net

429 Views, 10 Replies
08-08-2012 06:47 AM

Trying to export points to a csv with the object data that is attached to the point. I can use this and it works great for smaller projects, but crashes every time when I try to export a larg amount of points. The most it has successfully exported was 9000 points but on average it usually will get to 3000 points before i get a fatal error and everything crashes. 

 

 

 

Dim CSV_FileName As String = Nothing


' navigate to the point text csv file using a file dialog
Dim dlg As New System.Windows.Forms.SaveFileDialog()
dlg.InitialDirectory = "C:\"
dlg.Filter = "csv excel files (*.csv)|*.csv|All files (*.*)|*.*"

If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then

CSV_FileName = dlg.FileName().ToString
End If

Dim oPoints As Autodesk.AECC.Interop.Land.AeccPoints
Dim sFilename As String
'Dim iCount As Integer

oPoints = oAeccDB.Points

sFilename = CSV_FileName

Dim inWriter As New StreamWriter(sFilename)
Dim pid, desc As String
Dim x, y, z As Double

Dim oPoint As Autodesk.AECC.Interop.Land.AeccPoint

Dim cnt As Long = oPoints.Count
Dim expline(cnt) As String
Dim n As Long
For n = 0 To cnt - 1

oPoint = oPoints.Item(n)
pid = oPoint.Number
x = oPoint.Easting
y = oPoint.Northing
z = oPoint.Elevation
desc = oPoint.RawDescription

Dim id As ObjectId = PointEntity.FromAcadObject(oPoint)


Dim tables As Tables
tables = HostMapApplicationServices.Application.ActiveProject.ODTables
Dim records As Records = tables.GetObjectRecords(Convert.ToUInt32(0), id, Constants.OpenMode.OpenForRead, False)


' Iterate through all records
Dim record As Record = records.Item(0)

 

'For Each record In records
Dim msg(12) As String

' Get the table
Dim table As ObjectData.Table = tables(record.TableName)


Dim str As String = Nothing

 

' Get record info
Dim i As Integer
Dim upbound As Integer = Record.Count - 1
For i = 2 To upbound - 1
Dim val As MapValue = Record(i)

Str = val.StrValue
msg(i - 1) = str
Next i
inWriter.WriteLine(pid & "," & y & "," & x & "," & z & "," & desc & "," & msg(1) & "," & msg(2) & "," & msg(3) & "," & msg(4) & "," & msg(5) & "," & msg(6) & "," & msg(7) & "," & msg(8) & "," & msg(9) & "," & msg(10) & "," & msg(11))
' Next



Next n
inWriter.Flush()
inWriter.Dispose()

End Sub

Try create StringBulder and populate it with text lines

then write in in the csv file:

 

sw.Write (sb.ToString());

 

Iow, do it separatelly

 

~'J'~

 

I just imagine how it would be, coz I haven't have Civil3d on my end

See code

 

 <CommandMethod("exporter")> _
Public Sub ShowObjectDataInfo()
Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
Dim oAcadApp As AcadApplication = Application.AcadApplication
Try
If oAcadApp Is Nothing Then
oAcadApp = GetObject(, "AutoCAD.Application")
End If
Catch ex As System.Exception
ed.WriteMessage(ex.Message)
End Try
oAeccApp = oAcadApp.GetInterfaceObject("AeccXUiLand.AeccApplication.9.0")
oAeccDoc = oAeccApp.ActiveDocument
oAeccDB = oAeccApp.ActiveDocument.Database
Dim CSV_FileName As String = Nothing
Try
' navigate to the point text csv file using a file dialog
Dim dlg As New System.Windows.Forms.SaveFileDialog()
dlg.InitialDirectory = "C:\"
dlg.RestoreDirectory = True
dlg.Filter = "csv excel files (*.csv)|*.csv|All files (*.*)|*.*"
If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
CSV_FileName = dlg.FileName().ToString
End If
Dim oPoints As Autodesk.AECC.Interop.Land.AeccPoints
Dim sFilename As String
Dim iCount As Integer
oPoints = oAeccDB.Points
sFilename = CSV_FileName
Dim builder As New StringBuilder
Dim desc As String
Dim pid As Integer
Dim x, y, z As Double
For Each oPoint In oAeccDB.Points
pid = oPoint.Number
x = oPoint.Easting
y = oPoint.Northing
z = oPoint.Elevation
desc = oPoint.RawDescription
Dim id As ObjectId = PointEntity.FromAcadObject(oPoint)
Dim tables As Tables
tables = HostMapApplicationServices.Application.ActiveProject.ODTables
Dim records As Records = tables.GetObjectRecords(Convert.ToUInt32(0), id, Constants.OpenMode.OpenForRead, False)
' Iterate through all records
Dim record As Record = records.Item(0)
For Each record In records
Dim msg(12) As String
' Get the table
Dim table As ObjectData.Table = tables(record.TableName)
Dim str As String = Nothing
' Get record info
Dim i As Integer
Dim upbound As Integer = record.Count - 1
For i = 2 To upbound - 1
Dim val As MapValue = record(i)
str = val.StrValue
msg(i - 1) = str()
Next i
Dim rec As String = String.Format("{0}" + vbTab + "{1}" + vbTab + "{2}" + vbTab + "{3}" + vbTab + _
"{4}" + vbTab + "{5}" + vbTab + "{6}" + vbTab + "{7}" + vbTab + _
"{8}" + vbTab + "{9}" + vbTab + "{10}" + vbTab + "{11}" + vbTab + _
"{12}" + vbTab + "{13}" + vbTab + "{14}" + vbTab + "{15}",
pid, y, x, z, desc, msg(1), msg(2), msg(3), msg(4), msg(5), msg(6),
msg(7), msg(8), msg(9), msg(10), msg(11))
builder.AppendLine(rec)
Next
Using inWriter As New StreamWriter(sFilename)
inWriter.WriteLine(builder.ToString)
inWriter.Flush()
End Using
GC.Collect()
Catch ex As System.Exception
MsgBox(ex.Message + vbLf + ex.StackTrace)
Finally
End Try
End Sub

 

 

~'J'~

 

Valued Mentor
fieldguy
Posts: 382
Registered: ‎03-31-2005
Message 2 of 11 (419 Views)

Re: Crashing when exporting large amount of points to csv vb.net

08-08-2012 08:34 AM in reply to: tdunn3

If autocad wants to send a crash report, you can use the "View files" option and view the ".dmp" file.  Scroll down to the section called "appdata".  You might see some more info on where/why the crash occurs. 

 

If the problem occurs writing data to your csv file, you could try stringbuilder 1st.  Dump all of your point data into a stringbuilder, and then stream that to file.  You could also create large chunks with stringbuilder and write the chunks to file.

 

This is just a guess - I think it is obvious your code is OK.

Contributor
tdunn3
Posts: 18
Registered: ‎09-13-2007
Message 3 of 11 (401 Views)

Re: Crashing when exporting large amount of points to csv vb.net

08-08-2012 01:55 PM in reply to: fieldguy

do not know how to read the .dmp files. I tried using the stringbuilder and it did speed up the export but it still crashes with large amount of numbers. I did a little more debugging and found that most of the time it crashed when it gets to the  line:

 

oPoints = oAeccDB.Points

 

I tried removing that line and replacing my first for statement with 

 

For Each oPoint In oAeccDB.Points

 

then it crashed at that line. Anymore suggestions?

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 4 of 11 (396 Views)

Re: Crashing when exporting large amount of points to csv vb.net

08-08-2012 02:10 PM in reply to: tdunn3

Try create StringBulder and populate it with text lines

then write in in the csv file:

 

sw.Write (sb.ToString());

 

Iow, do it separatelly

 

~'J'~

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Valued Mentor
fieldguy
Posts: 382
Registered: ‎03-31-2005
Message 5 of 11 (384 Views)

Re: Crashing when exporting large amount of points to csv vb.net

08-08-2012 02:50 PM in reply to: tdunn3

You can view the dmp file with the button at the bottom - I think it says "View file contents".  Select the dmp file, then click on the view button.  It's been awhile since I have seen a crash report, but everything you need is on that dialog.

 

I am a map 3d user and have no experience with other verticals like Land Desktop.  Sorry!

Contributor
tdunn3
Posts: 18
Registered: ‎09-13-2007
Message 6 of 11 (380 Views)

Re: Crashing when exporting large amount of points to csv vb.net

08-08-2012 03:43 PM in reply to: fieldguy

Thanks for your help finally got it to work with the stringbuilder. Here is my final code that worked.

 

<CommandMethod("exporter")> _
Public Sub ShowObjectDataInfo()

 

Try
If oAcadApp Is Nothing Then
oAcadApp = GetObject(, "AutoCAD.Application")
End If
Catch ex As System.Exception
ed.WriteMessage(ex.Message)
End Try

oAeccApp = oAcadApp.GetInterfaceObject("AeccXUiLand.AeccApplication.9.0")
oAeccDoc = oAeccApp.ActiveDocument
oAeccDB = oAeccApp.ActiveDocument.Database

Dim CSV_FileName As String = Nothing


' navigate to the point text csv file using a file dialog
Dim dlg As New System.Windows.Forms.SaveFileDialog()
dlg.InitialDirectory = "C:\"
dlg.Filter = "csv excel files (*.csv)|*.csv|All files (*.*)|*.*"

If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then

CSV_FileName = dlg.FileName().ToString
End If

Dim oPoints As Autodesk.AECC.Interop.Land.AeccPoints
Dim sFilename As String
'Dim iCount As Integer

oPoints = oAeccDB.Points

sFilename = CSV_FileName
Dim builder As New StringBuilder
Dim inWriter As New StreamWriter(sFilename)
Dim desc As String
Dim pid As Integer
Dim x, y, z As Double

Dim oPoint As Autodesk.AECC.Interop.Land.AeccPoint = Nothing


For Each oPoint In oAeccDB.Points
pid = oPoint.Number
x = oPoint.Easting
y = oPoint.Northing
z = oPoint.Elevation
desc = oPoint.RawDescription

Dim id As ObjectId = PointEntity.FromAcadObject(oPoint)


Dim tables As Tables
tables = HostMapApplicationServices.Application.ActiveProject.ODTables
Dim records As Records = tables.GetObjectRecords(Convert.ToUInt32(0), id, Constants.OpenMode.OpenForRead, False)

 

' Iterate through all records
Dim record As Record = records.Item(0)

 

'For Each record In records
Dim msg(12) As String

' Get the table
Dim table As ObjectData.Table = tables(record.TableName)


Dim str As String = Nothing

 

' Get record info
Dim i As Integer
Dim upbound As Integer = record.Count - 1
For i = 2 To upbound - 1
Dim val As MapValue = record(i)

str = val.StrValue
msg(i - 1) = str
Next i

builder.Append(pid & "," & y & "," & x & "," & z & "," & desc & "," & msg(1) & "," & msg(2) & "," & msg(3) & "," & msg(4) & "," & msg(5) & "," & msg(6) & "," & msg(7) & "," & msg(8) & "," & msg(9) & "," & msg(10) & "," & msg(11)).AppendLine()

Next
Dim s As String = builder.ToString
inWriter.WriteLine(s)
inWriter.Flush()
inWriter.Dispose()
builder.Clear()
GC.Collect()


End Sub

Contributor
tdunn3
Posts: 18
Registered: ‎09-13-2007
Message 7 of 11 (377 Views)

Re: Crashing when exporting large amount of points to csv vb.net

08-08-2012 04:04 PM in reply to: tdunn3

Just noticed another bug. The drawing closes out ok but when I try to close out of Civil3d after running my command it crashes. Is there something that I am not closing or disposing of properly?

*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 8 of 11 (360 Views)

Re: Crashing when exporting large amount of points to csv vb.net

08-08-2012 11:18 PM in reply to: tdunn3

I recommend you to use in your code the following syntax

 

- Use Try ... Catch .. finally code block, it allow you to find

the problem points of code easily

 

- use Using.. End Using for all non-resident objects, e.g.:

 

using sw as SreamWriter= ....

sw.Write(...)

end using '' no need to close or dispose

 

same way for StringBuilder, i that case you don't have to Dispose

both of them as well

 

~'J'~

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
*Expert Elite*
Hallex
Posts: 1,569
Registered: ‎10-08-2008
Message 9 of 11 (356 Views)

Re: Crashing when exporting large amount of points to csv vb.net

08-09-2012 01:00 AM in reply to: tdunn3

I just imagine how it would be, coz I haven't have Civil3d on my end

See code

 

    <CommandMethod("exporter")> _
    Public Sub ShowObjectDataInfo()
        Dim ed As Editor = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor
        Dim oAcadApp As AcadApplication = Application.AcadApplication

        Try
            If oAcadApp Is Nothing Then
                oAcadApp = GetObject(, "AutoCAD.Application")
            End If
        Catch ex As System.Exception
            ed.WriteMessage(ex.Message)
        End Try

        oAeccApp = oAcadApp.GetInterfaceObject("AeccXUiLand.AeccApplication.9.0")
        oAeccDoc = oAeccApp.ActiveDocument
        oAeccDB = oAeccApp.ActiveDocument.Database

        Dim CSV_FileName As String = Nothing

        Try
            ' navigate to the point text csv file using a file dialog
            Dim dlg As New System.Windows.Forms.SaveFileDialog()
            dlg.InitialDirectory = "C:\"
            dlg.RestoreDirectory = True
            dlg.Filter = "csv excel files (*.csv)|*.csv|All files (*.*)|*.*"

            If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then

                CSV_FileName = dlg.FileName().ToString
            End If

            Dim oPoints As Autodesk.AECC.Interop.Land.AeccPoints
            Dim sFilename As String
            Dim iCount As Integer

            oPoints = oAeccDB.Points

            sFilename = CSV_FileName
            Dim builder As New StringBuilder

            Dim desc As String
            Dim pid As Integer
            Dim x, y, z As Double


            For Each oPoint In oAeccDB.Points

                pid = oPoint.Number
                x = oPoint.Easting
                y = oPoint.Northing
                z = oPoint.Elevation
                desc = oPoint.RawDescription

                Dim id As ObjectId = PointEntity.FromAcadObject(oPoint)


                Dim tables As Tables
                tables = HostMapApplicationServices.Application.ActiveProject.ODTables
                Dim records As Records = tables.GetObjectRecords(Convert.ToUInt32(0), id, Constants.OpenMode.OpenForRead, False)



                ' Iterate through all records
                Dim record As Record = records.Item(0)



                For Each record In records
                    Dim msg(12) As String

                    ' Get the table
                    Dim table As ObjectData.Table = tables(record.TableName)


                    Dim str As String = Nothing



                    ' Get record info
                    Dim i As Integer
                    Dim upbound As Integer = record.Count - 1
                    For i = 2 To upbound - 1
                        Dim val As MapValue = record(i)

                        str = val.StrValue
                        msg(i - 1) = str()
                    Next i

                    Dim rec As String = String.Format("{0}" + vbTab + "{1}" + vbTab + "{2}" + vbTab + "{3}" + vbTab + _
                                                      "{4}" + vbTab + "{5}" + vbTab + "{6}" + vbTab + "{7}" + vbTab + _
                                                      "{8}" + vbTab + "{9}" + vbTab + "{10}" + vbTab + "{11}" + vbTab + _
                                                      "{12}" + vbTab + "{13}" + vbTab + "{14}" + vbTab + "{15}",
                                                      pid, y, x, z, desc, msg(1), msg(2), msg(3), msg(4), msg(5), msg(6),
                                                      msg(7), msg(8), msg(9), msg(10), msg(11))
                    builder.AppendLine(rec)

                Next

                Using inWriter As New StreamWriter(sFilename)
                    inWriter.WriteLine(builder.ToString)
                    inWriter.Flush()
                End Using
                GC.Collect()

        Catch ex As System.Exception
            MsgBox(ex.Message + vbLf + ex.StackTrace)
        Finally

        End Try

    End Sub

 

 

~'J'~

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Contributor
tdunn3
Posts: 18
Registered: ‎09-13-2007
Message 10 of 11 (325 Views)

Re: Crashing when exporting large amount of points to csv vb.net

08-09-2012 10:30 AM in reply to: Hallex

That works great thanks for the code! 

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.