The mass values can be queried from Partslist and we can iterate through all rows and sum up all mass column values. We can create a custom property named "Mass" with PropID 99 and this property can be put into Title Block as TextBox object. We can also put some static text like "Total Weight" or "Total Mass" to the left of custom property.
The Advantage of using Custom property in Title Block is that it gets updated automatically when the property value is modified. The sample code demonstrates same.
If the drawing does not contain mass column in PartsList then you
can add one by following the procedure shown below
- Select PartsList
- Right click and Select "Edit PartsList..."
- Select Column Chooser Icon (2nd from left)
- In the left Combo box select PartsList
- At the list select "MASS" and click on "Add->" button
Sub UpdateMassInTitleBlock()
' The sample code calculates total
' weight from partslist and adds a
' custom property "mass" to drawing
' document, this custom property is
' then added to title block definition.
' Make sure that Mass column is
' added to partslist
' The custom property is added once
' to title block definition, whenever the
' value of custom property changes
' the value is automatically updated in
' Title block
' This sample should be tested on
' a drawing which has C/A2 sheet along
' With PartsList
' The co-ordinated of static text
' and property value are based on sheet size
Dim oDoc As Document = _
m_inventorApplication.ActiveDocument
If (oDoc.DocumentType = DocumentTypeEnum. _
kDrawingDocumentObject) Then
Dim oDrgDoc As DrawingDocument
oDrgDoc = oDoc
'sum up the mass from partslist
Dim oPartsList As PartsList
oPartsList = oDrgDoc.ActiveSheet. _
PartsLists.Item(1)
' Assuming that last column is mass
Dim dTotalWt As Double
For i = 1 To oPartsList.PartsListRows.Count
Dim nColInd As Long
nColInd = oPartsList.PartsListColumns.Count
' get weight from column , it also contains units
Dim sVal As String
sVal = oPartsList.PartsListRows. _
Item(i).Item(nColInd).ToString() ' CHNAGE 1
Dim pos As Integer
pos = InStrRev(sVal, " ")
' Get the units of mass value
Dim sUnits As String
sUnits = Mid(sVal, pos + 1)
sVal = LSet(sVal, pos - 1) 'chnage 2
If (sUnits = "lbmass") Then
dTotalWt = dTotalWt + CDbl(sVal) _
* (1.0# / 2.2) 'convert to Kg
Else
dTotalWt = dTotalWt + CDbl(sVal)
End If
Next
' Add the custom property mass if not present
' Get User Defined propertySet
Dim oPropSet As PropertySet
oPropSet = oDoc.PropertySets(
"User Defined Properties")
Dim oProp As Inventor.Property
oProp = oPropSet.ItemByPropId(99)
'change 2
Try
Call oPropSet.Add(dTotalWt, "Mass", 99)
Catch ex As Exception
oProp.Value = dTotalWt
End Try
' Check whether 'Total Weight' label is present
Dim oTitleDef As TitleBlockDefinition
oTitleDef = oDrgDoc.ActiveSheet. _
TitleBlock.Definition
Dim oTextB As TextBox
For Each oTextB In oTitleDef.Sketch.TextBoxes
If oTextB.Text = "Total Weight" Then
oDoc.Save()
Exit Sub
End If
Next
Dim oTG As TransientGeometry
oTG = m_inventorApplication.TransientGeometry
'Start edit of Title Block
Dim oSketch As DrawingSketch = Nothing
Call oTitleDef.Edit(oSketch)
' Add static text
Dim sText As String
sText = "<StyleOverride FontSize='.35'>" + _
"Total Weight</StyleOverride>"
Dim oTextBox As TextBox
oTextBox = oSketch.TextBoxes.AddFitted(
oTG.CreatePoint2d(43, 5), sText)
oTextBox.VerticalJustification = _
VerticalTextAlignmentEnum.kAlignTextMiddle
oTextBox.HorizontalJustification = _
HorizontalTextAlignmentEnum.kAlignTextLeft
' Add some property Value
' In the statement below Property
' Document= 'drawing' means that
' custom property is part of this
' drawing document.
' FormatID is GUID of User
' Defined(properties)
' Finally value 99 in PropertyID
' is value we assigned to Mass property
' The property value updates
' automatically in the title block when
' the value of property is modified.
' GUID of user defined properties
'{D5CDD505-2E9C-101B-9397-08002B2CF9AE}
sText = "<StyleOverride FontSize='0.35'>" + _
"<Property Document='drawing' " + _
"FormatID=" + _
"'{D5CDD505-2E9C-101B-9397-08002B2CF9AE}' " + _
"PropertyID='99' /></StyleOverride>"
oTextBox = oSketch.TextBoxes.AddFitted( _
oTG.CreatePoint2d(46, 5), sText)
oTextBox.VerticalJustification = _
VerticalTextAlignmentEnum.kAlignTextMiddle
oTextBox.HorizontalJustification = _
HorizontalTextAlignmentEnum.kAlignTextLeft
' End editing
Call oTitleDef.ExitEdit(True)
End If
End Sub