4 Replies Latest reply on Apr 19, 2017 7:58 AM by Gayrat Vlasov

    How to delete one constraint

    Gayrat Vlasov

      I'm have detail, "Sketch1" and set of constraints, what I'm add in code, like this:



      Now I'm need to find all constarints in "Sketch1" and delete one constraint "Horizontal1" (I'm don't know exact name of constraint)


      What methods I can use to delete one constraint ?


      I'm try to delete all constraint in Sketch, but this method don't work, why ?

        boolstatus = swDoc.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, False, 0, Nothing, 0)


        • Re: How to delete one constraint
          Nilesh Patel

          Hi Gayrat,


          Try following example: Get Sketch Constraints Example (VBA).


          If there is only one horizontal constraint then once you find that constraint as per the above example, use 'IModelDoc2.SketchConstraintsDel' method to delete the constraint.


          I have not tried it but it should work.



          • Re: How to delete one constraint
            Raghvendra Bhargava

            Select your sketch and try this:



            Dim swApp As SldWorks.SldWorks

            Dim Part As ModelDoc2

            Dim boolstatus As Boolean

            Dim longstatus As Long, longwarnings As Long


            Sub main()

            Set swApp = Application.SldWorks

            Set Part = swApp.ActiveDoc



            Dim swFeat As Feature

            Dim swSketch As Sketch

            Dim swSkRelMgr As SketchRelationManager

                Set swFeat = Part.SelectionManager.GetSelectedObject6(1, -1)

                Set swSketch = swFeat.GetSpecificFeature2

                Set swSkRelMgr = swSketch.RelationManager


            Dim vSkRelArr As Variant

                vSkRelArr = swSkRelMgr.GetRelations(swAll)


            If IsEmpty(vSkRelArr) = False Then

            Dim i As Integer

            Dim swSkRel  As SketchRelation

                For i = 0 To UBound(vSkRelArr)

                    Set swSkRel = vSkRelArr(i)  

                    If swSkRel.GetRelationType = 4 Then boolstatus = swSkRelMgr.DeleteRelation(swSkRel)


                Next i

            End If



            End Sub


            It will delete all Horizontal Relation in Sketch. You can create any logic to delete any specific Horizontal relation.

              • Re: How to delete one constraint
                Gayrat Vlasov

                Dear  Raghvendra Bhargava,

                Thank you very march for right answer!


                Dim swSkRel  As SketchRelation


                swSkRel  get reference to SketchRelation, but using it only possible to know only type of relation (GetRelationType )


                Is it possible to know relation name like "Vertical23" ? (for future use)


                At my case I'm need to remove all external link and I'm change filter in GetRelations(swAll) to swSkRelMgr.GetRelations(swSketchRelationFilterType_e.swExternal) and delete all external reference.