Hide Table of Contents

Add Smart Components Example (C#)

This example shows how to add Smart Components to an assembly.

//---------------------------------------------------------------------------

// Preconditions:

// Open:

// <SOLIDWORKS_install_dir>\samples\tutorial\smartcomponents\stepped_shaft.sldasm

//

// Postconditions:

// 1. <SOLIDWORKS_install_dir>\samples\tutorial\smartcomponents\Bearing.sldprt

//     is opened.

// 2. Bearing.sldprt is added

//     as three separate Smart Components at the

//     specified coordinates to stepped_shaft.sldasm.

//

// NOTE: Because this assembly is used in a SOLIDWORKS

//       online tutorial, do not save any changes

//       when you close the assembly document.

//---------------------------------------------------------------------

using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using System;

using System.Windows.Forms;

using System.Diagnostics;

namespace AddSmartComponentsCSharp.csproj

{

    public partial class SolidWorksMacro

    {

    

    public void Main()

    {

        

        ModelDoc2 swModel;

        AssemblyDoc swAssy;

        string tmpPath;

        ModelDoc2 tmpObj;

        Component2 swcomponent;

        string AssemblyTitle;

        int errors = 0;

        int warnings = 0;

        bool boolstat;

        

        swModel = (ModelDoc2)swApp.ActiveDoc;

        

        // Get title of assembly document

        AssemblyTitle = swModel.GetTitle();

        

        boolstat = true;

        string strCompModelname = null;

        strCompModelname = "Bearing.sldprt";

        

        // Because the component resides in the same folder as the assembly, get

        // the assembly's path, strip out the assembly filename, concatenate the

        // the rest of the path to the component filename, and use

        // this string to open the component

        tmpPath = swModel.GetPathName();

        int idx;

        idx = tmpPath.LastIndexOf("stepped_shaft.sldasm");

        string compPath;

        tmpPath = tmpPath.Substring(0, (idx));

        compPath = string.Concat(tmpPath, strCompModelname);

       

        

        

        // Open the component

        tmpObj = (ModelDoc2)swApp.OpenDoc6(compPath, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);

        

        // Check to see if the file is read-only or cannot be found; display error

        // messages if either

        if (warnings == (int)swFileLoadWarning_e.swFileLoadWarning_ReadOnly) {

            MessageBox.Show("This file is read-only.");

            boolstat = false;

        }

        

        if (tmpObj == null) {

            MessageBox.Show("Cannot locate the file.");

            boolstat = false;

        }

        

        //Re-activate the assembly so that you can add the component to it

        swModel = (ModelDoc2)swApp.ActivateDoc2(AssemblyTitle, true, ref errors);

        swAssy = (AssemblyDoc)swModel;

        

        // Add the component to the assembly document and

        // print the component name to the Immediate Window

        swcomponent = swAssy.AddSmartComponent(strCompModelname, 0, 0.13, -0.1);

        Debug.Print(swcomponent.Name2);

        swcomponent = swAssy.AddSmartComponent(strCompModelname, 0, 0.13, -0.42);

        Debug.Print(swcomponent.Name2);

        swcomponent = swAssy.AddSmartComponent(strCompModelname, 0, 0.13, 0.49);

        Debug.Print(swcomponent.Name2);

        

        swModel.ClearSelection2(true);

        

    }

    

    /// <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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Add Smart Components 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) 2015 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.