How to Control include/exclude state of components in Derived Assembly with code

How to Control include/exclude state of components in Derived Assembly with code

cencinaNB2ET
Advocate Advocate
1,374 Views
8 Replies
Message 1 of 9

How to Control include/exclude state of components in Derived Assembly with code

cencinaNB2ET
Advocate
Advocate

I have a Derived Assembly ( Part) that I need to control some occurrences to show or not from its parent Assembly.

 

I have this so far but I cant seem to use the DerivedAssemblyDefinition.InclusionOption nor explore the occurrences in the assembly from the part.

 

Any ideas?

 

 

Dim Doc_P As PartDocument = ThisDoc.Document
Dim oDoc_Def As ComponentDefinition = Doc_P.ComponentDefinition
Dim userParams As UserParameters = oDoc_Def.Parameters.UserParameters
Dim modelParams As ModelParameters = oDoc_Def.Parameters.ModelParameters
Dim userParams_A As UserParameters
Dim modelParams_A As ModelParameters
Dim iLogicAuto As Object = iLogicVb.Automation


Dim oDoc_Parent As AssemblyDocument
Dim oAsmDef As AssemblyComponentDefinition
Dim oDerAssemblyComp As DerivedAssemblyComponent
Dim oDerAssemblyCompDef As DerivedAssemblyDefinition
Dim Full_File_Name As String





Dim oDerEntity As DerivedAssemblyOccurrence For Each oDerAssemblyComp In Doc_P.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents oDerAssemblyComp.SuppressLinkToFile = False Next For Each oDerAssemblyComp In Doc_P.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents oDerAssemblyCompDef = oDerAssemblyComp.Definition Full_File_Name = oDerAssemblyComp.ReferencedDocumentDescriptor.FullDocumentName MessageBox.Show(Full_File_Name) 'MessageBox.Show(oDerAssemblyCompDef.Occurrences.Count) Dim Inclusion As DerivedComponentOptionEnum Inclusion = 27137 oDerAssemblyComp.Definition.InclusionOption = Inclusion 'For i = 1 To oDerAssemblyCompDef.Occurrences.Count 'oDerEntity = oDerAssemblyCompDef.Occurrences.Item(i) 'MessageBox.Show(oDerEntity.Name) 'Next
Next For Each oDerAssemblyComp In Doc_P.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents oDerAssemblyComp.SuppressLinkToFile = True Next



 

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

bradeneuropeArthur
Mentor
Mentor

with this:

 

Dim oDerEntity As DerivedAssemblyOccurrence
oDerEntity.InclusionOption =kDerivedExcludeAll

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 3 of 9

bradeneuropeArthur
Mentor
Mentor

This is the code from the help file:

 Derived Parts and Assemblies API Sample 

Description 
This sample demonstrates the use of the API to create derived parts and assemblies.
Code Samples 

VBA

This example used is a simple case of a part subtracted from a mold. To simplify setup, the program creates the parts representing the part and the mold. It then uses derived parts and assemblies to scale the part and subtract it from the mold.Public Sub MoldBaseSample()
    ' Initialize the string that defines the directory where the
    ' files will be created.
    Dim sFilePath As String
    sFilePath = "C:\Temp\"
    
    ' Call the functions to create the parts that represent the
    ' molded part and the mold base.
    Call CreateMoldPart(sFilePath & "MoldPart.ipt")
    Call CreateMoldBase(sFilePath & "MoldBase.ipt")
    
    ' Create a new part file to derive the mold part in.
    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
                 ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))
    
    ' Create a derived definition for the molded part.
    Dim oDerivedPartDef As DerivedPartUniformScaleDef
    Set oDerivedPartDef = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.CreateUniformScaleDef(sFilePath & "MoldPart.ipt")
    
    ' Set the scale to use.
    oDerivedPartDef.ScaleFactor = 1.1
    
    ' We could set other options for the derived part using the derived part definition.
    ' In this case the defaults are good except for the scale which we changed.
    
    ' Create the derived part.
    Call oPartDoc.ComponentDefinition.ReferenceComponents.DerivedPartComponents.Add(oDerivedPartDef)
    
    ' Save and close the part.  Uses SilentOperation to bypass the save dialog.
    ThisApplication.SilentOperation = True
    Call oPartDoc.SaveAs(sFilePath & "ScaledMoldPart.ipt", False)
    ThisApplication.SilentOperation = False
    oPartDoc.Close
    
    ' Create a new assembly file to put the mold base and scaled part together.
    Dim oAsmDoc As AssemblyDocument
    Set oAsmDoc = ThisApplication.Documents.Add(kAssemblyDocumentObject, _
                ThisApplication.FileManager.GetTemplateFile(kAssemblyDocumentObject))
    
    ' Create the matrix used to define the position of the occurrences.  When a new
    ' matrix is created it is initialized to an identity matrix.  This will cause
    ' parts to be placed into the assembly in the same location that are in part space.
    Dim oMatrix As Matrix
    Set oMatrix = ThisApplication.TransientGeometry.CreateMatrix
    
    ' Place the mold base.
    Dim oOcc As ComponentOccurrence
    Set oOcc = oAsmDoc.ComponentDefinition.Occurrences.Add(sFilePath & "MoldBase.ipt", oMatrix)
    
    ' Rename the occurrence.  This sample uses the name to identify the occurrence later.
    ' This isn't the only method that could be used, but is one of the simplest.
    oOcc.Name = "Mold Base"
    
    ' Place the scaled part.
    Set oOcc = oAsmDoc.ComponentDefinition.Occurrences.Add(sFilePath & "ScaledMoldPart.ipt", oMatrix)
    oOcc.Name = "Mold Part"
    
    ' Save and close the assembly.
    Call oAsmDoc.SaveAs(sFilePath & "MoldSample.iam", False)
    oAsmDoc.Close
    
    ' Create a new part to derive the assembly into, subtracting the part from the mold base.
    Set oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
                 ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))
    
    ' Create a derived definition for the molded assembly.
    Dim oDerivedAsmDef As DerivedAssemblyDefinition
    Set oDerivedAsmDef = oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.CreateDefinition(sFilePath & "MoldSample.iam")
    
    ' Set the part to be subtracted.
    oDerivedAsmDef.Occurrences.Item("Mold Part").InclusionOption = kDerivedSubtractAll
    
    ' Create the derived assembly.
    Call oPartDoc.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents.Add(oDerivedAsmDef)
End Sub

' Sub to create the part representing the molded part.
Private Sub CreateMoldPart(Filename As String)
    ' Create a new part document using the default part template.
    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
                 ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))
    
    ' Create a new sketch on the XY base workplane.
    Dim oSketch As PlanarSketch
    Set oSketch = oPartDoc.ComponentDefinition.Sketches.Add(oPartDoc.ComponentDefinition.WorkPlanes(3))
    
    Dim oTG As TransientGeometry
    Set oTG = ThisApplication.TransientGeometry
    
    ' Draw the geometry defining the shape of the part.
    Dim oPoints As ObjectCollection
    Set oPoints = ThisApplication.TransientObjects.CreateObjectCollection
    oPoints.Add oTG.CreatePoint2d(-5, 0)
    oPoints.Add oTG.CreatePoint2d(-4, 3)
    oPoints.Add oTG.CreatePoint2d(-2, 4)
    oPoints.Add oTG.CreatePoint2d(0, 3)
    oPoints.Add oTG.CreatePoint2d(3, 4)
    oPoints.Add oTG.CreatePoint2d(4, 2)
    oPoints.Add oTG.CreatePoint2d(5, 0)
   
    Dim oSpline As SketchSpline
    Set oSpline = oSketch.SketchSplines.Add(oPoints)
    oSpline.FitMethod = kSweetSplineFit
    
    Dim oLine As SketchLine
    Set oLine = oSketch.SketchLines.AddByTwoPoints(oSpline.FitPoint(1), oSpline.FitPoint(oSpline.FitPointCount))
    
    Dim oProfile As Profile
    Set oProfile = oSketch.Profiles.AddForSolid
    
    ' Create a revolved feature.
    Call oPartDoc.ComponentDefinition.Features.RevolveFeatures.AddFull(oProfile, oLine, kJoinOperation)
    
    ' Save and close the document.
    Call oPartDoc.SaveAs(Filename, False)
    oPartDoc.Close
End Sub

' Sub to create the part representing the mold base.
Private Sub CreateMoldBase(Filename As String)
    ' Create a new part document using the default part template.
    Dim oPartDoc As PartDocument
    Set oPartDoc = ThisApplication.Documents.Add(kPartDocumentObject, _
                 ThisApplication.FileManager.GetTemplateFile(kPartDocumentObject))
    
    ' Create a new sketch on the XY base workplane.
    Dim oSketch As PlanarSketch
    Set oSketch = oPartDoc.ComponentDefinition.Sketches.Add(oPartDoc.ComponentDefinition.WorkPlanes(3))
    
    ' Draw the geometry defining the shape of the part.
    Dim oTG As TransientGeometry
    Set oTG = ThisApplication.TransientGeometry
    Call oSketch.SketchLines.AddAsTwoPointRectangle(oTG.CreatePoint2d(-6, -5), oTG.CreatePoint2d(6, 5))
        
    Dim oProfile As Profile
    Set oProfile = oSketch.Profiles.AddForSolid
    
    ' Create an extruded feature.
    Dim oCompDef As PartComponentDefinition
    Set oCompDef = oPartDoc.ComponentDefinition
    Dim oExtrudeDef As ExtrudeDefinition
    Set oExtrudeDef = oCompDef.Features.ExtrudeFeatures.CreateExtrudeDefinition(oProfile, kJoinOperation)
    Call oExtrudeDef.SetDistanceExtent(5, kNegativeExtentDirection)
    Call oCompDef.Features.ExtrudeFeatures.Add(oExtrudeDef)

    ' Save and close the document.
    Call oPartDoc.SaveAs(Filename, False)
    oPartDoc.Close
End Sub

Regards,

Arthur Knoors

Autodesk Affiliations & Links:
blue LinkedIn LogoSquare Youtube Logo Isolated on White Background


Autodesk Software:Inventor Professional 2025 | Vault Professional 2024 | Autocad Mechanical 2024
Programming Skills:Vba | Vb.net (Add ins Vault / Inventor, Applications) | I-logic
Programming Examples:
Drawing List!|
Toggle Drawing Sheet!|
Workplane Resize!|
Drawing View Locker!|
Multi Sheet to Mono Sheet!|
Drawing Weld Symbols!|
Drawing View Label Align!|
Open From Balloon!|
Model State Lock!
Posts and Ideas:
My Ideas|
Dimension Component!|
Partlist Export!|
Derive I-properties!|
Vault Prompts Via API!|
Vault Handbook/Manual!|
Drawing Toggle Sheets!|
Vault Defer Update!

! For administrative reasons, please mark a "Solution as solved" when the issue is solved !


 


EESignature

0 Likes
Message 4 of 9

cencinaNB2ET
Advocate
Advocate

Forum 1.JPG

 

But That Entity is a derived assembly occurrence, meaning a part in the parent assembly right?

how can you include all on a part?

 

My assembly uses visibility on and off and sometimes place and delete parts.

So every time i Update the part it looses those parts there were taken in and out in the assembly.

Like the picture, i took those bolts out and then back in with code but the derived part doesn't show them any more.

 

 

 

 

 

0 Likes
Message 5 of 9

cencinaNB2ET
Advocate
Advocate

That code creates it. I already have it. Just need to edit it.

0 Likes
Message 6 of 9

cencinaNB2ET
Advocate
Advocate

Wouldn't this code just work and an existing Derived assembly part?

 

 

Dim Doc_P As PartDocument = ThisDoc.Document
Dim oDoc_Def As ComponentDefinition = Doc_P.ComponentDefinition
Dim oAsmDef As AssemblyComponentDefinition
Dim oDerAssemblyComp As DerivedAssemblyComponent
Dim oDerAssemblyCompDef As DerivedAssemblyDefinition
Dim Full_File_Name As String


oDerAssemblyComp = Doc_P.ComponentDefinition.ReferenceComponents.DerivedAssemblyComponents(1)
oDerAssemblyComp.SuppressLinkToFile = False
Dim Visibility_Percentage As Long = 0
oDerAssemblyCompDef = oDerAssemblyComp.Definition
Full_File_Name = oDerAssemblyComp.ReferencedDocumentDescriptor.FullDocumentName
MessageBox.Show(Full_File_Name)
oDerAssemblyCompDef.SetRemoveByVisibilityOptions(kDerivedRemovePartsOnly, Visibility_Percentage, True)



 

0 Likes
Message 7 of 9

cencinaNB2ET
Advocate
Advocate

Ok I can see the parts in the assembly and their Derived Component Option Enum But I cant change it?

 

 

Dim oDerEntity As DerivedAssemblyOccurrence
Dim Inclusion As DerivedComponentOptionEnum 

For i = 1 To  oDerAssemblyCompDef.Occurrences.Count
oDerEntity = oDerAssemblyCompDef.Occurrences.Item(i)'.InclusionOption
Inclusion =  oDerAssemblyCompDef.Occurrences.Item(i).InclusionOption
MessageBox.Show(oDerEntity.Name & vbCr & Inclusion)
oDerAssemblyCompDef.Occurrences.Item(i).InclusionOption = kDerivedIncludeAll
Next i
0 Likes
Message 8 of 9

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @cencinaNB2ET 

 

I wonder if there is a bug here?

 

If I set up a simple derived part of an assembly, and and I change one of the occurrences to be a bounding box, then i can use this code to change on or the other to be bounding or included.... but if I run it again it does not work... unless I manually change one again???

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

aaa.png

 

oInput = InputRadioBox("Select one", "Bounding Boxes", "Defined", True, "ilogic")


Dim oPart As PartComponentDefinition
oPart = ThisApplication.ActiveDocument.ComponentDefinition


Dim oDerAssyDef As DerivedAssemblyDefinition
oDerAssyDef = oPart.ReferenceComponents.DerivedAssemblyComponents(1).Definition

Dim oDerAssy As DerivedAssemblyOccurrence
For Each oDerAssy In oDerAssyDef.Occurrences

	If oInOPut = True Then
		oDerAssy.InclusionOption = DerivedComponentOptionEnum.kDerivedBoundingBox
	Else
		oDerAssy.InclusionOption = DerivedComponentOptionEnum.kDerivedIncludeAll
	End If

Next

oPart.ReferenceComponents.DerivedAssemblyComponents(1).Definition = oDerAssyDef

EESignature

Message 9 of 9

cencinaNB2ET
Advocate
Advocate

That's weird... But it works! thanks so much

0 Likes