Hide Table of Contents

Replace Component (VB.NET)

This example shows how to replace a component with a different component.

'---------------------------------------------------------------------------

' Preconditions: Specified file exists.

'

' Postconditions: The first instance of the shaft washer component is

' replaced with a lock washer.

'

' NOTE: The replacement does not fit properly with the model.

'--------------------------------------------------------------------------

Imports SolidWorks.Interop.sldworks

Imports SolidWorks.Interop.swconst

Imports System

Imports System.Diagnostics

Imports SwDocumentMgr

Partial Class SolidWorksMacro

    Public Sub main()

        ' Opening the file in SolidWorks Document Manager API, not SolidWorks.  

        ' This code is packaged in a SolidWorks macro for your convenience.

        Dim swCf As SwDMClassFactory

        swCf = New SwDMClassFactory

        Dim swDocMgr As SwDocumentMgr.SwDMApplication

        swDocMgr = swCf.GetApplication("your_license_key")

        Dim swDoc12 As SwDMDocument12

        Dim res As SwDocumentMgr.SwDmDocumentOpenError

        Dim dt As SwDmDocumentType

        dt = SwDmDocumentType.swDmDocumentAssembly

        Dim filename As String

        Debug.Print("Opening the assembly...")

        filename = "C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\advdrawings\98food processor.sldasm"

        swDoc12 = swDocMgr.GetDocument(filename, dt, False, res)

        If swDoc12 Is Nothing Or (res <> SwDmDocumentOpenError.swDmDocumentOpenErrorNone) Then

            Debug.Print("Error opening file....")

            Exit Sub

        End If

        Debug.Print("Getting the active configuration...")

        Dim activeConfig As SwDMConfiguration8

        Dim configMgr As SwDocumentMgr.SwDMConfigurationMgr

        configMgr = swDoc12.ConfigurationManager

        configMgr.GetConfigurationNames()

        activeConfig = configMgr.GetConfigurationByName(configMgr.GetActiveConfigurationName)

        If activeConfig Is Nothing Then

            Debug.Print("Error getting the active configuration...")

            Exit Sub

        End If

        Debug.Print("Getting the components of the active configuration...")

        Dim vComponents As Object

        vComponents = activeConfig.GetComponents

        Dim swDmComponent As SwDMComponent6

        Dim i As Integer

        For i = 0 To UBound(vComponents)

            swDmComponent = vComponents(i)

            ' Replace first instance of shaft washer component with lock washer

            If swDmComponent.Name = "shaft washer" Then

                Debug.Print("Replacing shaft washer with lock washer... " & swDmComponent.Replace("C:\Program Files\SolidWorks Corp\SolidWorks\samples\tutorial\smartcomponents\lockwasher.sldprt", "Default", False))

                Exit For

            End If

        Next

        swDoc12.Save()

        swDoc12.CloseDoc()

    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

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Replace Component (VB.NET)
*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) 2010 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.