Save Assembly to new location and delete parts

Save Assembly to new location and delete parts

Anonymous
Not applicable
2,212 Views
11 Replies
Message 1 of 12

Save Assembly to new location and delete parts

Anonymous
Not applicable

I have a main assembly. Using ilogic I have created a configuration of parts and sub-assemblys. Once I have a configuration I like, I want to click a button and save a "copy as" to a new folder, and a different name. Would like all the used parts & sub-assemblys to be placed in this new folder too.

 

Also I want to delete suppressed parts and sub-assemblys in the main assembly as its saved, I also want parts in sub-assemblys deleted too, they will be suppressed. My sub-assemblys have components suppressed that I want deleted too. After all this is done I would like to have the original assembly opened again, or maybe it didn't close at all.

Below I have code that will save the master assembly to a specified folder and some parts, and it deletes all main assembly suppressed parts and suppressed sub-assemblys but not the parts and sub-assemblys that are suppressed at the sub-asembly level, if you know what I mean. The below code also does'nt copy over all the sub-assemblys. I want it to work lile packNgo.

 

I found this code online and tried to piece it together.  I am still trying to learn this stuff..

Here is my code:

 

'Deleting the suppressed parts from the assembly
    Dim oComp As ComponentOccurrence
    Dim oComps As ComponentOccurrences
    oComps = ThisDoc.Document.ComponentDefinition.Occurrences

    For Each oComp In oComps
       If Component.IsActive(oComp.Name) = False Then oComp.Delete
       On Error Resume Next
    Next

 

'Delete Empty Folders
   oDoc = ThisDoc.Document
   oPane = oDoc.BrowserPanes("Model")
   oTopNode = oPane.TopNode


'Iterate through the browser folders
   For Each oFolder In oTopNode.BrowserFolders
   i = 0


'Iterate through the nodes in the folder
   oFolderNodes = oFolder.BrowserNode.BrowserNodes
   For Each oNode As BrowserNode In oFolderNodes

 

'count the nodes
    i = i+1
    Next


'delete the folder if the count = 0
   If i = 0 Then
   oFolder.Delete
   Else
End If
Next

 

'Saving a copy of the assembly components
  Dim refDocs as DocumentsEnumerator=ThisDoc.Document.AllReferencedDocuments
  Dim compCount As Integer = refDocs.Count
  For j = 1 To compCount
  refDocs.item(j).SaveAs( "C:\Users\aws01\Documents\Library\Pole Configurations\Parts\Part" & j & ".ipt", False)
Next

 

'Saving a copy of the assembly document
ThisDoc.Document.SaveAs( "C:\Users\aws01\Documents\Library\Pole Configurations\Assembly\PoleConfig.iam", False)

'---------------------------------------------------------------------------------------------------------------------------
'Save DWF File
  Test=InputBox("Add File name", "Please Add your file name", "Prefix number - File")

   If (Not System.IO.Directory.Exists(Test)) Then

   System.IO.Directory.CreateDirectory(Test)

End If
ThisDoc.Document.SaveAs("C:\Users\aws01\Documents\Library\Pole Configurations\" &Test &".dwf", True)

 

 

Thanks Very much

Anthony

0 Likes
Accepted solutions (1)
2,213 Views
11 Replies
Replies (11)
Message 2 of 12

Owner2229
Advisor
Advisor

Hi, here is quick modification of your code which you can try:

It could be done easier by ussing Subs(). It would then support more than two levels of assemblies, so let me know if you'd like me to rewrite it for you and/or if something doesn't work in this code (can't test it atm.).

 

    oDoc = ThisDoc.Document
'Deleting the suppressed parts from the assembly Dim oComp As ComponentOccurrence
Dim oComp1 As ComponentOccurrence Dim oComps As ComponentOccurrences
Dim oComps1 As ComponentOccurrences oComps = oDoc.ComponentDefinition.Occurrences For Each oComp In oComps If Component.IsActive(oComp.Name) = False Then oComp.Delete
If oComp.ReferencedDocumentDescriptor.ReferencedDocumentType = kAssemblyDocumentObject Then
oComps1 = oComp
For Each oComp1 In oComps1
If Component.IsActive(oComp1.Name) = False Then oComp.Delete
Next
End If On Error Resume Next Next 'Delete Empty Folders oPane = oDoc.BrowserPanes("Model") oTopNode = oPane.TopNode 'Iterate through the browser folders For Each oFolder In oTopNode.BrowserFolders i = 0 'Iterate through the nodes in the folder oFolderNodes = oFolder.BrowserNode.BrowserNodes For Each oNode As BrowserNode In oFolderNodes 'count the nodes i = i+1 Next 'delete the folder if the count = 0 If i = 0 Then oFolder.Delete Else End If Next 'Saving a copy of the assembly components Dim refDocs as DocumentsEnumerator = oDoc.AllReferencedDocuments
Dim refDocs1 as DocumentsEnumerator Dim compCount As Integer = refDocs.Count
Dim compCount1 As Integer For j = 1 To compCount If refDocs.Item(j).DocumentType = DocumentTypeEnum.kPartDocumentObject Then
refDocs.Item(j).SaveAs( "C:\Users\aws01\Documents\Library\Pole Configurations\Parts\Part" & j & ".ipt", False) Else If refDocs.Item(j).DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
refDocs.Item(j).SaveAs( "C:\Users\aws01\Documents\Library\Pole Configurations\Parts\Assembly" & j & ".iam", False)
refDocs1 = refDoc.Item(j).AllReferencedDocuments
compCount1 = refDocs1.Count
For k = 1 To compCount1
refDocs1.Item(k).SaveAs( "C:\Users\aws01\Documents\Library\Pole Configurations\Parts\Part" & k & ".ipt", False)
Next
End If
Next 'Saving a copy of the assembly document oDoc.SaveAs( "C:\Users\aws01\Documents\Library\Pole Configurations\Assembly\PoleConfig.iam", False) '--------------------------------------------------------------------------------------------------- 'Save DWF File Test=InputBox("Add File name", "Please Add your file name", "Prefix number - File") If (Not System.IO.Directory.Exists(Test)) Then System.IO.Directory.CreateDirectory(Test) End If ThisDoc.Document.SaveAs("C:\Users\aws01\Documents\Library\Pole Configurations\" &Test &".dwf", True)

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 3 of 12

Anonymous
Not applicable

Thank very much for your detailed response.  I copied the code and created an external rule.  I get the following error message: 

 

"Line 54: 'refDoc' is not declated.  It may be inaccessible due to its protection level."

 

Any thoughts on this?

 

Thanks

Anthony

0 Likes
Message 4 of 12

MechMachineMan
Advisor
Advisor
IT should say refDocs rather than refDoc.

If you look through the code above it, refDoc is not declared anywhere, or assigned a value, therefore the program doesn't know what to do with it. refDocs, however, is both, so the program knows exactly what it is.
Also, the ".item()" following refDoc seems gramatically off as we only need to access items from within a collection.

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 5 of 12

Anonymous
Not applicable

Thanks, I fixed the typo.  Not quite sure what this does.  

 

SyntaxEditor Code Snippet

refDocs.Item(j).SaveAs( "C:\Users\aws01\Documents\Library\Pole Configurations\Parts\Assembly" & j & ".iam", False)

 

I think the above code saves the current assembly with parts deleted.  I re-ran the code.  I have attached two screen captures, before and after.  Most of the parts get deleted.

 

Any thoughts.  I appologise I am not very good at this level of code, pretty advanced for me.

 

Thanks

Anthony

0 Likes
Message 6 of 12

MechMachineMan
Advisor
Advisor
From the code, it looks like it would delete the parts if they are suppressed, or they are confusing the oFolder section of the code...

--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
0 Likes
Message 7 of 12

Anonymous
Not applicable

Is there a solution.  I want all the sub-assemblies, parts & parts in sub-assemblies that are suppressed deleted.  Just not the other parts or assemblies.

 

Thanks

0 Likes
Message 8 of 12

Owner2229
Advisor
Advisor
Accepted solution

Alright, so I digged a bit deeper it this code and this is what I've got:

I tested it, so it should work now.

Most of the code is from Adam Nagy, so feel free to give him Kudos:

Autodesk Forum - Delete supressed parts

 

Sub Main()
    Dim oDoc As AssemblyDocument
    oDoc = ThisApplication.ActiveDocument
    Dim acd As AssemblyComponentDefinition
    acd = oDoc.ComponentDefinition
    Call DeleteSuppressedComponent(acd.Occurrences, acd)

    'Delete Empty Folders
    oPane = oDoc.BrowserPanes("Model")
    oTopNode = oPane.TopNode

    'Iterate through the browser folders
    For Each oFolder In oTopNode.BrowserFolders
        i = 0

        'Iterate through the nodes in the folder
        oFolderNodes = oFolder.BrowserNode.BrowserNodes
        For Each oNode As BrowserNode In oFolderNodes

            'count the nodes
            i = i+1
        Next

        'delete the folder if the count = 0
        If i = 0 Then
            oFolder.Delete
        End If
    Next

    'Path to the workplace
    oPath = "C:\Users\aws01\Documents\Library\Pole Configurations\"

    'Saving a copy of the assembly components
    Dim refDocs As DocumentsEnumerator = oDoc.AllReferencedDocuments
    Dim compCount As Integer = refDocs.Count
	Dim refDoc As Document
    For j = 1 To compCount
	refDoc = refDocs.Item(j)
'If the component in assembly is part then save it to "Parts" folder If refDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then refDoc.SaveAs(oPath & "Parts\Part" & j & ".ipt", False)
'If the component in assembly is subassembly then save it to "Assembly" folder Else If refDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then refDoc.SaveAs(oPath & "Assembly\Assembly" & j & ".iam", False) End If Next 'Saving a copy of the assembly document oDoc.SaveAs(oPath & "Assembly\PoleConfig.iam", False) '--------------------------------------------------------------------------------------------------- 'Save DWF File Test = InputBox("Add File name", "Please Add your file name", "Prefix number - File") If (Not System.IO.Directory.Exists(Test)) Then System.IO.Directory.CreateDirectory(Test) End If ThisDoc.Document.SaveAs(oPath & Test & ".dwf", True) End Sub Private oPath As String Sub DeleteSuppressedPatterns(cd As ComponentDefinition) If Not TypeOf cd Is AssemblyComponentDefinition Then Exit Sub End If Dim acd As AssemblyComponentDefinition acd = cd Dim op As OccurrencePattern For Each op In acd.OccurrencePatterns Dim allSuppressed As Boolean allSuppressed = True Dim ope As OccurrencePatternElement For Each ope In op.OccurrencePatternElements Dim co As ComponentOccurrence For Each co In ope.Occurrences If Not co.Suppressed Then allSuppressed = False Exit For End If Next If Not allSuppressed Then Exit For End If Next If allSuppressed Then op.Delete End If Next End Sub Sub DeleteSuppressedComponent(occs As ComponentOccurrences, cd As ComponentDefinition) Call DeleteSuppressedPatterns(cd) Dim occ As ComponentOccurrence For Each occ In occs If occ.Suppressed Then If occ.PatternElement Is Nothing Then occ.Delete End If Else Call DeleteSuppressedComponent(occ.SubOccurrences, occ.Definition) End If Next End Sub

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 9 of 12

Anonymous
Not applicable

WOW, thanks very much.  This code works like a charm.  This is going to be an excellent starting point for me to learn how you did this.

 

Thanks again for you help

Anthony

0 Likes
Message 10 of 12

Owner2229
Advisor
Advisor

You're welcomed, here you have the same code (a bit shortened on some lines) with some more descriptions, so you can learn from it. You also have it in attachment.

 

Sub Main()
    'Define the document
    Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
    Dim acd As AssemblyComponentDefinition = oDoc.ComponentDefinition
	
    'Start Sub to delete all suppressed components
    Call DeleteSuppressedComponent(acd.Occurrences, acd)

    'Get all browser Folders
    oPane = oDoc.BrowserPanes("Model")
    oTopNode = oPane.TopNode

    'Iterate through the browser folders
    For Each oFolder In oTopNode.BrowserFolders
        i = 0

        'Iterate through the nodes in the folder
        oFolderNodes = oFolder.BrowserNode.BrowserNodes
        For Each oNode As BrowserNode In oFolderNodes

            'Count the nodes
            i = i+1
        Next

        'Delete the folder if the count = 0
        If i = 0 Then
            oFolder.Delete
        End If
    Next

    'Path to workplace
    Dim oPath As String = "C:\Users\aws01\Documents\Library\Pole Configurations\"

    'Saving a copy of the assembly components
    Dim refDocs As DocumentsEnumerator = oDoc.AllReferencedDocuments

'Count all components Dim compCount As Integer = refDocs.Count

'Iterate through all components Dim refDoc As Document For j = 1 To compCount refDoc = refDocs.Item(j) 'If the component in assembly is part then save it to "Parts" folder If refDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then refDoc.SaveAs(oPath & "Parts\Part" & j & ".ipt", False) 'If the component in assembly is subassembly then save it to "Assembly" folder Else If refDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then refDoc.SaveAs(oPath & "Assembly\Assembly" & j & ".iam", False) End If Next 'Saving a copy of the assembly document oDoc.SaveAs(oPath & "Assembly\PoleConfig.iam", False) '--------------------------------------------------------------------------------------------------- 'Promt user to select file name and folder Test = InputBox("Add File name", "Please Add your file name", "Prefix number - File") 'If the folder doesn't exist, then create it If (Not System.IO.Directory.Exists(Test)) Then System.IO.Directory.CreateDirectory(Test) End If 'Save the DWF file oDoc.SaveAs(oPath & Test & ".dwf", True) End Sub 'Sub to delete all suppressed components Sub DeleteSuppressedComponent(occs As ComponentOccurrences, cd As ComponentDefinition) 'Start Sub to delete all subassembly components Call DeleteSuppressedPatterns(cd) 'Iterate through all components Dim occ As ComponentOccurrence For Each occ In occs 'If the component is suppressed and has no pattern (parent subassembly), then delete it If occ.Suppressed Then If occ.PatternElement Is Nothing Then occ.Delete End If 'If the component isn't suppressed, then start this Sub again for all subcomponents in this component Else Call DeleteSuppressedComponent(occ.SubOccurrences, occ.Definition) End If Next End Sub 'Sub to delete all subassembly components Sub DeleteSuppressedPatterns(cd As ComponentDefinition) 'If the component's type isn't assembly, then exit this Sub If Not TypeOf cd Is AssemblyComponentDefinition Then Exit Sub End If Dim acd As AssemblyComponentDefinition = cd 'Iterate through all components
Dim op As OccurrencePattern For Each op In acd.OccurrencePatterns Dim allSuppressed As Boolean = True Dim ope As OccurrencePatternElement 'Iterate through all pattern components in relation to currently selected component (see. 3 lines above) For Each ope In op.OccurrencePatternElements 'Iterate through all components in currently selected pattern component
Dim co As ComponentOccurrence For Each co In ope.Occurrences 'If any of the components isn't suppresed then return "False" for variable "allSuppressed" If Not co.Suppressed Then allSuppressed = False Exit For End If Next 'If "allSuppressed" isn't "True", then exit this iteration If Not allSuppressed Then Exit For End If Next 'If "allSuppressed" is "True", then delete the pattern component If allSuppressed Then op.Delete End If Next End Sub 

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
Message 11 of 12

Anonymous
Not applicable

Wow you are awesome.  Thanks very much 🙂

 

Anthony

0 Likes
Message 12 of 12

TechInventor20
Advocate
Advocate

hi, 

 

I'm looking for a code like this for days! you're know looking at the "active" document but what if you wan't to look at a document in your folder.

 

Example:

I want to rename and replace a assembly with the parts.

Assembly ;                                        C:\01 - Assembly\Testassembly1.iam

Part1 inside assembly ;                 C:\02 - Parts\Testpart1.ipt

Part2 inside assembly ;                C:\02 - Parts\Testpart2.ipt

 

New location and names would be :

C:\Newlocation\01 - assembly\Newassembly1.iam

C:\Newlocation\02 - parts\Newpart1.ipt

C:\Newlocation\02 - parts\Newpart2.ipt

 

How do you do this? 

Is this even possible?

0 Likes