Hide Table of Contents

Change Imported File Associated with an Imported Feature Example (VBA)

This example shows how to change the imported file associated with an imported feature.

'------------------------------------------------------------------
' Preconditions:
' 1. Verify that c:\temp exists.
' 2. Open public_documents\samples\tutorial\api\cylinder20.sldprt.
' 3. Click File > Save As > Parasolid (*.x_t) in Save as type.
' 4. Type c:\temp before cylinder20.X_T in File name and click Save.
' 5. Open public_documents\samples\tutorial\api\cam roller.sldprt.
' 6. Select 91212_182-P9_SWP_001-1-solid1 in the FeatureManager
'    design tree.
' 7. Open the Immediate window.
'
' Postconditions:
' 1. Replaces the selected imported feature with the specified
'    imported file, c:\temp\cylinder20.x_t.
' 2. Examine the Immediate window and click each imported feature
'    in the FeatureManager design tree.
'
' NOTE: Because the part is used elsewhere, do not save changes.
'-----------------------------------------------------------------
Option Explicit
Sub main()
    Const sImportFileName As String = "C:\temp\cylinder20.x_t"
    Dim swApp As SldWorks.SldWorks
    Dim swModel As SldWorks.ModelDoc2
    Dim swSelMgr As SldWorks.SelectionMgr
    Dim swFeat As SldWorks.Feature
    Dim swNewModel As SldWorks.ModelDoc2
    Dim swNewPart As SldWorks.PartDoc
    Dim vBodyArr As Variant
    Dim swBody As SldWorks.Body2
    Dim swTempBody As SldWorks.Body2
    Dim bRet As Boolean
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swSelMgr = swModel.SelectionManager
    Set swFeat = swSelMgr.GetSelectedObject6(1, -1)
    Debug.Print "File = " & swModel.GetPathName
    Debug.Print "  Feat = " & swFeat.Name
    Debug.Print "    FeatTypeName = " & swFeat.GetTypeName
    ' Open import file into a new part file
    bRet = swApp.LoadFile2(sImportFileName, "")
    Set swNewModel = swApp.ActiveDoc
    Set swNewPart = swNewModel
    ' Only consider solid bodies
    vBodyArr = swNewPart.GetBodies2(swSolidBody, True)
    ' Only consider first solid body
    Set swBody = vBodyArr(0)
    Set swTempBody = swBody.Copy
    bRet = swFeat.SetBody2(swTempBody, False)
    Debug.Print "       Body replaced? " & bRet
    ' Close SOLIDWORKS file associated with opening the import file
    swApp.QuitDoc swNewModel.GetTitle
    bRet = swModel.ForceRebuild3(False)
    swModel.ViewZoomtofit2
End Sub


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:   Change Imported File Associated with an Imported Feature Example (VBA)
*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) 2021 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.