如何在程序集中找到组件的mate实体的名称?
也就是说,如果一个组件在一个程序集中被使用了两次,并且有两个相同的伙伴使用相同的面(所述组件)和另一个组件,那么我能提取出两个组件相同的名称吗?例如,如果有两个立方体,C1和C2,并且每个立方体的一侧都有另一个立方体的配偶。我们能得到配偶的名字吗,比如C1:Ida和C2:Idb,它将识别立方体的一侧?
这里有一个类似的问题:https://forum.solidworks.com/thread/59399和建议的解决方案是
MateEntity2 2::Reference->MateReference::Name MateEntity2::Reference→MateReference::ReferenceEntity2→ModelDoc2::GetEntityName
我一直没能找到正确的呼叫顺序来得到这个。
此外,使用Solidworks中给出的示例,示例Csharp.htm似乎表明我们需要一个MateReference
,但是我无法正确地从一个MateEntity.Reference
转换为任何可用的类型。
下面的一些代码提供了一个可复制的示例(在solidworks 21中),许多尝试从MateEntity.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'.
}
}
}
(这是没有响应的跨站这里 )
发布于 2022-07-03 03:37:41
我不是一个有经验的用户,所以不确定这是否有帮助。API文档(https://help.solidworks.com/2022/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IMateEntity2~Reference.html?verRedirect=1)错误地指出(自2010年版本以来在列表中),MateEntity2.Reference的返回值为MateReference类型。实际上,它的类型是实体。有几个例子使用了实体= MateEntity2.Reference
https://stackoverflow.com/questions/69366119
复制相似问题