Accessing the data of the elements in the iam file with Inventor Api c#

Accessing the data of the elements in the iam file with Inventor Api c#

hayattangercekler2
Advocate Advocate
371 Views
3 Replies
Message 1 of 4

Accessing the data of the elements in the iam file with Inventor Api c#

hayattangercekler2
Advocate
Advocate

With Inventor Api c#, is it possible to access parameter data in ipts in an assembly iam file?

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

Zach.Stauffer
Advocate
Advocate

Yes, this is fairly simple. First you have to find the correct part. You can do this with a User Selection or by searching for a string or many other ways. Once you have the ComponentOccurrence of the part you want, you can access the PartComponentDefinition and that has the user parameters in it. I have written a couple of extensions to retrieve user parameters from a PartComponentDefinition, you can use these or adapt them to not be extensions.

 

 

public static T RetrieveParameter<T>(this PartComponentDefinition definition, string name)
		{
			var parameters = definition.Parameters.UserParameters;

			try
			{
				var value = parameters[name].Value;
				return (T)Convert.ChangeType(value, typeof(T));
			}
			catch (Exception e)
			{
				return default(T);
			}
		}

		public static void SetParameter(this PartComponentDefinition definition, string parameter, dynamic value)
		{
			var parameters = definition.Parameters.UserParameters;

			try
			{
				parameters[parameter].Value = value;
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
				throw;
			}
		}

 

0 Likes
Message 3 of 4

hayattangercekler2
Advocate
Advocate
Thanks for your answer but I don't quite understand what data to give for functions.
0 Likes
Message 4 of 4

Zach.Stauffer
Advocate
Advocate

It's an extension method, so you use it as if it was an in-built method for that object. Since these are extensions for PartComponentDefinition objects, you'll use it like this:

 

 

 

PartComponentDefinition partDef = selectedComponentOccurrence.Definition //or something to this effect, depending on how you are getting the correct occurrence from the assembly

string parameter = partDef.RetrieveParameter<string>("myParameter");     //inside of the <> you put the type you want.
partDef.SetParameter("myParameter", value);

 

 

You can find more information here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-meth...

 

0 Likes