iFeature insertion using parameters from the iFeature PartDocument

By Wayne Brill

If you have an iFeature that has parameters you want to use when you add the iFeature you can get the parameters from the ide file. This VBA and VB.NET code do this.

VBA

Public Function InsertiFeatureWithDefaultParameters _
    (ByVal oPartCompDef As PartComponentDefinition, _
                  ByVal iFeatureFileName As String, _
                         ByVal InputGeometry As Face)

    ‘Create the definition object for the specified
    ‘ide file.
    Dim oFeatureDef As iFeatureDefinition
    Set oFeatureDef = oPartCompDef. _
                             ReferenceComponents. _
                              iFeatureComponents. _
                 CreateDefinition(iFeatureFileName)
   
    ‘Open iFeature file in the background
    ‘to access its parameters
    Dim oFeatureDoc As PartDocument
    Set oFeatureDoc = ThisApplication.Documents _
                     .Open(iFeatureFileName, False)
   
    Dim oFeatureParams As Parameters
    Set oFeatureParams = oFeatureDoc. _
                     ComponentDefinition.Parameters
   
    Dim oFeatureInput As iFeatureInput
   
    ‘Iterate through iFeature inputs
    For Each oFeatureInput In oFeatureDef. _
                                    iFeatureInputs
   
    Select Case LCase(oFeatureInput.Prompt)
   
    Case "pick profile plane"
   
        If Not (TypeOf oFeatureInput Is _
                    iFeatureSketchPlaneInput) Then
            Debug.Print _
               "Invalid input: WorkPlane required"
            Exit Function
        End If
   
        Dim oWorkPlaneInput As _
                         iFeatureSketchPlaneInput
   
        Set oWorkPlaneInput = oFeatureInput
        oWorkPlaneInput.PlaneInput = InputGeometry
       
    Case Else
   
        Dim oParamInput As iFeatureParameterInput
        Set oParamInput = oFeatureInput
   
        If InStr(oParamInput.Expression, "ul") <> 0 Then
            ‘Do Nothing: unitless default value
        Else
            Set oParamInput = oFeatureInput
            Dim oParameter As Parameter
            ‘Retrieves parameter value and affects
            ‘ it to the iFeature’s input
        For Each oParameter In oFeatureParams
        ‘Not equal zero = "is in the string"
        If InStr(oParameter.Expression, _
         oParamInput.DefaultExpression) <> 0 Then
            oParamInput.Value = oParameter.Value
            Exit For
        End If
       
        Next
        End If
   
    End Select
    Next
   
    Call oFeatureDoc.Close(True)
   
    ‘Add the iFeature
    Call oPartCompDef.ReferenceComponents. _
              iFeatureComponents.Add(oFeatureDef)

End Function

Public Sub iFeatureParameterTest()

    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.ActiveDocument
   
    Dim iFeatureFileName As String
    iFeatureFileName = _
             "C:TempiFeatureWithDependency.ide"
   
    Dim oFace As Face
    Set oFace = oPartDoc.ComponentDefinition. _
                       SurfaceBodies(1).Faces(1)
   
    Call InsertiFeatureWithDefaultParameters _
(oPartDoc.ComponentDefinition, iFeatureFileName, oFace)

End Sub

VB.NET

Public Class Form1             Dim m_inventorApp As Inventor.Application _           
                               = Nothing             Private Sub Button1_Click(ByVal sender As  _                                    System.Object, _                      ByVal e As System.EventArgs) _                            Handles Button1.Click                 ' Get an active instance of Inventor            Try                m_inventorApp = System.Runtime. _                       InteropServices.Marshal. _           GetActiveObject("Inventor.Application")                 Catch 'Inventor not started                System.Windows.Forms.MessageBox. _                 Show("Start an Inventor session")                Exit Sub            End Try                 'Call the Sub            InsertiFeatureWithDefaultParameters()        End Sub        Public Sub InsertiFeatureWithDefaultParameters()                 Dim oPartDoc As PartDocument            oPartDoc = m_inventorApp.ActiveDocument                 Dim iFeatureFileName As String            iFeatureFileName = _                     "C:iFeatureWithDependency.ide"                 Dim oPartCompDef As PartComponentDefinition            oPartCompDef = oPartDoc.ComponentDefinition                 Dim oFace As Face            oFace = oPartDoc.ComponentDefinition. _                             SurfaceBodies(1).Faces(1)                 'Create the definition object for the             'specified ide file.             Dim oFeatureDef As iFeatureDefinition            oFeatureDef = oPartCompDef. _                             ReferenceComponents. _                              iFeatureComponents. _                 CreateDefinition(iFeatureFileName)                 'Open iFeature file in the background             'to access its parameters             Dim oFeatureDoc As PartDocument            oFeatureDoc = m_inventorApp.Documents. _                        Open(iFeatureFileName, False)            Dim oFeatureParams As Parameters            oFeatureParams = oFeatureDoc. _                       ComponentDefinition.Parameters                 Dim oFeatureInput As iFeatureInput                 'Iterate through iFeature inputs              For Each oFeatureInput In oFeatureDef. _                                        iFeatureInputs                Dim oLowerPrompt As String                oLowerPrompt = LCase(oFeatureInput.Prompt)                If oLowerPrompt = "pick profile plane" _                    Or oLowerPrompt = _                                "pick sketch plane" Then                    If Not _    (TypeOf oFeatureInput Is iFeatureSketchPlaneInput) _                                                     Then                        Debug.Print _              ("Error Invalid input: WorkPlane required")                        Exit Sub                    End If                    Dim oWorkPlaneInput As  _                                iFeatureSketchPlaneInput                    oWorkPlaneInput = oFeatureInput                    oWorkPlaneInput.PlaneInput = oFace                Else                    Dim oParamInput As  _                                  iFeatureParameterInput                    oParamInput = oFeatureInput                         Dim oParamExpression As String = _                                 oParamInput.Expression                         If oParamExpression.Contains("ul") _                                                    Then                        'Do Nothing unitless value                    Else                        oParamInput = oFeatureInput                        Dim oParameter As Parameter                        For Each oParameter In _                                         oFeatureParams                                 If InStr _                                 (oParameter.Expression, _                     oParamInput.DefaultExpression)  0 _                                                      Then                            
    oParamInput.Value = _                                          oParameter.Value                                Exit For                            End If                        Next                    End If                End If            Next            Call oFeatureDoc.Close(True)            'Add the iFeature                Call oPartCompDef.ReferenceComponents. _                       iFeatureComponents.Add(oFeatureDef)        End Sub         End Class

Comments

2 responses to “iFeature insertion using parameters from the iFeature PartDocument”

  1. slachance Avatar
    slachance

    Hi!
    I try your vbnet code in ilogic but dont work for me?
    error with sub’s or function!

  2. slachance Avatar
    slachance

    it’s possible to have a iFeatures edge direction input same to that?
    Dim oFace As Face
    oFace = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartFaceFilter, “SELECTIONNE TON PART”)
    Dim oEdgeX As Edge
    oEdgeX = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, “SELECTIONNE TA DIRECTION EN X.”)
    Dim oEdgeY As Edge
    oEdgeY = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, “SELECTIONNE TA DIRECTION EN Y.”)
    Case “FACE”
    Dim oPlaneInput As iFeatureSketchPlaneInput
    oPlaneInput = oInput
    oPlaneInput.PlaneInput = oFace
    Case “DIRECTION X”
    Dim oEdgeInput As iFeatureSketchEdgeInput
    oEdgeInput = oInput
    oEdgeInput.EdgeInput = oEdgeX

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading