Instead of trying to add a user property to all of the parts in your assembly, I would suggest making two array lists: one of all of the different parts that can be considered parts that go on a cut list, and a second one that you would select the parts you want to go into. That array list can then be used to select parts for your cut list. If you are using members from the content center this is significantly easier since you can reference if the stock number iproperty is empty or not, and what the G_L or PL value is. I've attached what I use below, but it's just the first step really. I'm assuming you already have a plan for handling the cut list after this, hope it helps.
Imports System
AddReference "System.Drawing.dll"
Imports System.Windows.Forms
Imports Inventor
Public Class StockSelection
Shared Dim oStockList As New ArrayList
Shared Dim oChosenStockList As New ArrayList
Shared Dim oText As String
Shared Dim Y As Double = 0
Public Sub Main()
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
MessageBox.Show("This rule only works for assemblies.", "Wrong Document Type")
Return
End If
Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oSubADoc As AssemblyDocument
Dim oSubADef As AssemblyComponentDefinition
oStockList.Clear
oChosenStockList.Clear
'Dim oOccs As oADoc.ComponentDefinition.Occurrences
Dim oOccs As ComponentOccurrences
Dim oOcc As ComponentOccurrence
Dim oSubOccs As ComponentOccurrences
Dim oSubOcc As ComponentOccurrence
For Each oOcc In oADef.Occurrences
If oOcc.Suppressed Then
'GoTo SkipSuppressed
Else
End If
Dim oOccDoc As Document = oOcc.Definition.Document
Dim oPSets As PropertySets = oOccDoc.PropertySets
If oOccDoc.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
If oOccDoc.PropertySets("Design Tracking Properties")("Stock Number").Value = "" Then
GoTo awshit1
Else
End If
Dim oStockSize As String = oOccDoc.PropertySets("Design Tracking Properties")("Stock Number").Value & " - " & oOccDoc.PropertySets("Design Tracking Properties")("Material").Value
oStockList.Add(oStockSize)
OccurenceName = oOcc.Name
Dim CutLength As String
Try
CutLength = oOccDoc.PropertySets("Inventor User Defined Properties")("G_L").Value
Catch
Try
CutLength = oOccDoc.PropertySets("Inventor User Defined Properties")("PL").Value
Catch
GoTo awshit1
End Try
End Try
Dim oCutLength As Double = Val(CutLength)
Else
'MessageBox.Show(oOcc.Name.ToString, "Title")
oSubADoc = oOccDoc
oSubADef = oSubADoc.ComponentDefinition
For Each oSubOcc In oSubADef.Occurrences
Dim oSubOccDoc As Document = oSubOcc.Definition.Document
If oSubOccDoc.PropertySets("Design Tracking Properties")("Stock Number").Value = "" Then
GoTo awshit2
Else
End If
Dim oStockSize As String = oSubOccDoc.PropertySets("Design Tracking Properties")("Stock Number").Value & " - " & oSubOccDoc.PropertySets("Design Tracking Properties")("Material").Value
oStockList.Add(oStockSize)
OccurenceName = oOcc.Name
Dim CutLength As String
Try
CutLength = oSubOccDoc.PropertySets("Inventor User Defined Properties")("G_L").Value
Catch
Try
CutLength = oSubOccDoc.PropertySets("Inventor User Defined Properties")("PL").Value
Catch
GoTo awshit2
End Try
End Try
Dim oCutLength As Double = Val(CutLength)
awshit2 :
Next
End If
awshit1 :
Next
For i = oStockList.Count - 1 To 0 Step -1
If oStockList.IndexOf(oStockList.Item(i)) <> i Then oStockList.RemoveAt(i)
Next
For i As Integer = 0 To oStockList.Count - 1
If i = 0 Then
oText = oStockList(i)
Else
oText = oText & vbLf & oStockList(i)
End If
Next
'MessageBox.Show(oText)
CreateForm("Select Stock Sizes (cancel with esc)")
Form :
MyForm.ShowDialog()
Dim oUParams As UserParameters = ThisApplication.ActiveDocument.ComponentDefinition.Parameters.UserParameters
Dim oUParam As UserParameter
Dim sName As String = "ChosenStockList"
Try
oUParam = oUParams.Item(sName)
oUParam.Delete
MultiValue.SetList(sName, oChosenStockList.ToArray)
Catch
oUParam = oUParams.AddByValue(sName, "", UnitsTypeEnum.kTextUnits)
MultiValue.SetList(sName, oChosenStockList.ToArray)
End Try
iPropGet("List Type")
If oStockList.count = oChosenStockList.Count Then
iProperties.Value("Custom", "List Type") = "Full"
Else
iProperties.Value("Custom", "List Type") = "Partial"
End If
'For k As Integer = 0 To oChosenStockList.Count - 1
' If k = 0 Then
' oChosenText = oChosenStockList(k)
' Else
' oChosenText = oChosenText & vbLf & oChosenStockList(k)
' End If
' Next
'MessageBox.Show(oChosenText, "Selected Stock Sizes")
End Sub
Private WithEvents MyForm As System.Windows.Forms.Form
Private Button0 As System.Windows.Forms.Button
Private Label(0) As System.Windows.Forms.Label
Private Label0 As System.Windows.Forms.Label
Public Sub CreateForm(Optional Name As String = vbNullString)
MyForm = New System.Windows.Forms.Form
MyForm.Text = Name
MyForm.AutoScaleMode = AutoScaleMode.Dpi
MyForm.Size = New Drawing.Size(500, 400) 'Width, Heigth
MyForm.MinimumSize = MyForm.Size
MyForm.MaximumSize = MyForm.Size
MyForm.Font = New Drawing.Font(MyForm.Font.FontFamily, 10)
MyForm.MaximizeBox = False
MyForm.MinimizeBox = True
MyForm.ShowIcon = False
MyForm.SizeGripStyle = SizeGripStyle.Hide
MyForm.StartPosition = FormStartPosition.Manual
MyForm.KeyPreview = True
AddLabel(10, 5, "Select The Stock Sizes You Want To Include")
CheckBox()
ContinueButton()
Button0.TabIndex = 1
End Sub
Public Function CheckBox()
Y = 0
For i = 0 To oStockList.Count - 1
y = y + 25
Dim oCheckBox As New CheckBox()
With oCheckBox
.Text = oStocklist(i)
oCheckBox.Top = y
.Left = 25
.AutoSize = True
End With
AddHandler oCheckBox.CheckedChanged, AddressOf oCheckBox_CheckedChanged
MyForm.Controls.Add(oCheckBox)
Next
End Function
Public Function ContinueButton() As System.Windows.Forms.Button
Button0 = New System.Windows.Forms.Button
Button0.Name = " Button0 "
Button0.Location = New Drawing.Point(10, Y + 25)
Button0.Text = "Continue"
Button0.Width = 465
Button0.Height = 25
MyForm.Controls.Add(Button0)
AddHandler Button0.Click, AddressOf Button0Click
Return Button0
End Function
Public Sub Button0Click()
MyForm.Close
End Sub
Private Sub MyForm_FormClosing(ByVal sender As Object, ByVal E As System.Windows.Forms.FormClosedEventArgs) Handles MyForm.FormClosed
If sender.Name = "CloseButton" Then
'MessageBox.Show(sender.Name)
End If
End Sub
Private Sub MyForm_KeyDown(ByVal sender As System.Object, ByVal E As System.Windows.Forms.KeyEventArgs) Handles MyForm.KeyDown
If E.KeyCode = Keys.Escape Then
MyForm.Close
ExitSub = True
End If
End Sub
Private Function AddLabel(PosX As Integer, PosY As Integer, Optional Caption As String = vbNullString, Optional Width As Integer = 100) As System.Windows.Forms.Label
Dim LC As Integer = Label.Length - 1
If Not Label(LC) Is Nothing Then
LC = LC + 1
ReDim Preserve Label(LC)
End If
Label(LC) = New System.Windows.Forms.Label
Label(LC).Name = "L" & LC
Label(LC).Location = New Drawing.Point(PosX, PosY)
Label(LC).Text = Caption
Label(LC).AutoSize = True
Label(LC).Width = Width
MyForm.Controls.Add(Label(LC))
Return Label(LC)
End Function
Private Sub oCheckBox_CheckedChanged(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
'oSender is a CheckBox
If oSender.Checked = True Then
'MsgBox("You just Checked the [" & oSender.Text & "] checkbox.", vbOKOnly + vbInformation, "EVENTFEEDBACK")
oChosenStockList.Add(oSender.Text)
Else
' MsgBox("You just UnChecked the [" & oSender.Text & "] checkbox.", vbOKOnly + vbInformation, "EVENTFEEDBACK")
oChosenStockList.Remove(oSender.Text)
End If
End Sub
Function iPropGet(PropName As String, Optional MissingSetVal As Object = "", Optional Doc As Inventor.Document = Nothing) As Object
If Doc Is Nothing Then Doc = ThisDoc.Document
Dim CustomPropSet As Inventor.PropertySet = Doc.PropertySets.Item("Inventor User Defined Properties")
Try
Dim CurrentValue As Object = CustomPropSet.Item(PropName).Value
Return CurrentValue
Catch
Dim oTransaction As Inventor.Transaction
Try
oTransaction = ThisApplication.TransactionManager.StartTransaction(Doc, "Create property """ & PropName & """")
CustomPropSet.Add(MissingSetVal, PropName)
oTransaction.End
Return MissingSetVal 'Successfully created/set.
Catch
If Not oTransaction Is Nothing Then oTransaction.Abort
Return -1 'Unable to create.
End Try
Return MissingSetVal
End Try
End Function
End Class