首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在程序集中查找mate实体的名称

如何在程序集中查找mate实体的名称
EN

Stack Overflow用户
提问于 2021-09-28 17:29:51
回答 1查看 327关注 0票数 1

如何在程序集中找到组件的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转换到另一种类型的尝试都失败了。谢谢你的指点。

代码语言:javascript
运行
复制
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'.
        }
    }
}

(这是没有响应的跨站这里 )

EN

回答 1

Stack Overflow用户

发布于 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69366119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档