1 Reply Latest reply on Dec 1, 2015 4:47 PM by Joe L

    How can I check if a sketch exists with a certain name then delete it if it does?

    Joe L

      I'm fairly new to this programming. 

      I currently have a program i wrote that creates a new sketch with a spline from excel then names that sketch.

       

      I've been attempting for a while to have a sub that searches the features/sketches already in the part for the name that will be used for the new sketch and if it exists, delete it.  I have been unsuccessful in either searching or deleting the feature.

       

      I do not have any current lines to show as they never seem to do what I'm looking for.

       

      I've checked the delete feature example

      2012 SOLIDWORKS API Help - Delete Selected Feature Example (VBA)

      but the program does not seem to work

       

      I've also tried

      2012 SOLIDWORKS API Help - Delete a Smart Feature Example (VBA)

      but also no luck

       

      If anyone has any suggestions, the help would be appreciated.

       

      My major hurdles are searching/selecting a feature by name and also what to call to delete that feature.

        • Re: How can I check if a sketch exists with a certain name then delete it if it does?
          Joe L

          I found a quick way to do this, thank you for checking this thread

           

          Dim swApp As Object

          Dim Part As Object

          Dim boolstatus As Boolean

          Dim longstatus As Long, longwarnings As Long

          Dim nameString  As String

           

          Sub main()

           

          nameString = "String name of sketch"

           

          Set swApp = CreateObject("SldWorks.Application")

          Set Part = swApp.ActiveDoc

          Part.ClearSelection2 True

           

          boolstatus = Part.Extension.SelectByID2(nameString, "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

          boolstatus = Part.Extension.SelectByID2(nameString, "SKETCH", 0, 0, 0, False, 0, Nothing, 0)

          Part.EditDelete

          End Sub