Listing the contents of an ObjectCollection

Listing the contents of an ObjectCollection

oransen
Collaborator Collaborator
592 Views
5 Replies
Message 1 of 6

Listing the contents of an ObjectCollection

oransen
Collaborator
Collaborator

As far as I can tell, once I have a RectangularOccurrencePattern I can get its inpiut components using ParentComponents. In C# (must be similar in VBA)

 

RectangularOccurrencePattern RecOccPat = (RectangularOccurrencePattern)OccPattern;
ObjectCollection Objs = RecOccPat.ParentComponents;

 But I can't find any help in how to loop over the ObjectCollection and listing the Parts or Assemblies it contains.

A longer example:

 

                if (OccPattern.Type == ObjectTypeEnum.kRectangularOccurrencePatternObject)
                {
                    RectangularOccurrencePattern RecOccPat = (RectangularOccurrencePattern)OccPattern;
                    ObjectCollection Objs = RecOccPat.ParentComponents;

                    int iObjCount = 0 ; 
                    foreach (Object O in Objs)
                    {
                        ABSup.Log.WriteLn(CLog.Type_e.ekLog, "   ",iObjCount.ToString(),"  ",O.GetType().ToString()," ",O.ToString());
                        // Crashes here, the cast is not valid  
                        ComponentOccurrence CompOcc = (ComponentOccurrence) O;
                        if (CompOcc != null)
                        {
                            ABSup.Log.WriteLn(CLog.Type_e.ekLog, "     Component occurrence");
                        }
                        iObjCount++;
                    }

 

Does anyone know how to interrogate object collections?

 

Is my logic or reasoning incorrect?

0 Likes
Accepted solutions (1)
593 Views
5 Replies
Replies (5)
Message 2 of 6

jjstr8
Collaborator
Collaborator
Accepted solution

I don't see anything wrong.  I can't duplicate your issue.  I'm not sure anything but ComponentOccurrence can be in the ParentCollection.  What's in the pattern?  If you want it to be bulletproof, use one of the following:

// Using a simple check
if (O is ComponentOccurrence)
{
     ABSup.Log.WriteLn(CLog.Type_e.ekLog, "     Component occurrence");
}

// Using pattern matching
if (O is ComponentOccurrence CompOcc)
{
     // ... do something with CompOcc
     ABSup.Log.WriteLn(CLog.Type_e.ekLog, "     Component occurrence");
}

// Using the 'as' operator
ComponentOccurrence CompOcc = O as ComponentOccurrence
if (CompOcc != null)
{
     // ... do something with CompOcc
     ABSup.Log.WriteLn(CLog.Type_e.ekLog, "     Component occurrence");
}

 

Message 3 of 6

oransen
Collaborator
Collaborator
What an idiot I was...thanks!
0 Likes
Message 4 of 6

jjstr8
Collaborator
Collaborator
What was the issue?
0 Likes
Message 5 of 6

oransen
Collaborator
Collaborator

As you said, I was not looking at or checking the type of the object, and in my IAM I has some patterns within patterns, which obviously could not be converted to an occurrence of a part...

0 Likes
Message 6 of 6

jjstr8
Collaborator
Collaborator

Aha!  I hadn't thought of patterns in patterns.  If that's the case, you may have to traverse it recursively.

0 Likes