Find InsertConstraint objects between two ComponentOccurrence objects

Find InsertConstraint objects between two ComponentOccurrence objects

stewart_sabadell
Participant Participant
361 Views
7 Replies
Message 1 of 8

Find InsertConstraint objects between two ComponentOccurrence objects

stewart_sabadell
Participant
Participant

Hi, looking for help with navigating object constraints in an Inventor assembly via C#.

 

We have two parts with 4 holes each in them, that are "connected" with two InsertConstraint objects. Using C# with the Inventor API, we need to be able to find exactly these 2 constraints.

 

Assume that we prompt the user to select, individually, the two parts, giving us two ComponentOccurrence objects. Let's call them fittingOcc and baseOcc. We can iterate over fittingOcc.Constraints, and identify any and all InsertConstraints in that set. We can do the same for baseOcc.

 

But it isn't this straightforward. Assume there may be more than 2 InsertConstraint objects in our sets.

 

Can we rely on using constraint1.Equals(constraint2)? Is this a true object equality, or does it only compare the Name properties? Occurrence names are not guaranteed to be unique within our entire assembly.

 

The other problem is that the constraints we seek may not "live" in the Constraints collection on the ComponentOccurrence objects we selected - they may "live" in *.ParentOccurrence.Constraints, or *.Parent.Constraints - or possibly at another level, if the parts are in separate deeply nested subassemblies.

 

We've tried getting the EdgeProxy from constraint.EntityOne and constraint.EntityTwo, then looked at its ContainingOccurrence property to try to determine if this is the constraint we are looking for. In theory we could compare edgeProxy.ContainingOccurrence.OccurrencePath with fittingOcc.OccurrencePath, but in nested sub-assemblies, we get a partial occurrence path from the edge proxy.

 

Could anyone provide guidance on the best way to accomplish this?

 

Stew Sabadell

0 Likes
Accepted solutions (1)
362 Views
7 Replies
Replies (7)
Message 2 of 8

A.Acheson
Mentor
Mentor

Hi @stewart_sabadell 

Maybe something like this will get you started for this. Working ok for top level constraints. 

  Dim occ1 As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Pick occurrence 1")
  Dim occ2 As ComponentOccurrence = ThisApplication.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "Pick occurrence 2")

    MessageBox.Show("Occ1: " & occ1.Name & vbLf &vbLf &"Occ2: " & occ2.Name,"Occurrences Selected" )
	
	For Each cons As AssemblyConstraint In occ1.Constraints
		If cons.AffectedOccurrenceOne Is occ1 AndAlso cons.AffectedOccurrenceTwo Is occ2 Then 
			MessageBox.Show(cons.Name, "Constraint Common between two occurrences")
		End If
	Next
If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 3 of 8

stewart_sabadell
Participant
Participant

Hi Alan, 

 

Thanks for the quick reply. I am working in C#. Do you know what the equivalent would be to the object equality tests that you have on this line?

If cons.AffectedOccurrenceOne Is occ1 AndAlso cons.AffectedOccurrenceTwo Is occ2 Then

I was under the impression that direct comparison of two ComponentOccurrence objects, in C# as per below, was not supported.

if (cons.AffectedOccurrenceOne == occ1 && cons.AffectedOccurrenceTwo == occ2)

Nevertheless I will try out your logic and see how it works!

 

Thanks.

 

Stew Sabadell

0 Likes
Message 4 of 8

A.Acheson
Mentor
Mentor

Hi @stewart_sabadell 

Unfortuantely I don't know what the comparison code is. Could you compare the occ name, document name, part number  or any other commonality between them?

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 5 of 8

stewart_sabadell
Participant
Participant

Hi Alan,

 

I tried out your original solution. The API does seem to support occ1 == occ2 syntax. But as you say, we don't really know what the equality logic is doing.

 

I thought I would provide more context on the issue. Here is an example assembly tree that demonstrates the complexity of identifying the two exact InsertConstraint objects I need for our edit operation.

 

Main Assembly

  • Model States
  • Relationships
  • Representations
  • Origin
  • Sub-assembly A:1
    • Model States, etc.
    • Part1:1
    • Component Pattern 1:1
      • Element:1
      • Element:2
      • Element:3
        • Part To Constrain To:3
    • Flush:161, etc.
    • Mate:39
    • Insert:25
    • Insert:26, and more
    • Insert:43
    • Insert:44
    • Insert:91, and more
  • PartB:1
  • PartB:2
  • PartB:3
  • Sub-Assembly B:1
    • Model States, etc.
    • Attached Part:1
      • Flush:1, etc.
      • Insert:3
      • Insert:4
      • Insert:9
      • Insert:10
    • Pre-attached Part:1
    • Pre-attached Part:2
    • Insert:43
    • Insert:44
    • Insert:65
    • Insert:66
  • More sub-assemblies, etc.

 

We select the two ComponentOccurrence object with:

Inventor.Application.CommandManager.Pick(SelectionFilterEnum.kAssemblyLeafOccurrenceFilter, "message");

 

The parts that get selected are Attached Part:1 and Part To Constrain To:3
The insert constraints we need, to support our edit operation, are Insert:43 and Insert:44. They are not in either Constraints collection on the selected objects.

And of course this is just one example, the relative structure of sub-assemblies and parts is unique in each case.

When I tried your proposal with this example, AffectedOccurrenceTwo.Name was not Part To Constrain To, but was instead Sub-assembly A.

 

Suggestions?

 

Stew Sabadell

0 Likes
Message 6 of 8

A.Acheson
Mentor
Mentor
Accepted solution

So it looks like the assembly constraints are on the sub assembly level which would be proxies. So you now need to compare the proxy entities and try and get back to the occurrence that owns them to match the occurrence selected. There might be many route to do this but this is just one I am thinking could work. 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 7 of 8

stewart_sabadell
Participant
Participant

Thanks Alan. One of the approaches we did try did something similar to what you describe: We obtained EntityOne and EntityTwo from each InsertConstraint, cast them to type EdgeProxy, then looked for equality using the edge proxy's ContainingOccurrence. 

 

As part of this equality test, we tried to compare the OccurrencePath of our original object with that of the ContainingOccurrence; unfortunately, in the case of the latter, it was truncated to the owning assembly, and was not a full path in the context of the assembly open in the editor. We have not yet found a way to somehow "promote" this occurrence to the full assembly context, in order to compare the occurrence paths. A comparison of the Name properties is not sufficient, as a sub-assembly may be instanced, and so the names duplicated in the context of the full assembly. 

 

What I have not yet tried is using a simple == test between the original ComponentOccurrence (the selected part) and the one coming from the proxy. 

 

However, I did expand on your logic, and early testing shows promise, with this logic:

 

  select the two parts, get occ1 and occ2

  foreach constraint in occ1.Constraints

      using ==, compare AffectedOccurrenceOne with occ1, and AffectedOccurrenceTwo with occ2 OR with occ2.ParentOccurrence

          if test passes, hang on to this constraint, until we find our 2 constraint objects

  if we found our constraints, return

  foreach constraint in occ1.ParentOccurrence.Constraints

      using ==, compare AffectedOccurrenceOne with occ1.ParentOccurrence, and AffectedOccurrenceTwo with occ2 OR occ2.ParentOccurrence

          if test passes, hang on to this constraint, until we find our 2 constraint objects

 

I need to do more testing before I can confirm this solves our problem. It does rely quite heavily on the accuracy of the == operator; it would be nice to know if this is reliable.

 

I will not be getting back to this until the new year. Thank you very much for your guidance in 2024, and have a great holiday!

 

Stew Sabadell

Message 8 of 8

stewart_sabadell
Participant
Participant

Just to wrap up this topic I was able to craft a solution that is working. I use a version of the logic above, combined with a test that compares occ1.Name and occ2.Name with constraint.EntityOne.ContainingOccurrence.Name and constraint.EntityTwo.ContainingOccurrence.Name. This additional check was needed to rule out false positives I was getting in some situations. 

 

So thank you Alan for putting me on the right track, leveraging the == operator with ComponentOccurrence objects, and using AffectedOccurrenceOne/Two on the constraints.

 

Stew Sabadell