Hide Table of Contents

Rename Component and Update References Example (C#)

This example shows how to rename a component and update its references.

//----------------------------------------------------------------------
// Preconditions:
// 1. Copy public_documents\samples\tutorial\EDraw\claw to c:\test\claw.
// 2. Open c:\test\claw\claw-mechanism.sldasm and save the file as 
//    claw-mechanism-copy.sldasm.
// 3. Close claw-mechanism-copy.sldasm and reopen claw-mechanism.sldasm.
// 4. Open the Immediate window.
//
// Postconditions:
// 1. Renames the center component to centerXXX.
// 2. Fires the RenameItemNotify event.
// 3. Saves the assembly.
// 4. Fires the RenamedDocumentNotify event.
// 5. Updates references.
// 6. Examine the FeatureManager design tree and Immediate window.
// 7. Close claw-mechanism.sldasm and open
//    c:\test\claw\claw-mechanism-copy.sldasm to verify that the
//    center component was renamed to centerXXX.
//---------------------------------------------------------------------
 
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
using System.Collections;
 
namespace Macro1CSharp.csproj
{
    public partial class SolidWorksMacro
    {
        public AssemblyDoc assy;
 
        public void Main()
        {
            ModelDoc2 swModel = default(ModelDoc2);
            ModelDocExtension swModelDocExt = default(ModelDocExtension);
            AssemblyDoc swAssy = default(AssemblyDoc);
            Hashtable openAssembly = default(Hashtable);
            int errors = 0;
            int warnings = 0;
            bool status = false;
 
            swAssy = (AssemblyDoc)swApp.ActiveDoc;
 
            //Set up event
            assy = (AssemblyDoc)swAssy;
            openAssembly = new Hashtable();
            AttachEventHandlers();
 
            swModel = (ModelDoc2)swAssy;
            swModelDocExt = (ModelDocExtension)swModel.Extension;
            status = swModelDocExt.SelectByID2("center-1@claw-mechanism""COMPONENT", 0, 0, 0, false, 0, null, 0);
            errors = swModelDocExt.RenameDocument("centerXXX");
            swModelDocExt.Rebuild((int)swRebuildOptions_e.swRebuildAll);
            status = swModel.Save3((int)swSaveAsOptions_e.swSaveAsOptions_Silent + (int)swSaveAsOptions_e.swSaveAsOptions_SaveReferenced, ref errors, ref warnings);
 
        }
 
        public void AttachEventHandlers()
        {
            AttachSWEvents();
        }
 
        public void AttachSWEvents()
        {
            if ((assy != null))
            {
                assy.RenameItemNotify += this.assy_RenameItemNotify;
                assy.RenamedDocumentNotify += this.assy_RenamedDocumentNotify;
            }
 
        }
 
        //Fire notification when item is renamed
        public int assy_RenameItemNotify(int entType, string oldName, string newName)
        {
            Debug.Print("RenameItemNotify fired");
            return 0;
        }
 
 
        //Fire notification for Rename Documents dialog
        public int assy_RenamedDocumentNotify(ref object swObj)
        {
            RenamedDocumentReferences swRenamedDocumentReferences = default(RenamedDocumentReferences);
            object[] searchPaths = null;
            object[] pathNames = null;
            int i = 0;
            int nbr = 0;
 
            swRenamedDocumentReferences = (RenamedDocumentReferences)swObj;
 
            swRenamedDocumentReferences.UpdateWhereUsedReferences = true;
            swRenamedDocumentReferences.IncludeFileLocations = true;
 
            searchPaths = (object[])swRenamedDocumentReferences.GetSearchPath();
            nbr = searchPaths.Length - 1;
            Debug.Print("Search paths:");
            for (i = 0; i <= nbr; i++)
            {
                Debug.Print(" " + searchPaths[i]);
            }
 
            swRenamedDocumentReferences.Search();
 
            pathNames = (object[])swRenamedDocumentReferences.ReferencesArray();
            nbr = pathNames.Length - 1;
            Debug.Print("References:");
            for (i = 0; i <= nbr; i++)
            {
                Debug.Print(" " + pathNames[i]);
            }
 
            swRenamedDocumentReferences.CompletionAction = (int)swRenamedDocumentFinalAction_e.swRenamedDocumentFinalAction_Ok;
 
            Debug.Print("RenamedDocumentNotify fired");
 
            return 0;
 
        }
        /// <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:   Rename Component and Update References 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) 2019 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.