Hide Table of Contents

Roll Back Model Example (C#)

This example shows how to step through the FeatureManager design tree of a model by rolling back to each feature in reverse sequence. Running an example like this can provide insight into the design intent of the user.

//-----------------------------------
// Preconditions:
// 1. Open a part or assembly document.
// 2. Open the Immediate window.
// 3. Run the macro.
//
// Postconditions:
// 1. Observe the FeatureManager design tree
//    while the macro executes.
// 2. Examine the Immediate window to see the
//    names of the features rolled back and forward.
//
// NOTE: The delay between steps is set to 1 second.
//-----------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
using System.Collections;
 
 
namespace EditRollbackCSharp.csproj
{
    public partial class SolidWorksMacro
    {
 
 
        public void Main()
        {
            ModelDoc2 swModel = default(ModelDoc2);
            ModelView swModelView = default(ModelView);
            PartDoc swPart = default(PartDoc);
            AssemblyDoc swAssy = default(AssemblyDoc);
            FeatureManager swFeatMgr = default(FeatureManager);
            Feature swFeat = default(Feature);
            object[] featFace = null;
            Face2 swFace = default(Face2);
            object rect = null;
            string featureName = null;
            ArrayList featName = new ArrayList();
            string nameString;
            int docType = 0;
            int i = 0;
            int j = 0;
            bool status = false;
 
            swModel = (ModelDoc2)swApp.ActiveDoc;
            swModelView = (ModelView)swModel.ActiveView;
            rect = null;
            swFeatMgr = (FeatureManager)swModel.FeatureManager;
 
            swFeat = (Feature)swModel.FirstFeature();
            docType = swModel.GetType();
            switch (docType)
            {
                case (int)swDocumentTypes_e.swDocPART:
                    swPart = (PartDoc)swModel;
                    break;
                case (int)swDocumentTypes_e.swDocASSEMBLY:
                    swAssy = (AssemblyDoc)swModel;
                    break;
                default:
                    Debug.Print("Open a part or assembly document, then rerun this macro.");
                    break;
            }
 
 
 
            while ((swFeat != null))
            {
                featureName = swFeat.Name;
                featName.Add(featureName);
                swFeat = (Feature)swFeat.GetNextFeature();
            }
 
            // Playback each feature in the FeatureManager design tree
            for (i = 0; i <= ((featName.Count) - 1); i++)
            {
                nameString = (string)featName[i];
                Debug.Print(nameString);
                status = swFeatMgr.EditRollback((int)swMoveRollbackBarTo_e.swMoveRollbackBarToAfterFeature, nameString);
                // Do not assert because you may be trying to roll back or roll forward
                // to a feature that cannot be rolled back or forward to; for example,
                // the Lighting or Annotations folder        
                //Debug.Assert status
 
                // Remove any previous highlights
                swModelView.GraphicsRedraw(rect);
                // Highlight feature if it has any geometry
                switch (docType)
                {
                    case (int)swDocumentTypes_e.swDocPART:
                        swPart = (PartDoc)swModel;
                        swFeat = (Feature)swPart.FeatureByName(nameString);
                        break;
                    case (int)swDocumentTypes_e.swDocASSEMBLY:
                        swAssy = (AssemblyDoc)swModel;
                        swFeat = (Feature)swAssy.FeatureByName(nameString);
                        break;
                }
 
                featFace = (object[])swFeat.GetFaces();
                if (featFace != null)
                {
                    for (j = 0; j <= ((featFace.GetUpperBound(0))); j++)
                    {
                        swFace = (Face2)featFace[j];
                        swFace.Highlight(true);
                    }
                }
 
                // Only pause if rollback is successful
                if (status)
                {
                    // Pause for 1 second
                    System.Threading.Thread.Sleep(1000);
                    // Allow SOLIDWORKS to refresh screen
                    System.Windows.Forms.Application.DoEvents();
                }
            }
            // Remove highlight from last feature
            swModelView.GraphicsRedraw(rect);
 
        }
 
        /// <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:   Roll Back Model 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) 2015 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.