Community
Inventor Programming - iLogic, Macros, AddIns & Apprentice
Inventor iLogic, Macros, AddIns & Apprentice Forum. Share your knowledge, ask questions, and explore popular Inventor topics related to programming, creating add-ins, macros, working with the API or creating iLogic tools.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

iLogic Assembly Component Occurrence Definition

6 REPLIES 6
Reply
Message 1 of 7
vampelas
16420 Views, 6 Replies

iLogic Assembly Component Occurrence Definition

Hi,

 

I have a simple code below trying to iterate through all the assembly component and copy the iProperties > Occurrece Name (i.e. "Part1:1") and paste it to iProperties > Project tab > Part Number field for each individual part in assembly.

 

' set a reference to the assembly component definintion.
' This assumes an assembly document is open.
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition

'Iterate through all of the occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences
    Dim oName As String
    oName = oOccurrence.Name
    iProperties.Value(oOccurrence.Name, "Project", "Part Number") = oName
    MessageBox.Show(oOccurrence.Name, "iLogic")
Next

 

The Debug message shows the correct output for every iteration, but when I check each part's properties they all have the Occurrence Name of the last part in the aseembly.

 

I am obivously doing something wrong with the code, but I can't get my head around it at the moment and was hoping someone could help me.

 

Many thanks,

Vas

6 REPLIES 6
Message 2 of 7

Hi vampelas,

 

I took just a moment to test the code as written and it seemed to work as expected. I can't offer much of a guess as to why you were seeing different results, but you might try testing it on a new simple assembly to see if it works.

 

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

Message 3 of 7

Hi Curtis,

 

Thank you for prompt resonse!

 

The assembly I am actually trying this is a basic assy template with multiple instaces \ occurrences of the same .ipt file... Maybe that's where the problem is, not sure .

 

It seems to iterate through the part occurrences properly, but eveytime it copies the iproperty value, it copies it to all the occurrence at once for however many times it iterates.

 

I hope this makes some sense...

 

Best Regards,

Vas

Message 4 of 7

Hi,

 

I think I've answered my own question there, because even in the assembly environment, when I try and change an iProperty of one of the part occurrences (i.e.. Part1:4), that iProperty value gets copied to all the Part occurrences in the assembly. Unless there is a particular code that will give me access to just one of the occurrence properties then I don't think this is possible.

 

I also failed to explain my initial issue properly the first time. My initial reason behind this code comes a customer support request... The customer has a lot of DWG drawings with views of an assembly, and within that assembly there are multiple instances of Parts (i.e.. Part1:1, Part1:2, ... , Part7).

 

The customer is then using the Auto-Balloon command to annotate those parts, with "Ignore Multiple Instances" option activated. And wants the Balloon text to show the Part name as shown in the Model Browser ((i.e.. Part1:1) to indicate the Occurrence number after the name.

 

My initial thought was trying the Inventor Style Manager > Dimensions > ISO Balloons and see which properties from the drawing and model can be tagged on the balloons.

 

This didn't really help, one because there was no property available that would accommodate that, and moreover because the way I see it, the Balloons take information from the Drawing file and the BOM table of the model, and not from the Model directly.

 

And also when you Auto-Balloon, by box selecting all parts in the view, the placement of Balloons does not start with Part1:1 for example, which makes it difficult to relate to the assembly information though vba or iLogic code.

 

In any case, I have developed the VBA code below so the user can add and add and run this  through his ribbon button. With a major disadvantage to the code, in that when the user performs an Auto-balloon annotation command and is about to select the Components, he needs to hold Ctrl button and select every part occurrence from the Model Browser individually and in a Decremental order (i.e..: Part1:7, Part1:6, ... , Part1:1) in order for the appropriate balloon text values to be overridden with the Occurrence names for the appropriate part.

 

I hope this makes some sense... I haven't got much experience with VBA code to be honest, but I enjoy playing around with Inventor API, and was hoping someone could check through this case and give some advise. Many thanks in advanced!

 

Sub UpdateBalloons()

' Set a reference to the Drawing Document.
Dim oDrawDoc As DrawingDocument
Set oDrawDoc = ThisApplication.ActiveDocument

' Set a reference to the the Full File Name of the Drawing.
Dim oFullFileName As String
oFullFileName = oDrawDoc.FullFileName

' Set a reference to the Full File name of the drawing,
' remove ".dwg" from name, and add ".iam" extension.
Dim oAsmName As String
oAsmName = Left(oFullFileName, Len(oFullFileName) - 4) & ".iam"

' Set Reference to the Assembly and Open Silently
Dim oAsmDoc As AssemblyDocument
Set oAsmDoc = ThisApplication.Documents.Open(oAsmName, False)

' Set a reference to the assembly component definintion.
Dim oAsmCompDef As AssemblyComponentDefinition
Set oAsmCompDef = oAsmDoc.ComponentDefinition

' Set reference to Balloon Counter
Dim BalloonCount As Long
' Set Ballon Counter to 1
BalloonCount = 1

' Iterate through all of the Part Occurrences
Dim oOccurrence As ComponentOccurrence
For Each oOccurrence In oAsmCompDef.Occurrences


    ' Set Reference to Occurrence Name
    Dim oOccName As String
    oOccName = oOccurrence.Name
    
    ' Set Reference to Part Document
    Dim invPart As Document
    Set invPart = oOccurrence.Definition.Document

    ' Set Reference to Part's iProperies
    Dim oPropertySet As PropertySet
    Set oPropertySet = invPart.PropertySets.Item("Design Tracking Properties")
    
    ' Set Reference to Part's iProperty "Part Number"
    Dim oPartNumber As Property
    Set oPartNumber = oPropertySet.Item("Part Number")
    
    ' Set Value of "Part Number" iProperty to Occurrence Name
    oPartNumber.Value = oOccName
    
    ' Set Reference to Active Sheet
    Dim oSheet As Sheet
    Set oSheet = oDrawDoc.ActiveSheet
    
    ' Set Reference to Ballon in sheet
    Dim oBalloon As Balloon
    Set oBalloon = oSheet.Balloons.Item(BalloonCount)
    
    ' Set Balloon stype to Hexagon
    oBalloon.SetBalloonType (kHexagonBalloonType)

    ' Set Reference to Ballon Set within Sheet
    Dim oBalloonValueSet As BalloonValueSet
    
    ' Iterate through all the balloons in sheet
    For Each oBalloonValueSet In oBalloon.BalloonValueSets
        ' Override Balloon text with Occurrence Name
        oBalloonValueSet.OverrideValue = oOccName
    Next
 
    ' Increase Balloon Counter by 1 within For loop
    BalloonCount = BalloonCount + 1
    
Next

oAsmDoc.Close (True)
End Sub

 

Best Regards,

 

Vas

Message 5 of 7
johnster100
in reply to: vampelas

Is there anyway to make this code loop through sub assemblies?

 

thanks

john

Message 6 of 7
yosso22
in reply to: johnster100

 

Stumbled across this old post while researching a solution...so here's a bit of code.

 

' http://inventbetter.blogspot.com
Option Explicit

Sub Main
	Dim oAsmDoc as document
	oAsmDoc = ThisApplication.ActiveDocument
	' Are we in a Assembly Document?
	If oAsmDoc.DocumentType <> kAssemblyDocumentObject Then
		Exit Sub
	End If
	' Call the Function which does the recursion
	Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences,1)
End Sub

Private Sub TraverseAssembly( Occurrences as ComponentOccurrences, Level as Integer)
	
' Iterate through all of the occurrences in this assembly. Level 1 components Dim oOcc as ComponentOccurrence Dim oDoc as Document Dim custPropSet As PropertySet
For Each oOcc in Occurrences
If (InStr(oOcc.Name, "Bolted") + InStr(oOcc.Name, "BC")) = 0 Then ' Get the selected item document occurrence name Try oDoc = oOcc.Definition.Document custPropSet = oDoc.PropertySets.Item("Inventor User Defined Properties") Dim oBrowserNode As String oBrowserNode = oOcc.Name
Try custPropSet.item("BOM_level").Value = Level Catch custPropSet.Add("", "BOM_level") custPropSet.item("BOM_level").Value = Level End Try If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then Call traverseAssembly(oOcc.SubOccurrences, Level + 1) End If Catch ' Nothing End Try
End If
Next End Sub

Some iLogic created to push a assembly level down through the assembly using recursion.

 

We are ignoring bolted connections.

 

Derived from several sources, main source - http://modthemachine.typepad.com/my_weblog/2009/03/accessing-assembly-components.html

 

Give Kudos to further enhance the value of these forums. Thank you! Smiley Happy
Message 7 of 7
steveh4
in reply to: vampelas

Sorry responded to wrong post.

 

Inventor---Vault Professional

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report