Hide Table of Contents

Fire Notifications When Renaming Components Example (C#)

This example shows how to fire notifications when you:

  • are about to rename a component.
  • rename a component.
//---------------------------------------------------
// Preconditions:
// 1. Verify that these documents exist in public_documents\tutorial\api:
//    * beam_boltconnection.sldasm
//    * beam with holes.sldprt
// 2. Open the Immediate window.
//
// Postconditions:
// 1. Open public_documents\tutorial\api\beam_boltconnection.sldasm.
// 2. Fires pre-notification before appending
//    123 to each assembly component's name.
// 3. Fires notification when appending 123 to
//    each assembly component's name.
// 4. Examine the FeatureManager design tree and the
//    Immediate window.
//
// NOTE: Because the assembly is used elsewhere, do
// not save changes.
//---------------------------------------------------
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 swAssy;
 
        public void Main()
        {
 
            ModelDoc2 swModel = default(ModelDoc2);
            ConfigurationManager swConfigMgr = default(ConfigurationManager);
            Configuration swConfig = default(Configuration);
            Component2 swRootComp = default(Component2);
            object[] Children = null;
            Component2 swChild = default(Component2);
            SelectionMgr swSelMgr = default(SelectionMgr);
            SelectData swSelData = default(SelectData);
            int ChildCount = 0;
            string oldName = null;
            string newName = null;
            bool bOldSetting = false;
            bool bRet = false;
            int i = 0;
            Hashtable openAssem = default(Hashtable);
 
            swModel = (ModelDoc2)swApp.ActiveDoc;
            swAssy = (AssemblyDoc)swModel;
 
            // Set up event
            swAssy = (AssemblyDoc)swModel;
            openAssem = new Hashtable();
            AttachEventHandlers();
 
            swConfigMgr = swModel.ConfigurationManager;
            swConfig = swConfigMgr.ActiveConfiguration;
            swRootComp = swConfig.GetRootComponent3(true);
            bOldSetting = swApp.GetUserPreferenceToggle((int)swUserPreferenceToggle_e.swExtRefUpdateCompNames);
            swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swExtRefUpdateCompNames, false);
            Children = (object[])swRootComp.GetChildren();
            ChildCount = Children.Length;
            swSelMgr = (SelectionMgr)swModel.SelectionManager;
            swSelData = (SelectData)swSelMgr.CreateSelectData();
            for (i = 0; i <= ChildCount - 1; i++)
            {
                swChild = (Component2)Children[i];
                // Changing component name requires component to be selected
                bRet = swChild.Select4(false, swSelData, false);
                oldName = swChild.Name2;
                newName = oldName + " 123";
                swChild.Name2 = newName;
            }
            swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swExtRefUpdateCompNames, bOldSetting);
 
 
        }
 
        public void AttachEventHandlers()
        {
            AttachSWEvents();
        }
 
        public void AttachSWEvents()
        {
            swAssy.PreRenameItemNotify += this.swAssy_PreRenameItemNotify;
            swAssy.RenameItemNotify += this.swAssy_RenameItemNotify;
        }
 
        private int swAssy_PreRenameItemNotify(int EntityType, string oldName, string newName)
        {
            Debug.Print("PRE-NOTIFICATION - about to rename component: " + oldName);
            return 0;
        }
        private int swAssy_RenameItemNotify(int EntityType, string oldName, string newName)
        {
            Debug.Print("NOTIFICATION - rename component: " + newName);
            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:   Fire Notifications When Renaming 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) 2017 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.