Hide Table of Contents

Get Components in Drawing View (C#)

This example shows how to get the components in a drawing view and how to change their line font styles.

// Preconditions:
// 1. Drawing document opened by the macro exists.
// 2. Drawing view is selected.
// 3. Open the Immediate window.
// Postconditions:
// 1. Specified drawing document is opened.
// 2. Drawing View1 is selected.
// 3. Gets the root and children components for Drawing
//    View1.
// 4. For each component:
//    a. Prints whether a drawing component is selected,
//       the name of the component, and the name of the
//       configuration to the Immediate window.
//    b. Disables the use of the document defaults for the
//       the component's line font style.
//    c. Sets new line style and line thickness for the component's
//       visible edges and prints the new settings and values to
//       the Immediate window.
//    d. Prints the file name of the component to the Immediate window.
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;

namespace LineFontsDrawingComponentCSharp.csproj
partial class SolidWorksMacro
public void Main()
            ModelDoc2 swModel =
            DrawingDoc swDraw =
            SelectionMgr swSelMgr =
SelectData swSelData = default(SelectData);
            ModelDocExtension swModelDocExt =
            View swView =
            DrawingComponent swRootDrawComp =
object[] vDrawChildCompArr = null;
            DrawingComponent swDrawComp = default(DrawingComponent);
            Component2 swComp =
            ModelDoc2 swCompModel =
string assemblyDrawing = null;
bool status = false;
int errors = 0;
int warnings = 0;
int lineWeight = 0;
double lineThickness = 0;

            assemblyDrawing =
"C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2017\\tutorial\\driveworksxpress\\mobile gantry.slddrw";
            swModel = (ModelDoc2)swApp.OpenDoc6(assemblyDrawing, (
int)swDocumentTypes_e.swDocDRAWING, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);

            swDraw = (DrawingDoc)swModel;
            swModelDocExt = (ModelDocExtension)swModel.Extension;
            swSelMgr = (SelectionMgr)swModel.SelectionManager;

            swSelData = (SelectData)swSelMgr.CreateSelectData();

            status = swDraw.ActivateView(
"Drawing View4");
            status = swModelDocExt.SelectByID2(
"Drawing View1", "DRAWINGVIEW", 0.104008832128, 0.1163870710783, 0, false, 0, null, 0);
            swView = (View)swSelMgr.GetSelectedObject6(1, -1);
            swRootDrawComp = (DrawingComponent)swView.RootDrawingComponent;

Debug.Print("File = " + swModel.GetPathName());
Debug.Print("  View = " + swView.Name);

            vDrawChildCompArr = (
foreach (object vDrawChildComp in vDrawChildCompArr)
                swDrawComp = (DrawingComponent)vDrawChildComp;

// Drawing component selected?
Debug.Print(" Drawing component selected = " + swDrawComp.Select(true, null));

// Returns NULL if underlying model is open in a different configuration
                swComp = (Component2)swDrawComp.Component;

if ((null != swComp))
// Returns NULL if drawing is lightweight
                    swCompModel = (ModelDoc2)swComp.GetModelDoc2();

Debug.Print("      Component                            = " + swComp.Name2);
Debug.Print("      Configuration                        = " + swComp.ReferencedConfiguration);

// Turn off using document default settings for component's line font style
                    swDrawComp.UseDocumentDefaults = false;
Debug.Print("      Default component line font in use   = " + swDrawComp.UseDocumentDefaults);
// Set new line style for visible edges
                    swDrawComp.SetLineStyle((int)swDrawingComponentLineFontOption_e.swDrawingComponentLineFontVisible, (int)swLineStyles_e.swLineCHAIN);
Debug.Print("        Line style for visible edges                      = " + swDrawComp.GetLineStyle((int)swDrawingComponentLineFontOption_e.swDrawingComponentLineFontVisible));
// Set new line thickness for visible edges
                    swDrawComp.SetLineThickness((int)swDrawingComponentLineFontOption_e.swDrawingComponentLineFontVisible, (int)swLineWeights_e.swLW_CUSTOM, 0.0003);
                    lineWeight = swDrawComp.GetLineThickness((
int)swDrawingComponentLineFontOption_e.swDrawingComponentLineFontVisible, out lineThickness);
Debug.Print("        Line weight style and thickness for visible edges = " + lineWeight + ", " + lineThickness * 1000 + " mm");

if ((null != swCompModel))
Debug.Print("      File                                 = " + swCompModel.GetPathName());
Debug.Print(" ");




/// <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:   Get Components in Drawing View (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) 2017 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.