MACRO LIBRARY

Replace referenced entity in reference plane

Free public access. No account or subscription required.

Files

Replace referenced entity in reference plane

Code

Module: mMain

'Demonstrates how to replace a referenced entity in a reference plane.
    
'Preconditions: "replace ref plane constraint.SLDPRT" or _
    "replace ref plane constraint pre2010.SLDPRT" is open and a face is selected.
'Result: Plane1 is re-associated with the selected face.
    
'Notes:
'Planes created pre-2010 are constrained differently than planes created in _
    2010+ and therefore require different API calls to modify selections. _
    See remarks in IRefPlaneFeatureData for differences between two.
    
'Written by Keith Rice
'CADSharp LLC
'www.cadsharp.com
    
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swPart As SldWorks.PartDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swSelObj As Object
Dim swFeat As SldWorks.Feature
Dim swData As SldWorks.RefPlaneFeatureData
    
Sub main()
    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    Set swPart = swModel
    Set swFeat = swPart.FeatureByName("Plane1")
    Set swSelMgr = swModel.SelectionManager
    Set swSelObj = swSelMgr.GetSelectedObject6(1, -1)
    
    If swFeat Is Nothing Or swSelObj Is Nothing Then Exit Sub
    
    Set swData = swFeat.GetDefinition
    swData.AccessSelections swModel, Nothing
    
    If swData.Type2 = swRefPlaneConstraintBase Then
        swData.Reference(swRefPlaneReference_First) = swSelObj
    Else
        Dim swRefEnt(0) As Object
        Set swRefEnt(0) = swSelObj
        swData.Selections = swRefEnt
    End If
    
    swFeat.ModifyDefinition swData, swModel, Nothing
End Sub