Hide Table of Contents

Rebuild and Rollback Event Handler Example (C#)

This example shows how to handle the pre-notification that is fired when an assembly document is rebuilt. It also shows how to handle the post-notification that is fired when an assembly document is either rebuilt or rolled back.

// ----------------------------------------------------------------------------

// Preconditions: Open:

// <install_dir>\samples\tutorial\cosmosfloxpress\ball valve\ball_valve.sldasm.

//

// Postconditions:

// 1.  A dialog pops up with the pre-notification of the rebuild

//     (check the taskbar for the minimized dialog).

// 2.  Click OK to close the dialog.

// 3.  The assembly is rebuilt.

// 4.  A dialog pops up with the post-notification of the rebuild

//     (check the taskbar for the minimized dialog).

// 5.  Click OK to close the dialog.

// 6.  Move rollback bar to just above Sketch1.

// 7.  A dialog pops up with the post-notification of the rebuild

//     (check the taskbar for the minimized dialog).

// 8.  Click OK to close the dialog.

// 9.  Observe the Immediate Window.

// 10. After moving the rollback bar several times,

//     click Reset in the IDE to disable the event handler.

// 11. Select Stop VSTA debugger on macro exit in

//     Tools > Options > System Options > General.

//

// NOTE: Do not save the assembly document,

// as it is used in SolidWorks tutorials.

// ----------------------------------------------------------------------------

using SolidWorks.Interop.sldworks;

using SolidWorks.Interop.swconst;

using System;

using System.Diagnostics;

namespace RegenPostNotify2EventHandlerForAssemblyDoc_CSharp.csproj

{

    partial class SolidWorksMacro

    {

        ModelDoc2 swModel;

        ModelDocExtension swModelDocExt;

        public AssemblyDoc swAssembly;

        public void Main()

        {

            swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swStopDebuggingVstaOnExit, false);

            swModel = (ModelDoc2)swApp.ActiveDoc;

            swModelDocExt = swModel.Extension;

            // Event notification

            swAssembly = (AssemblyDoc)swModel;

            AttachEventHandlers();

            // Rebuild the model

            swModel.ForceRebuild3(true);

        }

        public void AttachEventHandlers()

        {

            AttachSWEvents();

        }

        public void AttachSWEvents()

        {

            swAssembly.RegenNotify += this.swAssembly_RegenNotify;

            swAssembly.RegenPostNotify2 += this.swAssembly_RegenPostNotify2;

        }

        private int swAssembly_RegenNotify()

        {

            //Display message before a rebuild

            System.Windows.Forms.MessageBox.Show("A rebuild pre-notification event has been fired.");

            return 0;

        }

        private int swAssembly_RegenPostNotify2(object stopFeature)

        {

            //Display message after either a rebuild or a roll back

            if ((stopFeature != null))

            {

                //Rollback

                System.Windows.Forms.MessageBox.Show("A rollback post-notification event has been fired.");

                Feature feature = default(Feature);

                feature = (Feature)stopFeature;

                Debug.Print("The rollback bar is just above " + feature.Name + " in the FeatureManager design tree.");

            }

            else

            {

                //Rebuild

                System.Windows.Forms.MessageBox.Show("A rebuild post-notification event has been fired.");

            }

            return 0;

        }

        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:   Rebuild and Rollback Event Handler 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) 2014 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.