2 Replies Latest reply on Nov 16, 2012 5:18 PM by Brett Hausler

    Macro for Creating New Layers and Moving dims/annotations onto the New Layers

    Brett Hausler

      What I am trying to accomplish is to create four new layers on an existing SolidWorks drawing.  Once I have these layers I want to move all the dimensions on the sheet(s) onto one layer, tables onto another layer, other annotations onto a third layer, and the title block and border onto a forth layer.  I also want to change the document properties so new dims/tables/annotations will automatically go to the correct layer.

       

      So far I have been able to find enough info to help me get the new layers added, the dimensions onto the Dimension layer, and the document options changed.

       

      I still need to move all annotations onto an Annotation layer, tables onto a Table layer, and the title block/border, onto a Title Block layer.

       

      Below is the code I have so far.  Any help on this would be great!

       

      Brett

       

      Sub main()

       

          Dim swApp               As SldWorks.SldWorks

          Dim swDwg               As SldWorks.DrawingDoc

          Dim swLayerMgr          As SldWorks.LayerMgr

          Dim AnnoLayer           As Integer

          Dim TblsLayer           As Integer

          Dim DimsLayer           As Integer

          Dim TitleBlockLayer     As Integer

          Dim boolstatus          As Boolean

          Dim i                   As Integer

          Dim NumShts             As Integer

          Dim swView          As SldWorks.View

          Dim swAnn           As SldWorks.Annotation

          Dim swDispDim       As SldWorks.DisplayDimension

          Dim swAnno          As SldWorks.Annotation

       

          Set swApp = Application.SldWorks

       

          Set swDwg = swApp.ActiveDoc

       

          Set swLayerMgr = swDwg.GetLayerManager

         

          AnnoLayer = swLayerMgr.AddLayer("Annotations", "Annotations Layer", 968742753, 0, 0)

          TblsLayer = swLayerMgr.AddLayer("Tables", "Tables Layer", 968742753, 0, 0)

          DimsLayer = swLayerMgr.AddLayer("Dimensions", "Dimensions Layer", 968742753, 0, 0)

          TitleBlockLayer = swLayerMgr.AddLayer("Title Block", "Title Block Layer", 968742753, 0, 0)

         

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingNote, "Annotations")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingBalloon, "Annotations")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingBendTable, "Tables")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingLinearDimension, "Dimensions")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingDiameterDimension, "Dimensions")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingRadiusDimension, "Dimensions")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingHoleDimension, "Dimensions")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingAngleDimension, "Dimensions")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingChamferDimension, "Dimensions")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingOrdinateDimension, "Dimensions")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingDatum, "Annotations")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingSurfaceFinishSymbol, "Annotations")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingGeometricTolerance, "Annotations")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingWeldSymbol, "Annotations")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingBillOfMaterial, "Tables")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingRevisionTable, "Tables")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingGeneralTable, "Tables")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingHoleTable, "Tables")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingArcLengthDimension, "Dimensions")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingPunchTable, "Tables")

          boolstatus = swDwg.Extension.SetUserPreferenceString(swUserPreferenceStringValue_e.swDetailingLayer, swUserPreferenceOption_e.swDetailingWeldTable, "Tables")

       

          NumShts = swDwg.GetSheetCount

          For i = 1 To NumShts        ' blindly go to the first sheet

              swDwg.SheetPrevious

          Next i

       

          For i = 1 To NumShts

              Set swView = swDwg.GetFirstView

              While Not swView Is Nothing

                  Set swDispDim = swView.GetFirstDisplayDimension

                  While Not swDispDim Is Nothing

                      Set swAnn = swDispDim.GetAnnotation

                     

                      swAnn.Layer = "Dimensions"

                     

                      Set swDispDim = swDispDim.GetNext3

                  Wend

                 

                  Set swView = swView.GetNextView

              Wend

       

              swDwg.SheetNext

          Next i

       

      End Sub