3 Replies Latest reply on Nov 30, 2018 6:34 AM by Kevin Thompson

    Macro to insert a comment in the feature tree and rename

    Kevin Thompson

      I'm trying to write a macro that will insert a comment into the feature tree and then rename the "Comments" folder followed by renaming the newly inserted comment in the feature tree.  I've been successful in writing code to insert the comment and rename the "Comments" folder, however, I've been unsuccessful in renaming the newly inserted comment in the feature tree.  I've got some decent vb experience in Excel macros, but virtually no experience with SolidWorks macros so i'm a bit lost on what to do next.  Any help would be appreciated!  The code that I've tried follows:

      Dim swApp As Object

       

      Dim Part As Object

      Dim boolstatus As Boolean

      Dim feature As SldWorks.feature

      Dim longstatus As Long, longwarnings As Long

       

      Sub main()

       

      Set swApp = _

      Application.SldWorks

       

      Set Part = swApp.ActiveDoc

      Dim myModelView As Object

      Set myModelView = Part.ActiveView

      myModelView.FrameState = swWindowState_e.swWindowMaximized

       

      Dim myComment As Object

      Set myComment = Part.Extension.AddComment("TEST")

      boolstatus = Part.Extension.SelectByID2("Comments", "COMMENTSFOLDER", 0, 0, 0, False, 0, Nothing, 0)

      Set feature = Part.SelectionManager.GetSelectedObject6(1, 0)

      feature.Name = "Comments Folder Name"

      boolstatus = Part.Extension.SelectByID2("Comment1", "COMMENT", 0, 0, 0, False, 0, Nothing, 0)

      Set feature = Part.SelectionManager.GetSelectedObject6(1, 0)

      feature.Name = "Comment Feature Name"

       

      End Sub

       

      The comment gets inserted and the folder name gets updated to "Comments Folder Name" when i'm in break mode and run it with a break at the second 'feature.Name' command.  But when i run past the break and try to select the "Comment1" feature and change it's name, it changes the folder name again to what I want the comment feature name to be instead.  Any suggestions?

        • Re: Macro to insert a comment in the feature tree and rename
          Fifi Riri

          Hello, Try this

          Note that the comment won't be renamed, if there previously was a comment with this exact same name, even if it has been deleted. (i.e: you will need to close and re-opened the file to test the macro)

           

          Option Explicit

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swFeat As SldWorks.Feature

          Dim swCommentFolder As SldWorks.CommentFolder

          Dim swComment As SldWorks.Comment

          Sub main()

          Set swApp = Application.SldWorks

          Set swModel = swApp.ActiveDoc

          Set swFeat = swModel.FirstFeature

          Do While Not swFeat Is Nothing

              If swFeat.GetTypeName = "CommentsFolder" Then

                  Set swCommentFolder = swFeat.GetSpecificFeature2

                  swFeat.Name = "Comments Folder Name"

                  Set swComment = swCommentFolder.AddComment("Test")

                  swComment.Name = "Comment Name"

                  swModel.FeatureManager.UpdateFeatureTree

              End If

              Set swFeat = swFeat.GetNextFeature

          Loop

          End Sub

          • Re: Macro to insert a comment in the feature tree and rename
            Danniel Sims

            Try clearing the selection before you select the second item.  It may be the first item is still selected and it doesn't know what to do with two items.

             

            boolstatus = Part.Extension.SelectByID2("Comments", "COMMENTSFOLDER", 0, 0, 0, False, 0, Nothing, 0)

            Set feature = Part.SelectionManager.GetSelectedObject6(1, 0)

            feature.Name = "Comments Folder Name"

            Part.ClearSelection2 True

            boolstatus = Part.Extension.SelectByID2("Comment1", "COMMENT", 0, 0, 0, False, 0, Nothing, 0)

            Set feature = Part.SelectionManager.GetSelectedObject6(1, 0)

            feature.Name = "Comment Feature Name"