I am trying to add a coincident constraint to two existing lines using the code below but am getting the following error
Full code below
Public Shared Sub CoincidentConst(ByVal line1 As Entity, ByVal line2 As Entity)
Dim db As Database = Application.DocumentManager.MdiActiveDocument.Database
Dim tm As Autodesk.AutoCAD.DatabaseServices.TransactionManager = db.TransactionManager
'Dim line1 As Line, line2 As Line
Using myT As Transaction = tm.StartTransaction()
Dim bt As BlockTable = DirectCast(myT.GetObject(db.BlockTableId, OpenMode.ForRead, False), BlockTable)
Dim btr As BlockTableRecord = DirectCast(myT.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForRead, False), BlockTableRecord)
'line1 = New Line(New Point3d(18, 2, 0), New Point3d(12, 2, 0))
'line2 = New Line(New Point3d(2, 15, 0), New Point3d(7, 9, 0))
'btr.AppendEntity(line1)
'myT.AddNewlyCreatedDBObject(line1, True)
'btr.AppendEntity(line2)
'myT.AddNewlyCreatedDBObject(line2, True)
Dim subentEdgePath1 As FullSubentityPath
Dim subentEdgePath2 As FullSubentityPath
Dim subentPath1 As FullSubentityPath
Dim subentPath2 As FullSubentityPath
Using entity As Entity = DirectCast(myT.GetObject(line1.ObjectId, OpenMode.ForRead, False), Entity)
If entity Is Nothing Then
MsgBox("cannot get entity1")
Return
End If
Dim subentityIdPE As AssocPersSubentityIdPE
Dim peCls As RXClass = AssocPersSubentityIdPE.GetClass(GetType(AssocPersSubentityIdPE))
Dim pSubentityIdPE As IntPtr = entity.QueryX(peCls)
If pSubentityIdPE = IntPtr.Zero Then
MsgBox("cannot get pSubentityIdPE1")
Return
End If
subentityIdPE = TryCast(AssocPersSubentityIdPE.Create(pSubentityIdPE, False), AssocPersSubentityIdPE)
If subentityIdPE Is Nothing Then
MsgBox("cannot get subentityIdPE1")
Return
End If
Dim edgeSubentityIds As SubentityId() = Nothing
edgeSubentityIds = subentityIdPE.GetAllSubentities(entity, SubentityType.Vertex)
Dim startSID As SubentityId = SubentityId.Null, endSID As SubentityId = SubentityId.Null
Dim other As SubentityId() = Nothing
subentityIdPE.GetEdgeVertexSubentities(entity, edgeSubentityIds(0), startSID, endSID, other)
'subentityIdPE.GetVertexSubentityGeometry(entity, edgeSubentityIds(0))
subentEdgePath1 = New FullSubentityPath(New ObjectId(0) {line1.ObjectId}, edgeSubentityIds(0))
subentPath1 = New FullSubentityPath(New ObjectId(0) {line1.ObjectId}, startSID)
End Using
Using entity As Entity = DirectCast(myT.GetObject(line2.ObjectId, OpenMode.ForRead, False), Entity)
If entity Is Nothing Then
MsgBox("cannot get entity2")
Return
End If
Dim subentityIdPE As AssocPersSubentityIdPE
Dim peCls As RXClass = AssocPersSubentityIdPE.GetClass(GetType(AssocPersSubentityIdPE))
Dim pSubentityIdPE As IntPtr = entity.QueryX(peCls)
If pSubentityIdPE = IntPtr.Zero Then
MsgBox("cannot get pSubentityIdPE2")
Return
End If
subentityIdPE = TryCast(AssocPersSubentityIdPE.Create(pSubentityIdPE, False), AssocPersSubentityIdPE)
If subentityIdPE Is Nothing Then
MsgBox("cannot get subentityIdPE2")
Return
End If
Dim edgeSubentityIds As SubentityId() = Nothing
edgeSubentityIds = subentityIdPE.GetAllSubentities(entity, SubentityType.Edge)
Dim startSID As SubentityId = SubentityId.Null, endSID As SubentityId = SubentityId.Null
Dim other As SubentityId() = Nothing
subentityIdPE.GetEdgeVertexSubentities(entity, edgeSubentityIds(0), startSID, endSID, other)
'subentityIdPE.GetVertexSubentityGeometry(entity, edgeSubentityIds(0))
subentEdgePath2 = New FullSubentityPath(New ObjectId(0) {line2.ObjectId}, edgeSubentityIds(0))
subentPath2 = New FullSubentityPath(New ObjectId(0) {line2.ObjectId}, startSID)
End Using
Dim consGrpId As ObjectId = GetConstraintGroup(True)
Dim consGeomEdge1 As ConstrainedGeometry = Nothing
Dim consGeomEdge2 As ConstrainedGeometry = Nothing
Using constGrp As Assoc2dConstraintGroup = DirectCast(myT.GetObject(consGrpId, OpenMode.ForWrite, False), Assoc2dConstraintGroup)
consGeomEdge1 = constGrp.AddConstrainedGeometry(subentEdgePath1)
consGeomEdge2 = constGrp.AddConstrainedGeometry(subentEdgePath2)
Dim paths As FullSubentityPath() = New FullSubentityPath(1) {subentPath1, subentPath2}
Dim newConstraint As GeometricalConstraint = constGrp.AddGeometricalConstraint(GeometricalConstraint.ConstraintType.Coincident, paths)
End Using
Dim temp As [String] = ""
Dim networkId As ObjectId = AssocNetwork.GetInstanceFromDatabase(db, True, temp)
Using network As AssocNetwork = DirectCast(myT.GetObject(networkId, OpenMode.ForWrite, False), AssocNetwork)
Dim callBack As AssocEvaluationCallback = Nothing
network.Evaluate(callBack)
End Using
myT.Commit()
End Using
End Sub