Save parts from an assembly with sequential part numbers

Save parts from an assembly with sequential part numbers

bob_clark5J5M8
Contributor Contributor
609 Views
7 Replies
Message 1 of 8

Save parts from an assembly with sequential part numbers

bob_clark5J5M8
Contributor
Contributor

Hi, I have an assembly 

It has multiple parts inside.

I would like to copy this assembly to a new number  - "12345.iam", that bit is fine...

 

For the parts inside the assembly however, I would like to loop through each of these and save each with a new filename.

 

12345-001

12345-002

12345-003

etc. 

 

Can someone kindly show me how to make this loop please?

 

0 Likes
Accepted solutions (1)
610 Views
7 Replies
Replies (7)
Message 2 of 8

mslosar
Advisor
Advisor

I could be wrong, but the problem with that is you'll have a collection of parts and an assembly that won't point to any of those parts because there's a lot more to it than just renaming the files.

 

Base Inventor has an iLogic Copy Design in it that will copy the files to a new location with new names and keep the assemblies in tact.

 

Vault cad Copy Design as an aspect of vault that is much better and allows a lot more in the way of mass find/replace.

 

As far as looping through, there's an API example that shows you how to loop through a bom and access each part. All you'd have to do at each part is save as another filename, but as noted, it won't redo the assembly structures.

0 Likes
Message 3 of 8

bob_clark5J5M8
Contributor
Contributor

Hi there, thanks for your time....this is only part of a bigger piece of code. I am not renaming parts. I am pulling solid bodies from a multi-body part file, creating an assy, then saving those bodies as individual prt files and adding to the assy... I thought I'd keep it simple when I posted)

I just need to manage the suffix of each part I save (-001,-002 etc etc). 

 

0 Likes
Message 4 of 8

bob_clark5J5M8
Contributor
Contributor

At the moment it takes the name of the solid body and appends that to the new prt filename. I'd prefer to use a suffix of -001, -002 for the prt filenames.

 

any help is greatly appreciated ))

 

' Create an assembly from a multi body part
'define the active document
oDoc = ThisDoc.Document

'create a file dialog box
Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)


'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
'set the file name string to use in the input box
oFileDlg.FileName = iProperties.Value("Project", "Part Number")

'work with an error created when cancelling the save
oFileDlg.CancelError = True
On Error Resume Next
'specify the file dialog as a save dialog 
oFileDlg.ShowSave()

'catch an empty string in the imput
If Err.Number <> 0 Then
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
f = oFileDlg.FileName
'save the file
oDoc.SaveAs(f, False) 
End If

  ' Create the assembly
  Dim asm As AssemblyDocument
  asm = ThisApplication.Documents.Add(kAssemblyDocumentObject)
  
  Dim sb As SurfaceBody
  For Each sb In oDoc.ComponentDefinition.SurfaceBodies
    ' Create part for each body
    Dim prt As PartDocument
    prt = ThisApplication.Documents.Add(kPartDocumentObject)

    
    Dim dpcs As DerivedPartComponents
   dpcs = prt.ComponentDefinition.ReferenceComponents. _
      DerivedPartComponents
    
    Dim dpd As DerivedPartUniformScaleDef
    dpd = dpcs.CreateUniformScaleDef(oDoc.FullDocumentName)
       
    ' Exclude the other solid bodies
    Dim dpe As DerivedPartEntity
    For Each dpe In dpd.Solids
      ' Have to set it for all entities as the default value
      ' can differ in various cases 
      dpe.IncludeEntity = dpe.ReferencedEntity Is sb
    Next
    
    Call dpcs.Add(dpd)
    
          
    ' Save prt
    Call prt.SaveAs(f + " " + sb.Name + ".ipt", False)
        
    ' Place an instance of it inside the assembly
    Dim mx As Matrix
   mx = ThisApplication.TransientGeometry.CreateMatrix()
    Call asm.ComponentDefinition.Occurrences. _
      AddByComponentDefinition(prt.ComponentDefinition, mx)
    
    ' Close prt
    Call prt.Close
  Next
  
  Call asm.SaveAs(f + ".iam", False)
  'Call asm.Close

 

0 Likes
Message 5 of 8

bob_clark5J5M8
Contributor
Contributor

updated, it appears that the number is being incremented but for some reason it wont save the parts . Can anyone kindly help?

 

' Create assembly from a multi body part
'define the active document
oDoc = ThisDoc.Document

'create a file dialog box
Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)


'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()
'set the file name string to use in the input box
oFileDlg.FileName = iProperties.Value("Project", "Part Number")

'work with an error created when cancelling the save
oFileDlg.CancelError = True
On Error Resume Next
'specify the file dialog as a save dialog 
oFileDlg.ShowSave()

'catch an empty string in the imput
If Err.Number <> 0 Then
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
f = oFileDlg.FileName
'save the file
oDoc.SaveAs(f, False) 
End If

  ' Create the assembly
  Dim asm As AssemblyDocument
  asm = ThisApplication.Documents.Add(kAssemblyDocumentObject)
 
 n=0
  
  Dim sb As SurfaceBody
  For Each sb In oDoc.ComponentDefinition.SurfaceBodies
    ' Create part for each body
    Dim prt As PartDocument
    prt = ThisApplication.Documents.Add(kPartDocumentObject)

    Dim dpcs As DerivedPartComponents
    dpcs = prt.ComponentDefinition.ReferenceComponents. _
      DerivedPartComponents
    
    Dim dpd As DerivedPartUniformScaleDef
    dpd = dpcs.CreateUniformScaleDef(oDoc.FullDocumentName)
       
    ' Exclude the other solid bodies
    Dim dpe As DerivedPartEntity
    For Each dpe In dpd.Solids
      ' Have to set it for all entities as the default value
      ' can differ in various cases 
      dpe.IncludeEntity = dpe.ReferencedEntity Is sb
    Next
    
    Call dpcs.Add(dpd)
 
    ' Save prt
	n = n + 1 
	MessageBox.Show(n, "Title")
    Call prt.SaveAs(f + n + ".ipt", False)

	

    
    ' Place an instance of it inside the assembly
    Dim mx As Matrix
   mx = ThisApplication.TransientGeometry.CreateMatrix()
    Call asm.ComponentDefinition.Occurrences. _
      AddByComponentDefinition(prt.ComponentDefinition, mx)
    
    ' Close prt
    Call prt.Close
  Next
  
  Call asm.SaveAs(f + ".iam", False)
  'Call asm.Close

 

0 Likes
Message 6 of 8

A.Acheson
Mentor
Mentor

Hi @bob_clark5J5M8 

Can you share any error messages? I would use a try catch in the save as line to ensure you don't run into issues saving because of duplicate file numbers etc. Have you stopped the code to ensure the part is create with solid bodies before saving? 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 7 of 8

bob_clark5J5M8
Contributor
Contributor

Hi, again thanks for replying, it is appreciated. I use an "on error resume next" earlier in the code so have stayed away from a try / catch. But when running the code if the part does not 'close', then I know it hasn't saved the files, so I can determine if it is working or not.

I have managed to extract the solid body name for the suffix and then use a Len /Mid to delete the first 5 characters leaving only the remaining integer(s).. It works fine but it's more of a dirty workaround. I will continue with it today.. Thx

 

 

0 Likes
Message 8 of 8

bob_clark5J5M8
Contributor
Contributor
Accepted solution

Finally figured it out. Probably could be tidier but it works....Thx to all. I can now move on to dealing with sheetmetal parts and patterned features.. 

 

It starts off as this.

 

bob_clark5J5M8_0-1697361477429.png

 

After the code is ran, It outputs this.

 

bob_clark5J5M8_1-1697361577524.png

 

'Create an assembly from a multi body part
'define the active document
oDoc = ThisDoc.Document

'create a file dialog box
Dim oFileDlg As Inventor.FileDialog = Nothing
InventorVb.Application.CreateFileDialog(oFileDlg)

'set the directory to open the dialog at
oFileDlg.InitialDirectory = ThisDoc.WorkspacePath()

'set the file name string to use in the input box
oFileDlg.FileName = iProperties.Value("Project", "Part Number")

'work with an error created when cancelling the save
oFileDlg.CancelError = True
On Error Resume Next

'specify the file dialog as a save dialog 
oFileDlg.ShowSave()

'catch an empty string in the imput
If Err.Number <> 0 Then
MessageBox.Show("No File Saved.", "iLogic: Dialog Canceled")
ElseIf oFileDlg.FileName <> "" Then
f = oFileDlg.FileName

'save the file
oDoc.SaveAs(f, False) 
End If

' Create the assembly
Dim asm As AssemblyDocument
asm = ThisApplication.Documents.Add(kAssemblyDocumentObject)

Dim sb As SurfaceBody
For Each sb In oDoc.ComponentDefinition.SurfaceBodies
	
'Create part for each body
Dim prt As PartDocument
prt = ThisApplication.Documents.Add(kPartDocumentObject)

    
Dim dpcs As DerivedPartComponents
dpcs = prt.ComponentDefinition.ReferenceComponents. _
DerivedPartComponents
    
Dim dpd As DerivedPartUniformScaleDef
dpd = dpcs.CreateUniformScaleDef(oDoc.FullDocumentName)
       
'Exclude the other solid bodies

Dim dpe As DerivedPartEntity
For Each dpe In dpd.Solids
	
'Set for all entities
dpe.IncludeEntity = dpe.ReferencedEntity Is sb

Next    
Call dpcs.Add(dpd)
          
'Increment suffix
 Dim n As Integer
    n = n + 1
    n = n
	
'Build new filename for prt
Dim newFileName = f & n.ToString("-00#")

'Save prt
Call prt.SaveAs(newFileName + ".ipt", False)  

'Place an instance of it inside the assembly
Dim mx As Matrix
mx = ThisApplication.TransientGeometry.CreateMatrix()

Call asm.ComponentDefinition.Occurrences. _
AddByComponentDefinition(prt.ComponentDefinition, mx)
    
'Close prt
Call prt.Close

Next
  
Call asm.SaveAs(f + ".iam", False)
  

  

0 Likes