Hide Table of Contents

Fire Assembly Rebuild Events Example (C#)

This example shows how to handle pre-notification and post-notification events for an assembly, which are fired when the assembly is rebuilt.

// ---------------------------------------------------------------------------- 
// Preconditions: 
//  1. Verify that the specified assembly document to open exists.
//  2. Open the Immediate window. 
// 
// Postconditions: 
//  1. Opens the specified assembly document.
//  2. Inserts a cut-extrude assembly feature.
//  3. Pops up a dialog with the rebuild pre-notification. 
//  4. Click OK to close the dialog. 
//  5. Rebuilds the assembly. 
//  6. Pops up a dialog with the rebuild post-notification. 
//  7. Click OK to close the dialog.
//  8. Rolls back the rollback bar and pops up a dialog with the 
//     rebuild post-notification.
//  9. Click OK to close the dialog.
// 10. Examine the Immediate window and FeatureManager design tree.
// 11. Click Tools > Options > System Options > Stop VSTA debugger on macro exit > OK
// 
// NOTE: Because the assembly is used elsewhere, do not save changes.
// ---------------------------------------------------------------------------- 
 
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Diagnostics;
 
namespace Macro1.csproj
{
    partial class SolidWorksMacro
    {
        ModelDoc2 swModel;
        public AssemblyDoc swAssembly;
        SketchManager swSketchManager;
        ModelDocExtension swModelDocExt;
        SketchSegment swSketchSegment;
        FeatureManager swFeatureManager;
        Feature swFeature;
        SelectionMgr swSelectionMgr;
        string fileName;
        int errors;
        int warnings;
 
        public void Main()
        {
            swApp.SetUserPreferenceToggle((int)swUserPreferenceToggle_e.swStopDebuggingVstaOnExit, false);
 
            // Open assembly
            fileName = "C:\\Users\\Public\\Documents\\SOLIDWORKS\\SOLIDWORKS 2018\\samples\\tutorial\\smartcomponents\\stepped_shaft.sldasm";
            swModel = (ModelDoc2)swApp.OpenDoc6(fileName, (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, ""ref  errors, ref warnings);
 
            // Insert a cut-extrude assembly feature
            swModelDocExt = (ModelDocExtension)swModel.Extension;
            swModelDocExt.SelectByID2("Front Plane""PLANE", 0, 0, 0, false, 0, null, 0);
            swModel.ClearSelection2(true);
            swSketchManager = (SketchManager)swModel.SketchManager;
            swSketchManager.InsertSketch(true);
            swSketchSegment = (SketchSegment)swSketchManager.CreateCircle(-0.016076, -0.532382, 0.0, 0.044465, -0.546417, 0.0);
            swModel.ClearSelection2(true);
            swModelDocExt.SelectByID2("Arc1""SKETCHSEGMENT", 0, 0, 0, false, 0, null, 0);
            swFeatureManager = (FeatureManager)swModel.FeatureManager;
            swFeature = (Feature)swFeatureManager.FeatureCut3(truefalsefalse, 0, 0, 0.00254, 0.00254, falsefalsefalsefalse, 0.0174532925199433, 0.0174532925199433, falsefalsefalsefalsefalsetruetruetruetruefalse, 0, 0, false);
            swSelectionMgr = (SelectionMgr)swModel.SelectionManager;
            swSelectionMgr.EnableContourSelection = false;
            swModel.ClearSelection2(true);
 
            // Event notification 
            swAssembly = (AssemblyDoc)swModel;
            AttachEventHandlers();
 
            // Rebuild the model 
            swModel.ForceRebuild3(true);
 
            // Roll back the rollback bar
            swFeatureManager.EditRollback((int)swMoveRollbackBarTo_e.swMoveRollbackBarToBeforeFeature, swFeature.Name);
        }
 
        public void AttachEventHandlers()
        {
            AttachSWEvents();
        }
 
        public void AttachSWEvents()
        {
            swAssembly.RegenNotify += this.swAssembly_RegenNotify;
            swAssembly.RegenPostNotify2 += this.swAssembly_RegenPostNotify2;
        }
 
        private int swAssembly_RegenNotify()
        {
            // Display message before rebuild 
            System.Windows.Forms.MessageBox.Show("A rebuild pre-notification event was fired.");
            return 0;
        }
 
        private int swAssembly_RegenPostNotify2(object stopFeature)
        {
            // Display message after rebuild 
            if ((stopFeature != null))
            {
                Feature feature = default(Feature);
                feature = (Feature)stopFeature;
                Debug.Print("The rollback bar is above " + feature.Name + " in the FeatureManager design tree.");
            }
            System.Windows.Forms.MessageBox.Show("A rebuild post-notification event was 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:   Fire Assembly Rebuild Events 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) 2018 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.