iLogic Automatically Find, Save & Replace Specific Components

iLogic Automatically Find, Save & Replace Specific Components

sallen
Explorer Explorer
1,074 Views
6 Replies
Message 1 of 7

iLogic Automatically Find, Save & Replace Specific Components

sallen
Explorer
Explorer

Hi,

 

I've have created several configurators that covers a very wide range of variations. A lot of the components used are customised parts or subassemblies and these files are saved starting with _ so we know which parts need to saved and replaced in Inventor or copy designed via Vault. But there are also a lot of standard components that don't need to be copied. (These files don't start with _).

 

With the help from several posts with similar questions, I have written out rules to be able to select components in the configurator so it will automatically save and replace the selected component(s) but to take it to the next level, I'm trying to find a way to automatically save and replace all the components in the assembly that start with _. I am also hoping I can save it automatically in the same folder as the assembly/open document without using a dialog box.

 

I have the first part of the rule written out to find each component starting with _, but now stuck on how to save & replace these components when found. If I could have any help that would be great, thanks.

'Define the open document
Dim openDoc As Document
openDoc = ThisDoc.Document

'Look at all of the files referenced in the open document
Dim docFile As Document
For Each docFile In openDoc.AllReferencedDocuments                
'Format file name                   
Dim FNamePos As Long
FNamePos = InStrRev(docFile.FullFileName, "\", -1)                        
Dim docFName As String
docFName = Right(docFile.FullFileName, Len(docFile.FullFileName) -FNamePos)
'Create new filename
Dim NewFilename As String
NewFilename = iProperties.Value("Project", "Part Number") & docFName
'Create path to be saved in
NewPath = ThisDoc.Path
'Save & replace if it starts with _
If Left(docFName, 1) = "_" Then
i = MessageBox.Show(docFName & " Will Be Saved & Replaced With " & NewFilename & " In " & NewPath , "Save & Replace _ Components", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
End
If InventorVb.DocumentUpdate() Next

 

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

marcin_otręba
Advisor
Advisor

it is enough if you do:

 

docFile.SaveAs (NewPath & NewFilename,False	)

because i set savecopyas to false, when save operation will took place it will replace this document in all open assemblies.

 

if not then rather than going trough allreferenceddocuments go trough occurrences and then you can use:

 

marcin_otrba_0-1662455843188.png

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

Message 3 of 7

sallen
Explorer
Explorer
@marcin_otręba , Thanks for that, I had tried that out on another configurator yesterday and wasn't working but is working on a different configurator today.

 

I forgot to add, is there a possibility of copying the drawings to these components as well?

0 Likes
Message 4 of 7

marcin_otręba
Advisor
Advisor
Accepted solution

sure, you can use something like:

 

Function drawing(oldn As String, newn As String, xx As String)
ThisApplication.SilentOperation = True
Dim odraw As DrawingDocument 
If xx <> "" Then
 odraw = ThisApplication.Documents.Open(oldn, False)
odraw.SaveAs newn, True
 odraw.Close (True)
  odraw = ThisApplication.Documents.Open(newn, False)
 Dim oFD As FileDescriptor
 oFD = odraw.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor
 oFD.ReplaceReference (xx)

 odraw.Update
 odraw.Save

 odraw.Close(True)
 End 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

Message 5 of 7

sallen
Explorer
Explorer

@marcin_otręba Sorry if I'm making clear and obvious mistake here, this is the first time I've properly tried placing a function in my rules. I've put the function in to the rule and played about with it in a variety of ways to use it. It just always fails on the 'odraw = ThisApplication.Documents.Open (oldn, False)' line. I think I have placed the run function in the correct place now, after placing it in several different places. Also used different dim's to send to the function (old & new names, with & without paths, with & without extensions, etc.) but might not be understanding that correctly. I'm not sure where I'm getting it wrong, Could you help on which strings I need to sent to the functions from the original rule please?

 

Function Drawing(oldn As String, newn As String, xx As String)
	ThisApplication.SilentOperation = True

	Dim odraw As DrawingDocument
	
	If xx <> "" Then
		odraw = ThisApplication.Documents.Open(oldn, False)     <----- Fails here
		odraw.SaveAs(newn, True)
		odraw.Close(True)
		odraw = ThisApplication.Documents.Open(newn, False)
		Dim oFD As FileDescriptor
		oFD = odraw.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor
		oFD.ReplaceReference(xx)
		
		odraw.Update
		odraw.Save
		
		odraw.Close(True)
	End If

End Function 

 

0 Likes
Message 6 of 7

marcin_otręba
Advisor
Advisor

you must call your function like below:

 

Sub Main()
drawing(old drawing fullfilename With extension, New drawing fullfilename With extension, fullfile name Of model File To be replaced inside your drawing)
drawing("c:\oldmodeldrawing.idw", "c:\newmodeldrawing.idw", "c:\newmodel.ipt")
End Sub

 

Hi, maybe you want to check my apps:


DrawingTools   View&ColoringTools   MRUFolders

Message 7 of 7

sallen
Explorer
Explorer

@marcin_otręba brilliant, thanks for that. I had to add a little try, catch in the function because not all of the components had drawings to be copied. Got it all working now though and running well so far

 

Sub Main()
	'Define the open document
	Dim openDoc As Document = ThisDoc.Document
	Dim docFile As Document
	
	'Look at all of the files referenced in the open document
	For Each docFile In openDoc.AllReferencedDocuments
	
		'Filename calculations
		Dim OldModelFilenameAndPathLength As Long = Len(docFile.FullFileName)
		Dim OldPathLength As Long = InStrRev(docFile.FullFileName, "\")
		Dim OldModelFilenameLength As Long = OldModelFilenameAndPathLength - OldPathLength

		'Format old file names with extension
		Dim OldFilePath As String = Left(docFile.FullFileName, OldPathLength)
		Dim OldModelFilename As String = Right(docFile.FullFileName, OldModelFilenameLength)
		Dim OldModelFullFilename As String = docFile.FullFileName
		Dim OldDwgFilename As String = Left(OldModelFilename, (OldModelFilenameLength-4)) & ".idw"
		Dim OldDwgFullFilename As String = OldFilePath & OldDwgFilename

		'Create new filenames with extension
		Dim NewPath As String = ThisDoc.Path & "\"
		Dim NewModelFilename As String = iProperties.Value("Project", "Part Number") & OldModelFilename
		Dim NewModelFullFilename As String = NewPath & NewModelFilename
		Dim NewDwgFilename As String = iProperties.Value("Project", "Part Number") & OldDwgFilename
		Dim NewDwgFullFilename As String = NewPath & NewDwgFilename

		'Save & replace parts & drawings if it starts with _
		If Left(OldModelFilename, 1) = "_" Then
			'Save & Replace
			docFile.SaveAs(NewModelFullFilename, False)
			
			'Run Function
			Drawing(OldDwgFullFilename, NewDwgFullFilename, NewModelFullFilename)
			
		End If
	
		InventorVb.DocumentUpdate()
	
	Next

End Sub



Function Drawing(oldn As String, newn As String, xx As String)
	ThisApplication.SilentOperation = True

	Dim odraw As DrawingDocument
	
	If xx <> "" Then
		Try
			odraw = ThisApplication.Documents.Open(oldn, False)
			odraw.SaveAs(newn, True)
			odraw.Close(True)
			odraw = ThisApplication.Documents.Open(newn, False)
			Dim oFD As FileDescriptor
			oFD = odraw.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor
			oFD.ReplaceReference(xx)
			
			odraw.Update
			odraw.Save
			
			odraw.Close(True)
		Catch
			
		End Try
	End If

End Function

 

0 Likes