- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi everyone,
I apologize for the state of my code, I’ve been struggling with this for several days now without the desired result. I would appreciate some guidance on a more elegant solution.
The Concept:
I have a component in 'Edit-in-Place' mode within an assembly.
I trigger an operation on this component (Sheet Metal Unfold) that requires opening the part in a separate document to process it.
Once the operation is complete, I want to return to the original assembly and maintain the correct navigation/undo history.
The Problem:
Everything works as expected until the final step. Instead of returning to the assembly and focusing on the root assembly, the script leaves me stuck in the 'Edit-in-Place' mode of the original component where the whole process started.
I want the process to end with the root assembly active and the navigation history correctly restored, but my current approach keeps me trapped in the edit context of the child component.
Is there a more elegant way to handle this return to the root assembly? Maybe I am overcomplicating the whole process.
Thanks...
Here is my code:
Public Class AssemblyNavigator
' Logic to toggle Sheet Metal flat patterns and restore the original browser context
Sub Main()
Dim oADoc As AssemblyDocument = TryCast(ThisApplication.ActiveDocument, AssemblyDocument)
If oADoc Is Nothing Then Exit Sub
Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
Dim oEditOcc As ComponentOccurrence = oADef.ActiveOccurrence
If oEditOcc Is Nothing Then
MessageBox.Show("No occurrence is currently active.")
Exit Sub
End If
' 1. BUILD THE HIERARCHY LIST (From component up to root assembly)
Dim AssemblyPath As New List(Of ComponentOccurrence)
Dim oTempOcc As ComponentOccurrence = oEditOcc
Do While oTempOcc IsNot Nothing
AssemblyPath.Add(oTempOcc)
oTempOcc = oTempOcc.ParentOccurrence
Loop
' 2. PROCESS SHEET METAL UNFOLD/FOLD
Dim oTargetOcc As ComponentOccurrence = AssemblyPath.Item(0)
Dim oOccDoc As Document = oTargetOcc.Definition.Document
Dim EditDocument As PartDocument = Nothing
Try
EditDocument = TryCast(ThisApplication.Documents.Open(oOccDoc.FullDocumentName, True), PartDocument)
If EditDocument IsNot Nothing Then
Dim oSMDef As SheetMetalComponentDefinition = EditDocument.ComponentDefinition
If oSMDef.HasFlatPattern = True Then
oSMDef.FlatPattern.Delete()
Else
oSMDef.Unfold()
oSMDef.FlatPattern.ExitEdit()
End If
EditDocument.Dirty = True
End If
Catch ex As Exception
MessageBox.Show("Error processing flat pattern: " & ex.Message)
End Try
If EditDocument IsNot Nothing Then EditDocument.Close(False)
' 3. RESTORE ASSEMBLY CONTEXT (The problematic part)
Try
' Attempting to return to the root assembly and re-drill down to the original context
oADoc.Activate()
ThisApplication.UserInterfaceManager.DoEvents()
' Re-drilling the path to restore the browser focus
For i As Integer = AssemblyPath.Count - 1 To 0 Step -1
Dim TargetOcc As ComponentOccurrence = AssemblyPath(i)
TargetOcc.Edit()
ThisApplication.UserInterfaceManager.DoEvents()
Next
Catch ex As Exception
MessageBox.Show("Navigation failed: " & ex.Message)
End Try
Try
ThisApplication.ActiveEditDocument.SelectSet.Clear()
Catch
End Try
End Sub
End Class
Solved! Go to Solution.