VBA Shrinkwrap how to use AdditionalExcludedOccurrences

VBA Shrinkwrap how to use AdditionalExcludedOccurrences

clutsa
Collaborator Collaborator
1,595 Views
8 Replies
Message 1 of 9

VBA Shrinkwrap how to use AdditionalExcludedOccurrences

clutsa
Collaborator
Collaborator

I'm trying to automate shrink-wrapping some parts. I managed to cobble together the code below and it works great for making a SW of the whole assembly... I need to exclude some parts however. If I manually create a SW, I see the parts I select in the AdditionalExcludedOccurrences as Varient/Object/ComponentOccurrence. In the long term I will create my list of parts to exclude based on their part number but for now I just what to figure out how to get any object into the AEO collection object.

I've tried hard coding a ComponentOccurrence in to the code below and I've tried using a CommandManager.Pick (as seen in code) I just can't figure out how to add an object to AdditionalExcludedOccurrences. 

Sub STPMain()
Dim app As Application
Set app = ThisApplication
Dim doc As AssemblyDocument
Set doc = app.ActiveDocument
Dim compDef As AssemblyComponentDefinition
Set compDef = doc.ComponentDefinition

'create new part
Dim SimpleName As String 'unused at this time
SimpleName = Left(doc.FullDocumentName, Len(doc.FullDocumentName) - 4)
Dim partDoc As PartDocument
Set partDoc = app.Documents.Add(kPartDocumentObject, , True)
doc.Activate
Dim partCompDef As PartComponentDefinition
Set partCompDef = partDoc.ComponentDefinition

'create collection of parts to exclude 'just one for now
Dim ExcludeGroup As ObjectCollection
Set ExcludeGroup = app.TransientObjects.CreateObjectCollection
Call ExcludeGroup.Add(app.CommandManager.Pick(kAssemblyLeafOccurrenceFilter, "pick object"))
Debug.Print ("ExcludeGroup count = " & ExcludeGroup.Count)

Dim SWD As ShrinkwrapDefinition
Set SWD = partCompDef.ReferenceComponents.ShrinkwrapComponents.CreateDefinition(doc.FullDocumentName)
SWD.CreateIndependentBodiesOnFailedBoolean = True
SWD.DeriveStyle = kDeriveAsSingleBodyNoSeams
SWD.RemoveInternalParts = True
Call SWD.AdditionalExcludedOccurrences.Add(ExcludeGroup.Item(1)) 'help on this line please
Debug.Print ("Excluded item count = " & SWD.AdditionalExcludedOccurrences.Count)

Dim SWComp As ShrinkwrapComponent
Set SWComp = partCompDef.ReferenceComponents.ShrinkwrapComponents.Add(SWD)

End Sub

 

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Accepted solutions (2)
1,596 Views
8 Replies
Replies (8)
Message 2 of 9

JamieVJohnson2
Collaborator
Collaborator
Accepted solution

Since AdditionalExcludedOccurrences is an ObjectCollection.  and it is read-write.  you should be able to create a new object collection (dim newObjectCollection as ObjectCollection ; set newObjectCollection = ThisApplication.TransientObjects.CreateObjectCollection), add your objects, then set AdditionalExcludedOccurrences = newObjectCollection

 

Or just use the existing AdditionalExcludedOccurrences object collection, and the .Add command it comes with.

 

If that fails try to set the newObjectCollection = to the existing AdditionalExcludedOccurrences object collection, then use .Add command 

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 3 of 9

clutsa
Collaborator
Collaborator

I know I tried that last week because I actually uncommented the code that made my new object collection... but I gave it another try today just to verify it didn't work and sure enough now it works.Smiley Frustrated An answer is an answer however and I'm glad to be moving on. Thanks

Still would like to know why the .Add function isn't working as written in original post.

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 4 of 9

chandra.shekar.g
Autodesk Support
Autodesk Support

@clutsa,

 

Basically, AdditionalExcludedOccurrences property is a read / write property whose value is an Objectcollection. So, object collection of excluding occurrence should be set to AdditionalExcludedOccurrences property as shown below in VBA code.

 

Sub STPMain()
    Dim app As Application
    Set app = ThisApplication
    Dim doc As AssemblyDocument
    Set doc = app.ActiveDocument
    Dim compDef As AssemblyComponentDefinition
    Set compDef = doc.ComponentDefinition
    
    'create collection of parts to exclude 'just one for now
    Dim ExcludeGroup As ObjectCollection
    Set ExcludeGroup = app.TransientObjects.CreateObjectCollection
    Call ExcludeGroup.Add(app.CommandManager.Pick(kAssemblyLeafOccurrenceFilter, "pick object"))
    Debug.Print ("ExcludeGroup count = " & ExcludeGroup.Count)
    
    'create new part
    Dim SimpleName As String 'unused at this time
    SimpleName = Left(doc.FullDocumentName, Len(doc.FullDocumentName) - 4)
    Dim partDoc As PartDocument
    Set partDoc = app.Documents.Add(kPartDocumentObject, , True)
    doc.Activate
    Dim partCompDef As PartComponentDefinition
    Set partCompDef = partDoc.ComponentDefinition
    
    Dim SWD As ShrinkwrapDefinition
    Set SWD = partCompDef.ReferenceComponents.ShrinkwrapComponents.CreateDefinition(doc.FullDocumentName)
    SWD.CreateIndependentBodiesOnFailedBoolean = True
    SWD.DeriveStyle = kDeriveAsSingleBodyNoSeams
    SWD.RemoveInternalParts = True
    SWD.AdditionalExcludedOccurrences = ExcludeGroup
    Debug.Print ("Excluded item count = " & SWD.AdditionalExcludedOccurrences.Count)

    Dim SWComp As ShrinkwrapComponent
    Set SWComp = partCompDef.ReferenceComponents.ShrinkwrapComponents.Add(SWD)

End Sub

Thanks and regards,


CHANDRA SHEKAR G
Developer Advocate
Autodesk Developer Network



0 Likes
Message 5 of 9

clutsa
Collaborator
Collaborator

Thank you @chandra.shekar.g, that is how I've updated my code but what I fail to understand is why the .Add function of my "ExcludeGroup" ObjectCollection works but the .Add function of "AdditionalExcludedOccurrences" fails. If they are both an ObjectCollection why don't their functions function the same?

In my head it works like this... If I go to a car dealership and buy two identical cars (one for me and one for my wife) and I get in my wife's car put the key in the ignition and the car starts as expected. I get in my car and the key fits in the ignition but won't start my car. We go back to the dealer and say "My wife's car works fine but my car doesn't. I need you to fix it." and they reply "Your car works fine, you just have to start your wife's car and then jump your car and it starts." Why can't I just use the key (.Add) that comes with my car (AdditionalExcludedOccurrences)?

Sorry if I'm being difficult. I really love Inventor's products, I just don't understand why AdditionalExcludedOccurrences has a .Add function if you can't use it.

P.S. I've also had trouble with .Clear not appearing to do anything to AdditionalExcludedOccurrences either.

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 6 of 9

JamieVJohnson2
Collaborator
Collaborator

Is it possible that the property, although its holds the objectcollection type, doesn't actually have an objectcollection instance within it at default?  That would explain the .Add not working, but the .clear (if you verified the list existed) should work.  I have heard of reference types that don't modify without being replaced (old school arrays).  This could also be why you have to work with your own ObjectCollection, then replace the one that exists.  However it may be more like the common WPF style problem, where there is a model update trigger placed on the property being modified, where the property is only "modified" when you replace it, not just changing its contents.  Since ObjectCollection is not a type that notified of change, this could be the exact reason.

 

Public Property AdditionalExcludedOccurrences

Get

    Return _AdditionalExcludedOccurrences

End Get

Set (value as ObjectCollection)

    _AdditionalExcludedOccurrences = value

    RaiseEvent PropertyChanged (me, "AdditionalExcludedOccurrences")

End Set

 

 

Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/
0 Likes
Message 7 of 9

YuhanZhang
Autodesk
Autodesk
Accepted solution

@clutsa If you have a read-write property of an API object, and if the property type is an object from TransientObjects(ObjectCollection, Color, NameValueMap etc.), when you try to edit the property you should firstly declare a variable of the same type and edit the variable then set the variable back to the Object.Property.  The reason is when you read the ShrinkwrapDefinition.AdditionalExcludedOccurrences you will get a transient ObjectCollection which does not associate with the ShrinkwrapDefinition, so edit the transient ObjectCollection does not affect the ShrinkwrapDefinition, you should explicitly set your own ObjectCollection variable back to ShrinkwrapDefinition.AdditionalExcludedOccurrences. So you should do it like below code snippet demonstrates:

 

Dim ExcludeGroup As ObjectCollection

Set ExcludeGroup = SWD.AdditionalExcludedOccurrences ' you can either use this to get existing object collection or CreateObjectCollection to create an empty object collection


ExcludeGroup.Remove 1 ' edit the object collection to remove one

ExcludeGroup.Add oCompOccu1 ' edit the object collection to add one, say you have an oCompOccu1 already

SWD.AdditionalExcludedOccurrences = ExcludeGroup ' set the edited object collection back to the property.

 

 

An exception for this is the Camera, as you may notice that e.g. View.Camera is read-only so you can set a camera back to View.Camera, and what you should do is to declare a variable to get the View.Camera, and then you can use the Camera.Apply to apply any changes to the camera:

Dim oCamera As Camera


Set oCamera = oView.Camera '(say you have an oView already)


oCamera.Perspective  = True ' change the transient camera but it won't change the oView immediately


oCamera.Apply ' apply the changes back to the oView

 

 

Hope this clears.



If this solves the problem please click ACCEPT SOLUTION so other people can find it easily.



Rocky Zhang
Inventor API PD
Manufacturing Solutions
Autodesk, Inc.

Message 8 of 9

clutsa
Collaborator
Collaborator

@YuhanZhang

Well I still don't fully understand but that does give me some context to what I was seeing. I guess when I edit a matrix of an occurrence object I have to make a copy and assign that back so that's really not any different and follows your TransientObject example. 

Thank You

If I've helped you, please help me by supporting this idea.
Mass Override for Each Model State

Custom Glyph Icon for iMates

0 Likes
Message 9 of 9

jacquesP5H2G
Participant
Participant

Hi @clutsa 

 

I am interested in this code but not really a Inventor user, I want to schedule inventor to SW a few parts every evening so that I can use it in another software called ShipConstructor.

 

Is it possible you share the code that worked for you Please?

0 Likes