After testing the previous rule, I noticed that if the "size" iProperty is missing and the rule will error out. Here is the corrected version which adds the iProperty before attempting to assign the expression.
'http://inventortrenches.blogspot.com/2013/10/ilogic-how-to-learn-inventors.html
'https://forums.autodesk.com/t5/inventor-forum/add-create-external-rule-ilogic/td-p/6033312
'https://forums.autodesk.com/t5/inventor-forum/ilogic-that-tells-the-difference-between-parts-and-assemblies/td-p/6211252
DocType = ThisApplication.ActiveDocument.DocumentType
If DocType = DocumentTypeEnum.kAssemblyDocumentObject Then
MessageBox.Show("it's an assembly file - Exiting!", "iLogic")
ElseIf DocType = DocumentTypeEnum.kPartDocumentObject Then
'MessageBox.Show("it's a part file", "iLogic")
'Get the document we want to work with
Dim partDoc As PartDocument
partDoc = ThisApplication.ActiveDocument
Try
'https://modthemachine.typepad.com/my_weblog/2010/04/parameters-as-iproperties.html
'Export parameters as custom iproperty (Tick the box)
Dim param As Inventor.Parameter
Dim oFormat As CustomPropertyFormat
'[d0
param = partDoc.ComponentDefinition.Parameters.Item("d0")
param.ExposedAsProperty = True
oFormat = param.CustomPropertyFormat
oFormat.PropertyType = Inventor.CustomPropertyTypeEnum.kTextPropertyType
oFormat.Units = "in"
oFormat.ShowUnitsString = True
oFormat.Precision = Inventor.CustomPropertyPrecisionEnum.kSixteenthsFractionalLengthPrecision
'oFormat.ShowTrailingZeros = False
'oFormat.ShowLeadingZeros = False
']
'[d1
param = partDoc.ComponentDefinition.Parameters.Item("d1")
param.ExposedAsProperty = True
oFormat = param.CustomPropertyFormat
oFormat.PropertyType = Inventor.CustomPropertyTypeEnum.kTextPropertyType
oFormat.Units = "in"
oFormat.ShowUnitsString = True
oFormat.Precision = Inventor.CustomPropertyPrecisionEnum.kSixteenthsFractionalLengthPrecision
']
'[Thickness
param = partDoc.ComponentDefinition.Parameters.Item("Thickness")
param.ExposedAsProperty = True
oFormat = param.CustomPropertyFormat
oFormat.PropertyType = Inventor.CustomPropertyTypeEnum.kTextPropertyType
oFormat.Units = "in"
oFormat.ShowUnitsString = True
oFormat.Precision = Inventor.CustomPropertyPrecisionEnum.kSixteenthsFractionalLengthPrecision
']
Catch
MessageBox.Show("One of the parameters is missing", "iLogic")
End Try
' Get the User Defined property set.
Dim UserDefinedPropSet As PropertySet
UserDefinedPropSet = partDoc.PropertySets.Item("Inventor User Defined Properties")
' Get the Size property.
Dim SizeProp As Inventor.Property
Try
SizeProp = UserDefinedPropSet.Item("Size")
Catch
' Failed to get the property, which means it doesn't exist so we'll create it.
UserDefinedPropSet.Add("", "Size")
SizeProp = UserDefinedPropSet.Item("Size")
End Try
'Add an expression to custom property
SizeProp.Expression = _
"=<Thickness> X <d1> X <d0> LG."
MessageBox.Show("Size: " & SizeProp.Value, "iLogic- Success iProp Updated")
ElseIf DocType = DocumentTypeEnum.kDrawingDocumentObject Then
MessageBox.Show("it's a drawing file - Exiting!", "iLogic")
End If
And here is another version using VB.Net method instead of a pure iLogic rule. I have just place the parameter export into a sub routine as they have all the same formatting it makes the rule a little easier to read.
Sub Main
DocType = ThisApplication.ActiveDocument.DocumentType
If DocType = DocumentTypeEnum.kAssemblyDocumentObject Then
MessageBox.Show("it's an assembly file - Exiting!", "iLogic")
ElseIf DocType = DocumentTypeEnum.kPartDocumentObject Then
'Get the document we want to work with
Dim partDoc As PartDocument
partDoc = ThisApplication.ActiveDocument
Try
'Pass document and parameter to sub routine
ParameterExport(partDoc, "d0")
ParameterExport(partDoc, "d1")
ParameterExport(partDoc, "Thickness")
Catch
MessageBox.Show("One of the parameters is missing", "iLogic")
End Try
' Get the User Defined property set.
Dim UserDefinedPropSet As PropertySet
UserDefinedPropSet = partDoc.PropertySets.Item("Inventor User Defined Properties")
' Get the Size property.
Dim SizeProp As Inventor.Property
Try
SizeProp = UserDefinedPropSet.Item("Size")
Catch
' Failed to get the property, which means it doesn't exist so we'll create it.
UserDefinedPropSet.Add("", "Size")
SizeProp = UserDefinedPropSet.Item("Size")
End Try
'Add an expression to custom property
SizeProp.Expression = _
"=<Thickness> X <d1> X <d0> LG."
MessageBox.Show("Size: " & SizeProp.Value, "iLogic- Success iProp Updated")
ElseIf DocType = DocumentTypeEnum.kDrawingDocumentObject Then
MessageBox.Show("it's a drawing file - Exiting!", "iLogic")
End If
End Sub
Sub ParameterExport(oPartDoc As Document, oParam As String)
'Export parameters as custom iproperty (Tick the box)
Dim param As Inventor.Parameter
Dim oFormat As CustomPropertyFormat
param = oPartDoc.ComponentDefinition.Parameters.Item(oParam)
param.ExposedAsProperty = True
'Carry out formatting
oFormat = param.CustomPropertyFormat
oFormat.PropertyType = Inventor.CustomPropertyTypeEnum.kTextPropertyType
oFormat.Units = "in"
oFormat.ShowUnitsString = True
oFormat.Precision = Inventor.CustomPropertyPrecisionEnum.kSixteenthsFractionalLengthPrecision
End Sub
If this solved a problem, please click (accept) as solution.
Or if this helped you, please, click (like)
Regards
Alan