Hide Table of Contents

Turn Cameras On and Off Example (C#)

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.
//----------------------------------------------------------------------------
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
namespace CameraType_CSharp.csproj
{
    
partial class SolidWorksMacro
    {

        
// Utility struct to combine the name and ID of a camera
        public struct Camera_t
        {
            
public string Name;
            
public int Id;
        }

        
public void Main()
        {
            
ModelDoc2 swModel = default(ModelDoc2);
            
ModelView swModelView = default(ModelView);
            
Camera swCamera = default(Camera);
            
int lNumCameras = 0;
            
int lCameraId = 0;
            
Feature swFeature = default(Feature);
            
Camera_t[] aCameras = null;
            
bool bValue = false;
            
string sFeatureName = null;
            
int lTypeOfCamera = 0;
            
int lTypeOfCameraPosition = 0;


            
// Get active document
            swModel = (ModelDoc2)swApp.ActiveDoc;

            
// Get the active model view for the active document
            swModelView = (ModelView)swModel.ActiveView;

            
// Check if a camera view is active
            swCamera = swModelView.Camera;

            
Debug.Print("");

            
if ((swCamera == null))
            {
                
Debug.Print("No active camera.");
            }
            
else
            {
                
Debug.Print("Camera is active.");
                
Debug.Print("  Name = " + swCamera.ID);
            }

            
// Turn off camera view
            swModelView.Camera = null;

            
// Get the number of cameras
            lNumCameras = swModel.Extension.GetCameraCount();

            
Debug.Print("");
            
Debug.Print("Number of cameras = " + lNumCameras);

            aCameras =
new Camera_t[lNumCameras];


            
for (lCameraId = 0; lCameraId <= (lNumCameras - 1); lCameraId++)
            {
                
// 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 = (Feature)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))
                {
                    
Debug.Print("Type of camera = Aimed at target");
                }
                
else
                {
                    
Debug.Print("Type of camera = Floating");
                }

                
// Get the type of camera position
                lTypeOfCameraPosition = swCamera.PositionType;

                
if ((lTypeOfCameraPosition == 1))
                {
                    
Debug.Print("Type of camera position = Cartesian");
                }
                
else
                {
                    
Debug.Print("Type of camera position = Spherical");
                }

            }

            
// Now switch the view to each camera

            for (lCameraId = 0; lCameraId <= (lNumCameras - 1); lCameraId++)
            {
                bValue = swModelView.SetCameraByName(aCameras[lCameraId].Name);

                
if ((bValue == false))
                {
                    
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);
                }

                swModelView.Camera =
null;

            }

        }

        
public SldWorks swApp;

    }


}



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 (C#)
*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) 2018 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.