Okay so what am I'm doing wrong.
Dim CompPt As New Point3d(Easting, Northing, Elevation) 'Easting, Northing, Elevation declared as Double
MsgBox(CompPt.X) 'Valid Message
MsgBox(CompPt.Y) 'Valid Message
MsgBox(CompPt.Z) 'Valid Message
Dim i As Integer = CogoPoints.Count
MsgBox(i) 'Yes that's how many points i.e. confirms link to collection.
Dim pointId As ObjectId = CogoPoints.Add(CompPt) ' ***** CRASH!!! *******
Any help whatsoever would be appreciated.
Thanks in advance.
Civil 3D 2013 SP1
Win7
Solved! Go to Solution.
Solved by Jeff_M. Go to Solution.
Just as a quick test, I tried this very simple code snip (sorry, c#) but it works fine in 2013 SP1. So my guess is there is something else in your code affecting this.
[CommandMethod("TestAddPt")] public void testaddpt() { CivilDocument doc = CivilApplication.ActiveDocument; AcDb.ObjectId ptId = doc.CogoPoints.Add(new Autodesk.AutoCAD.Geometry.Point3d(0.0, 10.0, 5.0)); }
Well I don't believe it's my code.
I tried both of the following and after MsgBox(i) of "2" (two points in my test.dwg) AutoCAD crashes.
I added MsgBox("Test") to confirm it wasn't an error further down the road.
MsgBox(i)
Try
CogoPoints.Add(CompPt)
Catch ex As Exception
MsgBox(ex.Message)
End Try
MsgBox("Test")
MsgBox(i)
Try
CogoPoints.Add(CompPt)
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox(ex.Message)
End Try
MsgBox("Test")
Would definitely be easier to diagnose the problems if we saw more of the code. I am guessing you are running a loop of some kind to add some points to your drawing.
Private Sub Button_COMP_Click(sender As Object, e As EventArgs) Handles Button_COMP.Click
Dim m_doc As CivilDocument = Nothing
Dim m_trans As Transaction = Nothing
Dim m_Database As Database = Nothing
Dim m_Editor As Editor = Nothing
Dim docCol As Autodesk.AutoCAD.ApplicationServices.DocumentCollection = Application.DocumentManager
m_Database = docCol.MdiActiveDocument.Database
m_Editor = docCol.MdiActiveDocument.Editor
m_doc = Autodesk.Civil.ApplicationServices.CivilApplication.ActiveDocument()
Dim CogoPoints As CogoPointCollection = m_doc.CogoPoints
'Confirmed as valid link to CogoPointCollection
Dim oAlignments As ObjectIdCollection
oAlignments = m_doc.GetAlignmentIds
Dim b As Boolean
b = False
Dim s As String
Dim oAlignment As Alignment = Nothing 'Alignment used for comps.
Dim oProfile As Profile = Nothing 'Profile for 3D comps (see b2D_Comps)
Dim b2D_Comps As Boolean = False
Dim dSta As Double
Dim sRawSta As String
Dim Northing As Double
Dim Easting As Double
Dim Elevation As Double
Dim dOffset As Double 'Offset for point calculations i.e. Alignment + Hub Offset
If TextBox_ALIGNOFF.Text = "" Then
TextBox_ALIGNOFF.Text = "0.00"
End If
If TextBox_HUBOFF.Text = "" Then
TextBox_HUBOFF.Text = "0.00"
End If
dOffset = Double.Parse(TextBox_ALIGNOFF.Text) + Double.Parse(TextBox_HUBOFF.Text)
Dim dElevMod As Double
If TextBox_ELEV_MOD.Text = "" Then
TextBox_ELEV_MOD.Text = "0.00"
End If
dElevMod = Double.Parse(TextBox_ELEV_MOD.Text)
m_trans = m_Database.TransactionManager.StartTransaction()
'Set oAlignment
For Each ObjId As ObjectId In oAlignments
oAlignment = m_trans.GetObject(ObjId, OpenMode.ForRead)
If oAlignment.Name = ComboBox_ALIGN.Text Then
b = True
Exit For
End If
Next
If b = False Then
MsgBox("Alignment not defined.", MsgBoxStyle.Exclamation)
Exit Sub
End If
'Set oProfile or b2D_Comps
If ComboBox_Profiles.Text = "" Then
s = "Profile NOT Set." + vbCrLf + "Points are 2D?"
Dim Answer As MsgBoxResult = MsgBox(s, MsgBoxStyle.YesNo)
If Answer = MsgBoxResult.Yes Then
b2D_Comps = True
Else
Exit Sub
End If
Else
b = False
Dim oProfiles As ObjectIdCollection
oProfiles = oAlignment.GetProfileIds
For Each ObjIdPro As ObjectId In oProfiles
oProfile = m_trans.GetObject(ObjIdPro, OpenMode.ForRead)
If oProfile.Name = ComboBox_Profiles.Text Then
b = True
Exit For
End If
Next
End If
'Left side comps
If RadioButton_L.Checked = True Or RadioButton_B.Checked = True Then
Dim dOffsetL As Double
dOffsetL = dOffset * (-1)
For Each row In DataGridView_STAOFF.Rows
dSta = Double.Parse(row.Cells(0).Value)
sRawSta = row.Cells(1).Value
If RadioButton_B.Checked = True Then
sRawSta = sRawSta.Replace("L_R", "L")
End If
oAlignment.PointLocation(dSta, dOffsetL, Easting, Northing)
If b2D_Comps = True Then
Elevation = 0
Else
Elevation = oProfile.ElevationAt(dSta) + dElevMod
End If
Dim CompPt As New Point3d(Easting, Northing, Elevation)
'MsgBox(CompPt.X)
'MsgBox(CompPt.Y)
'MsgBox(CompPt.Z)
Dim i As Integer = CogoPoints.Count
MsgBox(i) '************************************************CRASHES AFTER THIS*************************
Try
CogoPoints.Add(CompPt)
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox(ex.Message)
End Try
MsgBox("Test")
'Dim cogoPoint As CogoPoint = TryCast(pointId.GetObject(OpenMode.ForWrite), CogoPoint)
'cogoPoint.RawDescription = sRawSta
Next
End If 'Left side comps
Transactions must be disposed. THe best way to handle them is to place them in a Using block:
Using m_trans As Transaction ....your code m_trans.Commit() End Using
However, I'm not so sure that is the issue with your code. I changed the sample Iposted to add 10 points and it does so without issue, so there must be something occurring when you code is execting that you aren't seeing. If you could provide a working solution (so I don't have to recreate your form) and a test drawing, I would be willing to help you track it down.
Okay, I modified the WinForm so you don't have to select a alignment or profile if you use the drawing included.
On the right hand size:
Enter a zero in the RichTextBox, press the Add Stations button and then press the COMPUTE POINTS button.
Okay, I can't attach a 647KB zip file.
OK, not sure why I didn't think of this before. When manipulating the drawing's database from inside a Modeless dialog you must Lock the Document. Again, this is easiest done with a Using block. I removed the m_trans DIm from above then modified the code like so and it works without crashing:
Using doclock As DocumentLock = docCol.MdiActiveDocument.LockDocument() Using m_trans As Transaction = m_Database.TransactionManager.StartTransaction() 'Set oAlignment For Each ObjId As ObjectId In oAlignments oAlignment = m_trans.GetObject(ObjId, OpenMode.ForRead) If oAlignment.Name = ComboBox_ALIGN.Text Then b = True Exit For End If Next If b = False Then MsgBox("Alignment not defined.", MsgBoxStyle.Exclamation) Exit Sub End If 'Set oProfile or b2D_Comps If ComboBox_Profiles.Text = "" Then s = "Profile NOT Set." + vbCrLf + "Points are 2D?" Dim Answer As MsgBoxResult = MsgBox(s, MsgBoxStyle.YesNo) If Answer = MsgBoxResult.Yes Then b2D_Comps = True Else Exit Sub End If Else b = False Dim oProfiles As ObjectIdCollection oProfiles = oAlignment.GetProfileIds For Each ObjIdPro As ObjectId In oProfiles oProfile = m_trans.GetObject(ObjIdPro, OpenMode.ForRead) If oProfile.Name = ComboBox_Profiles.Text Then b = True Exit For End If Next End If 'Left side comps If RadioButton_L.Checked = True Or RadioButton_B.Checked = True Then Dim dOffsetL As Double dOffsetL = dOffset * (-1) For Each row In DataGridView_STAOFF.Rows dSta = Double.Parse(row.Cells(0).Value) sRawSta = row.Cells(1).Value If RadioButton_B.Checked = True Then sRawSta = sRawSta.Replace("L_R", "L") End If oAlignment.PointLocation(dSta, dOffsetL, Easting, Northing) If b2D_Comps = True Then Elevation = 0 Else Elevation = oProfile.ElevationAt(dSta) + dElevMod End If Dim CompPt As New Point3d(Easting, Northing, Elevation) 'CompPt is valid! 'MsgBox(CompPt.X) 'MsgBox(CompPt.Y) 'MsgBox(CompPt.Z) Dim i As Integer = CogoPoints.Count MsgBox(i) 'Dim pointId As ObjectId = cgPoints.Add(CompPt, sRawSta) 'Dim pointId As ObjectId = CogoPoints.Add(CompPt) '**************************Doesn't like it! Try CogoPoints.Add(CompPt) Catch ex As System.Exception MsgBox(ex.Message) End Try MsgBox("Test") 'Dim cogoPoint As CogoPoint = TryCast(pointId.GetObject(OpenMode.ForWrite), CogoPoint) 'cogoPoint.RawDescription = sRawSta Next End If 'Left side comps m_trans.Commit() End Using ''disposes Transaction End Using ''disposes DocumentLock