Hide Table of Contents

Turn Cameras On and Off Example (VB.NET)

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.
'----------------------------------------------------------------------------
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System.Runtime.InteropServices
Imports System
Imports System.Diagnostics


Partial Class SolidWorksMacro

    
' Utility struct to combine the name and ID of a camera
    Structure Camera_t
        
Dim Name As String
        Dim Id As Integer
    End Structure

    Sub main()

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


        ' Get active document
        swModel = swApp.ActiveDoc

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

        
' Check if a camera view is active
        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
            swCamera = swModel.Extension.GetCameraById(lCameraId)

            
' A camera is a feature
            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


    Public swApp As SldWorks


End Class

 



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 (VB.NET)
*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) 2016 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.