How to edit IFeature parameters via VBA?

Anonymous

How to edit IFeature parameters via VBA?

Anonymous
Not applicable

Hi, everyone.


Here is some of Inventor's Design Accelerator models with attached IFeature parameters:
C:\Program Files\Autodesk\Inventor 2014\Design Accelerator\Models\

 

For instance, I want to change values of IFeature params of SpurGear model: FD-SpurGearMM.ipt using VBA macro, where

 

da_aw is work Center Distance
da_b is width
da_beta is angle Beta
da_d Pitch is diameter
da_da is Outside Diameter
da_df is Root Diameter
da_dw is work pitch diameter
da_z is number of teeth for Gear1
da_z2 is number of teeth for Gear2
da_lefttooth 1/0 Yes/No Allow is left helix teeth
da_righttooth 1/0 Yes/No Allow is rigth helix teeth
da_straigthtooth 1/0 Yes/No Allow is straigth teeth

 

1.png

 

My search brings me to such source-code:

 

Sub Edit_iFeature_Parameters_Sample()
  Dim oDoc As PartDocument
  Set oDoc = ThisApplication.ActiveDocument

  'reference to the first iFeature object
  Dim oiFeature As iFeature
  Set oiFeature = oDoc.ComponentDefinition.Features.iFeatures.Item(1)

  'reference to the definition
  Dim iDef As iFeatureDefinition
  Set iDef = oiFeature.iFeatureDefinition

  'inputs collection
  Dim oInputs As iFeatureInputs
  Set oInputs = iDef.iFeatureInputs

  'root name
  Dim iName As String
  iName = oiFeature.Name & ":"

  'iterate inputs collection
  Dim oInput As iFeatureInput

  For Each oInput In oInputs


    'Debug.Print oInput.Name

    If oInput.Type = kiFeatureParameterInputObject Then
      Dim oParInput As iFeatureParameterInput
      Set oParInput = oInput

      Dim oPar As Parameter
      Set oPar = oParInput.Parameter

      Select Case UCase(oParInput.Name)
        Case UCase(iName + "Diameter")
            oPar.Expression = "1006 in"

        Case UCase(iName + "Depth")
            oPar.Expression = "0.6 in"
      End Select

    End If

  Next
  oDoc.Update
  Beep
End Sub

 

Please, help me modify this for my case, or suggest your own way of solving issue.

 

P.S. Thanks in advance and excuse me for my english

0 Likes
Reply
Accepted solutions (1)
1,918 Views
1 Reply
Reply (1)

Vladimir.Ananyev
Alumni
Alumni
Accepted solution

You should use correct input's names.

Could you please try this variant,  it changes "da_z" and "da_z2" parameters values.

 

Sub Edit_iFeature_Parameters_Sample()
'edit spur gear sample
'C:\Program Files\Autodesk\Inventor 2014\Design Accelerator\Models\FD-SpurGearMM.ipt

  Dim oDoc As PartDocument
  Set oDoc = ThisApplication.ActiveDocument

  'reference to the iFeature object by name
  Dim oiFeature As iFeature
  Set oiFeature = oDoc.ComponentDefinition.features.iFeatures.Item("Spur Gear")

  'reference to the iFeature definition
  Dim iDef As iFeatureDefinition
  Set iDef = oiFeature.iFeatureDefinition

  'inputs collection
  Dim oInputs As iFeatureInputs
  Set oInputs = iDef.iFeatureInputs

  'iterate inputs collection
  Dim oInput As iFeatureInput

  For Each oInput In oInputs
  
    'we need in parameter inputs only
    If oInput.Type = kiFeatureParameterInputObject Then
      Dim oParInput As iFeatureParameterInput
      Set oParInput = oInput

      Dim oPar As Parameter
      Set oPar = oParInput.Parameter
      
      Select Case UCase(oParInput.Name)
        Case UCase("da_z")
            oPar.Expression = "15 ul"
        Case UCase("da_z2")
            oPar.Expression = "21 ul"
      End Select
    End If
  Next
  oDoc.Update
  Beep
End Sub

 

BTW you may get the same result  changing parameter's values directly using Parameters collection methods.

 

  Dim oDoc As PartDocument
  Set oDoc = ThisApplication.ActiveDocument

  Dim oPars As Parameters
  Set oPars = oDoc.ComponentDefinition.Parameters

  Dim oPar As Parameter
  Set oPar = oPars.Item("da_z")
  oPar.Expression = "15 ul"

  Set oPar = oPars.Item("da_z2")
  oPar.Expression = "21 ul"
  
  oDoc.Update  ' apply new values

 cheers,

 


Vladimir Ananyev
Developer Technical Services
Autodesk Developer Network

0 Likes