Hide Table of Contents

Get and Set Blocks Data in Any Document Example (VBA)

This example shows how to get blocks data in part, assembly, and drawing documents. It also shows how to change the angles of the blocks.




' Preconditions: Part, assembly, or drawing document containing

'                one or more block definitions

'                and block instances is open.


' Postconditions: The angles of all block instances are modified.



Option Explicit


Dim swApp As SldWorks.SldWorks

Dim swModel As SldWorks.ModelDoc2

Dim skMgr As SldWorks.SketchManager

Dim pBlock As SldWorks.SketchBlockDefinition

Dim pInst As SldWorks.SketchBlockInstance

Dim leaderPt As SldWorks.MathPoint

Dim insPt As SldWorks.MathPoint

Dim vInstPt As Variant

Dim vInstances As Variant

Dim vBlocks As Variant

Dim nInstanceCount As Long

Dim itr As Long

Dim jtr As Long

Dim bLinkToFile As Boolean

Dim strInsPoint As String


Sub main()


    Set swApp = Application.SldWorks

    Set swModel = swApp.ActiveDoc

    Set skMgr = swModel.SketchManager


    ' To change the angles, you must edit the block's sketches



    vBlocks = skMgr.GetSketchBlockDefinitions


    ' Exit if no blocks

    If IsEmpty(vBlocks) Then

        Exit Sub

    End If


    ' Process block definitions

    For itr = 0 To UBound(vBlocks)


        Set pBlock = vBlocks(itr)


        ' Block defintion linked to file?

        Debug.Print "   Link To File = " & pBlock.LinkToFile


        ' Block linked file name

        Debug.Print "   Link File Name = " & pBlock.FileName


        ' Block definition insertion point

        Set insPt = pBlock.InsertionPoint

        vInstPt = insPt.ArrayData

        strInsPoint = "   Insertion point: x = " + CStr(vInstPt(0) * 1000) + " , y = " + CStr(vInstPt(1) * 1000) + " , z = " + CStr(vInstPt(2) * 1000)

        Debug.Print strInsPoint


        ' Number of block instances of this block definition

        nInstanceCount = pBlock.GetInstanceCount

        Debug.Print "   Instance Count = " & nInstanceCount


        ' Process block instances

        If nInstanceCount > 0 Then

            vInstances = pBlock.GetInstances

            For jtr = 0 To UBound(vInstances)

                Set pInst = vInstances(jtr)


                ' Block instance position

                Set insPt = pInst.InstancePosition

                vInstPt = insPt.ArrayData

                strInsPoint = "        Instance position: x = " + CStr(vInstPt(0) * 1000) + " , y = " + CStr(vInstPt(1) * 1000) + " , z = " + CStr(vInstPt(2) * 1000)

                Debug.Print strInsPoint


                ' Get block instance angle

                Debug.Print "        Original angle = " & pInst.Angle

                ' Change block instance angle

                pInst.Angle = 90 / 57.3

                Debug.Print "        Modified angle = " & pInst.Angle


                ' Block instance scale

                Debug.Print "        Scale = " & pInst.Scale


                ' Block instance owner's sketch name

                Debug.Print "        Owner Sketch = " & pInst.GetSketch.Name


            Next jtr

        End If


    Next itr


End Sub

Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

Subject:   Feedback on Help Topics
Page:   Get and Set Blocks Data in Any Document Example (VBA)
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:


We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again

Web Help Content Version: API Help (English only) 2012 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.