Hide Table of Contents

Insert Variable Pattern Feature Example (C#)

This example shows how to insert a variable pattern feature.

//----------------------------------------------------------------------------
// Preconditions:
// 1. Verify that the specified part to open exists.
// 2. Open the Immediate window.
//
// Postconditions:
// 1. Inserts a variable pattern feature.
// 2. Examine the graphics area and the Immediate window.
//
// NOTE: Because this model is used elsewhere, do not save any
// changes when closing it.
//----------------------------------------------------------------------------
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Runtime.InteropServices;
using System;
using System.Diagnostics;
 
namespace IDimPatternFeatureDataCSharp.csproj
{
    public partial class SolidWorksMacro
    { 
 
        public void Main()
        {
            ModelDoc2 swModel = default(ModelDoc2);
            ModelDocExtension swModelDocExt = default(ModelDocExtension);
            FeatureManager swFeatureManager = default(FeatureManager);
            Feature swFeature = default(Feature);
            DimPatternFeatureData swDimensionDrivenTablePatternFeat = default(DimPatternFeatureData);
            string fileName = null;
            bool status = false;
            int errors = 0;
            int warnings = 0;
            int nbr = 0;
            int dimNbr = 0;
            int i = 0;
            int j = 0;
            string[] controllingDimNames = null;
            string controllingDimName = null;
            string instanceName = null;
            string[] instanceNames = null;
            string patternName = null;
 
            fileName = "C:\\Program Files\\SolidWorks Corp\\SolidWorks\\samples\\tutorial\\api\\cstick.sldprt";
            swModel = (ModelDoc2)swApp.OpenDoc6(fileName, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, ""ref errors, ref warnings);
            swModelDocExt = (ModelDocExtension)swModel.Extension;
 
            status = swModelDocExt.SelectByID2("Sweep1""BODYFEATURE", 0, 0, 0, true, 0, null, 0);
            status = swModelDocExt.SelectByID2("Sketch1""SKETCH", 0, 0, 0, true, 0, null, 0);
            swModel.ActivateSelectedFeature();
            swModel.ClearSelection2(true);
 
            //Select feature to pattern
            status = swModelDocExt.SelectByID2("Sweep1""BODYFEATURE", 0, 0, 0, false, 4, null, 0);
 
            //Select reference geometry to drive seed feature
            status = swModelDocExt.SelectByID2("Sketch1""SKETCH", 0, 0, 0, true, 1048576, null, 0);
 
            //Populate pattern table
            swFeatureManager = (FeatureManager)swModel.FeatureManager;
            status = swFeatureManager.InsertVaryInstanceOverride("D3@Sketch2@cstick.SLDPRT", 256, 1, 0, 1, 0, 0.04);
            status = swFeatureManager.InsertVaryInstanceOverride("D4@Sketch2@cstick.SLDPRT", 256, 1, 0, 1, 0, 0.03);
            status = swFeatureManager.InsertVaryInstanceOverride("D1@Sketch2@cstick.SLDPRT", 256, 1, 0, 2, 0, 0.105);
            status = swFeatureManager.InsertVaryInstanceOverride("D3@Sketch2@cstick.SLDPRT", 256, 1, 0, 2, 0, 0.06);
            status = swFeatureManager.InsertVaryInstanceOverride("D4@Sketch2@cstick.SLDPRT", 256, 1, 0, 2, 0, 0.05);
            //Insert the advanced variable pattern feature
            //and access its feature data
            swFeature = (Feature)swFeatureManager.FeatureAdvancedTableDrivenPattern();
            swDimensionDrivenTablePatternFeat = (DimPatternFeatureData)swFeature.GetDefinition();
            status = swDimensionDrivenTablePatternFeat.AccessSelections(swModel, null);

            nbr = swDimensionDrivenTablePatternFeat.GetInstanceCount();
            Debug.Print("Number of pattern instances: " + nbr);
            dimNbr = swDimensionDrivenTablePatternFeat.GetControllingDimensionCount();
            Debug.Print("Number of controlling dimensions: " + dimNbr);
            Debug.Print("  Controlling dimension names: ");
            controllingDimNames = new string[dimNbr + 1];
            for (i = 0; i <= dimNbr - 1; i++)
            {
                controllingDimNames[i] = swDimensionDrivenTablePatternFeat.GetControllingDimensionName(i);
                controllingDimName = controllingDimNames[i];
                Debug.Print("     " + controllingDimName);
            }
 
            Debug.Print("Names of pattern instances:");
            instanceNames = new string[nbr];
            j = 0;
            for (i = 1; i <= nbr; i++)
            {
                instanceName = swDimensionDrivenTablePatternFeat.GetInstanceNameByIndex(i);
                Debug.Print("  " + instanceName);
                instanceNames[j] = instanceName;
                j = j + 1;
            }
 
            Debug.Print("Table row indices of pattern instances:");
            for (i = 0; i <= nbr - 1; i++)
            {
                Debug.Print("  " + swDimensionDrivenTablePatternFeat.GetTableRowIndex(instanceNames[i]) + ": " + instanceNames[i]);
            }
 
            Debug.Print("Pattern dimension names: ");
            j = 1;
            for (i = 0; i <= nbr - 1; i++)
            {
                patternName = swDimensionDrivenTablePatternFeat.GetInstanceDimensionName(instanceNames[i], controllingDimNames[i + 1]);
                Debug.Print("  " + patternName + ": " + instanceNames[i] + ": Controlling dimension name: " + swDimensionDrivenTablePatternFeat.GetControllingDimensionName(i + 1));
                j = j + 1;
            }
 
            Debug.Print("Pattern instance suppression state:");
            for (i = 0; i <= nbr - 1; i++)
            {
                Debug.Print("  " + swDimensionDrivenTablePatternFeat.GetInstanceSuppressStateByIndex(swDimensionDrivenTablePatternFeat.GetTableRowIndex(instanceNames[i])) + ": " + instanceNames[i]);
            }

            swDimensionDrivenTablePatternFeat.ReleaseSelectionAccess();
 
        }
 
        /// <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:   Insert Variable Pattern Feature 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.