.NET
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problem adding constraint to existing entities

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
Anonymous
503 Views, 2 Replies

Problem adding constraint to existing entities

I am trying to add a coincident constraint to two existing lines using the code below but am getting the following error

Capture.PNG

 

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

 

 

 

2 REPLIES 2
Message 2 of 3
Virupaksha_aithal
in reply to: Anonymous

Hi,

 

see if code shared with http://adndevblog.typepad.com/autocad/2013/01/a-simplified-net-api-for-accessing-autocad-parameters-... helps?

 

 



Virupaksha Aithal KM
Developer Technical Services
Autodesk Developer Network

Message 3 of 3
Anonymous
in reply to: Anonymous

I figured it out after I posted. The following line of code was bad. The subentityType \was set for vertex instead of edge.

 

 edgeSubentityIds = subentityIdPE.GetAllSubentities(entity, SubentityType.Edge)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

AutoCAD Inside the Factory


Autodesk Design & Make Report