Anyone please help.
I have an assembly that contains multiple subassemblise where I want to set or create a LOD.
I have a problem og getting the sub-assemblies to change as I somehow only get the top-level assembly to act to the following code.
The following code is written in the iLogic editor:
Start:
Dim asmDoc As AssemblyDocument
asmDoc = ThisApplication.ActiveDocument
Dim oAsmCompDef As AssemblyComponentDefinition
oAsmCompDef = asmDoc.ComponentDefinition
Dim oLODRep As LevelOfDetailRepresentation
Dim doc As Document
For Each doc In asmDoc.AllReferencedDocuments
' Check for assembly documents.
If doc.DocumentType = kAssemblyDocumentObject Then
Try
'Activate a writeable LOD (master LOD is not writeable)
AsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Item("Test").activate
Catch
'Assume error means this LOD does not exist, so create it (will be deleted at end)
oLODRep = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Add("Test")
End Try
End If
Next
End
Can anyone point me in the right direction of getting the sub assemblies to act on the code above?
Thanks in advance
Kari
Solved! Go to Solution.
Solved by Vladimir.Ananyev. Go to Solution.
Solved by Vladimir.Ananyev. Go to Solution.
Use ComponentOccurrence.SetLevelOfDetailRepresentation method to switch components to the desired LOD.
At the top level of assembly tree you may consider something like this :
For Each oOcc As ComponentOccurrence In oMainAssyDef.Occurrences If oOcc.Suppressed Then Continue For Try If oOcc.DefinitionDocumentType _ = DocumentTypeEnum.kAssemblyDocumentObject Then oOcc.SetLevelOfDetailRepresentation(TargetLODName) End If Catch End Try Next
To do the same work in multi-level main assembly you may consider the following workflow.
1. AllReferencedDocuments property of the top assembly document gives you access to all referenced subassembly documents.
2. For every found subassembly you may find all occurrences that reference this subassembly document at all levels in the main assembly. Use ComponentOccurrences.AllReferencedOccurrences property for this purpose.
3. Then you may call ComponentOccurrence.SetLevelOfDetailRepresentation method to set desired LOD in every subassembly occurrence.
Alternative workflow: you may recursively traverse an assembly and set desired LOD in every subassembly occurrence.
See “Traverse an Assembly API Sample” in the Inventor API Help.
Cheers,
1. You could get ComponentDefinition object.from ComponentOccurrence.Definition property.
2. To create new LOD use RepresentationsManager:
' Create a new level of detail representation. ' The new representation is automatically activated. Dim oLODRep As LevelOfDetailRepresentation Set oLODRep = oAsmCompDef.RepresentationsManager.LevelOfDetailRepresentations.Add("Part2Suppressed")
Look at the “Create a level of detail representation API Sample” in the Inventor API Help.
Cheers,
I cannot figure it out... I works fine for the top-level assembly according to the API-samples You are reffering to, but when it comes to the occurrences in the assembly I fail.
Dim oMainAssyDef As AssemblyComponentDefinition
oMainAssyDef = ThisApplication.ActiveDocument.ComponentDefinition
Dim oLODRep As LevelOfDetailRepresentation
Try
'Activate a writeable LOD (master LOD is not writeable)
oMainAssyDef.RepresentationsManager.LevelOfDetailRepresentations.Item("Test").activate
Catch
'Assume error means this LOD does not exist, so create it (will be deleted at end)
oLODRep = oMainAssyDef.RepresentationsManager.LevelOfDetailRepresentations.Add("Test")
End Try
'Iterate through all of the referenced documents at
'all levels of the assembly.
For Each oOcc As ComponentOccurrence In oMainAssyDef.Occurrences
'If the Occurent is suppressed then leav it as such
If oOcc.Suppressed Then Continue For
Try
If oOcc.DefinitionDocumentType _
= DocumentTypeEnum.kAssemblyDocumentObject Then
oOcc.SetLevelOfDetailRepresentation("Test")
End If
Catch
'Assume error means this LOD does not exist, so create it (will be deleted at end)
'THIS IS WHERE I FAIL...
End Try
Next
I have looked through the API samples but I cannot figure out how to access the ComponentOccurrence.Definition for each individual occurrence in the iteration. Please assist me some more.
Could you look at this thread:
Perfect!
With:
"If oOcc.Suppressed Then Continue For"
And some other small addtioins/adjustments... This will fit fine for an external rule.
Thanks!
I have taken the task to traverse through the assembly.
It works fine, but, when I traverse the assembly structure I cannot maintain the LOD that I set throughout the structure as it changes when the code reaches the next level, see attached code and picture.
The assembly that I tested on consists of:
The code does the following: (The numbering relates to the pictures in the attached file)
How can I maintain the LOD of all the assemblies and subassemblies?
Or should I try another approach?
Any input would be helpful.
CODE start
PrivateSub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
' Get the active assembly.
Dim oAsmDoc AsAssemblyDocument
oAsmDoc = _invApp.ActiveDocument 'ThisApplication.ActiveDocument
'Debug.Print(oAsmDoc.DisplayName)
Dim oAsmDocDef AsAssemblyComponentDefinition
oAsmDocDef = oAsmDoc.ComponentDefinition
Dim ActiveLODRepName AsString
Try
ActiveLODRepName = oAsmDocDef.RepresentationsManager.ActiveLevelOfDetailRepresentation.Name
Catch ex1 AsException
ActiveLODRepName = "Test"
EndTry
' Call the function that does the recursion.
Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences, 1, ActiveLODRepName)
EndSub
PrivateSub TraverseAssembly(Occurrences AsComponentOccurrences, _
Level AsInteger, ActiveLODRepName AsString)
' Iterate through all of the occurrence in this collection. This
' represents the occurrences at the top level of an assembly.
Dim oOcc AsComponentOccurrence
Dim oCompDef AsAssemblyComponentDefinition
ForEach oOcc In Occurrences
' Check to see if this occurrence represents a subassembly
' and recursively call this function to traverse through it.
If oOcc.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
If oOcc.Suppressed ThenContinue For
Try
'Activate a writeable LOD (master LOD is not writeable)
oOcc.SetLevelOfDetailRepresentation(ActiveLODRepName)
Catch
'Assume error means this LOD does not exist, so create it
oCompDef = oOcc.Definition
oCompDef.RepresentationsManager.LevelOfDetailRepresentations.Add(ActiveLODRepName)
oOcc.SetLevelOfDetailRepresentation(ActiveLODRepName)
EndTry
Call TraverseAssembly(oOcc.SubOccurrences, Level + 1, ActiveLODRepName)
EndIf
Next
EndSub
CODE end
I prefer two-step workflow.
Step 1
We can obtain the list of all subassembly documents filtering TopAssemblyDoc.AllReferencedDocuments collection. In every found assembly document we may (if required) set up all desired LODs (+ update & save)
Step 2
For every custom LOD in the top assembly:
Activate this LOD in the top assembly and then traverse top assembly tree to activate this LOD in subassemblies using SetLevelOfDetailRepresentation(LODname) method.