Changing the parameters of assembly parts based on an Excel file

Changing the parameters of assembly parts based on an Excel file

kurs-autodesk
Participant Participant
480 Views
3 Replies
Message 1 of 4

Changing the parameters of assembly parts based on an Excel file

kurs-autodesk
Participant
Participant

Greetings, colleagues!

I ask for help in solving the problem. The conditions are as follows:

1. I have an assembly in which 100 rectangular plates are inserted.

2. Each plate is a unique file with a unique name.

3. The size of each plate is determined using two numeric user parameters "A" and " B " and initially these parameters are the same for all plates.

4. I also have an Excel file in which there is a column with the names of the plates and two columns with the numeric values of the parameters "A" and "B".

Task: from the assembly file containing my 100 plates, using the ilogic rule, run a loop that will iterate through all the parts from the assembly, match each part name with the name in the Excel column and change the parameters for this part based on data from neighboring excel columns.

I would be grateful for any help

0 Likes
481 Views
3 Replies
Replies (3)
Message 2 of 4

A.Acheson
Mentor
Mentor

Do you have these plates positioned in the assembly or are using the assembly more so to work with all the plates at once? It will likely be easier to create one file and do a save as and change the parameters based on the excel file.


Looping through the assembly can add complexity if it is not really needed it would be easier to work on the part level.

Are all 100 plates going to be unique in the end or will they be copies of each other? 

Do you have an image or files you can attach to show your design intent? Also are you using sheetmetal part for the plate file?

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 4

kurs-autodesk
Participant
Participant

Good afternoon and thanks for the feedback.
You are right, the assembly was needed to process all the parts from the assembly space. In fact, there is an original part, in which 2 parameters and the name are changed, if you use "save as". This is a sheet metal part. All details will differ from each other precisely in two parameters of length and width.

Excel file has three columns, first is name, second and third are length and width parameters.
Example file in attachment

0 Likes
Message 4 of 4

WCrihfield
Mentor
Mentor

Hi @kurs-autodesk.  The sample file you attached confused me, because it seems to be a DXF instead of an Inventor document.  Anyways, I think I may have an iLogic rule you could use on in that assembly, which will loop through each part type component within, get the part's name (from file name), attempts to retrieve the two values from the Excel file (correct file name needs to be specified), using that name to find the right row.  Then it tries to set those two values as the values of the two parameters within the part.  Then updates and saves each part after parameter values are changed.  Then when done, updates the main assembly.

It's a somewhat complex task, so the code is a bit complex looking too.  You will need to change the name of the Excel file and/or sheet it is looking for within the code.

 

Sub Main
	If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
		MsgBox("An Assembly Document must be active for this rule to work. Exiting.",vbCritical, "WRONG DOCUMENT TYPE")
		Exit Sub
	End If
	Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
	Dim oADef As AssemblyComponentDefinition = oADoc.ComponentDefinition
	For Each oOcc As ComponentOccurrence In oADef.Occurrences
		Dim oA, oB As Double 
		GetParamValsFromExcel(oOcc, oA, oB)
		SetOccParamVal(oOcc, "A", oA, "B", oB)
	Next
	oADoc.Update2(True)
End Sub

'oA and oB are passed in, given values, then passed back out with those values in place
Sub GetParamValsFromExcel(oComp As ComponentOccurrence, ByRef oA As Double, ByRef oB As Double)
	If Not TypeOf oComp.Definition Is PartComponentDefinition Then Exit Sub
	Dim oPDoc As PartDocument = oComp.Definition.Document
	Dim oName As String = System.IO.Path.GetFileNameWithoutExtension(oPDoc.FullFileName)
	Dim oExcelFile As String = "C:\Temp\MyExcelFile.xlsx" '<<< CHANGE THIS >>>
	Dim oSheetName As String = "Sheet1"
	GoExcel.Open(oExcelFile, oSheetName)
	GoExcel.TitleRow = 1
	GoExcel.FindRowStart = 2
	Dim oRow As Integer = GoExcel.FindRow(oExcelFile, oSheetName, "Name", "=", oName)
	oA = GoExcel.CellValue("B" & oRow)
	oB = GoExcel.CellValue("C" & oRow)
End Sub

Sub SetOccParamVal(oComp As ComponentOccurrence, oP1Name As String, oP1Val As Double, oP2Name As String, oP2Val As Double)
	If Not TypeOf oComp.Definition Is PartComponentDefinition Then Exit Sub
	Dim oPDef As PartComponentDefinition = oComp.Definition
	For Each oParam As Inventor.Parameter In oPDef.Parameters
		If oParam.Name = oP1Name Then
			oParam.Value = oP1Val
		ElseIf oParam.Name = oP2Name Then
			oParam.Value = oP2Val
		End If
	Next
	Dim oPDoc As PartDocument = oPDef.Document
	oPDoc.Update2(True)
	oPDoc.Save
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) 👍.

If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes