Functions for project files .IPT

Top  Previous  Next

Here's a function which will get the currect active project file:

 

CString GetActiveDesignProject ()

{

    CComPtr<DesignProjectManager> pManager;

    theApp.GetInvAppPtr()->get_DesignProjectManager (&pManager) ;

 

    CComPtr<DesignProject> pDesignProject ;

    pManager->get_ActiveDesignProject (&pDesignProject) ;

 

    BSTR bstrDesignProjectName ;

    pDesignProject->get_FullFileName (&bstrDesignProjectName) ;

 

    return CString (bstrDesignProjectName) ;

}

 

Notice that it also illustrates converting from a BSTR to a CString.

 

Here's a function (with lots of internal checks) for setting the current active project:

 

void SetActiveProject(const CString& kcsFullProjName)

{

    // Check to make sure a document isn't open.

    if (GetNumDocummentsOpenInInventor() > 0) {

        TRACE (L"SAP but some documents are still open\n") ;

        return ;

    }

 

    if (!FileExists (kcsFullProjName)) {

        TRACE (L"SAP %s but file does not exist",kcsFullProjName) ;

        return ;

    }

 

    CComPtr<DesignProjectManager> pManager;

    HRESULT hRes =theApp.GetInvAppPtr()->get_DesignProjectManager (&pManager) ;

    if (FAILED(hRes)) {

        TRACE (L"SAP %s get_DesignProjectManager failed",kcsFullProjName) ;

        return ;

    }

 

    // Get the list of all projects...

    CComPtr<DesignProjects> pProjectList ;

    hRes = pManager->get_DesignProjects (&pProjectList) ;

    if (FAILED(hRes)) {

        TRACE (L"SAP %s get_DesignProjects failed",kcsFullProjName) ;

        return ;

    }

 

    // This assumes that the project file exists...

    CComPtr<DesignProject> pDesignProject ;

    CComBSTR bstrFullProjectName (kcsFullProjName) ;

    hRes = pProjectList->get_ItemByName (bstrFullProjectName,&pDesignProject) ;

    if (FAILED(hRes)) {

        TRACE (L"SAP %s get_ItemByName failed",kcsFullProjName) ;

        return ;

    }

 

    // Make the project the active one...

    hRes = pDesignProject->Activate (FALSE)  ;

    if (FAILED(hRes)) {

        TRACE (L"SAP %s Activate failed",kcsFullProjName) ;

        return ;

    }

 

    // Check that the change has actually happened...

    if (GetActiveDesignProject().CompareNoCase(kcsFullProjName) != 0) {

        TRACE (L"SAP tried to activate <%s>\n"

                                 L"but active project is still <%s>",

                                 kcsFullProjName,

                                 GetActiveDesignProject()) ;

    }

}

 

 

The above also shows to how get a CComBSTR from a CString.

 

 

Text, images and diagrams © 2021 Owen F. Ransen. All rights reserved. (But copy the source code as much as you want!)