3 Replies Latest reply on Dec 12, 2017 4:41 PM by Alex Burnett

    Macro to delete a note

    Frédérik Drouin

      I created a macro that create a note at a position in a drawing and then save this drawing as a PDF in the same folder, but now i want to delete this note after the save as PDF and i don't know how to delete it without selected it. Does anyone know how to do that ? Here is my code :

       

      Dim swApp As Object

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim longstatus As Long, longwarnings As Long

       

       

      Sub main()

       

       

      Set swApp = Application.SldWorks

      Set swModel = swApp.ActiveDoc

        

                  If (swModel Is Nothing) Then

              MsgBox "No active file found in SolidWorks." & Chr(13) & Chr(13) _

                  & "Please load/activate a SolidWorks Drawing and TRY again!", vbExclamation

                  End

                 End If

        

                  If (swModel.GetType <> swDocDRAWING) Then

              MsgBox "Current file is not a SolidWorks Drawing." & Chr(13) & Chr(13) _

                  & "Please Open/Activate a SolidWorks Drawing and TRY again!", vbExclamation

                  End

                 End If

       

       

      Set Part = swApp.ActiveDoc

      Dim myModelView As Object

      Set myModelView = Part.ActiveView

      myModelView.FrameState = swWindowState_e.swWindowMaximized

       

       

      Dim myNote As Object

      Dim myAnnotation As Object

      Dim myTextFormat As Object

      Set myNote = Part.InsertNote("$PRP:""SW-Short Date(Short Date)""   TYPE YOUR TEXT HERE")

      If Not myNote Is Nothing Then

         myNote.LockPosition = False

         myNote.Angle = 0

         boolstatus = myNote.SetBalloon(0, 0)

         Set myAnnotation = myNote.GetAnnotation()

         If Not myAnnotation Is Nothing Then

            longstatus = myAnnotation.SetLeader3(swLeaderStyle_e.swNO_LEADER, 0, True, False, False, False)

            boolstatus = myAnnotation.SetPosition(1.18224818891099E-02, 7.61066835915902E-03, 0)

            boolstatus = myAnnotation.SetTextFormat(0, True, myTextFormat)

         End If

      End If

      Part.ClearSelection2 True

      Part.WindowRedraw

       

       

      Dim FilePath As String

      Dim PathSize As Long

      Dim PathNoExtention As String

      Dim NewFilePath As String

       

       

      FilePath = Part.GetPathName

      PathSize = Strings.Len(FilePath)

      PathNoExtention = Strings.Left(FilePath, PathSize - 6)

      NewFilePath = PathNoExtention & "pdf"

       

       

      Part.SaveAs2 NewFilePath, 0, True, False

       

       

       

       

      End Sub

        • Re: Macro to delete a note
          Matthew Cempa

          I suspect that you need to select the note using (I'm just typing here so no intellisense to check my work):

          dim myAnnotation as SldWorks.Annotation

          set myAnnotation = myNote.GetAnnotation

          myAnnotation.Select3 False, Nothing

          Part.Extension.DeleteSelection2 1

           

          It makes it a lot easier if you change the dim statements to the SolidWorks types rather than leaving them all as objects.  That way you get intellisense to help you with the proper arguments.  The idea above should get you going at least.

            • Re: Macro to delete a note
              Alex Burnett

              Matthew Cempa wrote:

               

              I suspect that you need to select the note using (I'm just typing here so no intellisense to check my work):

              dim myAnnotation as SldWorks.Annotation

              set myAnnotation = myNote.GetAnnotation

              myAnnotation.Select3 False, Nothing

              Part.Extension.DeleteSelection2 1

               

              It makes it a lot easier if you change the dim statements to the SolidWorks types rather than leaving them all as objects. That way you get intellisense to help you with the proper arguments. The idea above should get you going at least.

              I have a project that does something similar. I select my annotation as you show but use swDraw.EditDelete() which removes my note. I'm not sure if they work the same way or not but I thought I'd chime in for the sake of expanding on your solution.

            • Re: Macro to delete a note
              Nilesh Patel

              Hi Frederik,

               

              You can use IModelDocExtension::SelectByID2 method to select the note. You need to specify empty string at 'Name' parameter and 'Type' parameter as "NOTE' and location of the note which you have specified in the line 'boolstatus = myAnnotation.SetPosition(1.18224818891099E-02, 7.61066835915902E-03, 0)'. Then use IModelDoc2:EditDelete method to delete the note.

               

              Another approach you can use it that once you have created the note, you can set the its name using INote::SetName method. Use IModelDocExtension::SelectByID2 method to select the note. But this time specify the 'Name' parameter as the name of your note. You can specify X, Y, Z as 0 in this case. Then use IModelDoc2:EditDelete method to delete the note.

               

              Regards,

              Nilesh