Hide Table of Contents

Turn Cameras On and Off Example (VBA)

This example shows how to turn cameras on and off.

'----------------------------------------------------------------------------
' Preconditions: Open a SOLIDWORKS model that contains at least one camera.
'
' Postconditions: The cameras are turned on and off.
'----------------------------------------------------------------------------

Option Explicit

' Utility struct to combine the name and ID of a camera
Type Camera_t
    Name As String
    Id   As Long
End Type

Dim swApp As SldWorks.SldWorks

Sub main()

    Dim swModel                 As SldWorks.ModelDoc2
    Dim swModelView             As SldWorks.ModelView
    Dim swCamera                As SldWorks.Camera
    Dim lNumCameras             As Long
    Dim lCameraId               As Long
    Dim swFeature               As SldWorks.Feature
    Dim aCameras()              As Camera_t
    Dim bValue                  As Boolean
    Dim sFeatureName            As String
    Dim lTypeOfCamera           As Long
    Dim lTypeOfCameraPosition   As Long

    ' Connect to SOLIDWORKS
    Set swApp = Application.SldWorks
 

    ' Get active document
    Set swModel = swApp.ActiveDoc
       

    ' Get the active model view for the active document
    Set swModelView = swModel.ActiveView
 

    ' Check if a camera view is active
    Set swCamera = swModelView.Camera

    Debug.Print

    If (swCamera Is Nothing) Then
        Debug.Print "No active camera."
    Else
        Debug.Print "Camera is active."
        Debug.Print "  Name = " & swCamera.Id
    End If

    ' Turn off camera view
    swModelView.Camera = Nothing

    ' Get the number of cameras
    lNumCameras = swModel.Extension.GetCameraCount

    Debug.Print
    Debug.Print "Number of cameras = " & lNumCameras

    ReDim aCameras(lNumCameras - 1)

    For lCameraId = 0 To (lNumCameras - 1)

        ' Valid ID:
        '  0 <= ID <= (ModelDocExtension::GetCameraCount - 1)
        ' IDs are reassigned if a camera is deleted
        aCameras(lCameraId).Id = lCameraId

        ' Get the camera
        Set swCamera = swModel.Extension.GetCameraById(lCameraId)

        ' A camera is a feature
        Set swFeature = swCamera

       ' Get the names of the feature and camera
        sFeatureName = swFeature.Name

        aCameras(lCameraId).Name = sFeatureName

        Debug.Print
        Debug.Print "Type of feature = "; swFeature.GetTypeName
        Debug.Print "Camera name = " & sFeatureName

        ' Get the type of camera
       lTypeOfCamera = swCamera.Type

        If (lTypeOfCamera = 1) Then
            Debug.Print "Type of camera = Aimed at target"
        Else
            Debug.Print "Type of camera = Floating"
        End If

        ' Get the type of camera position
        lTypeOfCameraPosition = swCamera.PositionType

        If (lTypeOfCameraPosition = 1) Then
            Debug.Print "Type of camera position = Cartesian"
        Else
            Debug.Print "Type of camera position = Spherical"
        End If

    Next lCameraId

    ' Now switch the view to each camera
    For lCameraId = 0 To (lNumCameras - 1)

        bValue = swModelView.SetCameraByName(aCameras(lCameraId).Name)

        If (bValue = False) Then
            Debug.Print "Failed to set model view to use camera " & aCameras(lCameraId).Name
        Else
            Debug.Print "Model view set to use camera " & aCameras(lCameraId).Name
        End If

        swModelView.Camera = Nothing

    Next lCameraId

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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Turn Cameras On and Off Example (VBA)
*Comment:  
*   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:

x

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
x

Web Help Content Version: API Help (English only) 2019 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.