Delete Specific Sketch Constraint Using VBA

Delete Specific Sketch Constraint Using VBA

danijel.radenkovic
Collaborator Collaborator
1,431 Views
4 Replies
Message 1 of 5

Delete Specific Sketch Constraint Using VBA

danijel.radenkovic
Collaborator
Collaborator

Hello Everyone,

I am searching over the intenet but I have not found yet a way to delete an specific constraint (for example coincident) between two lines.

About lines:

oLine1 = xySketch.SketchLines.Item(1)

oLine2 = xySketch.SketchLines.Item(2)

and their coincident relation is:

oLine2.StartSketchPoint.Merge oLine1.EndSketchPoint

 

I want to break this relation between lines, remove or delete. Every other constraints and relation in the sketch I must keep.

I have found a way to delete all constraints or delete constraints by type but this is not a solution.

 

 

Best regards

Danijel

Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes
Accepted solutions (1)
1,432 Views
4 Replies
Replies (4)
Message 2 of 5

danijel.radenkovic
Collaborator
Collaborator

Just trying to check again. Maybe someone can help or give me advice?

 

Regards

Danijel

Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes
Message 3 of 5

danijel.radenkovic
Collaborator
Collaborator

One more time.

Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes
Message 4 of 5

ekinsb
Alumni
Alumni
Accepted solution

How about this?

 

Public Sub BreakLines()
    Dim partDoc As PartDocument
    Set partDoc = ThisApplication.ActiveDocument
    
    Dim sk As PlanarSketch
    Set sk = partDoc.ComponentDefinition.Sketches.Item(1)
    
    Dim line1 As SketchLine
    Dim line2 As SketchLine
    Set line1 = ThisApplication.CommandManager.Pick(kSketchCurveLinearFilter, "Pick line 1")
    Set line2 = ThisApplication.CommandManager.Pick(kSketchCurveLinearFilter, "Pick line 2")
    
    ' Find the coincident constraint from each line that is to the same point.
    Dim constraint As Object
    For Each constraint In line1.Constraints
        If TypeOf constraint Is CoincidentConstraint Then
            Dim coinc1 As CoincidentConstraint
            Set coinc1 = constraint
            
            ' Find the point that's connected to line1 by this constraint.
            Dim pnt1 As SketchPoint
            If coinc1.EntityOne Is line1 Then
                Set pnt1 = coinc1.EntityTwo
            Else
                Set pnt1 = coinc1.EntityOne
            End If
                        
            ' Iterate through the constraints on the second line to find the one that shares this point.
            Dim constraint2 As Object
            For Each constraint2 In line2.Constraints
                If TypeOf constraint2 Is CoincidentConstraint Then
                    Dim coinc2 As CoincidentConstraint
                    Set coinc2 = constraint2
                    
                    Dim pnt2 As SketchPoint
                    If coinc2.EntityOne Is line2 Then
                        Set pnt2 = coinc2.EntityTwo
                    Else
                        Set pnt2 = coinc2.EntityOne
                    End If
                    
                    If pnt1 Is pnt2 Then
                        If coinc1.Deletable Then
                            coinc1.Delete
                            Exit Sub
                        ElseIf coinc2.Deletable Then
                            coinc2.Delete
                            Exit Sub
                        End If
                    End If
                End If
            Next
        End If
    Next
End Sub

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 5 of 5

danijel.radenkovic
Collaborator
Collaborator

Mr. Ekins you are the best.

It works PERFECT!

Thank you very much again! Smiley Happy

 

I would like to solve one more doubt.

By the way, I wrote one month ago an topic about how to get information about an selected sketch line.

For example, some of my projects are not created using VBA, but for me it will be very usefull if I make some modification using VBA.

While I was creating a sketches manualy I didn't tought about sketching order so now If I want to modify some line I need acsess on line (for example line is 2nd,3rd created or in vba item(1), item(2), item(3)). To get the information about the line I made some function or constraint on the line and then change number i in item(i) and watch in lines behavior after debug. It can be very hard to find the right line. For me it will be the best if I could know the i in msgbox or debug.print of selected line.

 

I have got help from BrendonBG and I tried to apply but I have not solved yet.

Here is a link:

https://forums.autodesk.com/t5/inventor-customization/find-specific-line-in-part-2d-sketch-using-vba...

 

Thank you in advance.

 

Regards

Danijel

 

 

 

Best regards

Danijel

Inventor 2018/Windows 10 x64
If this information was helpful, please consider marking it as an Accepted Solution by using the Accept as Solution. Kudos are also gladly accepted.
0 Likes