Thank you so much basnederveen! This code is a great help, and I'm much closer to a solution now. I've been working on this code a bit but some things aren't working quite right.
Everything is getting saved as I wanted now with the proper names, but the code only derives the first solid body each time it goes through the loop, rather than moving on to the next solid each time.
Also, I get an error message when it tries to place those parts in an assembly that reads:
Error in rule: Derive Parts from Solid Bodies 4, in document: MSB DER Test.ipt
The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
If only the first of those two issues is fixed, this code will be a huge help to me. If you can help me actually place those parts in an assembly I will be very grateful.
Sub Main()
' set a reference to the active partdocument
Dim prt As PartDocument
'Set prt = ThisApplication.ActiveDocument
prt = ThisApplication.ActiveDocument
'Set up folder that part is stored in, as well as default template.
Dim template As String
Dim folder As String
template = "F:\Drafting & Design\The Box\Inventor\Templates\Standard.ipt"
folder = PathName(prt.fullFileName)
Dim oName As String
oName = ThisDoc.FileName(False) 'without extension
MessageBox.Show("This file name is: " & oName, "Title")
'MessageBox.Show("Folder is: " & folder, "Title")
' create an object collection to store the parts to put in assembly
Dim prtCol As ObjectCollection
'Set prtCol = ThisApplication.TransientObjects.CreateObjectCollection
prtCol = ThisApplication.TransientObjects.CreateObjectCollection
'Setting loop counter
Dim a As Integer: a=0
' loop through the filtered selection
Dim sb As SurfaceBody
For Each sb In prt.componentdefinition.SurfaceBodies
' create a new part to derive the solid body in
Dim newPart As PartDocument
'Set newPart = ThisApplication.Documents.Add(kPartDocumentObject, template, True)
newPart = ThisApplication.Documents.Add(kPartDocumentObject, template, True)
' set a reference to the derivedpartcomponents
Dim dpcs As DerivedPartComponents
'Set dpcs = newPart.ComponentDefinition.ReferenceComponents.DerivedPartComponents
dpcs = newPart.ComponentDefinition.ReferenceComponents.DerivedPartComponents
' create the scale definition
Dim dpd As DerivedPartUniformScaleDef
'Set dpd = dpcs.CreateUniformScaleDef(prt.fullFileName)
dpd = dpcs.CreateUniformScaleDef(prt.fullFileName)
' set the settings in another sub
'Call settingsDerivedPart(dpd, sb)
Call dpcs.Add(dpd)
Call prtCol.Add(newPart)
' set the part title to the solidbodies name
'newPart.PropertySets.Item("Inventor Summary Information").Item("Title").Value = sb.Name 'sb.CreatedByFeature.Name
a=a+1
Dim suffix As String
If a = 1
suffix = "WEB"
End If
If a = 2
suffix = "TF"
End If
If a = 3
suffix = "BF"
End If
' Save the part
ThisApplication.SilentOperation = True
'Call newPart.SaveAs(folder & sb.Name & " " & suffix & ".ipt", False)
Call newPart.SaveAs(folder & oName & " " & suffix & ".ipt", False)
ThisApplication.SilentOperation = False
Next sb
MessageBox.Show("I finished going through all the parts.", "Title")
' find opened assembly in which the sketch part is
Dim asm As AssemblyDocument
'Set asm = ThisApplication.Documents.Open("ADD YOUR ASSEMBLY FULLFILENAME")
asm = ThisApplication.Documents.Add(kAssemblyDocumentObject, folder, True)
' place in assembly?
'Set prt = Nothing
prt = Nothing
For Each prt In prtCol
' create an empty matrix
Dim mx As Matrix
'Set mx = ThisApplication.TransientGeometry.CreateMatrix()
mx = ThisApplication.TransientGeometry.CreateMatrix()
Call asm.ComponentDefinition.Occurrences.AddByComponentDefinition(prt.ComponentDefinition, mx)
Call prt.Close(True)
Next
End Sub
Function PathName(FullPath As String) As String
' return all left of last \
PathName = Left(FullPath, InStrRev(FullPath, "\"))
End Function