簡體   English   中英

如何在裝配體中查找配合實體的名稱

[英]How to find the name of the mate entity in an assembly

如何找到裝配體中零部件的配合實體的名稱?

也就是說,如果一個零部件在一個裝配體中使用了兩次,並且有兩個重合的配合使用(所述零部件的)與另一個零部件相同的面,我可以為兩個配合提取一個相同的名稱嗎? 例如,如果有兩個立方體 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但我無法投射正確地從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'.
        }
    }
}

(這是交叉張貼在這里沒有回應)

我不是經驗豐富的用戶,所以不確定這是否有幫助。 API 幫助文檔 ( https://help.solidworks.com/2022/english/api/sldworksapi/SolidWorks.Interop.sldworks~SolidWorks.Interop.sldworks.IMateEntity2~Reference.html?verRedirect=1 ) 錯誤地指出 (at自 2010 版起列出)MateEntity2.Reference 的返回值是 MateReference 類型。 實際上它是實體類型。 有幾個使用 Entity = MateEntity2.Reference 的例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM