Macro idea

Library for macros
Monstrum Mathias
Posts: 8
Joined: Mon Apr 15, 2024 2:03 am
Answers: 0
x 5
x 3

Macro idea

Unread post 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?
User avatar
josh
Posts: 295
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 16
x 22
x 508

Re: Macro idea

Unread post 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
User avatar
Stefan Sterk
Posts: 37
Joined: Tue Aug 10, 2021 2:40 am
Answers: 3
x 51
x 77

Re: Macro idea

Unread post 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?
User avatar
josh
Posts: 295
Joined: Thu Mar 11, 2021 1:05 pm
Answers: 16
x 22
x 508

Re: Macro idea

Unread post 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
Monstrum Mathias
Posts: 8
Joined: Mon Apr 15, 2024 2:03 am
Answers: 0
x 5
x 3

Re: Macro idea

Unread post by Monstrum Mathias »

Thanks Josh. Works like a charm!
Post Reply