I found some old VB code to run form an iPart, which once updating works well enough.
But it clashes with some other code in another routine that creates & formats the custom property I use.
I can't work out how to get the two to align so the result is the same.
This is the code that creates the custom property "c_Mass"
I manually add the field in the iPart Author but I have to select each member to generate the value for mass.
Dim c_Mass As Decimal = iProperties.Mass
Dim strMass As String
'
Select Case c_Mass
Case < 1
strMass = CStr(Round(c_Mass, 6, MidpointRounding.AwayFromZero))& " kg"
Case 1 To 10
strMass = CStr(Round(c_Mass, 2, MidpointRounding.AwayFromZero))& " kg"
Case > 10
strMass = CStr(Round(c_Mass, 1, MidpointRounding.AwayFromZero))& " kg"
End Select
'
iProperties.Value("Custom", "c_Mass")= strMass
I then found some more code which automates the process. I modified it so it ran properly with my custom property & with the latest version of VB.
Sub Main ()
' Get the active document. This assumes it is a part.
Dim oPartDoc As PartDocument
oPartDoc = ThisApplication.ActiveDocument
' Check that this part is an iPart factory.
If Not oPartDoc.ComponentDefinition.IsiPartFactory Then
MsgBox ("This part must be an iPart factory.")
Exit Sub
End If
Dim oFactory As iPartFactory
oFactory = oPartDoc.ComponentDefinition.iPartFactory
' Check that there's a "Weight" column in the iPart table,
' and get its index in the table.
Dim iWeightColumnIndex As Long
iWeightColumnIndex = GetColumnIndex(oFactory, "c_Mass")
If iWeightColumnIndex = -1 Then
MsgBox ("The column ""c_Mass"" does not exist in the table.")
Exit Sub
End If
' Iterate through the rows
Dim oRow As iPartTableRow
For Each oRow In oFactory.TableRows
' Make this the active row so the model will recompute.
oFactory.DefaultRow = oRow
' Set the row value for the weight column.
oRow.Item(iWeightColumnIndex).Value = strMass
Next
End Sub
'
' Function that given a factory and the name or a column will return
' the index number of the column, if it’s found in the factory’s
' table. If the column is not found it returns –1. The comparison
' of the name is done in a case insensitive way.
Function GetColumnIndex(ByVal Factory As iPartFactory, _
ByVal ColumnName As String) As Long
' Iterate through all of the columns looking for a
' match to the input name.
Dim i As Long
For i = 1 To Factory.TableColumns.Count
Dim oColumn As iPartTableColumn
oColumn = Factory.TableColumns.Item(i)
'
' Compare this column with the input name.
If LCase(oColumn.DisplayHeading) = LCase(ColumnName) Then
' A matching column was found so exit.
GetColumnIndex = i
Exit Function
End If
Next
' The column wasn't found so return -1.
GetColumnIndex = -1
End Function
This works OK, but the format of c_Mass is a lot of decimal places & no suffix, whereas the 1st bit of code returns 5 decimal places with the suffix "kg".
Un-commenting the "Get the Weight" lines (dMass etc) returns the right value with the suffix "kg" but only to 3 decimal places (which means the weights are either 0.000 or 0.001 when they should be 0.00013).
I tried adding the Select Case code to the automated process but it returned values of "0 kg" for every member.
I then found this bit of code:
Dim pCustMass As Parameter
Try
pCustMass = cd.Parameters.UserParameters.Item("c_Mass")
Catch
pCustMass = cd.Parameters.UserParameters.AddByValue("c_Mass", 0, UnitsTypeEnum.kGramMassUnits)
End Try
With pCusMass
.ExposedAsProperty = True
.CustomPropertyFormat.PropertyType = CustomPropertyTypeEnum.kTextPropertyType
.CustomPropertyFormat.Precision = CustomPropertyPrecisionEnum.kFiveDecimalPlacePrecision
.CustomPropertyFormat.Units = UnitsTypeEnum.kGramMassUnits
.CustomPropertyFormat.ShowUnitsString = False
'End With
But the "cd" bit cause problems.
I'm sure it's a relatively simple thing, but I can't fathom how to change the decimal places if the following is used:
' Get the weight.
Dim dMass As Double
dMAss = oPartDoc.ComponentDefinition.MassProperties.Mass
'
' Convert it to current mass units defined by the document.
Dim strMass As String
strMass = oPartDoc.UnitsOfMeasure.GetStringFromValue(dMass, kDefaultDisplayMassUnits)
Can't find what you're looking for? Ask the community or share your knowledge.