Retrieving Material ID for Applied Materials in Assemblies (.iam) or Parts (.ipt)

Retrieving Material ID for Applied Materials in Assemblies (.iam) or Parts (.ipt)

pts_user
Contributor Contributor
878 Views
8 Replies
Message 1 of 9

Retrieving Material ID for Applied Materials in Assemblies (.iam) or Parts (.ipt)

pts_user
Contributor
Contributor

I am currently attempting to retrieve the material ID for materials applied to assemblies (.iam) or parts (.ipt). While I have successfully obtained the material name and other details, I am encountering difficulty in obtaining the material ID.

If anyone has insights or suggestions on how to retrieve the material ID for the applied material, I would greatly appreciate your input.

For your reference, please find the code snippet below.

  • For assemblies:

 

Inventor.Document doc = m_InventorApplication.ActiveDocument;
AssemblyDocument asmdoc = doc as AssemblyDocument;
AssemblyComponentDefinition asmDef = (doc as AssemblyDocument).ComponentDefinition;
string matName = asmDef.DefaultMaterial.Name;

 

  • For parts:

 

PartDocument prtdoc = Componentocc.Definition.Document as PartDocument;
PartComponentDefinition partDef = (prtdoc as PartDocument).ComponentDefinition;
string matName = partDef.Material.Name;

 

 

0 Likes
879 Views
8 Replies
Replies (8)
Message 2 of 9

WCrihfield
Mentor
Mentor

Hi @pts_user.  What do you mean by Material ID?  Do you have an example of where to find that information manually?  The 'Material' data is stored as a MaterialAsset object.  Appearances and physical properties are stored as regular Asset objects.  Each Asset type object has a bunch of AssetValue objects associated with them.  AssetValue is the base Class for other more specific types of AssetValues that are for much more specific uses/data.  You may want to iterate though each AssetValue in a MaterialAsset to see if it contains the data you are looking for.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 9

pts_user
Contributor
Contributor

Hi @WCrihfield ,

 

We can't get this information manually but let me give you some more details.

Each material is assigned a unique internal ID (UID)in Fusion360. In Fusion360, if I need that unique internal ID (UID) of a material, I can retrieve it using material()->id(). For reference, please consult the Fusion API documentation at the following link: https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-db98ea61-96d7-40e0-9bc4-f7a376c7fa6c.

Like wise the material in Inventor also might have an unique internal ID (UID) associated with it and I want that.

 

0 Likes
Message 4 of 9

WCrihfield
Mentor
Mentor

Hi @pts_user.  The MaterialAsset (and the regular Asset) have both a DisplayName property, and a Name property.  The DisplayName is what you normally see in the list, the regular Name is stable for all languages.  If neither of those returns the information you are looking for, then maybe there is something in one of the AssetValues of the Asset that will provide the information you are looking for.  I am not familiar with the Fusion360 API, or its Material object, but there is no longer a regular Material object in the Inventor API since around 2013.  You can read more about this at the following online help page.

Consistent Materials (Materials and Appearances) 

I have also attached a text file containing an iLogic rule you can try out.  It can be ran from either a part or assembly.  If ran from a part, it will get its active material, and provide a full report about its details.  If ran from an assembly, it will get the 'DefaultVirtualComponentMaterial' and report about that.  And if the assembly is a weldment, it will get its WeldBeadMaterial and report about that as well.  However, an assembly does not have its own active material, like a part does, because everything that exists within an assembly is from the components that you put into the assembly, with the exception of the weld beads in a weldment.  It currently outputs its report into the iLogic Log window, but that could be changed to write it out to a text file, or something like that.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 5 of 9

d_eckel
Contributor
Contributor

Hi @WCrihfield . thank you for sharing the code.

Compared to you, I'm probably just getting in the the iLogics and try to learn a bit from day to day 😁

 

I have a question / not to your code. In line 11 (elsif) you check wether it is a Part Document or an Assembly Document. Nevertheless you check both times for "oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject"

So it probably needs to ba a "kAssemblyDocumentObject" in line 11?

0 Likes
Message 6 of 9

WCrihfield
Mentor
Mentor

Hi @d_eckel.  You are correct.  That appears to be a mistake.  The following line of code in Line 10:

ElseIf oDoc.DocumentType = DocumentTypeEnum.kPartDocumentObject Then

...should have been changed to be like this:

ElseIf oDoc.DocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then

My original code was just for use in parts, because they are the only place where materials matters to me.  I added the other stuff for assemblies to the code yesterday, just before posting it on the forum.  I tested the final code on a couple parts at that time, but not on any assembly yet before posting it.

 

I actually had multiple different similar rules locally that I had used for similar purposes before, each doing things a bit differently, and generating different feedback, in different formats.  I sort of gathered together some of the best stuff from all of them into one rule, to provide the most information.  After I corrected that one line of code that you mentioned, I also changed a couple other minor things that helped it work better.  Just inside the custom Sub routine named "CollectMaterialAssetData", I commented out that first line of code that adds "Material Report:", because in this example, we are already adding a header line within the 'Sub Main' area.  Then down within the custom Sub routine named "CollectAssetValueData", where it is dealing with a 'kAssetValueTypeChoice', I added " = {}" after those two lines of code declaring Array type variables, as a way to assign them an initial value.  That fixed the error it was handling (and logging) at that point.

 

After making these changes, I tried the corrected code on a regular assembly, and it only returned information about the default virtual component material, then I tried it on a weldment type assembly, and it returned data about both the virtual component material, and the weld bead material, as expected, without any errors.

I attached the corrected and updated code as a text file for you to review again.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 7 of 9

pts.user2
Contributor
Contributor

Hi @WCrihfield ,

 

Thanks for providing the text file for reference.

I've extracted the UID from MaterialAsset -> PhysicalPropertiesAsset -> Name.

The obtained name is "MaterialInv_072_physmat_aspects_1:PhysicalGen_103" but I'm interested in getting only "MaterialInv_072" The current name is concatenated with material aspect details.

Just wanted to check if there's a method to obtain only the desired name i.e. "MaterialInv_072".

0 Likes
Message 8 of 9

WCrihfield
Mentor
Mentor

Not that I know of.  I am not even sure where that specific information would be stored.  The material is just known by its regular Name or DisplayName property values.  I have never needed any other additional 'identity' of a material, and have not seen anyone else need any additional identity for one.  I have no idea how those AssetValue objects get their names/values, other than the stuff we enter into the 'Material Editor' dialog.  Over the years, several people have attempted to find specific pieces of data shown in the material editor dialog by code, and have run into trouble at times with some of it, but I do not recall anything specific about any additional internal ID for a material.

Anyways, I hope you are able to find what you are looking for.  If you do, let us know.  It may just take more research/exploration than others have needed to do in the past.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 9 of 9

pts.user2
Contributor
Contributor

Hi @WCrihfield,

 

Thanks for the overall suggestions and I will try to figure out a way or else will fetch material UID from the  MaterialAsset -> PhysicalPropertiesAsset -> Name as I discussed earlier.

0 Likes