Hide Table of Contents

Add Attribute to Feature and Include in Library Feature Example (VB.NET)

This example shows how to add an attribute to a feature and include that attribute with the feature if the feature is saved as a library feature. This example also includes instructions on how to verify that the attribute is included on each instance of the library feature.


' Preconditions:

'   1. Open a new part document.

'   2. Sketch a rectangle and extrude it.

'   3. Sketch a straight slot that fits on a face of

'      of the just-created extrude and cut-extrude the slot.

'      The cut-extrude should be named Extrude2.


' Postconditions: The Extrude2 feature is added to the part document with

'                an attribute of TestAttribute, which is visible in the

'                FeatureManager design tree.


' To verify that the attribute is included in a library feature:

'   1. Drag the Extrude2 feature to the Design Library and

'      save the library feature.

'   2. Close, and optionally save, the part document.

'   3. Open a model document and drag-and-drop the just-created library

'      feature on the model.

'   4. Expand the just-dropped library feature in the FeatureManager design

'      tree.


'      Extrude2 and TestAttribute should appear beneath the

'      just-dropped library feature in the FeatureManager design tree.


'   5. Close, and optionally save, the model document.


Imports SolidWorks.Interop.sldworks

Imports SolidWorks.Interop.swconst

Imports System.Diagnostics

Imports System


Partial Class SolidWorksMacro


    Public Sub main()


        Dim swModel As ModelDoc2

        Dim swModelDocExt As ModelDocExtension

        Dim swSelectionMgr As SelectionMgr

        Dim swFeature As Feature

        Dim swAttribute As SolidWorks.Interop.sldworks.Attribute

        Dim swAttributeDef As AttributeDef

        Dim swFace As Face2

        Dim Faces As Object

        Dim bool As Boolean


        swModel = swApp.ActiveDoc

        swModelDocExt = swModel.Extension

        swSelectionMgr = swModel.SelectionManager


        ' Create attribute

        swAttributeDef = swApp.DefineAttribute("TestPropagationOfAttribute")

        bool = swAttributeDef.AddParameter("TestAttribute", swParamType_e.swParamTypeDouble, 2.0#, 0)

        bool = swAttributeDef.Register


        ' Select the feature to which to add the attribute

        bool = swModelDocExt.SelectByID2("Extrude2", "BODYFEATURE", 0, 0, 0, False, 0, Nothing, 0)

        swFeature = swSelectionMgr.GetSelectedObject6(1, -1)

        Debug.Print("Name of feature to which to add attribute: " & swFeature.Name)


        ' Add the attribute to one of the feature's faces

        Faces = swFeature.GetFaces

        swFace = Faces(0)

        swAttribute = swAttributeDef.CreateInstance5(swModel, swFace, "TestAttribute", 0, swInConfigurationOpts_e.swAllConfiguration)

        swAttribute.IncludeInLibraryFeature = True

        Debug.Print("Include attribute in library feature? " & swAttribute.IncludeInLibraryFeature)




    End Sub


    ''' <summary>

    ''' The SldWorks swApp variable is pre-assigned for you.

    ''' </summary>

    Public swApp As SldWorks


End Class

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

Subject:   Feedback on Help Topics
Page:   Add Attribute to Feature and Include in Library Feature Example (VB.NET)
*   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:


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

Web Help Content Version: API Help (English only) 2012 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.