如何在装配体中找到零部件的配合实体的名称?
也就是说,如果一个零部件在装配体中使用了两次,并且有两个重合配合使用(所述零部件的)同一个面与另一个零部件,我可以提取两个配合的相同名称吗?例如,如果有两个立方体 C1 和 C2,并且每个立方体的一侧与另一侧有一个配对。我们能得到一个伙伴的名字,比如 C1:Ida 和 C2:Idb 来识别立方体的一边吗?
这里给出了一个类似的问题:https ://forum.solidworks.com/thread/59399并且提供的建议解决方案是
MateEntity2::Reference->MateReference::Name
MateEntity2::Reference→MateReference::ReferenceEntity2→ModelDoc2::GetEntityName
我一直无法找到正确的调用顺序来获得这个。
此外,使用 Solidworks API 中给出的示例,示例https://help.solidworks.com/2021/English/api/sldworksapi/Get_Mate_Reference_Properties_Example_Csharp.htm似乎表明我们想要一个MateReference
但我无法正确投射从 aMateEntity.Reference
到任何可用的类型。
下面的一些代码提供了一个可重现的示例(在solidworks 21 中),其中许多尝试从aMateEntity.Reference
转换为另一种类型都失败了。感谢您的任何指示。
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'.
}
}
}
(这是交叉张贴在这里没有回应)