Message 1 of 3
Not able to get mass in iPart table

Not applicable
10-24-2018
09:19 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Having struggled a lot in bringing mass property in ipart table in order to get configuration drawings, through some source I got a macro code which I was asked to run. but I got error while executing the same. kindly help me to resolve the issue. Below is the macro I got
Public Sub UpdateWeightOfiParts()
‘ Get the active document. This assumes it is a part.
Dim oPartDoc As PartDocument
Set 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
Set oFactory = oPartDoc.ComponentDefinition.iPartFactory
' Check that there's a "Weight" column in the iPart table,
‘ Get the active document. This assumes it is a part.
Dim oPartDoc As PartDocument
Set 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
Set 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, "Weight")
If iWeightColumnIndex = -1 Then
MsgBox "The column ""weight"" 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
' Get the weight.
Dim dWeight As Double
dWeight = oPartDoc.ComponentDefinition.MassProperties.Mass
' Convert it to current mass units defined by the document.
Dim strWeight As String
strWeight = oPartDoc.UnitsOfMeasure.GetStringFromValue( _
dWeight, kDefaultDisplayMassUnits)
' Set the row value for the weight column.
oRow.Item(iWeightColumnIndex).Value = strWeight
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.
Private 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
Set 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
Dim iWeightColumnIndex As Long
iWeightColumnIndex = GetColumnIndex(oFactory, "Weight")
If iWeightColumnIndex = -1 Then
MsgBox "The column ""weight"" 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
' Get the weight.
Dim dWeight As Double
dWeight = oPartDoc.ComponentDefinition.MassProperties.Mass
' Convert it to current mass units defined by the document.
Dim strWeight As String
strWeight = oPartDoc.UnitsOfMeasure.GetStringFromValue( _
dWeight, kDefaultDisplayMassUnits)
' Set the row value for the weight column.
oRow.Item(iWeightColumnIndex).Value = strWeight
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.
Private 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
Set 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