4 Replies Latest reply on Jul 31, 2015 3:39 PM by Todd Carpenter

    Unable to cast COM object

    Todd Carpenter

      Not sure what I'm doing wrong here.  This was working.  I may just been lucky, but I have an add-in I modeled after the "API: How to make calls crossing thread boundaries in SolidWorks C# add-in", with the exception of the Thread portion since I'm not using a form, I wasn't sure this applied (or how I would work that in).  I have another application from the outside calling the add-in.  Being a rookie C# programmer, the threading portion is a bit out of my league, but like I said, this was working.  Here's the complete error:

       

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

       

      Here's the code generating the error (from the add-in):

       

              //
              // Dimension Overrides.
              //
              public string OverrideDims(string[] dimVals)
              {            
                  IModelDoc2 swDoc = (IModelDoc2)iSwApp.ActiveDoc;
                  // Check swDoc - easy for users to place a command prior to a "Submit Session" rule.
                  //
                  if (swDoc == null)
                      return "Error getting the active document, this routine must be called after at least one Submit Session rule.";
                  
                  try
                  {
                      IDrawingDoc swDrawing = (IDrawingDoc)swDoc;
                  }
                  catch(Exception ex)
                  {
                      return "Error: " + ex.Message + " a drawing must be active.";
                  }
      
      
                  ISelectionMgr swSelMgr = swDoc.SelectionManager; 
                  INote swNote = null;
                  bool boolstatus = false;
                  string dimName;
                  string dimVal;
      
      
                  for (int i = 0; i < dimVals.Length; i += 2)
                  {
                      dimName = dimVals[i];
                      dimVal = dimVals[i + 1];
      
      
                      boolstatus = swDoc.Extension.SelectByID2(dimName, "DIMENSION", 0, 0, 0, false, 0, null, 0);
                      if (boolstatus)
                      {
                          boolstatus = swDoc.EditDimensionProperties2(0, 0, 0, "", "", true, 9, 2, true, 1, 1, dimVal, "", false, "", "", true);
                          if (!boolstatus) return "Error setting: " + dimName + " to: " + dimVal;
                      }
      
      
                      if (!boolstatus) boolstatus = swDoc.Extension.SelectByID2(dimName, "NOTE", 0, 0, 0, false, 0, null, 0);
      
      
                      if (boolstatus)
                      {
                          //swSelMgr = (ISelectionMgr)swDoc.SelectionManager;
                          swNote = (INote)swSelMgr.GetSelectedObject6(1, -1);
                          boolstatus = swNote.SetText(dimVal);
                          if (!boolstatus) return "Error setting: " + dimName + " to: " + dimVal;
                      }
      
      
                      if (!boolstatus) return "Error selecting/finding Dimension: " + dimName;
      
      
                      swDoc.ClearSelection2(true);
                  }
                  return "0";
              } // end OverrideDims.
      

       

      Thanks for any input.