References not being held to selected objects in macros

References not being held to selected objects in macros

MBF-CAD
Participant Participant
950 Views
11 Replies
Message 1 of 12

References not being held to selected objects in macros

MBF-CAD
Participant
Participant

I'm running into a bit of a frustrating issue with a few macros I've written that I'm starting to create larger add-ins from.  A lot of what I'm doing depends on user interaction.  I'll use the latest one as an example:

 

We have a way of creating cutlists for fabricated doors/jambs that is unique to the business I work for.  It's been done this way for 20+ years and it's what the guys out in the shop are used to.  Because of that, I am creating a custom table to mimic this way.  I'm starting simple, and will build up to automating each side of a truck and the doors it contains.  For the sake of simplicity, I am placing the door assembly on a sheet and selecting it to grab the AssemblyDocument from to create the table.  All works fine in that regard, and the table is created, but it doesn't update with a change in the door size.  I know there is an AddLink method, but it throws an error when I pass in the assembly document's FullFileName (thisCustomTable.AddLink(asmDoc.FullFileName).  Regardless, this also happens to me when extruding a profile - save, close, open the file back up, change the profile, and it fails.

 

I am positive this is something simple I must be missing, but I can't for the life of me figure it out.  I have used both CommandManager.Pick and also written my own class for SelectionEvents (similar to that in the documentation, but a little different).

 

Any insight into what I may be missing is greatly appreciated!

Accepted solutions (1)
951 Views
11 Replies
Replies (11)
Message 2 of 12

Anonymous
Not applicable

I think, I understood your post right. After you make a table all references to selected parts are gone. Therefore you will need to use ObjectCollection to "remember" what was selected. Here is an example:

Sub DimTest()

    Dim doc As DrawingDocument
    Set doc = ThisApplication.ActiveDocument

    Dim collec As ObjectCollection
    Set collec = ThisApplication.TransientObjects.CreateObjectCollection
    
    Dim obj As Variant
    For Each obj In doc.SelectSet
        collec.Add obj
    Next

    For Each obj In collec
        If TypeOf obj Is DrawingDimension Then
            Dim drawDim As DrawingDimension
            Set drawDim = obj
            drawDim.HideValue = True
            drawDim.OverrideModelValue = True
            drawDim.Text.FormattedText = "ADN rules!"
        End If
    Next

    For Each obj In collec
       doc.SelectSet.Select obj
    Next
End Sub
0 Likes
Message 3 of 12

Anonymous
Not applicable

First off, I posted the original post with the wrong account, so I apologize.

I used an ObjectCollection on the extrude routine (it was for extruding multiple bodies at once - really it was just iterative extrudes) and it doesn't hold.  I also just tried to push the ObjectsEnumerator into an ObjectCollection on the table program and it didn't change with a change in the door on that either.

0 Likes
Message 4 of 12

BrianEkins
Mentor
Mentor
Accepted solution

A reference to an entity is a somewhat fragile thing.  A reference certainly does not survive closing and re-opening a file.  In some cases, it also doesn't even survive an edit to the model.  If you have a reference to an object and will need to get to that same object again at some point in the future, there are two options.  One is to add an attribute to the object, effectively naming the object, and then using the attribute to find the object later.  The second is to get the objects reference key and save the reference key somewhere and then use the reference key to get the object again.  

 

Here's a blog post I wrote a while ago that discusses reference keys in detail and has a paragraph talking about attributes.  http://modthemachine.typepad.com/my_weblog/2015/09/understanding-reference-keys-in-inventor.html

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 5 of 12

Anonymous
Not applicable

@BrianEkins

Thank you for the response!  The article was very informative.  In the 3-ish months I've been deep-diving into Inventor's API, I've read many of your articles but somehow missed this one.  Is it safe to assume that this is how associativity is retained in the in-built functionality such as Extrude?

0 Likes
Message 6 of 12

mjordan3
Advocate
Advocate

@BrianEkins, @Anonymous

 

I'd love to find a solution for this as well.  I detailed this problem here: extrudefeature failure on sketch edit but have not figured out a solution yet.  I've read the article you wrote Brian, but am unsure how that would be used with an ExtrudeFeature.  I've modifed your example code from the article (shown below) but it doesn't fix the error -- after closing and reopening the part file the ExtrudeFeature's profile reference seems to be lost.

 

' Get the active document. 
Dim oPartDoc As Document 
    oPartDoc = ThisApplication.ActiveDocument 
    
' Have a face and edge selected. 
Dim selProfile As Profile
    selProfile = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kSketchProfileFilter, "Select face.")

' Set a reference to the ReferenceKeyManager object. 
Dim refKeyMgr As ReferenceKeyManager 
    refKeyMgr = oPartDoc.ReferenceKeyManager

' Create a key context. 
Dim keyContext As Long 
    keyContext = refKeyMgr.CreateKeyContext 

' Get reference keys from the selected entities. 
Dim profileRefKey(0) As Byte 
    selProfile.GetReferenceKey(profileRefKey)', keyContext) 

'Bind profile reference key to oProfile object
Dim oProfile As Profile
    oProfile = refKeyMgr.BindKeyToObject(profileRefKey)

' create new extrusion using the ref key
Dim oCompDef As PartComponentDefinition = oPartDoc.ComponentDefinition
Dim oExtrude As ExtrudeFeature
    oExtrude = oCompDef.Features.ExtrudeFeatures.AddByDistanceExtent(oProfile, "1/4", kPositiveExtentDirection, kNewBodyOperation)
0 Likes
Message 7 of 12

BrianEkins
Mentor
Mentor

@MBF-CAD, Regarding your question about if this is the same functionality that is used internally.  The answer is yes.  The reference keys exposed by the API is exposing some of the internal functionality.  There's a lot more than this to have a fully associative model, but this one piece.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 8 of 12

BrianEkins
Mentor
Mentor

@mjordan3, The problem in your case is how you've declared the array that you're using the hold the array of bytes that represents the reference key.  In VB.NET you can declare an empty array but you also need to initialize it.  Just change the line to this and your program will work.

Dim profileRefKey() As Byte = {}
---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 9 of 12

Anonymous
Not applicable

@BrianEkins The above code alone should work if the file is closed then reopened?  I was under the impression that the reference key would either have to be saved out to an external file or stored in an attribute of an object within the part document?  I ask because I did essentially this same thing this morning and it didn't keep the reference once I closed and reopened the file.  I just used close to this code in VBA with no joy too.  I also tried creating an attribute for the part document and then one for the extrude feature, storing the reference key within them and then using that to create the extrude.  Neither of those held the reference either.

0 Likes
Message 10 of 12

BrianEkins
Mentor
Mentor

@Anonymous, There appears to be a bug with profiles when using either reference keys or attributes.  A workaround would be to get some of the sketch entities that are used to define the profile and then bind back to those and use them to find the desired profile.  It's not trivial but I think it would be possible.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 11 of 12

Anonymous
Not applicable

@BrianEkins  I have no problem with non-trivial solutions (this morning I've been working on a C# add-in for copying and resizing assemblies composed of multiple frame generator and regular subassemblies and ipt's).  I very much appreciate all your help with this!

0 Likes
Message 12 of 12

mjordan3
Advocate
Advocate

Has anyone had any more progress on this?  Just curious.

 

Also, I don't have 2019 installed, so I don't know if this bug has been fixed in the new version of Inventor.  Does anyone know?

0 Likes