Macro idea
-
- Posts: 7
- Joined: Mon Apr 15, 2024 2:03 am
- x 4
- x 2
Macro idea
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
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".
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
- Stefan Sterk
- Posts: 37
- Joined: Tue Aug 10, 2021 2:40 am
- x 51
- x 77
Re: Macro idea
Not sure, but couldn't this be fixed by using the following StartRecordingUndoObject Method (IModelDocExtension) - 2024 - SOLIDWORKS API Help?
Re: Macro idea
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.
-
- Posts: 7
- Joined: Mon Apr 15, 2024 2:03 am
- x 4
- x 2
Re: Macro idea
Thanks Josh. Works like a charm!