Error unfolding parts

Error unfolding parts

Anonymous
Not applicable
403 Views
2 Replies
Message 1 of 3

Error unfolding parts

Anonymous
Not applicable

I have a few issues with the code below. It's meant to loop through each sheet metal document

  1. Unfold it if no flat pattern exists
  2. Go into the edit flat pattern environment if a flat pattern does exist
  3. Do something to the flat pattern then exit the flat pattern (I've left this bit out of the code as its not where the problem is)

When I start a new session of Inventor, open an assembly that contains sheet metal parts and run the rule. I'll get an unspecified error (Exception from HRESULT blah blah blah) on the first sheet metal document my loop encounters.

If I then manually open each sheet metal part in the model then close them (without saving) and run the rule again, it works with no errors. What does opening and closing them change?

 

After that's solved, the next issue... if a flat pattern doesn't exist, it will open that part in a new window. How can I prevent that? or at least close the part if it must open it to create the flat pattern

 

And lastly... my ribbon bar in the assembly changes to the flat pattern one (see attached image). I need to open another window then return to the assembly for it to revert back.

 

' Get the active assembly. 
Dim oAsmDoc As AssemblyDocument 
oAsmDoc = ThisApplication.ActiveDocument

' Get all of the referenced documents. 
Dim oRefDocs As DocumentsEnumerator 
oRefDocs = oAsmDoc.AllReferencedDocuments 

' Iterate through the list of documents. 
Dim oRefDoc As Document 
  
' Check for a non-part document
If oAsmDoc.DocumentType <> kAssemblyDocumentObject Then
    MsgBox ("The Active document must be an 'Assembly'!")
    Exit Sub
End If

' Iterate through all referenced documents beneath the active assembly
For Each oRefDoc In oRefDocs
    
    ' Define summary property set of part
    oSummaryPropertySet = oRefDoc.PropertySets.Item("Inventor Document Summary Information")

    If oSummaryPropertySet.Item("Category").Value = "Sheet Metal Part" Then
        
        ' Set reference to document component definition
        Dim oCD As SheetMetalComponentDefinition = oRefDoc.ComponentDefinition
        
        ' Create flat pattern if none
        If Not oCD.HasFlatPattern() Then
            oCD.Unfold()
        Else
        ' Edit flat pattern
            oCD.FlatPattern.Edit()
        End If
        
        'Perform action on flat pattern then exit flat pattern
        
    End If

Next

 

0 Likes
404 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable

P.S... the documents are definitely sheet metal ones, even though I'm querying the 'Category' iProperty 

0 Likes
Message 3 of 3

Anonymous
Not applicable

I can solve the first problem with a Try Catch block.... but its slow and nasty having to open and close all the parts. 

I tried changing the open visibility Boolean to False, but that gives me the unspecified error, so it seems it needs to be visibly open.

        Try
            ' Create flat pattern if none
            If Not oCD.HasFlatPattern() Then
                oCD.Unfold()
            Else
            ' Edit flat pattern
                oCD.FlatPattern.Edit()
            End If
        Catch
            part = oRefDoc.ComponentDefinition.Document.FullDocumentName
            openDoc = ThisApplication.Documents.Open(part,True)
            ' Create flat pattern if none
            If Not oCD.HasFlatPattern() Then
                oCD.Unfold()
            Else
            ' Edit flat pattern
                oCD.FlatPattern.Edit()
            End If
            openDoc.Close()
        End Try    

 

Any other suggestions?

0 Likes