Apply one process to all componets in the assembly (including all subassemblies)

Apply one process to all componets in the assembly (including all subassemblies)

rbertoletti
Enthusiast Enthusiast
319 Views
4 Replies
Message 1 of 5

Apply one process to all componets in the assembly (including all subassemblies)

rbertoletti
Enthusiast
Enthusiast

Hi everyone,

I'm trying to create a rule in order to make a "batch find and replace" of all the elements inside the assembly and all sub-assemblies.

 

The process i need to perform is the following:

 

	Dim oOccName As String = oOcc.Name
	Dim oOccDoc As Document = oOcc.Definition.Document
	Dim oOccFullFileName As String = oOccDoc.FullFileName
	
'DEFINES BACKSLASH AS THE SUBDIRECTORY SEPARATOR
	Dim strCharSep As String = System.IO.Path.DirectorySeparatorChar

'FIND THE POSITION OF THE LAST BACKSLASH IN THE PATH
	oBackSlsPos = InStrRev(oOccFullFileName, "\", -1)
	
'GET THE FILE NAME WITH THE EXTENSIONS
	oOccFileName = Right(oOccFullFileName, Len(oOccFullFileName) -oBackSlsPos)

'REPLACE THE TEMPLATE FILE WITH THE PANEL FILE
Dim oPanelItemsDirectory As String = ThisDoc.Path & "\" & "\LIBRARY\"
Dim oPanelCode As String = ThisDoc.FileName(False)
Dim oNewComp As String = oPanelItemsDirectory & oPanelCode & "_" & oOccFileName

Component.Replace(oOccName, oNewComp, True

 

Thanks in advance!

0 Likes
Accepted solutions (1)
320 Views
4 Replies
Replies (4)
Message 2 of 5

WCrihfield
Mentor
Mentor

Hi @rbertoletti.  Here is a similar iLogic rule routine you can try out.  It deals with file references directly, instead of just assembly components.  It is also recursive, so that it will step down to sub structure references as many times as is needed.  This can be a tricky process though, so make sure that every file being referenced by the assembly has a copy file in that sub directory with the same name.  It does seem a bit odd to have a whole folder full of files with the same exact names as other existing files.  That usually causes problems, so the replacement files usually have slightly different file names.  But then again we use one main project file for everything we do, instead of a different project file for every assembly, so it may not be an issue for you.

Here is the file references replacement code you can try out, if you want.

Sub Main
	Dim oDoc As Document = ThisDoc.Document
	If oDoc.FileSaveCounter = 0 Then Return 'can't access file, if no file exists yet
	Dim oFDs As FileDescriptorsEnumerator = oDoc.File.ReferencedFileDescriptors
	If oFDs.Count = 0 Then Return
	sReplacementPath = ThisDoc.Path & "\LIBRARY\"
	If System.IO.Directory.Exists(sReplacementPath) = False Then Return
	sFileName = ThisDoc.FileName(False)
	RecursivelyReplaceFileReferences(oFDs) 'run custom Sub routine
	If oDoc.RequiresUpdate Then oDoc.Update2(True)
	'If oDoc.Dirty Then oDoc.Save2(True)
	MsgBox("This rule's work has finished.", vbInformation, "Job Is Done!")
End Sub

Dim sReplacementPath As String
Dim sFileName As String

Sub RecursivelyReplaceFileReferences(oFDs As FileDescriptorsEnumerator)
	If oFDs Is Nothing OrElse oFDs.Count = 0 Then Return
	For Each oFD As Inventor.FileDescriptor In oFDs
		If oFD.ReferenceDisabled Or oFD.ReferenceMissing Then
			Logger.Debug("ReferenceDisabled or ReferenceMissing, so skipping one.")
			Continue For
		End If
		Dim sRefFileName As String = System.IO.Path.GetFileName(oFD.FullFileName) 'with extension
		Dim sNewFFN As String = sReplacementPath & sFileName & "_" & sRefFileName
		If System.IO.File.Exists(sNewFFN) = False Then
			Logger.Debug("The following replacement file does not exist:" & vbCrLf & sNewFFN)
			Continue For
		End If
		Dim oChildFDs As FileDescriptorsEnumerator = Nothing
		If oFD.ReferencedFile IsNot Nothing Then
			oChildFDs = oFD.ReferencedFile.ReferencedFileDescriptors
		End If
		Try : oFD.ReplaceReference(sNewFFN)
		Catch e As Exception
			Logger.Error("Error replacing:" & vbCrLf & 	oFD.FullFileName & vbCrLf & _
			"with:" & vbCrLf & sNewFFN & vbCrLf & _
			e.Message & vbCrLf & e.StackTrace & vbCrLf)
			'Continue For 
		End Try
		If oChildFDs IsNot Nothing AndAlso oChildFDs.Count > 0 Then
			RecursivelyReplaceFileReferences(oChildFDs)
		End If
	Next 'oFD
End Sub

If this solved your problem, or answered your question, please click ACCEPT SOLUTION .
Or, if this helped you, please click (LIKE or KUDOS) 👍.

 

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 3 of 5

rbertoletti
Enthusiast
Enthusiast
Ok it works fine with all the elements at the first level, but is it possible to extend this for all the sub assemblies elements?

Thanks
0 Likes
Message 4 of 5

WCrihfield
Mentor
Mentor
Accepted solution

Hi @rbertoletti.  I apologize if the routine is not functioning as planned.  Since I do not use that copy design type process myself, I was unable to test the code before posting it.  I may need to move the 4 lines of code within the 'RecursivelyReplaceFileReferences' Sub routine that capture 'child' FileDescriptors from the current file, down below the Try...Catch statement used for the 'ReplaceReference' method.  That way the child FileDescriptors will hopefully be obtained from the 'replacement' file, instead of the 'original' file.  I hope that first run did not mess up your 'template' components.  You may want to check on those.  Also, did you look within your iLogic Log window for any of the feedback messages?  I tried to make the process be as error free as possible, but I did not create a Log message as feedback for every possible situation.  Maybe this version of the code will work better for you.  I have attached the entire edited code as a test file this time.  Proceed with caution, since I still have not tested this myself yet.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 5 of 5

rbertoletti
Enthusiast
Enthusiast
Hi @WCrihfield.
Unfortunately the code doesn't work also with the latest change.
It doesn't show any error but it still replace only the element in the first level.
Let me know if you have another solution.

Thanks
0 Likes