- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello All
I am having issues trying to bind a refkey to a part edge selected within the assembly context.
What I am trying to achieve is to store selection sets inside an assembly file by writing the refkeys and key context to custom iproperties. I can then query the election sets later to get information like total length of edges, etc.
Rule 1: Prompts to select part edges within an assembly, as each edge is selected it adds it to a string variable separated by comma's. Once the selection is complete, write the string variable to a custom iProperty where it can be retrieved later. Likewise for the key context.
Rule 2: Checks the custom iProperties to see if any selection sets exist, if yes then split the string variable by the comma's to get each individual refkey. Once I have the refkey I should be able to retrieve the object (edge) and get my length info, etc.
The problem I am having is getting the object from the refkey and key context. It keeps throwing an error on this line.
foundEdge = refKeyMgr.BindKeyToObject(edgeRefKey, refKeyContext)
I am a bit stuck on this.
I have used the same rules to do something similar for selecting sketch elements inside a part file (see: Rules for selection set of sketch lines and calculating the length. - Autodesk Community - Inventor) and that has worked fine, but it doesn't need the keycontext. I think this is where it is going wrong.
I have been referencing this post (https://modthemachine.typepad.com/my_weblog/2015/09/understanding-reference-keys-in-inventor.html).
Any help is appreciated.
Rule 1:
Sub Main() If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then MessageBox.Show("This rule can only be run in an .iam environment" & vbNewLine & "The rule will exit...", "Error Handling",MessageBoxButtons.OK,MessageBoxIcon.Error) Return End If ' Get the active document. Dim oDoc = ThisDoc.Document ' Set a reference to the ReferenceKeyManager object. Dim refKeyMgr As ReferenceKeyManager = oDoc.ReferenceKeyManager ' Create a key context. Dim keyContext As Long = refKeyMgr.CreateKeyContext ' Get the key context as an array of bytes and ' convert it to a string. Dim contextArray() As Byte = New Byte() {} refKeyMgr.SaveContextToArray(keyContext, contextArray) Dim strContext As String strContext = refKeyMgr.KeyToString(contextArray) iProperties.Value("Custom", "RefKeyContext") = strContext Dim SelectionSet As New ArrayList oCustomPropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties") userParams = ThisDoc.Document.ComponentDefinition.Parameters.UserParameters For Each oProp In oCustomPropertySet If oProp.name.Contains("RefKeys") Then SelectionSet.Add(oProp.name) End If Next SelectionSet.Add("Add new selection set") SelectedParam = InputListBox("", SelectionSet, SelectionSet, Title := "Available Parameters", ListName := "Please select an Option") If SelectedParam = "Add new selection set" 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 oEdge As Edge oEdge = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kPartEdgeFilter, "Select a edge to add to the combined measurement") Dim refKey() As Byte = New Byte() {} oEdge.GetReferenceKey(refKey, keyContext) Dim strRefKey As String = refKeyMgr.KeyToString(refKey) Dim strAccumulatedRefKey As String = strAccumulatedRefKey & "," & strRefKey oHSet.AddItem(oEdge) 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
Rule 2:
'https://forums.autodesk.com/t5/inventor-programming-ilogic/out-of-memory-error-while-working-with-referencekey/td-p/10590755 'https://modthemachine.typepad.com/my_weblog/2015/09/understanding-reference-keys-in-inventor.html Sub Main() 'On Error Resume Next If ThisApplication.ActiveDocumentType <> kAssemblyDocumentObject Then MessageBox.Show("This rule can only be run in an .iam environment" & vbNewLine & "The rule will exit...", "Error Handling",MessageBoxButtons.OK,MessageBoxIcon.Error) Return End If ' Get the active document Dim oDoc = ThisDoc.Document userParams = ThisDoc.Document.ComponentDefinition.Parameters.UserParameters oCustomPropertySet = oDoc.PropertySets.Item("Inventor User Defined Properties") oContinue = False For Each oProp In oCustomPropertySet If oProp.name.Contains("RefKeyContext") Then oContinue = True End If Next If oContinue = False Then MessageBox.Show("This document is missing essential information to execute this rule. Please ensure the selection sets have been created and the RefKeyContext property exists." & vbNewLine & "The rule will exit...", "Error Handling",MessageBoxButtons.OK,MessageBoxIcon.Error) Return End If ' Get a reference to the reference key manager. Dim refKeyMgr As ReferenceKeyManager = oDoc.ReferenceKeyManager ' Read the reference key strings from the file. Dim context As String = iProperties.Value("Custom", "RefKeyContext") ' Convert the string to byte arrays. Dim contextArray() As Byte = New Byte() {} refKeyMgr.StringToKey(context, contextArray) ' Create the context by loading the data. Dim refKeyContext As Long refKeyContext = refKeyMgr.LoadContextFromArray(contextArray) Dim SelectionSet As New ArrayList 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 ParameterName = SelectionSet(i).Replace("_RefKeys","") ParamExists = False Dim strRefKeysArray() As String = iProperties.Value("Custom", SelectionSet(i)).Split(",") AccumulatedLength = 0 For j = 1 To strRefKeysArray.Count - 1 Dim strRefKey As String = strRefKeysArray(j) Dim edgeRefKey() As Byte = New Byte() {} refKeyMgr.StringToKey(strRefKey, edgeRefKey) Dim foundEdge As Edge foundEdge = refKeyMgr.BindKeyToObject(edgeRefKey, refKeyContext) '********* ' Dim oEdge As Edge ' 'oEdge = oDoc.SelectSet.Item(1) ' oEdge = refKeyMgr.BindKeyToObject(edgeRefKey) ' Dim oEval As CurveEvaluator = oEdge.Evaluator ' Dim oMin, oMax, oLength As Double ' oEval.GetParamExtents(oMin, oMax) ' oEval.GetLengthAtParam(oMin, oMax, oLength) '*********** ' If TypeOf foundObject Is Inventor.Edge Then ' 'MessageBox.Show("Message", "Title") ' Dim foundLine As Edge ' 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 += oLength 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
Solved! Go to Solution.