Hide Table of Contents

Create Linear Pattern Example (C#)

This example shows how to create a linear-pattern feature using preselected direction features, preselected pattern seed features, and variable spacing between pattern instances.

//----------------------------------------------------------------------------
// Preconditions: Open install_dir\samples\tutorial\api\varyinstance.sldprt
//
// Postconditions: A linear-pattern feature is created.
//
// NOTE: Because the 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;
 
namespace CreateLinearPatternCSharp.csproj
{
    partial class SolidWorksMacro
    {
        public void Main()
        {
            ModelDoc2 swModel = default(ModelDoc2);
            ModelDocExtension swModelDocExt = default(ModelDocExtension);
            FeatureManager swFeatureManager = default(FeatureManager);
            bool boolstatus = false;
            int status = 0;
 
            swModel = (ModelDoc2)swApp.ActivateDoc3("varyInstance.sldprt"false, (int)swRebuildOnActivation_e.swUserDecision, ref status);
            swModelDocExt = (ModelDocExtension)swModel.Extension;
 
            swModel.ClearSelection2(true);
 
            boolstatus = swModelDocExt.SelectByID2("Cut-Extrude1""BODYFEATURE", 0.00774979914490359, 0.00364333445241982, -0.0354413541641527, false, 4, null, 0);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to select a seed feature"); }
 
            boolstatus = swModelDocExt.SelectByID2("""EDGE", -5.99771410065841E-05, 0.0688469352660661, -0.0402202172175059, true, 1, null, 0);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to select an edge for direction 1"); }
 
            boolstatus = swModelDocExt.SelectByID2("Fillet1""BODYFEATURE", 0.000829480066556698, 0.0045558314310199, -0.0352439589983078, true, 4, null, 0);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to select a seed feature"); }
 
            boolstatus = swModelDocExt.SelectByID2("""EDGE", -4.73572225700991E-06, -0.0401494819566892, -0.0490816550993962, true, 2, null, 0);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to select an edge for direction 2"); }
 
            swFeatureManager = (FeatureManager)swModel.FeatureManager;

            boolstatus = swFeatureManager.InsertVaryInstanceIncrement("D1@Sketch2@varyInstance.SLDPRT", 2, 1, 0, 0.003);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to add an increment value to dimension D1@Sketch2@varyInstance.SLDPRT in direction 1"); }
 
            boolstatus = swFeatureManager.InsertVaryInstanceIncrement("D1@Cut-Extrude1@varyInstance.SLDPRT", 2, 1, 0, -0.001);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to add an increment value to dimension D1@Cut-Extrude1@varyInstance.SLDPRT in direction 1"); }
 
            boolstatus = swFeatureManager.InsertVaryInstanceIncrement("Space Increment", 2, 2, 0, 0.01);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to add an increment value to direction 1 spacing"); }
 
            boolstatus = swFeatureManager.InsertVaryInstanceIncrement("D1@Sketch2@varyInstance.SLDPRT", 2, 1, 1, 0.004);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to add an increment value to dimension D1@Sketch2@varyInstance.SLDPRT in direction 2"); }
 
            boolstatus = swFeatureManager.InsertVaryInstanceIncrement("D1@Fillet1@varyInstance.SLDPRT", 2, 1, 1, 0.0001);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to add an increment value to dimension D1@Fillet1@varyInstance.SLDPRT in direction 2"); }
 
            boolstatus = swFeatureManager.InsertVaryInstanceIncrement("Space Increment", 2, 2, 1, 0.015);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to add an increment value to direction 2 spacing"); }
 
            boolstatus = swFeatureManager.InsertVaryInstanceOverride("D1@Sketch2@varyInstance.SLDPRT", 2, 1, -1, 4, 1, 0.05);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to override value of dimension D1@Sketch2@varyInstance.SLDPRT at instance (4, 1)"); }
 
            boolstatus = swFeatureManager.InsertVaryInstanceOverride("D1@Fillet1@varyInstance.SLDPRT", 2, 1, -1, 4, 1, 0.005);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to override value of dimension D1@Fillet1@varyInstance.SLDPRT at instance (4, 1)"); }
 
            boolstatus = swFeatureManager.InsertVaryInstanceOverride("D1@Fillet1@varyInstance.SLDPRT", 2, 1, -1, 2, 3, 0.004);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to override value of dimension D1@Fillet1@varyInstance.SLDPRT at instance (2, 3)"); }
 
            boolstatus = swFeatureManager.InsertVaryInstanceOverride("Space Increment", 2, 2, 0, 4, 1, 0.13);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to override value of direction 1 spacing increment at instance (4, 1)"); }
 
            boolstatus = swFeatureManager.InsertVaryInstanceOverride("Space Increment", 2, 2, 1, 2, 3, 0.15);
            if (boolstatus == false) { ErrorMsg(swApp, "Failed to override value of direction 2 spacing increment at instance (2, 3)"); }
 
            Feature swFeature = default(Feature);
            swFeature = (Feature)swFeatureManager.FeatureLinearPattern3(5, 0.02, 4, 0.025, falsetrue"NULL""NULL"falsetrue);
            if (swFeature == null) { ErrorMsg(swApp, "Failed to create a vary instance linear pattern"); }
        }
 
        public string ErrorMsg(SldWorks swApp, string Message)
        {
            swApp.SendMsgToUser2(Message, 0, 0);
            swApp.RecordLine("'*** WARNING - General");
            swApp.RecordLine("'*** " + Message);
            swApp.RecordLine("");
            return "";
        }
 
 
        /// <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:   Create Linear Pattern 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.