Suppressing a part occurrence based on an iProperty.

Suppressing a part occurrence based on an iProperty.

Anonymous
Not applicable
787 Views
4 Replies
Message 1 of 5

Suppressing a part occurrence based on an iProperty.

Anonymous
Not applicable

Hello Everyone,

 

So I am trying to build a custom 3rd party application for Inventor using the API. I have a utility class that does a lot of heavy lifting for me. I am trying to create a method that suppresses all other parts except the part with a specified iProperty value in the description.

 

In my code, I have been able to find the part with the particular iProperty value, but when I check the occurrences of that part in the assembly, the count is zero. Here is a snippet of the code I am using, is there anything I am doing wrong? 

 

 

 public static void SuppressAllOccurancesExecpted(String iPropertyId, DocumentsEnumerator documents)
        {
            foreach (Document document in documents)
            {
                PropertySet propertySet = document.PropertySets["Design Tracking Properties"];
                Property description = null;
                PartDocument part = null;
                PartComponentDefinition partComp = null;
                ComponentOccurrences occurances = null;

                foreach (Property property in propertySet)
                {
                    if(property.Name == "Description")
                    {
                        description = property;
                        if(description.Value == iPropertyId)
                        {
                            part = (PartDocument)document;
                            partComp = part.ComponentDefinition;

                            MessageBox.Show(part.DisplayName);

                            occurances = partComp.Occurrences;

                            MessageBox.Show(occurances.Count.ToString());
                        }
                        break;
                    }
                }
            }
        }

 

 

Thank you very much for you assistance! 

0 Likes
Accepted solutions (2)
788 Views
4 Replies
Replies (4)
Message 2 of 5

Michael.Navara
Advisor
Advisor
Accepted solution

I don't know, what does it mean Occurrences in PartComponentDefinition, but what you are looking for is Occurrences in assembly context. This can't be obtained from part.

Dim asmDoc As AssemblyDocument
Dim asmDef = asmDoc.ComponentDefinition
For Each occ As ComponentOccurrence In asmDef.Occurrences
	Dim occDoc As Document = occ.Definition.Document
	If occDoc.PropertySets("Design Tracking Properties")("Description").Value = iPropertyId Then
		'Do something
	Else
		'Do something else
	End If
Next

 

Message 3 of 5

shiftctrl.io
Enthusiast
Enthusiast
Accepted solution

As mentioned, a Part document doesn't have occurrences, only Assembly documents do.  I've cleaned up your code a little by limited the scope of the variables, added a check to process only Assembly documents and switched the document itself from Part to Assembly. 

 

Haven't tested it, but I think it will work. 

 

 

public static void SuppressAllOccurancesExecpted(String iPropertyId, DocumentsEnumerator documents)
{
	foreach (Document document in documents.Cast<Document>().Where(x => x.DocumentType == DocumentTypeEnum.kAssemblyDocumentObject))
	{
		PropertySet propertySet = document.PropertySets["Design Tracking Properties"];

		foreach (Property property in propertySet)
		{
			if (property.Name == "Description")
			{
				var description = property;
				if (description.Value == iPropertyId)
				{
					var assyDoc = (AssemblyDocument)document;
					var assyComp = assyDoc.ComponentDefinition;

					MessageBox.Show(assyDoc.DisplayName);

					var occurances = assyComp.Occurrences;

					MessageBox.Show(occurances.Count.ToString());
				}
				break;
			}
		}
	}
}

 

 

0 Likes
Message 4 of 5

Anonymous
Not applicable

Thank you @Michael.Navara I was able to make the changes I want based on your code. I totally forgot I was suppose to use occurrences in an assembly 

0 Likes
Message 5 of 5

Anonymous
Not applicable

Thank you @shiftctrl.io your solution is more elegant

0 Likes