Replacing multiple Model References in .idw by referencing a spreadsheet.

Replacing multiple Model References in .idw by referencing a spreadsheet.

s.pollard
Participant Participant
123 Views
1 Reply
Message 1 of 2

Replacing multiple Model References in .idw by referencing a spreadsheet.

s.pollard
Participant
Participant

Hi,

I have been using the code below to replace the model reference for a part file on a single sheet drawing then saving the drawing as that number.

This code works well but, what I would like to be able to do is modify the code to replace multiple references on several sheets, where each sheet contains a different model which could be an assembly or a part file.

Each sheet would only contain a single model reference and the save as would be the first model reference from Sheet 1, although this is the least important part of the process as can be manually saved once references have been replaced.

 

Dim oFileDlg As Inventor.FileDialog
ThisApplication.CreateFileDialog(oFileDlg)
oFileDlg.Filter = "Microsoft Excel Files (*.xlsx; *.xls)|*.xlsx;*.xls|"
 
Dim openDoc As Document
openDoc = ThisDoc.Document
Dim oFD As FileDescriptor
oFD = openDoc.ReferencedFileDescriptors(1).DocumentDescriptor.ReferencedFileDescriptor
 
 
'define the XLS to look at
myXLS = "C:\Local  Folder\my_Spreadsheet.xlsx"
''uncomment the line below to open the spreadsheet but would need to change the name
''GoExcel.Open(myXLS, "")
 
'define the correct columns, rows and cells
myOriginalNumberColumn = "UK_Part_Number"
myNewNumberColumn = "A"
 
'Define the Original Part Number
myNumber = iProperties.Value(openDoc,"Project", "Part Number")
 
'find the row containing the original part number
myRow = GoExcel.FindRow(myXLS, "Sheet1", myOriginalNumberColumn, "=", myNumber)
 
'find the corresponding New Number cell
myCell = GoExcel.CellValue(myXLS, "Sheet1", myNewNumberColumn & myRow)
 
Dim myCellString As String = myCell
 
'replace model reference with new version  
                 oFD.ReplaceReference("C:\Local  Folder\" & myCell & ".ipt")
openDoc.Update()
 
'save off a copy
ThisDoc.Document.SaveAs(ThisDoc.Path & "\" & myCell & ".idw", True)
 
Has anybody attempted similar who could offer any assistance please?
 

 

0 Likes
124 Views
1 Reply
Reply (1)
Message 2 of 2

C_Haines_ENG
Collaborator
Collaborator

See if this works. There is very little error checking in this code, and its assuming you have at least one view / sheet and your excel doc contains the replacement.

 

 

Sub Main

	Dim oDoc As DrawingDocument = ThisDoc.Document
	Dim myXLS As String = "C:\Local  Folder\my_Spreadsheet.xlsx"
	
	'LOOP THROUGH ALL SHEETS IN THE DRAWING
	For Each oSheet As Sheet In oDoc.Sheets
		
		'GET THE FIRST VIEW ON THAT SHEET
		Dim oView As DrawingView = oSheet.DrawingViews(1)
	
		'GETS THE REFERENCED DOCUMENT OF THAT VIEW, GETS PART NUMBER
		Dim oRefDoc As Document = oView.ReferencedDocumentDescriptor.ReferencedDocument
		Dim PartNum As String = oRefDoc.PropertySets("Design Tracking Properties")("Part Number").Value
		
		'SEARCHES EXCEL FOR THE NEW PART NUMBER
		Dim oRow As Integer = GoExcel.FindRow(myXLS, "Sheet1", "UK_Part_Number", "=", PartNum)
		Dim NewFile As String = GoExcel.CellValue(myXLS, "Sheet1", "A" & oRow)
		
		If NewFile IsNot Nothing
			
			'REPLACES FILE
			Dim oFileDesc As FileDescriptor = oView.ReferencedDocumentDescriptor.ReferencedFileDescriptor
			oFileDesc.ReplaceReference("C:\Local  Folder\" & NewFile & ".ipt")

		End If

	Next
	
	oDoc.Update

End Sub

 

 

 

0 Likes