Delete referenced files in ipt's and iam's

Delete referenced files in ipt's and iam's

fuchs35Q96
Explorer Explorer
834 Views
5 Replies
Message 1 of 6

Delete referenced files in ipt's and iam's

fuchs35Q96
Explorer
Explorer

Hi there,

 

My problem: I have one MasterIPT which is referenced to several parts and assemblies and I want to delete this reference in all of this components. Is there any possibility to do this with an iLogic rule?

 

I have tried it with the FileDescriptor method, but theres unfortunately no 'Delete'-Member in this like in the ReferencedOLEFileDescriptor.

 

Can anyone point me in the direction on how to tackle this?

0 Likes
Accepted solutions (2)
835 Views
5 Replies
Replies (5)
Message 2 of 6

WCrihfield
Mentor
Mentor

Hi @fuchs35Q96.  Can you maybe explain how this master part is being referenced by the other parts and assemblies in more detail.  Can you do what you are wanting to do manually?  If so, how are you doing it manually?

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 6

fuchs35Q96
Explorer
Explorer

Hello @WCrihfield,

 

thanks for the answer. I try to be more precise: Im working on a configurator and for this I have a master part (.ipt) in which I have added several user parameters and forms to control the dimensions of other parts. For this I have linked this master part to all of the parts which build up the sub assemblies and later the main assembly (instead of an Excel file for example). I have done this manually in the parameters window (little 'fx' button) for each relevant part. 

Now I could delete this master part link by hand in the parameters window by right clicking on the path of the linked file and then click 'delete directory'. Im searching for a way to manage this with an iLogic rule, because I have to do it with a bunch of parts. ‌‌


I have tried it with the FileDescriptor like this (just a little example of how I tried whether it even works):

 

Public Sub Main()
	
	Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument	
	Dim oOLEFD As ReferencedOLEFileDescriptor
	Dim oFD As FileDescriptor
		
	For Each oOLEFD In oDoc.ReferencedOLEFileDescriptors
		oOLEFD.Delete '<-- exists and work for deleting 3rd-Party files like Excel-Sheets
	Next
	
	For Each oFD In oDoc.ReferencedDocumentDescriptors
'		oFD.Delete '<-- this 'Delete'-Member does not exist, but how I can delete a referenced IV-File with iLogic like the way with the OLE above?
	Next
	
End Sub

 

I hope you can see what I mean. ‌‌😉


Greetings

 

0 Likes
Message 4 of 6

WCrihfield
Mentor
Mentor

Hi @fuchs35Q96.  I believe I know what you need then.  It sounds like you have 'linked' some parameters from your master part into your other assembly components, which actually works through the 'Derive' system, so those parameters have actually been 'derived' into those components.  Below is an iLogic rule example you can try out for 'breaking the link' between those components and the master part's parameters.  But you will need to edit at leas one line within this code, where it checks the file name of the master part.  However, if there is only 1 possible file that has been linked this way, then you may not even need to confirm the file name, so you could just skip the lines of code that extract the file name and check it.  I did not leave a lot of comments in the code, so if you see something that you do not understand, feel free to ask about it.

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting rule.", vbCritical, "iLogic")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	Dim oAllRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments
	For Each oRefDoc As Document In oAllRefDocs
		If oRefDoc.IsModifiable = False Then Continue For
		Dim oParams As Inventor.Parameters = Nothing
		Try : oParams = oRefDoc.ComponentDefinition.Parameters
		Catch : Continue For : End Try
		If oParams Is Nothing Then Continue For
		Dim oDPTables As DerivedParameterTables = oParams.DerivedParameterTables
		If oDPTables.Count = 0 Then Continue For
		For Each oDPTable As DerivedParameterTable In oDPTables
			'if linked to a part, it wil have a referenced component, if linked to Excel it will not
			If oDPTable.HasReferenceComponent = True Then
				Dim oRC As ReferenceComponent = oDPTable.ReferenceComponent
				If oRC.LinkedToFile Then
					Dim sFFN As String = oRC.ReferencedDocumentDescriptor.ReferencedFileDescriptor.FullFileName
					If sFFN = "C:\Temp\MasterPart.ipt" Then '<<< !! EDIT THIS !! >>>
						Try : oRC.BreakLinkToFile 'could also try oRC.Delete
						Catch : Logger.Error("Error breaking link to file.") : End Try
					End If
				End If
			End If
		Next 'oDPTable
	Next 'oRefDoc
	If oADoc.RequiresUpdate Then oADoc.Update2(True)
	'If oADoc.Dirty Then oADoc.Save2(True)
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)

0 Likes
Message 5 of 6

WCrihfield
Mentor
Mentor
Accepted solution

Actually, that example did not attempt to remove the link from the main assembly itself, just its referenced documents.  Below is an alternate version of that code in which the code for the primary purpose of the rule has been placed out into a separate custom Sub routine, so that the same code can be ran on the main assembly itself first, then on each of the referenced documents also.  The optional second input in the custom sub routine named "sRefFullFileName" is for if you want to specify the full file name (path, file name, & file extension) of the 'master part' that you are expecting the parameters to be linked to.  If supplied, it will only attempt to break that one specific link, and no others.  If not specified, it will attempt to break the link to all parameters that are derived from parts.  Currently, this should not attempt to break the link to parameters that are linked to an Excel file, but it could be expanded further to include that functionality also.

 

Sub Main
	If ThisDoc.Document.DocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting rule.", vbCritical, "iLogic")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisDoc.Document
	BreakLinkToDerivedPartParams(oADoc)
	Dim oAllRefDocs As DocumentsEnumerator = oADoc.AllReferencedDocuments
	For Each oRefDoc As Document In oAllRefDocs
		BreakLinkToDerivedPartParams(oRefDoc)
	Next 'oRefDoc
	If oADoc.RequiresUpdate Then oADoc.Update2(True)
	'If oADoc.Dirty Then oADoc.Save2(True)
End Sub

Sub BreakLinkToDerivedPartParams(oDoc As Inventor.Document, Optional sRefFullFileName As String = vbNullString)
	If oDoc.IsModifiable = False Then Exit Sub
	Dim oParams As Inventor.Parameters = Nothing
	Try : oParams = oDoc.ComponentDefinition.Parameters : Catch : Exit Sub : End Try
	If oParams Is Nothing Then Exit Sub
	Dim oDPTables As DerivedParameterTables = oParams.DerivedParameterTables
	If oDPTables.Count = 0 Then Exit Sub
	For Each oDPTable As DerivedParameterTable In oDPTables
		If oDPTable.HasReferenceComponent = True Then 'result of derived part
			Dim oRC As ReferenceComponent = oDPTable.ReferenceComponent
			If oRC.LinkedToFile Then
				If String.IsNullOrEmpty(sRefFullFileName) Then
					Try : oRC.BreakLinkToFile : Catch : End Try
				Else
					Dim sFFN As String = oRC.ReferencedDocumentDescriptor.ReferencedFileDescriptor.FullFileName
					If sFFN = sRefFullFileName Then Try : oRC.BreakLinkToFile : Catch : End Try
				End If
			End If
		End If
	Next 'oDPTable
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)

0 Likes
Message 6 of 6

fuchs35Q96
Explorer
Explorer
Accepted solution

Hi @WCrihfield,

 

before I read it, I came up with that one has to handle this with the parameters and not with the linked file itself. So I found the derived parameter method. Your solution helped to complete the code for my case.

 

Here it is (I have only one same master file (.ipt) which is linked to all parts and assemblies and I want to delete it in all components):

 

 

 

Public Sub Main()
	
	If Not ThisDoc.Document.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("Diese Regel muss aus einem Baugruppendokument ausgeführt werden. Regel beendet.", vbCritical, "iLogic")
		Exit Sub
	End If
	
	Dim oAsmDoc As AssemblyDocument = ThisDoc.Document
	
	DeleteDerivedParams(oAsmDoc)
	
	Dim oAllRefDocs As DocumentsEnumerator = oAsmDoc.AllReferencedDocuments
	
	For Each oRefDoc As Document In oAllRefDocs
		DeleteDerivedParams(oRefDoc)
	Next
	
End Sub	

Function DeleteDerivedParams(oDoc As Inventor.Document, Optional sRefFullFileName As String = vbNullString)
	'Delete parameters which come from an IV-Document (.ipt or .iam)
	If oDoc.IsModifiable = False Then Exit Function
	
	Dim oParams As Inventor.Parameters = Nothing
	
	Try
		oParams = oDoc.ComponentDefinition.Parameters
	Catch
		Exit Function
	End Try
	
	If oParams Is Nothing Then Exit Function
	
	If oParams.DerivedParameterTables.Count > 0 Then
		For Each oDevTable As DerivedParameterTable In oParams.DerivedParameterTables
			If oDevTable.HasReferenceComponent = False Then
				oDevTable.Delete
			End If
		Next
	Else
		MsgBox("Keine verlinkten Parameter vorhanden. Regel beendet.")
		Exit Function
	End If	
	
	'Delete parameters which come from an Excel file
'	If oParams.ParameterTables.Count > 0 Then
'		For Each oParamTable As ParameterTable In oParams.ParameterTables
'			If oParamTable.Linked = False
'				oParamTable.Delete
'			Else
'				For Each oOLE As ReferencedOLEFileDescriptor In oDoc.ReferencedOLEFileDescriptors
'					oOLE.Delete
'				Next
'				oParamTable.Delete
'			End If
'		Next
'	End If

End Function

 

(the last part is if one has an additional Excel file linked in a document)

 

Edit: Thank you for the quick answers and the solution! 😉

0 Likes