Hi,
I'm using VBA in Inventor 2010. I want to create a macro that adds 3 fields to a part for LRNGTH, WIDTH and THICKNESS, as well as comments and setting the export to be export out. My program is below and works well. However i would like to end the macro with the parameter dialog box open. What code do i need to do this?
Sub AddBomFields()
'adds LENGTH, WIDTH and THICKNESS user parameters to part
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.ActiveDocument
Dim oCompDef As PartComponentDefinition
Set oCompDef = oPartDoc.ComponentDefinition
Dim oUserParam As UserParameter
Set oUserParam = oCompDef.Parameters.UserParameters.AddByValue("LENGTH", 0, kInchLengthUnits)
Set oUserParam = oCompDef.Parameters.UserParameters.AddByValue("WIDTH", 0, kInchLengthUnits)
Set oUserParam = oCompDef.Parameters.UserParameters.AddByValue("THICKNESS", 0, kInchLengthUnits)
' Get the Parameters object. Assumes a part or assembly document is active.
Dim oParameters As Parameters
Set oParameters = ThisApplication.ActiveDocument.ComponentDefinition.Parameters
' Get the parameter named "LENGTH".
Dim oLengthParam As Parameter
Set oLengthParam = oParameters.Item("LENGTH")
oLengthParam.Comment = "BOM LENGTH"
oLengthParam.ExposedAsProperty = True
' Get the parameter named "WIDTH".
Dim oWidthParam As Parameter
Set oWidthParam = oParameters.Item("WIDTH")
oWidthParam.Comment = "BOM WIDTH"
oWidthParam.ExposedAsProperty = True
' Get the parameter named "THICKNESS".
Dim oThicknessParam As Parameter
Set oThicknessParam = oParameters.Item("THICKNESS")
oThicknessParam.Comment = "BOM THICKNESS"
oThicknessParam.ExposedAsProperty = True
ThisApplication.ActiveDocument.Update
Dim Msg, Style, Title, Help, Ctxt, Response
Msg = "Length, Width and Thickness parameters added." ' Define message.
Style = vbOKOnly ' Define buttons.
Title = "Parameter Addition Complete" ' Define title.
' Display message.
Response = MsgBox(Msg, Style, Title)
End Sub
Solved! Go to Solution.
Solved by barbara.han. Go to Solution.
Solved by barbara.han. Go to Solution.
To fire a command, you need to find the internal name of that command, then call the Execute method on the corresponding ControlDefinition object.
Here is the code to fire Parameters dialog:
ThisApplication.CommandManager.ControlDefinitions.Item("AppParametersCmd").Execute
You can use the following code to get all internal names of all commands:
Sub test()
Dim oControlDef As ControlDefinition
Open "c:\1.txt" For Output As #1
Dim s As String
For Each oControlDef In ThisApplication.CommandManager.ControlDefinitions
s = oControlDef.InternalName & ":" & oControlDef.DescriptionText
Write #1, s
Next
Close #1
End Sub
Barbara Han
Developer Technical Services
Autodesk
Is there a way to condense my code to allow the export and comments to be added at the same time as the parameter is created?
I finally got time to look into this thread again. You can simplify your code so that there is no need to read the parameter later in order to change its properties. Here is the simplified version:
Sub AddBomFields()
'adds LENGTH, WIDTH and THICKNESS user parameters to part
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.ActiveDocument
Dim oCompDef As PartComponentDefinition
Set oCompDef = oPartDoc.ComponentDefinition
Dim oUserParam As UserParameter
Set oUserParam = oCompDef.Parameters.UserParameters.AddByExpression("LENGTH", 0, kInchLengthUnits)
oUserParam.Comment = "BOM LENGTH"
oUserParam.ExposedAsProperty = True
Set oUserParam = oCompDef.Parameters.UserParameters.AddByValue("WIDTH", 0, kInchLengthUnits)
oUserParam.Comment = "BOM WIDTH"
oUserParam.ExposedAsProperty = True
Set oUserParam = oCompDef.Parameters.UserParameters.AddByValue("THICKNESS", 0, kInchLengthUnits)
oUserParam.Comment = "BOM THICKNESS"
oUserParam.ExposedAsProperty = True
ThisApplication.CommandManager.ControlDefinitions.Item("AppParametersCmd").Execute
End Sub
Thank you Barbara, I understand VB well enough, it's just getting the Inventor sysntax down.
Yes, it would be even simpler if AddByVaule or an other API has more parameters to set all those properties in a line of code, but unluckily no that API. How it should not be difficult to use currect way to do the same.
Barbara,
I have the following code to add a length field. How do I set the unit string to off so the "in" does not display?
Sub AddBomFieldsL()
'adds LENGTH, WIDTH and THICKNESS user parameters to part
Dim oPartDoc As PartDocument
Set oPartDoc = ThisApplication.ActiveDocument
Dim oCompDef As PartComponentDefinition
Set oCompDef = oPartDoc.ComponentDefinition
Dim oUserParam As UserParameter
Set oUserParam = oCompDef.Parameters.UserParameters.AddByExpression("LENGTH", 0, kInchLengthUnits)
oUserParam.Comment = "BOM LENGTH"
oUserParam.ExposedAsProperty = True
ThisApplication.CommandManager.ControlDefinitions.Item("AppParametersCmd").Execute
End Sub
Answered my own question:
oUserParam.CustomPropertyFormat.ShowUnitsString = False
oUserParam.CustomPropertyFormat.Precision = kTwoDecimalPlacesPrecision