0 Replies Latest reply on Dec 6, 2011 4:28 PM by Azarm Salimi

    problem with getting children

    Azarm Salimi

      Hi again,

       

      I also tried this code . No luck.

      Not be able to get the children and traverse the assembly..

       

       

      void CreateAssemblyMap(CComPtr<IModelDoc2> pModelDoc, CComPtr <IComponent2>/*IComponent2Ptr*/  swComp )

      {

       

       

           MAP_OBJECT* mo = NULL;

           bstr_t name = pModelDoc->GetTitle();   

           CString spName(name.GetBSTR());   

          long    nChildren;

       

           CComPtr<IComponent2>  oChild;

           CComPtr< IAssemblyDoc> swThisAssemblyDoc;

           mo = new MAP_OBJECT();   

          CComBSTR    sPathName;

       

          try

          {

       

          BSTR bPath = pModelDoc->GetPathName();

          CString sPath(bPath);

          mo->filename = sPath;

          CFileStatus status;

          BOOL bFileExists = CFile::GetStatus( sPath, status );

          printf("createAsemblyMap: PathName = %s\n", mo->filename);

          fflush(stdout);

       

          pModelDoc->QueryInterface(__uuidof(IAssemblyDoc), reinterpret_cast<void**>(&swThisAssemblyDoc));

       

          mo->oAssy = swThisAssemblyDoc;

       

          printf("createAsemblyMap: assemly pointer = %s\n", mo->oAssy);

          fflush(stdout);

       

          if ( bFileExists )

          {

       

              try

              {

       

              mo->partName = spName;

              printf("createAsemblyMap: partName  = %s\n", name);

              fflush(stdout);

              }

              catch(const _com_error &roError )

              {

              #ifdef _DEBUG   

              printf("SolidWorks: Failed to get partName with the error - %s", roError.ErrorMessage());

              fflush(stdout);

              #endif)

       

       

              }

       

          }

       

       

       

      #ifdef _DEBUG

          printf("createAssemblyMap [%s][%s][%s]\n", mo->partName, mo->instanceName, sPath);

          fflush(stdout);

      #endif

       

       

              if ( pModelDoc->GetType() == swDocASSEMBLY )

       

              {

       

       

       

                                      BSTR cpstr = swComp->GetName2();

                                      CString spName(cpstr);

                                      mo->instanceName = spName;

                                      g_map[mo->partName] = mo;

                                      nChildren = swComp->IGetChildrenCount();

       

                                      printf("\n g_map emlements : oAssy = %s partName= %s InstaceName = %s fileName= %s nChildren = %d  \n", mo->oAssy, mo->partName, mo->instanceName, mo->filename, nChildren);

                                      fflush(stdout);

              }

       

                                  /   typedef CComPtr<IComponent2> LPCOMPONENT2;

       

                                      LPCOMPONENT2 pChildren;

                                      pChildren = swComp->IGetChildren(); 

                                       int childrenCount;

                                       childrenCount = swComp->IGetChildrenCount();

                                       std::cout << "\n  chilrenCount = " << childrenCount << std::endl;

                                       // Recursively traverse the children

       

                                      for (long i=1; i<childrenCount; i++ )   // Note Ubound 0 is 1 child

       

                                      {

                                          BSTR path;

                                          LPCOMPONENT2 oChild  = (&pChildren)[i];

                                          path = oChild->GetPathName();

       

       

                                          std::cout << " path = " << path << std::endl;

       

       

                                              try

                                              {

       

       

       

                                      std::cout << "Componenet name = " << oChild->GetName2().GetBSTR() << std::endl;

       

                                              }

                                          catch(const _com_error &roError)

                                                  {

                                                      #ifdef _DEBUG   

                                                              std::cout <<"SolidWorks: Failed to get Child i = %d" << i <<" the error is  - " <<  roError.ErrorMessage() << std::endl;

                                                              fflush(stdout);

                                                      #endif

                                                  }

       

       

                                          //    CreateAssemblyMap(oChild->GetModelDoc2(), oChild);

       

                                      }

       

                                     ..

                                     .

                                     .

                                     .

       

      I am using Visual Studio 2010.

      I appreciate any help.

       

      Azarm