Rules for selection set of sketch lines and calculating the length.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
We had some digitized patterns with a lot of small lines, and I needed to get the total lengths for different sections. The obvious method was to use reference dimensions and add them up in a user parameter, but that would've been quite visually distracting and tedious to check the equation.
So, I have created these rules, one to create/modify a selection set that is saved in the part file. Second one gets the lengths of all the lines and adds it to a user parameter for future use, third one can display the lines to verify what is selected.
Seems to be working quite well, no reason it couldn't be modified to create custom selection sets for faces, edges, etc. May be helpful to someone else.
1. Create selection Set
Sub Main()
Dim oDoc = ThisDoc.Document
oCustomPropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
userParams = ThisDoc.Document.ComponentDefinition.Parameters.UserParameters
Dim SelectionSet As New ArrayList
For Each oProp In oCustomPropertySet
If oProp.name.Contains("RefKeys") Then
SelectionSet.Add(oProp.name)
End If
Next
SelectionSet.Add("Create New?")
SelectedParam = InputListBox("", SelectionSet, SelectionSet, Title := "Available Parameters", ListName := "Please select an Option")
If SelectedParam = "Create New?" Then
myparam = InputBox("Enter the selection set name", "Title", "Test")
iProperties.Value("Custom", myparam & "_RefKeys") = ""
Try
oParameter = userParams.AddByValue(myparam & "_Value", "", UnitsTypeEnum.kMillimeterLengthUnits)
Catch
'Parameter already exists
End Try
SelectionSet.Add(myparam & "_RefKeys")
SelectedParam = myparam & "_RefKeys"
End If
Dim oHSet As HighlightSet = oDoc.CreateHighlightSet()
oHSet.Color = ThisApplication.TransientObjects.CreateColor(185, 0, 0) 'Red
Start :
Dim entity = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchCurveFilter, "Select a line")
oDoc.SelectSet.Select(entity)
oLine = oDoc.SelectSet.Item(1)
Dim refKey() As Byte = New Byte() {}
oLine.GetReferenceKey(refKey)
Dim strRefKey As String = ThisDoc.Document.ReferenceKeyManager.KeyToString(refKey)
Dim strAccumulatedRefKey As String = strAccumulatedRefKey & "," & strRefKey
oHSet.AddItem(oLine)
Q = MessageBox.Show("Select another edge?", "Create Selection Set", MessageBoxButtons.YesNo)
oDoc.SelectSet.Clear()
If Q = vbYes Then
GoTo Start
End If
iProperties.Value("Custom", SelectedParam) = strAccumulatedRefKey
oHSet.Clear()
End Sub
2. Compute selection sets
Sub Main()
Dim oDoc = ThisDoc.Document
userParams = ThisDoc.Document.ComponentDefinition.Parameters.UserParameters
oCustomPropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
Dim SelectionSet As New ArrayList
Dim strParamNameArray() As String
For Each oProp In oCustomPropertySet
If oProp.name.Contains("RefKeys") Then
SelectionSet.Add(oProp.name)
End If
Next
If SelectionSet.Count <= 0 Then
Exit Sub
End If
For i = 0 To SelectionSet.Count - 1
strParamNameArray = SelectionSet(i).Split("_")
ParameterName = strParamNameArray(0)
ParamExists = False
Dim strRefKeysArray() As String
strRefKeysArray = iProperties.Value("Custom", SelectionSet(i)).Split(",")
AccumulatedLength = 0
For j = 1 To strRefKeysArray.Count - 1
Dim strRefKey As String = strRefKeysArray(j)
Dim refKeyMgr As ReferenceKeyManager = oDoc.ReferenceKeyManager
Dim edgeRefKey = New Byte() {}
Call refKeyMgr.StringToKey(strRefKey, edgeRefKey)
Dim foundObject As Object = refKeyMgr.BindKeyToObject(edgeRefKey)
If TypeOf foundObject Is Inventor.SketchLine Then
Dim foundLine As SketchLine
foundLine = refKeyMgr.BindKeyToObject(edgeRefKey)
Length = foundLine.Length
ElseIf TypeOf foundObject Is Inventor.SketchArc Then
Dim foundArc As SketchArc
foundArc = refKeyMgr.BindKeyToObject(edgeRefKey)
Length = foundArc.Length
Else
Length = 0
'Found object type is not supported
End If
AccumulatedLength += Length
Next
For Each oParam In userParams
If oParam.Name.Contains(ParameterName & "_Value") Then
ParamExists = True
Parameter.Param(oParam.Name).Value = AccumulatedLength
End If
Next
If ParamExists = False Then
'MessageBox.Show("ParameterName: " & ParameterName, "Title")
oParameter = userParams.AddByValue(ParameterName & "_Value", AccumulatedLength, UnitsTypeEnum.kMillimeterLengthUnits)
End If
Next
RuleParametersOutput()
iLogicVb.UpdateWhenDone = True
End Sub
3. Display Selection Sets
Sub Main()
Dim oDoc = ThisDoc.Document
userParams = ThisDoc.Document.ComponentDefinition.Parameters.UserParameters
oCustomPropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties")
Dim SelectionSet As New ArrayList
Dim strParamNameArray() As String
Dim oHSet As HighlightSet = oDoc.CreateHighlightSet()
oHSet.Color = ThisApplication.TransientObjects.CreateColor(185, 0, 0) 'Red
For Each oProp In oCustomPropertySet
If oProp.name.Contains("RefKeys") Then
SelectionSet.Add(oProp.name)
End If
Next
If SelectionSet.Count <= 0 Then
Exit Sub
End If
For i = 0 To SelectionSet.Count - 1
strParamNameArray = SelectionSet(i).Split("_")
ParameterName = strParamNameArray(0)
ParamExists = False
Dim strRefKeysArray() As String
strRefKeysArray = iProperties.Value("Custom", SelectionSet(i)).Split(",")
AccumulatedLength = 0
For j = 1 To strRefKeysArray.Count - 1
Dim strRefKey As String = strRefKeysArray(j)
Dim refKeyMgr As ReferenceKeyManager = oDoc.ReferenceKeyManager
Dim edgeRefKey = New Byte() {}
Call refKeyMgr.StringToKey(strRefKey, edgeRefKey)
' Bind back the item.
Dim foundEdge As Object
foundEdge = refKeyMgr.BindKeyToObject(edgeRefKey)
oHSet.AddItem(foundEdge)
Next
MessageBox.Show("Lines displayed for: " & SelectionSet(i))
oHSet.Clear()
Next
RuleParametersOutput()
iLogicVb.UpdateWhenDone = True
End Sub