Hide Table of Contents

Select All in Part, Assembly, or Drawing (C#)

This example shows how to select everything in the graphics area of a part or assembly document or in the sheet of a drawing document, as if you box-selected everything in the graphics area or the sheet.

// Preconditions:
// 1. Verify that the part, assembly, and drawing documents opened by the macro
//    exist.
// 2. Open the Immediate window.
// Postconditions:
// 1. Examine:
//    * Sheet to verify that all of the entities in the drawing
//      are selected.
//    * Immediate window to see how many entities are selected.
// 2. Click Window > bolt-assembly.sldasm to switch to the assembly
//    document.
// 3. Examine:
//    * Graphics area to verify that the all of the components
//      in the assembly are selected.
//    * Immediate window to see how many components are selected.
// 4. Click Window > bolt.sldprt to switch to the part document.
// 5. Examine:
//    * Graphics area to verify that the all of the edges
//      in the part are selected.
//    * Immediate window to see how many edges are selected.
// NOTE: Because these documents are used elsewhere, do not save changes.

using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;

namespace SelectAllModelDocExtensionCSharp.csproj
partial class SolidWorksMacro

public void Main()
            ModelDoc2 swModel;
            ModelDocExtension swModelDocExt;
            SelectionMgr swSelMgr;
string partFile = null;
string assemblyFile = null;
string drawingFile = null;
int errors = 0;
int warnings = 0;

// Open a part document and select all edges in the part
            partFile = "C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2017\\introsw\\bolt.sldprt";
            swModel = (ModelDoc2)swApp.OpenDoc6(partFile, (
int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            swModelDocExt = (ModelDocExtension)swModel.Extension;
            swSelMgr = (SelectionMgr)swModel.SelectionManager;
//Select all edges in part
            SelectAllinDocument(swModel, swModelDocExt, swSelMgr);

// Open an assembly document and select all components in the assembly
            assemblyFile = "C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2017\\introsw\\bolt-assembly.sldasm";
            swModel = (ModelDoc2)swApp.OpenDoc6(assemblyFile, (
int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            swModelDocExt = (ModelDocExtension)swModel.Extension;
            swSelMgr = (SelectionMgr)swModel.SelectionManager;
//Select all components in assembly
            SelectAllinDocument(swModel, swModelDocExt, swSelMgr);

// Open a drawing document and select all entities in the drawing
            drawingFile = "C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2017\\introsw\\bolt-assembly.slddrw";
            swModel = (ModelDoc2)swApp.OpenDoc6(drawingFile, (
int)swDocumentTypes_e.swDocDRAWING, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
            swModelDocExt = (ModelDocExtension)swModel.Extension;
            swSelMgr = (SelectionMgr)swModel.SelectionManager;
//Select all entities in drawing
            SelectAllinDocument(swModel, swModelDocExt, swSelMgr);


public void SelectAllinDocument(ModelDoc2 swModel, ModelDocExtension swModelDocExt, SelectionMgr swSelMgr)
int selCount;

// Select all edges in a part, all components in an assembly,
            // or all entities in a drawing

// Get and print the number of selections
            selCount = 0;
            selCount = swSelMgr.GetSelectedObjectCount2(-1);

switch (swModel.GetType())
case (int)swDocumentTypes_e.swDocPART:
Debug.Print("Number of edges selected in part          = " + selCount);
case (int)swDocumentTypes_e.swDocASSEMBLY:
Debug.Print("Number of components selected in assembly = " + selCount);
case (int)swDocumentTypes_e.swDocDRAWING:
Debug.Print("Number of entities selected in drawing    = " + selCount);
Debug.Print("Unknown type of document.");


/// <summary>
        /// The SldWorks swApp variable is pre-assigned for you.
        /// </summary>
        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

Subject:   Feedback on Help Topics
Page:   Select All in Part, Assembly, or Drawing (C#)
*   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) 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.