Hide Table of Contents

Create Library Feature Data Object and Library Feature With References (VBA)

This example shows how to create a library feature with references in order to position the library feature on a model. First you create the library feature; then you access it to set references.

'-------------------------------------------
' Preconditions:
' 1. Model and library part exist.
' 2. Model is open.
' 3. Plane1 and Point1@Location Point sketch point features exist.
'
' Postconditions:
' 1. Library feature is defined and initialized.
' 2. Library feature is created on selected face on the model.
' 3. References are set for the library feature to position
'    it on the model.
'-------------------------------------------
Option Explicit
Dim swFeat As SldWorks.Feature
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swFeatMgr As SldWorks.FeatureManager
Dim swLibFeat As SldWorks.LibraryFeatureData
Dim selMgr As SldWorks.SelectionMgr
Dim boolstatus As Boolean
Dim obj() As Object
Dim vRefs As Variant, vRefTypes As Variant, refType As Variant
Dim nRefCount As Long
Private Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set selMgr = swModel.SelectionManager
    Set swFeatMgr = swModel.FeatureManager
    
    ' Create library feature
    Set swLibFeat = swFeatMgr.CreateDefinition(swFmLibraryFeature)
    ' Initialize newly created library feature using the specified library part
    boolstatus = swLibFeat.Initialize("C:\MyLibraryParts\SlotWithRefs.SLDLFP")      
    ' Get the type of references required for the library feature
    nRefCount = swLibFeat.GetReferencesCount
    vRefs = swLibFeat.GetReferences2(swLibFeatureData_FeatureRespect, vRefTypes)
    If Not IsEmpty(vRefTypes) Then
        Debug.Print "Types of references required: "
        For Each refType In vRefTypes
            Debug.Print vbTab + CStr(refType)
        Next
    End If    
    ' Set the name of the active library feature configuration
    swLibFeat.ConfigurationName = "Default"    
    ' Select the face where to create the library feature
    boolstatus = swModel.Extension.SelectByID2("", "FACE", -0.008955455851577, 0.01051592924882, 0.00400000000019, False, 0, Nothing, 0)
    ' Create the library feature
    Set swFeat = swFeatMgr.CreateFeature(swLibFeat)    
    ' Access the library feature to position it on the part
    Set swLibFeat = Nothing
    Set swLibFeat = swFeat.GetDefinition
    boolstatus = swLibFeat.AccessSelections(swModel, Nothing)    
    ' Select the point and plane where to position the library feature
    boolstatus = swModel.Extension.SelectByID2("Point1@Location Point", "EXTSKETCHPOINT", 0, 0, 0, True, 0, Nothing, 0)
    boolstatus = swModel.Extension.SelectByID2("Plane1", "PLANE", 0, 0, 0, True, 0, Nothing, 0)
    Dim selCount As Integer
    selCount = selMgr.GetSelectedObjectCount2(-1)
    selCount = selCount - 1    
    ReDim obj(selCount) As Object    
    Dim i As Integer
    For i = 0 To selCount
        Set obj(i) = selMgr.GetSelectedObject6(i + 1, -1)
    Next    
    ' Set the references for locating the library feature on the part
    Dim vLibRefs As Variant
    vLibRefs = obj
    swLibFeat.SetReferences (vLibRefs)    
    ' Update the definition of the library feature
    boolstatus = swFeat.ModifyDefinition(swLibFeat, swModel, Nothing)    
    swModel.ClearSelection2 True    
End Sub


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 Library Feature Data Object and Library Feature With References (VBA)
*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.