CWContactManager.FindTouchingFacePairs / FindNonTouchingPairs always return 0, while C# interop may throw SEHException

Hi everyone,

I am trying to automate the detection of touching faces / contact sets in SolidWorks Simulation, and I am running into a problem with the CWContactManager API.

Environment

  • SolidWorks 2020
  • SolidWorks Simulation / CosmosWorks
  • Main implementation in C#
  • Additional verification in VBA macro

What I am trying to do

I want to reproduce the native Simulation behavior of:

  • automatically finding touching faces
  • automatically finding touching face groups / contact sets

The relevant API methods I am testing are:

  • FindTouchingFacePairs
  • FindTouchingEdgeFacePairs
  • FindNonTouchingPairs

From the object browser, the signatures appear as:

  • FindTouchingFacePairs(VarCompBodies, NFaces As Long)
  • FindTouchingEdgeFacePairs(VarCompBodies, NEdgeFaces As Long)
  • FindNonTouchingPairs(VarCompBodies, DMin As Double, DMax As Double, NEnts As Long)

Problem summary

I have confirmed that:

  1. I can successfully get CWStudy.ContactManager
  2. I can successfully select valid targets in the assembly using SelectByID2
  3. I can successfully pass selected objects into the API in VBA without runtime exceptions

However:

  • in C#, calling these methods often throws SEHException / “external component has thrown an exception”
  • in VBA, the calls succeed, but they always return:
    • NFaces = 0
    • NEdgeFaces = 0
    • NEnts = 0

So at the moment I cannot get any valid face pairs from the API.

What I already tested

1. Different target object types

I tested all of the following as VarCompBodies:

  • IBody2 + IBody2
  • Component + IBody2
  • Component + Component

All of them return 0.

2. Different array packaging

I tested:

  • Variant() arrays
  • Object() arrays

Same result: no pairs found.

3. Selection-based objects instead of manually created objects

I rebuilt selection in the assembly using SelectByID2, then read objects back from SelectionMgr.

For SOLIDBODY, this works correctly.

4. SelectionMgr inspection

When selecting SOLIDBODY, SelectionMgr.GetSelectedObject6 returns IBody2.

When selecting COMPONENT, GetSelectedObject6 seems to return a generic object, while GetSelectedObjectsComponent3 gives the expected component.

VBA test result

Here is a simplified description of the VBA results:

  • SelectByID2(..., "SOLIDBODY", ...) succeeds
  • SelectionMgr confirms 2 selected objects
  • object types look valid
  • FindTouchingFacePairs(...) returns 0
  • FindTouchingEdgeFacePairs(...) returns 0
  • FindNonTouchingPairs(...) returns 0

No VBA runtime error occurs in these tests.

C# behavior

In C#, I can also obtain ContactManager, but when calling:

  • FindTouchingFacePairs
  • FindNonTouchingPairs

I often get SEHException at the COM boundary.

That makes me suspect there may be some .NET interop marshaling issue, but even in VBA I still get zero results, so I think there may be another requirement I am missing.

Main question

Does anyone know what exact object/context these methods expect in practice?

Specifically:

  1. Should VarCompBodies contain:
    • IBody2 objects?
    • Component2 objects?
    • a mixed array?
    • only objects coming directly from the current selection set?
  2. Do these methods require some additional Simulation/native command context before they return valid results?
  3. Is there any difference between:
    • bodies obtained from SelectionMgr
    • bodies obtained from Face2.GetBody()
    • bodies obtained from Component2.GetBodies3()
  4. Are these methods expected to work directly in automation, or are they only reliable when used inside the native “Find Contact Sets / Local Interactions” workflow?

One more observation

When I use the native Simulation UI and click faces to add recognition targets, the UI highlights the entire solid body, not just the face, and the highlight style is different from normal component selection.
This makes me wonder whether the native command is using a more specific internal selection/context than what I am reproducing in code.

Any advice would be appreciated

If anyone has a working example of:

  • FindTouchingFacePairs
  • FindNonTouchingPairs
  • FindTouchingEdgeFacePairs

especially in:

  • VBA
  • C#
  • SolidWorks 2020 / Simulation

I would really appreciate it.

Thanks.