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:
FindTouchingFacePairsFindTouchingEdgeFacePairsFindNonTouchingPairs
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:
- I can successfully get
CWStudy.ContactManager - I can successfully select valid targets in the assembly using
SelectByID2 - 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 = 0NEdgeFaces = 0NEnts = 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 + IBody2Component + IBody2Component + Component
All of them return 0.
2. Different array packaging
I tested:
Variant()arraysObject()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", ...)succeedsSelectionMgrconfirms 2 selected objects- object types look valid
FindTouchingFacePairs(...)returns0FindTouchingEdgeFacePairs(...)returns0FindNonTouchingPairs(...)returns0
No VBA runtime error occurs in these tests.
C# behavior
In C#, I can also obtain ContactManager, but when calling:
FindTouchingFacePairsFindNonTouchingPairs
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:
- Should
VarCompBodiescontain:IBody2objects?Component2objects?- a mixed array?
- only objects coming directly from the current selection set?
- Do these methods require some additional Simulation/native command context before they return valid results?
- Is there any difference between:
- bodies obtained from
SelectionMgr - bodies obtained from
Face2.GetBody() - bodies obtained from
Component2.GetBodies3()
- bodies obtained from
- 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:
FindTouchingFacePairsFindNonTouchingPairsFindTouchingEdgeFacePairs
especially in:
- VBA
- C#
- SolidWorks 2020 / Simulation
I would really appreciate it.
Thanks.
