Hi Georg.
I'm glad you asked.
I hope this code helps you out.
'On Error Resume Next
'part from line
Dim oDoc As Inventor.Document
oDoc = ThisApplication.ActiveDocument
'Dim oSelectSet As Inventor.SelectSet
'oSelectSet = oDoc.SelectSet
' Get the selected curve from the select set.
Dim oDrawCurveSeg As DrawingCurveSegment
oDrawCurveSeg = ThisApplication.CommandManager.Pick _
(Inventor.SelectionFilterEnum.
kDrawingCurveSegmentFilter,
"Select a drawingCurve")
' Get the parent DrawingCurve
Dim oDrawCurve As DrawingCurve
Try
oDrawCurve = oDrawCurveSeg.Parent
Catch ex As Exception
Exit Sub
End Try
' Get the model geometry this curve represents.
Dim oModelGeom As Object
oModelGeom = oDrawCurve.ModelGeometry
' Check to see if the returned object supports
' the ContainingOccurrence property.
Dim oOcc As ComponentOccurrence
'oOcc = oModelGeom.ContainingOccurrence
Dim oModelGeometry =
oDrawCurveSeg.Parent.ModelGeometry
Dim oCompDef As ComponentDefinition
Try
oCompDef = oModelGeometry.parent.componentdefinition
partName = oCompDef.Document.DisplayName
oOcc = oModelGeom.ContainingOccurrence
'Get the drawing view
Dim oDrwView As DrawingView
oDrwView = oDrawCurve.Parent
Catch ex As Exception
MessageBox.Show("Problem using part: " & partName, "iLogic")
Exit Sub
End Try
'Dim oPart As String
oPart = oCompDef.Document.DisplayName & ".ipt"
Dim AutoType As String
Dim uom as UnitsOfMeasure = oDoc.UnitsOfMeasure
Dim oRB As Box
Try
oRB = oOcc.RangeBox
Catch ex As Exception
MessageBox.Show("error with dims", "iLogic")
End Try
Dim minp As point = oRB.minPoint
Dim maxp As point = oRB.maxPoint
Dim AutoName As String
AutoName = "-"
InventorVb.DocumentUpdate()
Try
oCheckFile = iProperties.Value(oPart, "Project", "Part Number")
Catch
MessageBox.Show("Error finding file for part: " & partName, "iLogic")
Exit Sub
End Try
'Get part dimensions
dp=0
X = Round(uom.ConvertUnits ((maxP.X - minP.X), "cm", uom.LengthUnits), dp)
Y = Round(uom.ConvertUnits ((maxP.Y - minP.Y), "cm", uom.LengthUnits), dp)
Z = Round(uom.ConvertUnits ((maxP.Z - minP.Z), "cm", uom.LengthUnits), dp)
'MessageBox.Show("Extents: " & X & " : " & Y & " : " & Z, "iLogic")
'order dimensions largest to smallest
Thickness = MinOfMany(X, Y, Z)
Length = MaxOfMany(X, Y, Z)
Width = X
If Thickness = X Or Length = X Then
Width = Y
If Thickness = Y Or Length = Y Then
Width = Z
End If
End If
'get mass prop for beams and channel
StockNoPropLast = Right((iProperties.Value(oPart, "Project", "Stock Number")), Len(iProperties.Value(oPart, "Project", "Stock Number"))-InStrRev(iProperties.Value(oPart, "Project", "Stock Number"), "x"))
'get type
Try
AutoType = Parameter(oPart, "Type")
Catch ex As Exception
'Test parameters
Try
parametro = Parameter(oPart, "G_NG")
G_NG = 1
Catch
G_NG = 0
End Try
Try
parametro = Parameter(oPart, "G_T")
G_T = 1
Catch
G_T = 0
End Try
Try
parametro = Parameter(oPart, "G_W")
G_W = 1
Catch
G_W = 0
End Try
Try
parametro = Parameter(oPart, "G_ER")
G_ER = 1
Catch
G_ER = 0
End Try
Try
parametro = Parameter(oPart, "G_T1")
G_T1 = 1
Catch
G_T1 = 0
End Try
If G_NG = 1 Then
AutoType = "PFC"
ElseIf G_T1 = 0 And G_W = 1 And G_ER = 0 And G_T = 1 Then
AutoType = "RHS"
ElseIf G_T = 1 And G_W = 0 Then
AutoType = "CHS"
ElseIf G_T = 0 And G_W = 0 Then
AutoType = "RD BAR"
ElseIf G_T1 = 1 And G_NG = 0 Then
AutoType = "UC"
ElseIf G_ER = 1 Then
AutoType = "RSA"
ElseIf G_W = 1 And G_T = 0 Then
AutoType = "F BAR"
Else
AutoType = ""
End If
End Try
oType = InputBox("Treat as:", "Part Type", AutoType)
'set default name
Select Case UCase(oType)
Case "PLATE"
AutoName = "PLT " & Thickness & " THK " & Length & " x " & Width
Case "F BAR"
AutoName = "F/BAR " & Width & " x " & Thickness
Case "CHS"
Try
AutoName = "CHS " & Parameter(oPart, "G_H") & " OD x " & Parameter(oPart, "G_T") & " WT"
Catch
MessageBox.Show("Error getting parameters", "iLogic")
AutoName = ""
End Try
Case "SHS", "RHS"
Try
If Parameter(oPart, "G_W") = Parameter(oPart, "G_H") Then
AutoName = "SHS " & Parameter(oPart, "G_H") & " x " & Parameter(oPart, "G_W") & " x " & Parameter(oPart, "G_T")
Else
AutoName = "RHS " & Parameter(oPart, "G_H") & " x " & Parameter(oPart, "G_W") & " x " & Parameter(oPart, "G_T")
End If
Catch
MessageBox.Show("Error getting parameters", "iLogic")
AutoName = ""
End Try
Case "RD BAR"
Try
AutoName = "RD BAR DIA. " & Parameter(oPart, "G_H")
Catch
MessageBox.Show("Error getting parameters", "iLogic")
AutoName = ""
End Try
Case "RSA"
Try
AutoName = "RSA " & Parameter(oPart, "G_H") & " x " & Parameter(oPart, "G_W") & " x " & Parameter(oPart, "G_T")
Catch
MessageBox.Show("Error getting parameters", "iLogic")
AutoName = ""
End Try
Case "PFC"
Try
AutoName = "PFC " & Parameter(oPart, "G_H") & " x " & Parameter(oPart, "G_W") & " x " & StockNoPropLast
Catch
MessageBox.Show("Error getting parameters", "iLogic")
AutoName = ""
End Try
Case "UC"
Try
AutoName = "UC " & Replace(iProperties.Value(oPart, "Project", "Stock Number"), "x", " x ")
Catch
MessageBox.Show("Error getting parameters", "iLogic")
AutoName = ""
End Try
Case "UB"
Try
AutoName = "UB " & Replace(iProperties.Value(oPart, "Project", "Stock Number"), "x", " x ")
Catch
MessageBox.Show("Error getting parameters", "iLogic")
AutoName = ""
End Try
Case Else
AutoName = ""
End Select
oNewDescription = InputBox("Description for '" & partName & "':", "Description Editor", AutoName)
If oNewDescription = "" Then
MessageBox.Show("Description change cancelled.", "iLogic")
Else
iProperties.Value(oPart, "Project", "Description") = oNewDescription
InventorVb.DocumentUpdate()
iLogicVb.RunRule("Dimensions - Individual")
End If
A few notes:
- Dimensions of non-content centre parts are aligned to the assembly's User Coordinate System, so tilted components will show incorrect extents. I'm currently working on this.
- I'm no code expert and alot of this rule was stitched together from a large variety of sources. Code is quite untidy in places and probably does not use the most efficient methods.
- Rule works by selecting a line from an assembly view. It doesn't currently work for part views unless they are part-isolated views from an assembly.
In saying that, this code has worked perfectly for me thus far, and is going to be a BIG time saver.
If you have any probs, let me know.