vba code for opening part in the assy

vba code for opening part in the assy

Anonymous
Not applicable
2,334 Views
9 Replies
Message 1 of 10

vba code for opening part in the assy

Anonymous
Not applicable

hi,

 

id like to open and save each part in the assy using vba code..

 

can someone help me with this...

 

thank you!!

0 Likes
2,335 Views
9 Replies
Replies (9)
Message 2 of 10

robmatthews
Collaborator
Collaborator

Even standard/content centre parts?

 

Do you want it recursive? That is, do you only want to open, save and close the initial level documents, or do you want to burrow right down into the assembly tree?

=============================================
This is my signature, not part of my post.
0 Likes
Message 3 of 10

robmatthews
Collaborator
Collaborator

Well, here's something quick and dirty that does what I think you want.  It does not check whether or not a part is Standard/Content centre, and it does not close the assembly document that you start from. It is recursive.

 

 

Public Sub OpenSaveAll()
' Written 2013 by Rob Matthews
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
Dim oRefDoc As Document
Dim bFoundRefs As Boolean
Dim iCounter As Integer
If oDoc.DocumentType = kAssemblyDocumentObject Then
    For iCounter = 1 To oDoc.ReferencedFiles.count
        ThisApplication.Documents.Open (oDoc.ReferencedFiles(iCounter).FullFileName)
        Set oRefDoc = ThisApplication.ActiveDocument
        If oRefDoc.DocumentType = kAssemblyDocumentObject Then
            Call OpenSaveAll
        End If
        oRefDoc.Dirty = True
        ThisApplication.SilentOperation = True
        oRefDoc.Save
        oRefDoc.Close
        ThisApplication.SilentOperation = False
        Set oRefDoc = Nothing
    Next
End If
MsgBox "Completed."
End Sub

 

=============================================
This is my signature, not part of my post.
0 Likes
Message 4 of 10

Anonymous
Not applicable

This will open all part document in the assembly, save and close the same.

Public Sub Assy_Part_Save()
    Dim oAssy As AssemblyDocument
    Set oAssy = ThisApplication.ActiveDocument
    
    Dim oAssyDef As AssemblyComponentDefinition
    Set oAssyDef = oAssy.ComponentDefinition
    
    Dim oPart As PartDocument
    Dim strPartName As String
    
    Dim oOcc As ComponentOccurrence
    ThisApplication.SilentOperation = True
    For Each oOcc In oAssyDef.Occurrences.AllLeafOccurrences
        If oOcc.DefinitionDocumentType = kPartDocumentObject Then
           strPartName = oOcc.ReferencedDocumentDescriptor.FullDocumentName
           Set oPart = ThisApplication.Documents.Open(strPartName, True) 'use False if you do not want to see the open document
           oPart.Save
           oPart.Close
        End If
    Next
    ThisApplication.SilentOperation = False
        
End Sub

 

0 Likes
Message 5 of 10

robmatthews
Collaborator
Collaborator

If you use .occurences instead of .referencedfiles, isn't there the possibility that you will open, save and close the same files multiple times, if they are in the assembly more than once?

 

=============================================
This is my signature, not part of my post.
0 Likes
Message 6 of 10

Anonymous
Not applicable

Yes you are correct. When I tried I used a simple assembly with 3 parts only.

0 Likes
Message 7 of 10

Anonymous
Not applicable

>>>>rob

 

what does dirty and recursive means??

0 Likes
Message 8 of 10

robmatthews
Collaborator
Collaborator

Recursive means the function calls itself.  That is, it opens each referenced file, and if the file is an assembly, then the function calls itself to start again. In this way, all parts are opened, saved and closed, even when they are buried deep down in sub-assemblies.

 

I have used "Dirty" in two places: in the description, it means that I just wrote a quick function, without very much error checking or anything like that; it might not be too robust, it might break when it finds unusual situations, and if it does break, it won't do it elegantly, with a user-designed error message.

 

Within the code itself, I marked each file as dirty, so that Inventor thinks that the file needs a save.

 

I hope this helps.

=============================================
This is my signature, not part of my post.
0 Likes
Message 9 of 10

ekinsb
Alumni
Alumni

Why do you want to open and save each part?  Why not just save each part?  Part and subassemblies don't need to be the active document to be able to be saved and they're already open as a result of being referenced by the assembly.  The easiest way to save all documents referenced by an assembly is to do use the Save2 method of the AssemblyDocument object.  This will save the assembly and all referenced documents that have been edited.


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
Message 10 of 10

robmatthews
Collaborator
Collaborator

Brian makes a valid point. Why open? If you still want to explicitly save every single file, I have modified my routine to do that. I've added a counter, too.

 

I don't know enough about Save2 and collections to be able to help with that though.

 

Option Explicit
Public CountFiles As Long

Sub OpenSaveAll()
Dim oDoc As Document
Set oDoc = ThisApplication.ActiveDocument
Dim oRefDoc As Document
Dim bFoundRefs As Boolean
Dim iCounter As Integer
CountFiles = 0
Call SaveAll(oDoc)
oDoc.Save
CountFiles = CountFiles + 1
MsgBox "Completed. Saved " & CountFiles & " files."
End Sub

Sub SaveAll(oDoc As Document)
' Written 2013 by Rob Matthews
Dim oRefDoc As Document
Dim sName As String
Dim bFoundRefs As Boolean
Dim iCounter As Integer
If oDoc.DocumentType = kAssemblyDocumentObject Then
    For iCounter = 1 To oDoc.ReferencedFiles.count
        Set oRefDoc = oDoc.ReferencedFiles(iCounter)
        sName = oRefDoc.FullFileName
        If oRefDoc.DocumentType = kAssemblyDocumentObject Then
            Call SaveAll(oRefDoc)
        End If
        oRefDoc.Dirty = True
        ThisApplication.SilentOperation = True
        oRefDoc.Save
        CountFiles = CountFiles + 1
        ThisApplication.SilentOperation = False
        Set oRefDoc = Nothing
    Next
End If
End Sub

 

=============================================
This is my signature, not part of my post.
0 Likes