Hide Table of Contents

Zoom to Region Example (VBA)

This example shows how to zoom to a specific region.



' Problem:

'       This sample code shows how to use:


'           IModelDoc2::ViewZoomTo2


'       The input parameters may not be obvious, but

'       they are related to the current view orientation.

'       The code shows how to transform points

'       from model space so that the resulting view is

'       zoomed to points selected in model space.


' Preconditions:

'       (1) Part or assembly is open.

'       (2) Two points are picked in the graphics window.


' Postconditions:

'       Graphics area is zoomed to minimally include

'       the two selection points.



Option Explicit

Sub main()

    Dim swApp                   As SldWorks.SldWorks

    Dim swModel                 As SldWorks.ModelDoc2

    Dim swModView               As SldWorks.ModelView

    Dim swViewOrient            As SldWorks.MathTransform

    Dim swSelMgr                As SldWorks.SelectionMgr

    Dim swMathUtil              As SldWorks.MathUtility

    Dim vSelPt1                 As Variant

    Dim vSelPt2                 As Variant

    Dim swSelPt1                As SldWorks.MathPoint

    Dim swSelPt2                As SldWorks.MathPoint

    Dim i                       As Long

    Dim bRet                    As Boolean

    Set swApp = CreateObject("SldWorks.Application")

    Set swMathUtil = swApp.GetMathUtility

    Set swModel = swApp.ActiveDoc

    Set swSelMgr = swModel.SelectionManager

    Set swModView = swModel.ActiveView

    Set swViewOrient = swModView.Orientation3


    vSelPt1 = swSelMgr.GetSelectionPoint(1)

    vSelPt2 = swSelMgr.GetSelectionPoint(2)


    Set swSelPt1 = swMathUtil.CreatePoint((vSelPt1))

    Set swSelPt2 = swMathUtil.CreatePoint((vSelPt2))

    Set swSelPt1 = swSelPt1.MultiplyTransform(swViewOrient)

    Set swSelPt2 = swSelPt2.MultiplyTransform(swViewOrient)


    swModel.ViewZoomTo2 _

        swSelPt1.ArrayData(0), swSelPt1.ArrayData(1), swSelPt1.ArrayData(2), _

        swSelPt2.ArrayData(0), swSelPt2.ArrayData(1), swSelPt2.ArrayData(2)

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:   Zoom to Region 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) 2013 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.