To answer your first question, there is a way to have an assembly open and then modify each part in the assembly. You can either access the properties of the each part document using code like below, which will modify the part number of all sub-assemblies to match the file name, minus the extension.
'Define the open document
Dim openDoc As Document
openDoc = ThisDoc.Document
Dim docFile As Document
''format file name
' FNamePos = InStrRev(docFile.FullFileName, "\", -1)
' 'Dim docFName As String
' 'returns file name with extension
'docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)
''returns the number of characters in the file name
'nameLen = Len(docFName)
''trims off the file extension (last 4 characters)
'nameNoExtension = Left(docFName,nameLen-4)
'kAssemblyDocumentObject = 12291
If openDoc.DocumentType = 12291 Then
'Iterate though the assembly files in the assembly
For Each docFile In openDoc.AllReferencedDocuments
'kPartDocumentObject = 12290
If docFile.DocumentType = 12291 Then
'format file name
'Dim FNamePos As Long
FNamePos = InStrRev(docFile.FullFileName, "\", -1)
'Dim docFName As String
'returns file name with extension
docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) - FNamePos)
'returns the number of characters in the file name
nameLen = Len(docFName)
'trims off the file extension (last 4 characters)
nameNoExtension = Left(docFName,nameLen-4)
'set part number to file name (without extension)
If iProperties.Value(docFName,"Project", "Part Number") <> nameNoExtension Then
iProperties.Value(docFName,"Project", "Part Number") = nameNoExtension
Else
End If
'check for empty Rev #
If iProperties.Value(docFName,"Project", "Revision Number") = "" Then
iProperties.Value(docFName,"Project", "Revision Number") = 1
Else
End If
End If
Next
Else
MessageBox.Show("You must have a valid Assembly document open before using this code!", "File Type Mismatch!")
End If
The other, more brute force way of doing that is to follow the same work flow that a person would follow and to mak each part active and then do what changes you need to do.
Dim docFile As Document
If openDoc.DocumentType = 12291 Then
For Each docFile In openDoc.AllReferencedDocuments
If docFile.DocumentType = 12290 Then
Dim assemblyDoc As AssemblyDocument
assemblyDoc = openDoc
Dim assemblyDef As AssemblyComponentDefinition
assemblyDef = assemblyDoc.ComponentDefinition
Dim partDoc As PartDocument
partDoc = ThisApplication.Documents.Open(docFile.FullFileName, False)
Dim FNamePos As Long
FNamePos = InStrRev(docFile.FullFileName, "\", -1)
Dim docFName As String
docFName = Mid(docFile.FullFileName, FNamePos + 1, Len(docFile.FullFileName) - FNamePos)
'This line here actually makes the part the active part in Inventor
ThisDoc.Launch(docFile.FullFileName)
Check = iProperties.Mass(docFName)
'MessageBox.Show(Check)
'This checks to see if each part has mass (actually exists)
If Check > 0.01 Then
'MessageBox.Show("File recognized as existing.")
'This runs the commands that DXF's each part file
Main2()
doc=docfile
doc.Close(True)
Else
'MessageBox.Show("File recognized as not existing.")
doc=docfile
doc.Close(True)
End If
End If
Next
Else
MessageBox.Show("You must have a valid Assembly document open before using this code!", "File Type Mismatch!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
End If
MessageBox.Show("DXF-ing Complete. Now closing Assembly.")
openDoc = ThisDoc.Document
doc = openDoc
doc.Close(True)
End Sub
The above code will go through and make each part active, execute whatever commands are in the sub Main2, and then go back to the assembly and move onto the next part. This code is much more time consuming but you can see what the code is doing as it goes. This is a portion of a larger code I wrote that exports each face of a part in an assembly as a DXF for CNC cutting. I can post up the entire code if you want to see everything.