Import only missing parameters from xml? -topic transfer from Inventor Forum-

Import only missing parameters from xml? -topic transfer from Inventor Forum-

BStreeper
Participant Participant
726 Views
2 Replies
Message 1 of 3

Import only missing parameters from xml? -topic transfer from Inventor Forum-

BStreeper
Participant
Participant

I originally posted this question in the general Inventor forum, but what I'm trying to be able to do is use an exported Parameters XML file on our business server to serve as a company set of standard parameters. 

As we are new to Inventor, we are currently creating these, so the list of useful, and not useful custom parameters is changing as we all become more familiar with Inventors many uses.

This has resulted in several I-logic "external rules" (also kept on the server in one location, and currently evolving) to return errors in older files that do not have all the newer parameters. 

To combat this I have added a button to our "Standards" form (created again to ease the transitions to the new software) which runs the below simple script. 

 

oDoc = ThisDoc.Document
iLogicVb.Automation.ParametersXmlLoad(ThisDoc.Document, “file location\Standard-params.xml")

 

This scrip essentially just imports our xml standard parameters file in whatever its current state is, from the central server location.

 

ACTUAL QUESTION -I know there was quite a bit a pre-amble above-

 

Is there a way to use this XML file import function without resetting the values of any parameters that already existed in the older files?

  • Typically when i use this to update a file, it will reset all the "drop down/ multi value" parameters back to their "default" state.

 

 

first tab of Standards Form [for ref] :

STANDARD FORM-21-APR-2022.png

 

 

originally posted here:
https://forums.autodesk.com/t5/inventor-forum/import-only-missing-parameters-from-xml/m-p/11117747/h...

0 Likes
Accepted solutions (1)
727 Views
2 Replies
Replies (2)
Message 2 of 3

dalton98
Collaborator
Collaborator

It sounds like it would be easier to add the parameters with ilogic instead of importing a xml file. You could add all the new parameters to an excel sheet then use the GoExcel functions in ilogic. The code would look something like this:

Sub main
Dim oDoc As PartDocument
oDoc = ThisApplication.ActiveDocument

Dim oParams As Parameters
oParams = oDoc.ComponentDefinition.Parameters

GoExcel.Open("filename.xls", "Sheet1")
'number of excel rows
For i = 1 To 10
	newParam = GoExcel.CellValue("A" & i)
	Call params(oParams, newParam)
Next

GoExcel.Save
GoExcel.Close
End Sub

Sub params(oParams As Parameters, newParam As String)
paramexists = False
Dim oParam As Inventor.Parameter
For Each oParam In oParams
	If oParam.Name = newParam
		paramexists = True
	End If
Next
If paramexists = False
	oParams.UserParameters.AddByExpression(newParam, "value", UnitsTypeEnum.kTextUnits)
End If
End Sub
0 Likes
Message 3 of 3

Zach.Stauffer
Advocate
Advocate
Accepted solution

You can use the built-in XML library to handle the xml file. Here is some code that worked for a few different parameters I tried. I assumed you have an XML list where all of the "d0" type parameters have been removed. This is for an assembly, if you are doing this in a part you'll need to change it to be a PartDocument instead of an AssemblyDocument. Or have 2 rules, 1 for each.

 

Imports System.Xml
AddReference "System.Xml"

Dim assemblyDoc As AssemblyDocument = ThisDoc.Document
Dim userParams As UserParameters = assemblyDoc.ComponentDefinition.Parameters.UserParameters

Dim xmlDoc As XmlDocument = New XmlDocument
xmlDoc.Load("C:\workspace\params.xml")		'change filepath to where you have your xml file saved

Dim nodes As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("//ParamWithValue")			'this is the root level node name for Inventor Parameters

Logger.Debug("Nodes: " & nodes.Count)			'just a check to make sure the file is being read correctly

For Each node As XmlNode In nodes
	Dim name As String = node.SelectSingleNode("name").InnerText				
	Dim typeCode As String = node.SelectSingleNode("typeCode").InnerText
	Dim value = node.SelectSingleNode("value").InnerText
	
	Try
		userParams.Item(name).Value = userParams.Item(name).Value				'tries to set the parameter to itself. If this fails, means parameter doesn't exist and will be created. Setting it to itself keeps the value the same.
	Catch
		If typeCode = "String" Then			'this doesn't handle multi-value parameters, but could be expanded to do so
			userParams.AddByValue(name, value, kTextUnits)
		Else If typeCode = "Boolean" Then
			userParams.AddByValue(name, Convert.ToBoolean(value), kBooleanUnits)
		Else
			userParams.AddByExpression(name, value, typeCode)
		End If
	End Try	
Next