Either change a parameter or delete and add a parameter on .prt

Either change a parameter or delete and add a parameter on .prt

ladderman
Contributor Contributor
362 Views
3 Replies
Message 1 of 4

Either change a parameter or delete and add a parameter on .prt

ladderman
Contributor
Contributor

Good morning,

 

My goal:

1.  Have iLogic look for a parameter called "MATERIALQTY" and change it to "MaterialQty". 

2.  Or have iLogic find both versions of the two variations of "materialqty" and make the changes I need for part.

 

I am running my iLogic on specific parts, not from an asm.  If run just the following;

	OMATQTY = Parameter.Param("MaterialQty")
	OMATQTY.IsKey​ = False
	OMATQTY.ExposedAsProperty = False

The code will do exactly what I am looking for as long as "MaterialQty" exist on the part.  But if "MATERIALQTY" is there instead I receive an error.

 

I did find code that I thought would help;

    Dim oDoc As Document = ThisDoc.Document
    Dim oCD As ComponentDefinition = oDoc.ComponentDefinition
    'Dim oParam As Parameters = oCD.Parameters
    Dim oParam As UserParameters = oCD.Parameters.UserParameters
    
    For Each oPara As Parameter In oParam
        Dim oName As String = oPara.Name
		
	If UCase(Left(oName, 11)) = "MATERIALQTY" Then oPara.Delete

 

But I found that this code will delete both "MATERIALQTY" and "MaterialQty" parameters.

 

Is there a way to either find "MATERIALQTY" and change it to "MaterialQty" in the parameters so I can change .iskey and .exposedasproperty to false???  Or have iLogic look for both versions and then change either of them to iskey and .exposedasproperty to false???

 

 

 

 

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

dalton98
Collaborator
Collaborator
Accepted solution

Try this

Dim oDoc As PartDocument = ThisDoc.Document
Dim oCD As ComponentDefinition = oDoc.ComponentDefinition
Dim oParams As UserParameters = oCD.Parameters.UserParameters
Dim oParam As UserParameter

For Each oParam In oParams
	If oParam.Name.IndexOf("MATERIALQTY", 0, StringComparison.InvariantCultureIgnoreCase) > -1
		Try
			oParam.Name = "MaterialQTY"
			oParam.IsKey = False
			oParam.ExposedAsProperty = False
		Catch
			oParam.Delete
		End Try
	End If
Next

 

Message 3 of 4

ladderman
Contributor
Contributor

@dalton98 

 

Thank you very much for that!  I would have never figured this code out.

 

Can you breakdown how this line is working by chance..?

If oParam.Name.IndexOf("MATERIALQTY", 0, StringComparison.InvariantCultureIgnoreCase) > -1

What does the IndexOf and ",0," do exactly???

0 Likes
Message 4 of 4

dalton98
Collaborator
Collaborator

It was the first result on Stack Overflow

 

This is how I understand it...

.indexof("materialqty") returns a numerical value. It is useful for finding the n'th member of a string or a multi-value parameter in inventor.  And it returns -1 if it doesn't exist. ", 0," is either setting the start value or the number  of members in the string.

 

Looking back at the post you could also do a .contains() method to compare the string to a value., but this way  might have just been longer.