2 Replies Latest reply on Jun 25, 2014 9:29 AM by Klaudiusz Skiba

    Inserting blocks on different layer

    Klaudiusz Skiba

      Hello,

      In my macro I'm inserting few different blocks. They automatically are inserted on balloon layer on my drawing. I would like those blocks to be not inserted on any layers or in other words the layer would be specified as NONE.

      Thanks

        • Re: Inserting blocks on different layer
          Tom Bostick

          Try this.

           

          Dim swApp As SldWorks.SldWorks

          Dim swModel As SldWorks.ModelDoc2

          Dim swDwg As SldWorks.DrawingDoc

          Dim swBlkDef As SldWorks.SketchBlockDefinition

          Dim swBlk As SldWorks.SketchBlockInstance

          Dim swSkMgr As SldWorks.SketchManager

          Dim vBlkDef As Variant

          Dim vBlkInst As Variant

          Dim nbrBlks As Integer

          Dim blkLayer As String

          Dim bRet As Boolean

          Dim longWarnings As Long, longStatus As Long

           

           

          Sub main()

           

           

              Set swApp = Application.SldWorks

              Set swModel = swApp.ActiveDoc

                  If swModel Is Nothing Then

                      MsgBox "No file open."

                      Exit Sub

                  ElseIf swModel.GetType = swDocDRAWING Then

                      Set swDwg = swModel

                  End If

              Set swSkMgr = swModel.SketchManager

             

              vBlkDef = swSkMgr.GetSketchBlockDefinitions

              blkLayer = ""

             

              If Not IsEmpty(vBlkDef) Then

                  For i = 0 To UBound(vBlkDef)

                      Set swBlkDef = vBlkDef(i)

                     

                      vBlkInst = swBlkDef.GetInstances

                     

                      nbrBlks = swBlkDef.GetInstanceCount

                     

                      If nbrBlks > 0 Then

                          For j = 0 To nbrBlks - 1

                              Set swBlk = vBlkInst(j)

                             

                              swBlk.Layer = blkLayer

                          Next j

                      End If

                  Next i

              End If

             

          End Sub

            • Re: Inserting blocks on different layer
              Klaudiusz Skiba

              I didn't try your code because I'm inserting my blocks from files, but simple change of layers before hand and after did the trick.

              Here is a code to change layer before and after inserting anything.

               

                  Set swApp = Application.SldWorks

                  Set swModel = swApp.ActiveDoc

                  Set swSktManager = swModel.SketchManager

                 

                  Set swLayerMgr = swModel.GetLayerManager

                  checkStr = swLayerMgr.GetCurrentLayer

                  checkInt = swLayerMgr.SetCurrentLayer("")   ' layer changed to none or any other specified

                 

                  'do your work here on different layer

               

                  checkInt = swLayerMgr.SetCurrentLayer(checkStr) ' layer changed back to the original one