How can I find the name of a mate entity of a component in an assembly?
That is, if a component is used twice in an assembly and has two coincident mates using the same face (of said component) with another component, can I extract a name that is the same for both mates? e.g. if there are two cubes C1 and C2 and a side of each cube has a mate with the other. Can we get a name of the mate, like C1:Ida and C2:Idb which will identify the side of the cube?
There was a similar question given here :https://forum.solidworks.com/thread/59399 and the suggested solution provided was
MateEntity2::Reference->MateReference::Name
MateEntity2::Reference→MateReference::ReferenceEntity2→ModelDoc2::GetEntityName
I have been unable to find the correct sequence of calls to get this.
Also, using examples given in the Solidworks API, the example, https://help.solidworks.com/2021/English/api/sldworksapi/Get_Mate_Reference_Properties_Example_Csharp.htm, seems to show that we want a MateReference
but I have been unable to cast correctly from a MateEntity.Reference
to any usable type.
Some code below provides a reproducible example (in solidworks 21) with many failed attempts to cast from a MateEntity.Reference
to another type. Thanks for any pointers.
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Diagnostics;
namespace SWquestion
{
class Program
{
static void Main(string[] args)
{
// set up
ModelDoc2 swModel = default;
Mate2 swMate = default(Mate2);
Feature swFeat = default;
Feature swMateFeat = null;
Feature swSubFeat = default;
MateEntity2 swMateEnt = default;
string fileName = null;
int errors = 0;
int warnings = 0;
// Start SW
SldWorks.SldWorks swApp;
swApp = new SldWorks.SldWorks
{
Visible = false
};
//Open the assembly document :
fileName = @"C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2021\samples\tutorial\api\wrench.sldasm";
swModel = (ModelDoc2)swApp.OpenDoc6(fileName, (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings);
// Iterates through the FeatureManager design tree to find Mates
swFeat = (Feature)swModel.FirstFeature();
while ((swFeat != null))
{
if ("MateGroup" == swFeat.GetTypeName())
{
swMateFeat = (Feature)swFeat;
break;
}
swFeat = (Feature)swFeat.GetNextFeature();
}
// grab first mate entity : can get parameters etc
swSubFeat = (Feature)swMateFeat.GetFirstSubFeature();
swMate = (Mate2)swSubFeat.GetSpecificFeature2();
swMateEnt = swMate.MateEntity(0);
// Mate entity reference has property value 'Mate reference'
// But how is this object used?
// https://help.solidworks.com/2021/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IMateEntity2~Reference.html
object swRef = swMateEnt.Reference;
Debug.Print("swRef: " + swRef.GetType()); // com_object
// How to get the name??
// perhaps https://help.solidworks.com/2021/English/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IMateReference_members.html
// Tried various casts, all with error
IMateReference ms = (IMateReference)swMateEnt.Reference; // error: Unable to cast COM object of type 'System.__ComObject' to interface type 'SolidWorks.Interop.sldworks.IMateReference'.
// MateReference ms = (MateReference)swMateEnt.Reference; // com error on cast
// MateEntity2 ms = (MateEntity2)swMateEnt.Reference; // com error on cast
// ModelDoc2 ms = (ModelDoc2)swMateEnt.Reference; // com error on cast
// string[] ms = (string[])swMateEnt.Reference; // com error on cast from ComObject to string[]
// double[] ms = (double[]) swMateEnt.Reference; // com error on cast
// Try a different route from link below
// https://help.solidworks.com/2021/English/api/sldworksapi/Get_Mate_Reference_Properties_Example_CSharp.htm
//ModelDocExtension swModelDocExt = (ModelDocExtension)swModel.Extension;
//SelectionMgr swSelMgr = (SelectionMgr)swModel.SelectionManager;
//bool boolstatus = false;
//string mtName = swSubFeat.Name;
//boolstatus = swModelDocExt.SelectByID2(mtName, "MATE", 0, 0, 0, false, 0, null, 0);
//Feature swFeature = (Feature)swSelMgr.GetSelectedObject6(1, -1);
//MateReference swMateReference = (MateReference)swFeature.GetSpecificFeature2(); // error: 'Unable to cast COM object of type 'System.__ComObject' to interface type 'SolidWorks.Interop.sldworks.MateReference'.
}
}
}
(This is cross-posted here without response)
I am not an experienced user, so not sure if this helps. The API Help document (https://help.solidworks.com/2022/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IMateEntity2~Reference.html?verRedirect=1) wrongly states that (at list since version 2010) the return value of MateEntity2.Reference is of type MateReference. In fact it is of type Entity. There are a couple of example that use Entity = MateEntity2.Reference