1 Reply Latest reply on Jan 22, 2015 6:51 AM by Ot User Ot

    SolidWorks C# API AssemblyDoc GetComponents Return null

    Chuan Hung

      Hi,

       

      I'm trying to read an assembly file and get the components it has. I wrote the function below and the debug message prints out the correct number of components my assembly file has. However, assemblyDoc.GetComponents(false) always returns null. Does anyone know what the problem could be?

       

      public void LoadSolidWorks ( string fileName ) {

              int error = 0;

              int warning = 0;

              swApp.SetCurrentWorkingDirectory ( Application.persistentDataPath );

              ModelDoc2 swDoc = swApp.OpenDoc6 ( fileName, ( int ) swDocumentTypes_e.swDocASSEMBLY,

                                                 ( int ) swOpenDocOptions_e.swOpenDocOptions_ReadOnly, "", ref error, ref warning );

              AssemblyDoc assemblyDoc = ( AssemblyDoc ) swDoc;

       

              Debug.Log ( "error " + error + " warning " + warning );

              Debug.Log ( "Path " + swDoc.GetPathName() );

              Debug.Log ( "Components Count: " + assemblyDoc.GetComponentCount ( true ) );

       

              // ALL DEBUG MESSAGES ARE CORRECT UP TILL HERE

       

              object[] objs = ( object[] ) assemblyDoc.GetComponents ( false );

      }

       

      Thanks!

        • Re: SolidWorks C# API AssemblyDoc GetComponents Return null
          Ot User Ot

          Hi chuan,

           

          Did you get this problem resolved?

           

          I have tried with the below code and got the components successfully.

           

          SldWorks.SldWorks swApp;

          swApp = new SldWorks.SldWorks();

           

          SldWorks.AssemblyDoc doc = swApp.OpenDoc(filename, (int)swDocumentTypes_e.swDocASSEMBLY);

          object[] comps = doc.GetComponents(false);

           

          foreach (object comp in comps)

          {

               IComponent2 icomp = (IComponent2)comp;

               IModelDoc2 modeldoc2 = icomp.GetModelDoc2();

               Console.WriteLine (modeldoc2.GetPathName());

          }

          swApp.CloseAllDocuments(true);

          swApp.ExitApp();

          swApp = null;