4 Replies Latest reply on Sep 18, 2013 1:27 PM by John R. Sitek

    Move block in drawing using macro.

    John R. Sitek

      I think I need to use SelectByID2 and MoveOrCopy but not sure.  The block is located on the sheet background, not foreground.  Please just paste the code if you respond.  Please do not point me elsewhere.

        • Re: Move block in drawing using macro.
          Keith Rice

          John,

           

          A macro that accomplished this would require you to select the block (unless you wanted this action to occur to all blocks on the foreground, programmatically---but you didn't specify this). Once selected, you would want to have a keyboard shortcut linked to a macro that simply did this:

           

          1. Cut the block using ISldworks::RunCommand

          2. Leave Edit Sheet Format using IDrawingDoc::EditSheet

          3. Rebuild the par tusing IModelDoc2::ForceRebuild3

          4. Plaste the blockusing ISldworks::RunCommand

           

          When it's all said and done, you've saved only a few mouse clicks and a keyboard stroke. So I assume you're looking for something more complex than this, but I hope this gets you headed in the right direction.

           

          Keith

          Learn the SolidWorks API

            • Re: Move block in drawing using macro.
              John R. Sitek

              As stated, the block is on the sheet background, and already exists.  I know the block name and the current coordinates would have to be extacted depending on the sheet scale.  I need to do this in VBA.

                • Re: Move block in drawing using macro.
                  Simon Turner

                  Option Explicit

                  Option Compare Text

                   

                  Dim swApp As SldWorks.SldWorks

                  Dim Part As ModelDoc2

                  Dim myUtil As MathUtility

                     

                  Sub main()

                      Dim selMgr As SelectionMgr

                      Dim sa As Variant

                      Dim myBlockDef As SketchBlockDefinition

                      Dim myMgr As SketchManager

                      Dim i As Integer

                      Dim myFeat As Feature

                     

                      Set swApp = Application.SldWorks

                      Set Part = swApp.ActiveDoc

                      If Part Is Nothing Then

                          MsgBox "No active Drawing document"

                          Exit Sub

                      End If

                     

                      If Part.GetType <> 3 Then

                          MsgBox "No active Drawing document"

                          Exit Sub

                      End If

                      Set myMgr = Part.SketchManager

                      Set myUtil = swApp.GetMathUtility

                     

                      sa = myMgr.GetSketchBlockDefinitions

                      If IsEmpty(sa) Then Exit Sub

                      For i = 0 To UBound(sa)

                          Set myBlockDef = sa(i)

                          Set myFeat = myBlockDef.GetFeature

                         

                          If myFeat.Name = "Block1" Then

                              MoveBlockInstances myBlockDef, 0.15, 0.25

                          End If

                      Next

                     

                  End Sub

                   

                  Sub MoveBlockInstances(myBlockDef As SketchBlockDefinition, x As Double, y As Double)

                      Dim myInst As SketchBlockInstance

                      Dim sa As Variant

                      Dim i As Integer

                     

                      sa = myBlockDef.GetInstances

                      If IsEmpty(sa) Then Exit Sub

                      For i = 0 To UBound(sa)

                          Set myInst = sa(i)

                          MoveBlockInstance myInst, x, y

                      Next

                  End Sub

                   

                  Sub MoveBlockInstance(myInst As SketchBlockInstance, x As Double, y As Double)

                      Dim sa As Variant

                      Dim i As Integer

                      Dim mpt As MathPoint

                      Dim p(2) As Double

                     

                      Set mpt = myInst.InstancePosition

                      sa = mpt.ArrayData

                      p(0) = sa(0) + x

                      p(1) = sa(1) + y

                      p(2) = 0

                      sa = p

                      Set mpt = myUtil.CreatePoint(sa)

                      myInst.InstancePosition = mpt

                  End Sub