Page 1 of 1

Macro idea

Posted: Mon Jun 03, 2024 5:59 am
by Monstrum Mathias
A macro that collects all dangling relations and dimensions in a sketch and deletes them, would be a neat button to have. Often there are multiple dangling relations in a sketch and for some reason newer versions of SW tend to hide them from view. This function exists in the display/delete relations viewer, but as a macro it could save a lot of clicks in the long run. Anyone have something like this lying around?

Re: Macro idea

Posted: Mon Jun 03, 2024 11:42 am
by josh
Works on the currently active sketch, or the selected sketch if no sketch is active.

A word of caution, if you delete relations using the UI, then exit the sketch and tell it to "discard changes", the relations are not permanently deleted. Relations deleted using this macro cannot be un-deleted by exiting the sketch with the red "X".

Code: Select all

Sub main()
    Dim swApp As SldWorks.SldWorks
    Dim swDoc As SldWorks.ModelDoc2
    Dim swRelMgr As SldWorks.SketchRelationManager
    Dim swSketch As SldWorks.Sketch
    Dim vRels As Variant
    Dim swRel As SldWorks.SketchRelation
    Dim i As Long
    
    Set swApp = Application.SldWorks
    Set swDoc = swApp.ActiveDoc
    Set swSkMgr = swDoc.SketchManager
    Set swSketch = swDoc.GetActiveSketch2
    If swSketch Is Nothing Then
        If swDoc.SelectionManager.GetSelectedObjectType3(1, -1) = swSelSKETCHES Then
            Set swSketch = swDoc.SelectionManager.GetSelectedObject6(1, -1).GetSpecificFeature
        Else
            MsgBox "No active or selected sketch"
            Exit Sub
        End If
    End If
    Set swRelMgr = swSketch.RelationManager
    If swRelMgr.GetRelationsCount(swDangling) > 0 Then
        vRels = swRelMgr.GetRelations(swDangling)
        For i = 0 To UBound(vRels)
            Set swRel = vRels(i)
            swRelMgr.DeleteRelation swRel
        Next i
        MsgBox UBound(vRels) + 1 & " dangling relations deleted from " & swSketch.Name
    Else
        MsgBox "No dangling relations were found in " & swSketch.Name
    End If
End Sub

Re: Macro idea

Posted: Mon Jun 03, 2024 11:51 am
by Stefan Sterk
josh wrote: Mon Jun 03, 2024 11:42 am ...
Relations deleted using this macro cannot be un-deleted by exiting the sketch with the red "X".
...
Not sure, but couldn't this be fixed by using the following StartRecordingUndoObject Method (IModelDocExtension) - 2024 - SOLIDWORKS API Help?

Re: Macro idea

Posted: Mon Jun 03, 2024 12:24 pm
by josh
Doesn't seem to do anything. I haven't used that before. Based on the help, I think I've added it per the proper syntax, but no entry shows up in the Undo stack, Undo doesn't work, and exiting the sketch with "Discard changes" also doesn't bring them back.
image.png

Re: Macro idea

Posted: Tue Jun 04, 2024 3:38 am
by Monstrum Mathias
Thanks Josh. Works like a charm!