iLogic Save and Replace Assembly and custom parts

iLogic Save and Replace Assembly and custom parts

ILU
Enthusiast Enthusiast
2,202 Views
6 Replies
Message 1 of 7

iLogic Save and Replace Assembly and custom parts

ILU
Enthusiast
Enthusiast

hi guys,

 

For some time I am looking for a way to save and replace an assembly and some of it's parts via iLogic.

I fond a good rule to save and replace Main assembly and ALL parts into it, but we are working with Vault and many of parts from Main assembly are standard parts saved into vault, which doesn't need to get saved and replaced.

 I would either look for a way of not saving and replacing the vaulted parts ( read only parts ) or to save only one part as the "master" file ( or more file's if possible ).

I tried with parts/component occurrences but I'm stuck.

 

the two codes/rules that I tried to combine are:

  1.  Find a part by key name, even when this was solution on another post, I just couldn't adapt it to my needs.
Dim oAsmCompDef As AssemblyComponentDefinition
        oAsmCompDef = ThisApplication.ActiveDocument.ComponentDefinition
        
        'Iterate through all of the occurrences
        Dim oOccurrence As ComponentOccurrence
        
        For Each oOccurrence In oAsmCompDef.Occurrences
            Dim oName As String
            If oOccurrence.Name.contains("family name") And oOccurrence.Name.contains("instance number") Then
                oName = oOccurrence.Name
                If condition Then
                    oOccurrence.suppress
                    
                Else
                    oOccurrence.unsuppress
                End If
                            
                Exit For
            End If
            
        Next

2. Save and replace the whole assembly and all parts into it...

Sub Main
	'_____________
	oProjectfol = InputBox("Fill in  P nr.", "Project Nr. ?", "P00001")
	
	If oProjectfol = "" Then
		Return
	End If
	Suffix = InputBox("Example: 'Copy.01'","Fill in Part.nr !","Part.0123")
	If Suffix = "" Then
		Return
	End If
		
	Dim dir3 As String = ("C:\Workspace\_OurCompany\Projecten\") & "\" & oProjectfol
	
	'Check for the "Project folder" folder and create it if it does not exist
If Not System.IO.Directory.Exists(dir3) Then
 System.IO.Directory.CreateDirectory(dir3)

End If




		oInvDoc = ThisDoc.Document
		Dim refDocs As DocumentsEnumerator = oInvDoc.AllReferencedDocuments
		Dim refDoc As Document
		
		For Each refDoc In refDocs
			
					'detect If document Is Assembly And save As Assembly		
			If refDoc.DocumentType = 12291 Then
			
				'define new Name for copy of the referenced document
				NewFileName = "-" & refDoc.DisplayName
						
				'check For extension In name And remove If there
				If NewFileName.EndsWith(".iam") = True Then
					NewFileName = NewFileName.Substring(0,NewFileName.length-4)
					'MessageBox.Show(NewFileName)
				End If
				
				refDoc.SaveAs(dir3 & "\" & oProjectfol & NewFileName & "-" & Suffix & ".iam", False)
			
			Else
			
				'initialize object for PartComponentDefinition to access property if part is member of content center
				Dim oPartCompDef As PartComponentDefinition
				oPartCompDef = refDoc.ComponentDefinition
				
				If Not oPartCompDef.IsContentMember Then
				
					NewFileName = "-" & refDoc.DisplayName 
					
					If NewFileName.EndsWith(".ipt") = True Then
							NewFileName = NewFileName.substring(0,NewFileName.length-4)
							'MessageBox.Show(NewFileName)
					End If
				
					refDoc.SaveAs(dir3 & "\" & oProjectfol & NewFileName & "-" & Suffix & ".ipt",False)
				Else
					'MessageBox.Show("The component" & refDoc.DisplayName & "is a content center part and will not be copied!")
				End If
			End If
		
		Next
			NewAssemblyName = "-" & ThisDoc.FileName(True)
			'InputBox("What is the new name for the main assembly?", "New module name", oProjectfol & "-" & ThisDoc.FileName(True))
			
			If NewAssemblyName = "" Then
				NewAssemblyName = ThisDoc.FileName(True)
				If NewAssemblyName.EndsWith(".iam") = True Then
					NewAssemblyName = NewAssemblyName.Substring(0,NewAssemblyName.length-4)
				End If
				'MessageBox.Show(NewAssemblyName,"Old assembly name")
				ThisDoc.Document.SaveAs(dir3 & "\" & oProjectfol & NewAssemblyName & "-" & Suffix & ".iam" ,False)
			Else
				'MessageBox.Show(NewAssemblyName, "New assembly name")
				If NewAssemblyName.EndsWith(".iam") = True Then
					NewAssemblyName = NewAssemblyName.Substring(0,NewAssemblyName.length-4)
				End If
				ThisDoc.Document.SaveAs(dir3 & "\" & oProjectfol & NewAssemblyName & "-" & Suffix & ".iam",False)
			End If
			
			Dim ctrl As ControlDefinition
			ctrl = ThisApplication.CommandManager.ControlDefinitions("AppSaveAllCmd")
			ctrl.Execute()
			'Open Folder
			Try
			System.Diagnostics.Process.Start(dir3)
			Catch
				End Try

End Sub

 

0 Likes
Accepted solutions (1)
2,203 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable

Hi @ILU .

 

In terms of filename, how would you differenciate the non-standard parts (need to be replaced)   from the standard parts (dont need to be replaced)?

Does it has different filename length?

Any keywords in standard parts that doesnt exist in non-standard?

Does standard parts are all saved in the same folder?

0 Likes
Message 3 of 7

ILU
Enthusiast
Enthusiast

hi @Anonymous ,

 

  • Does it has different filename length?

most of our Standard parts , that will not need to be saved and replaced, have the following structure 0101-4-0987.ipt ( or .iam )

  • Any keywords in standard parts that doesnt exist in non-standard?

Mostly our standard parts are numbers as above example

  • Does standard parts are all saved in the same folder?

No, Standard parts are saved on external server, as mentioned , in Vault, the other are saved on project file folder localy, on mine "C"

 

for a first example I would like to save and replace a main assembly having 3 parts.

2 standard, having the above structure ( 0000-0-0000.ipt ) and 

1 part named " Master.ipt "

 

 

0 Likes
Message 4 of 7

ILU
Enthusiast
Enthusiast

@JhoelForshav 

Hi   , I am pretty sure it is not an impossible task, do you have time to have a look on above post?

Thank's for your precious time 😁

0 Likes
Message 5 of 7

ILU
Enthusiast
Enthusiast

hi @Curtis_Waguespack ,  can you help me with this ilogic rule?

 

Mainly I hove ( for moment ) an assembly with 5 parts, from which i am willing to save and replace the Master part and Main assembly 

0 Likes
Message 6 of 7

Curtis_Waguespack
Consultant
Consultant
Accepted solution

Hi @ILU 

 

Here is a quick example, where the assembly looks like this

Curtis_W_0-1620749146889.png

and looks like this after the rule is run

 

Curtis_W_1-1620749181450.png

 

 

I hope this helps.
Best of luck to you in all of your Inventor pursuits,
Curtis
http://inventortrenches.blogspot.com

 

 

 

 

 

Sub Main()

	Dim oAsmDoc As AssemblyDocument
    oAsmDoc = ThisApplication.ActiveDocument
    Call TraverseAssembly(oAsmDoc.ComponentDefinition.Occurrences)
	oName = ThisDoc.FileName(False) 'without extension
    oAsmDoc.SaveAs (ThisDoc.Path & "\" & "New_" & oName & ".iam", False)
End Sub

Sub TraverseAssembly(Occurrences As ComponentOccurrences)
 	oAsmDoc = ThisApplication.ActiveDocument 

	Dim oOcc As ComponentOccurrence
    Dim doc As Document
    For Each oOcc In Occurrences
        If oOcc.DefinitionDocumentType = kAssemblyDocumentObject Then
            Call TraverseAssembly(oOcc.SubOccurrences)
        End If
  		doc = oOcc.Definition.Document
		sPath = oOcc.ReferencedFileDescriptor.FullFileName
		
		'split path and name into an array named sSplit
		'example:
		'	"C:\Temp\Part1.ipt
		'	sSplit(0) = "C:"
		'	sSplit(1) = "Temp"
		'	sSplit(2) = "Part1.ipt"
		sSplit = sPath.Split("\")
		
		'get the Upper Bounds of the array
		'knowing that this will be the File name with extention 
		sName = sSplit(UBound(sSplit))
		
		'only process parts with master in name
		If UCase(sName).Contains("MASTER") = True Then
			
			'remove file name from path
			sPath = Replace(sPath, sName,"")
			
			'add prefix to file name
			sName = "New_" & sSplit(UBound(sSplit))

			'save new part 
	        doc.SaveAs(sPath & sName, False)
		End If

    Next 
End Sub

 

 

 

 

 

EESignature

Message 7 of 7

ILU
Enthusiast
Enthusiast

you are awesome Curtis
Thank you

0 Likes