Hide Table of Contents

Add Smart Components Example (VB.NET)

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.



Imports SolidWorks.Interop.sldworks

Imports SolidWorks.Interop.swconst

Imports System

Imports System.Diagnostics


Partial Class SolidWorksMacro


    Public Sub main()


        Dim swModel As ModelDoc2

        Dim swAssy As AssemblyDoc

        Dim tmpPath As String

        Dim tmpObj As ModelDoc2

        Dim boolstat As Boolean

        Dim swcomponent As Component2

        Dim AssemblyTitle As String

        Dim errors As Long

        Dim warnings As Long


        swModel = swApp.ActiveDoc


        ' Get title of assembly document

        AssemblyTitle = swModel.GetTitle


        boolstat = True

        Dim strCompModelname As String

        strCompModelname = "Bearing.sldprt"


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

        ' the assembly's path and use it when opening the component

        tmpPath = Left(swModel.GetPathName, InStrRev(swModel.GetPathName, "\"))


        ' Open the component

        tmpObj = swApp.OpenDoc6(tmpPath + strCompModelname, swDocumentTypes_e.swDocPART, 0, "", errors, warnings)


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

        ' messages if either

        If warnings = swFileLoadWarning_e.swFileLoadWarning_ReadOnly Then

            MsgBox("This file is read-only.")

            boolstat = False

        End If


        If tmpObj Is Nothing Then

            MsgBox("Cannot locate the file.")

            boolstat = False

        End If


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

        swModel = swApp.ActivateDoc2(AssemblyTitle, True, errors)

        swAssy = 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)


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


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




    End Sub


    ''' <summary>

    ''' The SldWorks swApp variable is pre-assigned for you.

    ''' </summary>

    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

Subject:   Feedback on Help Topics
Page:   Add Smart Components Example (VB.NET)
*   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) 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.