Library item API Sample

Description

Demonstrates using library items using the API.

To use the sample, create a new Python or C++ script and copy and paste this code, replacing the default code. The sample also used an saved user document with Library component(s). Create document, save it and insert Library item into the document. When running the script, have a that design open.

Code Samples

#include <Core/CoreAll.h>
#include <Fusion/FusionAll.h>

using namespace adsk::core;
using namespace adsk::fusion;

Ptr<Application> app;
Ptr<UserInterface> ui;

extern "C" XI_EXPORT bool run(const char* context)
{
    app = Application::get();
    if (!app)
        return false;

    ui = app->userInterface();

    Ptr<Product> product = app->activeProduct();
    if (!product)
        return false;

    Ptr<Design> design = product;

    // Get the 'LibraryItem' property group in the root component of the design.

    auto allComponents = design->allComponents();
    auto allCompCount = allComponents->count();
    for (auto i = 0; i < allCompCount; ++i)
    {
        auto comp = allComponents->item(i);
        // iterate group properties for Library component only
        if (comp->isLibraryItem())
        {
            auto compName = comp->name();
            ui->messageBox(compName);

            auto rootGroups = comp->propertyGroups();
            auto groupLibraryItem = rootGroups->itemById("libraryItem");

            // test properties from the libraryItem group
            auto countLibProps = groupLibraryItem->count();
            for (int j = 0; j < countLibProps; ++j)
            {
                auto prop = groupLibraryItem->item(j);
                auto propName = prop->name();
                // get string value from string property
                auto stringProperty = static_cast<StringProperty*>(prop->queryInterface(StringProperty::classType()));
                ui->messageBox(propName + " = " + stringProperty->value());
            }
        }
    }

    return true;
}

#ifdef XI_WIN

#include <windows.h>

BOOL APIENTRY DllMain(HMODULE hmodule, DWORD reason, LPVOID reserved)
{
    switch (reason)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

#endif // XI_WIN