MACRO LIBRARY

Delete sketch relations and fully define sketch from origin

Free public access. No account or subscription required.

Files

Delete all sketch relations and fully define the sketch from the origin

Code

'This will delete all of the sketche relations from the sketch of a selected feature and then _
    fully define the sketch from the origin
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Option Explicit
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSelFeat As SldWorks.Feature
Dim swEntity As SldWorks.Entity
Dim swSelData As SldWorks.SelectData
Dim swSkMan As SldWorks.SketchManager
Dim swSketch As SldWorks.Sketch
Dim swSketchSeg As SldWorks.SketchSegment
Dim swSketchPt As SldWorks.SketchPoint
    
Dim vFeatures As Variant
Dim vSketchSegs As Variant
Dim vSketchPts As Variant
Dim i As Integer
Dim q As Integer
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSkMan = swModel.SketchManager
    
    'make sure that part is open
    If swModel Is Nothing Then
        swApp.SendMsgToUser2 "Please open a part document.", swMbWarning, swMbOk
        End
    End If
    If swModel.GetType <> swDocPART Then
        swApp.SendMsgToUser2 "Please open a part document.", swMbWarning, swMbOk
        End
    End If
    
    'get selected object
    Set swSelMgr = swModel.SelectionManager
    Set swSelFeat = swSelMgr.GetSelectedObject6(1, -1)
    Set swSelData = swSelMgr.CreateSelectData
    
    If swSelFeat Is Nothing Then
        swApp.SendMsgToUser2 "Could not get parents. Please select a feature.", swMbWarning, swMbOk
        End
    End If
    
    'get the parent sketch
    vFeatures = swSelFeat.GetParents
    For i = 0 To UBound(vFeatures)
        If vFeatures(i).GetTypeName2 = "ProfileFeature" Then
            'select the parent sketch and open the sketch
            Set swEntity = vFeatures(i)
            swEntity.Select4 False, swSelData
            swSkMan.InsertSketch True
            
            'cycle through all sketch segments and sketch points, deleting all relations
            Set swSketch = vFeatures(i).GetSpecificFeature2
            vSketchSegs = swSketch.GetSketchSegments
            For q = 0 To UBound(vSketchSegs)
                Set swSketchSeg = vSketchSegs(q)
                swSketchSeg.Select4 False, swSelData
                swModel.SketchConstraintsDelAll
            Next q
            vSketchPts = swSketch.GetSketchPoints2
            For q = 0 To UBound(vSketchPts)
                Set swSketchPt = vSketchPts(q)
                swSketchPt.Select4 False, swSelData
                swModel.SketchConstraintsDelAll
            Next q
            
            'fully define to origin
            swModel.Extension.SelectByID2 "", "EXTSKETCHPOINT", 0, 0, 0, False, 6, Nothing, 0
            Dim hDatumObj As Object
            Dim vDatumObj As Object
            swSkMan.FullyDefineSketch True, True, 1023, True, 1, hDatumObj, 1, vDatumObj, 1, 1
            swSkMan.InsertSketch True
        End If
    Next i
End Sub
    
'ModelDoc2::SketchConstraintsDelAll requires the sketch segment to be selected, so you must loop through all sketch _
    segs in order to delete
    
'To use all sketch relations: 1023
    
'Mark in selectbyid2 needs to be 6, though I am not sure why