Not able to get mass in iPart table

Not able to get mass in iPart table

Anonymous
Not applicable
419 Views
2 Replies
Message 1 of 3

Not able to get mass in iPart table

Anonymous
Not applicable

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,
    ' 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
0 Likes
420 Views
2 Replies
Replies (2)
Message 2 of 3

j_weber
Mentor
Mentor

Hi

the code work perfect, but in the VBA environment as an VBA macro.

Paste the code over the VBA editor in the module1 of the part and run the code




Jörg Weber
CAD Systemtechniker für AutoCAD, Inventor, Vault





Message 3 of 3

JamieVJohnson2
Collaborator
Collaborator

Here is an Ilogic stored (vb.net hybrid ver 7+) code option we use to capture mass and store in a parameter exported to custom iProperty.  You can't use the name "Mass" in your custom iProperty, or Inventor will just ignore it, so we used CusMass (custom mass) as a name, anything not already used will do.  This does not do what the other does cycling through all the parts, so you will need to add that to get the full desired result.

Dim aDoc As PartDocument = ThisDoc.Document
Dim cd As ComponentDefinition = aDoc.ComponentDefinition
Dim pCusMass As Parameter
    Try
        pCusMass = cd.Parameters.UserParameters.Item("CusMass")
    Catch
        pCusMass = cd.Parameters.UserParameters.AddByValue("CusMass", 0, UnitsTypeEnum.kLbMassMassUnits)
    End Try
    With pCusMass
        .ExposedAsProperty = True
        .CustomPropertyFormat.PropertyType = CustomPropertyTypeEnum.kTextPropertyType
        .CustomPropertyFormat.Precision = CustomPropertyPrecisionEnum.kZeroDecimalPlacePrecision
        .CustomPropertyFormat.Units = UnitsTypeEnum.kLbMassMassUnits
        .CustomPropertyFormat.ShowUnitsString = False
    End With
	pCusMass.Value = iProperties.Mass / 2.204623

BTW as said earlier, the other code is VBA (version 6-).  The evidence is in the use of the Set statement, vb.Net doesn't use that anymore.

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/