16 Replies Latest reply on Aug 14, 2015 12:42 PM by Deepak Gupta

    Macro to delete dangling centermarks

    David Dewey

      I am running a macro that currently deletes dangling dimensions and notes.

       

      However, we heavily use centermarks and centerlines here. I was wondering if there was a way to find those two elements and determine if they are dangling as well?

       

      I can share the code I found here if that would be helpful.

        • Re: Macro to delete dangling centermarks
          Keith Rice

          Please post a simple drawing with such a "dangling centermark".

           

          Keith

          SolidWorks API Training

          • Re: Macro to delete dangling centermarks
            Deepak Gupta

            Yes, you can loop thru the drawing and find the centermarks and centerlines, check whether there are dangling OR not, and then delete them.

             

            I think you're using this macro Re: Delete dangling dimensions from Adam Hoffman

             

            If yes then you need to edit these line:

             

            If swAnno.GetType = 4 Then

             

            Change 4 to 13 for centermarks and 15 for centerlines

             

            bSelect = swModel.Extension.SelectByID2(sItemName + "@" + CStr(swView.Name), "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

            Change DIMENSION to CENTERMARKSYM for centermarks and CENTERLINE for centerlines

              • Re: Macro to delete dangling centermarks
                David Dewey

                Hello Deepak,

                Thank you for the quick response. This does work and I am sure I can get it to work to delete centermarks, centerlines as well as dangling dimensions, However what would be the most efficient way to structure this code? If then else statements to include centermarks and centerlines in the main if then for searching for type?

                 

                ' ****************************************************************************** '

                '                            Clean Up Drawing Macro                              '

                ' ****************************************************************************** '

                 

                 

                'Macro will traverse through all drawing views

                'and delete dangling dimensions and notes

                 

                'Written by Adam Hoffman

                'Was written using SolidWorks 2014 SP 2.0

                'This macro is provided as is and the creator is not responsible for any mishaps

                 

                 

                Dim swApp As SldWorks.SldWorks

                Dim swModel As SldWorks.ModelDoc2

                Dim swDraw As SldWorks.DrawingDoc

                Dim swSelMgr As SldWorks.SelectionMgr

                Dim swView As SldWorks.View

                Dim swAnno As SldWorks.Annotation

                Dim sAnnoType As String

                Dim swNote As SldWorks.Note

                 

                 

                Dim bSelect As Boolean

                Dim bDeleteFlag As Boolean

                Dim sItemName As String

                Const swDrawingDoc = 3

                Const swAssemblyDoc = 2

                Const swPartDoc = 1

                 

                 

                Sub CleanUpMain()

                Set swApp = Application.SldWorks

                Set swModel = swApp.ActiveDoc

                'If the active document is not a drawing exit program

                If Not swModel.GetType() = swDrawingDoc Then

                   MsgBox ("You Must Run This Macro Inside Of A Drawing!")

                   Exit Sub

                  

                ElseIf swModel.GetType() = swDrawingDoc Then

                   Set swDraw = swModel

                  

                End If

                 

                 

                swModel.ClearSelection2 (True) 'Make sure all selections are cleared before you start

                bDeleteFlag = False

                Set swView = swDraw.GetFirstView 'This is the overall drawing itself

                Set swView = swView.GetNextView 'This is the actual first view in the drawing

                 

                 

                'Start the loop through views in the drawing

                Do While Not swView Is Nothing

                   'Start the dangling Note removal

                   Set swNote = swView.GetFirstNote

                  

                   Do While Not swNote Is Nothing

                  

                      sItemName = ""

                      sItemName = swNote.GetName()

                     

                      Dim sNoteText As String

                      sNoteText = ""

                     

                      sNoteText = swNote.GetText()

                     

                      'I found when dealing with notes this was more reliable

                      'than using the swAnno.IsDangling method

                      If sNoteText = "?" Or sNoteText = "" Then

                  

                         bSelect = swModel.Extension.SelectByID2(sItemName + "@" + CStr(swView.Name), "NOTE", 0, 0, 0, True, 0, Nothing, 0)

                        

                         bDeleteFlag = True

                        

                      End If

                  

                      Set swNote = swNote.GetNext

                  

                   Loop

                  

                   If bDeleteFlag = True Then

                  

                      'After going through all the notes in this view delete the ones that have been selected

                      boolstatus = swModel.Extension.DeleteSelection2(2)

                     

                   End If

                  

                   'End of dangling Note removal

                  

                   swModel.ClearSelection2 (True)

                   bDeleteFlag = False

                   boolstatus = False

                     

                   'Start the dangling dimension removal

                   Set swAnno = swView.GetFirstAnnotation3()

                  

                   Do While Not swAnno Is Nothing

                    

                     If swAnno.GetType = 4 Then

                       

                         sItemName = ""

                        

                         If swAnno.IsDangling Then

                            sItemName = swAnno.GetName()

                        

                            bSelect = swModel.Extension.SelectByID2(sItemName + "@" + CStr(swView.Name), "DIMENSION", 0, 0, 0, True, 0, Nothing, 0)

                            bDeleteFlag = True

                        

                         End If

                        

                      End If

                     

                      Set swAnno = swAnno.GetNext3()

                     

                   Loop

                  

                   If bDeleteFlag = True Then

                  

                      'After going through all the dimensions in this view delete the ones that have been selected

                      boolstatus = swModel.Extension.DeleteSelection2(2)

                     

                   End If

                  

                   'End of dangling dimension removal

                 

                   Set swView = swView.GetNextView

                Loop 'End of loop through drawing views and overall program

                 

                 

                End Sub

                 

              • Re: Macro to delete dangling centermarks
                David Dewey

                Thank you Deepak. I just saw this now. I will contact our VAR. In the meantime, do you know what we would need to add dangling ordinate dimensions? I've poked around a bit but haven't been able to see how to get them selected.