2 Replies Latest reply on Oct 31, 2017 10:27 AM by Adam Davidson

    C# - Get the Feature selected of an assembly or part

    Erika Gómez

      I try to get current select feature in assembly o part document of SolidWorks 2014, I have this code
      swModel = (ModelDoc2)swApp.ActiveDoc;
                 
      swDocExt = swModel.Extension;
                
      swSelMgr = (SelectionMgr)swModel.SelectionManager;

      swFeat = (Feature)swSelMgr.GetSelectedObject6(1, -1);

      But I get the following error in the cast swFeat = (Feature)swSelMgr.GetSelectedObject6(1, -1)


      Unable to cast COM object of type 'System.__ComObject' to interface type 'SolidWorks.Interop.sldworks.Feature'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{83A33D38-27C5-11CE-BFD4-00400513BB57}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))."

       

      Somebody can help me? Any sugestion?

       

      Thanks in advance.

        • Re: C# - Get the Feature selected of an assembly or part
          Artem Taturevych

          Make sure that the feature selected in the Feature Manager Design Tree (not in the graphics area) and this is the first selected object.

          • Re: C# - Get the Feature selected of an assembly or part
            Adam Davidson

            I just had EXACTLY the same exception. That COM IID is for the Feature interface which doesn't exist on the COM object I was looking at, which is why the (Feature) cast failed for us both.

             

            Using this code snippet here I discovered that the COM Object wrapper I had implemented SolidWorks.Interop.sldworks.IBodyFolder. You may have something different, which also isn't a Feature instance as we both expected it to be.

             

            Here is the C# code I used in case it's useful for someone. Call it like this:    ComObject.GetType(selectedObject)

             

            using System;

            using System.Reflection;

            using System.Runtime.InteropServices;

             

            namespace MyAddin {

                internal static class ComObject {

                    static Assembly InteropAssembly = Assembly.GetAssembly(typeof(SolidWorks.Interop.sldworks.SldWorks));

             

                    internal static Type GetType(object comObject) {

                        // get the com object and fetch its IUnknown

                        IntPtr iunkwn = Marshal.GetIUnknownForObject(comObject);

             

                        // enum all the types defined in the interop assembly

                        Type[] types = InteropAssembly.GetTypes();

             

                        // find the first implemented interop type

                        foreach (Type type in types) {

                            // get the iid of the current type

                            Guid iid = type.GUID;

                            if (!type.IsInterface || iid == Guid.Empty) {

                                // com interop type must be an interface with valid iid

                                continue;

                            }

             

                            // query supportability of current interface on object

                            IntPtr ipointer = IntPtr.Zero;

                            Marshal.QueryInterface(iunkwn, ref iid, out ipointer);

             

                            if (ipointer != IntPtr.Zero) {

                                // yeah, that’s the one we’re after

                                return type;

                            }

                        }

             

                        // no implemented type found

                        return null;

                    }

                }

            }