What's new

V22 - New API for Text on Canvas

NateLiquidGravity

Alibre Super User
There seems to be a lot missing from add-on documentation. Digging into every example add-on yields more info but I feel like I'm looking thru a keyhole.
 

simonb65

Alibre Super User
There seems to be a lot missing from add-on documentation. Digging into every example add-on yields more info but I feel like I'm looking thru a keyhole.
Totally agree. The 'limited' functionality examples and digging around in the Visual Stuido Object Browser trying to reverse engineer and figure out function syntax is a right pain. I've written an internal company helper add-on, but it's not been straight forward by any means!
 

simonb65

Alibre Super User
resolved, use the IADAddOnCanvasDisplay -> DrawScreenText function for HOOPS rendering ...

Simple helper function (with fixed font, size and text colour) ...
C++:
#define COLOR_ARGB(a,r,g,b)            (long)((a<<24) | (r<<16) | (g<<8) | b)

void HOOPS_DrawText(IADAddOnCanvasDisplay* pCanvasDisplay, LPWSTR text, long x, long y)
{
    BSTR segmentName = SysAllocString(L"text segment");
    BSTR str = SysAllocString(text);
    BSTR fontName = SysAllocString(L"Consolas");

    LONG64 segment;
    pCanvasDisplay->AddSubSegment(NULL, segmentName, &segment);

    LONG64 result;
    pCanvasDisplay->DrawScreenText(segment, str, x, y, TextAlignment::TextAlignment_TopLeft, fontName, 12.0f, COLOR_ARGB(255, 0, 0, 0), VARIANT_TRUE, &result);
 
    SysFreeString(segmentName);
    SysFreeString(str);
    SysFreeString(fontName);
}

Example usage ...
C++:
HRESULT _stdcall CMyAddOnCommand::On3DRender(void)
{
    // With the Hoops based rendering engine (V2019 and later), the add-on need not redraw its graphics unless it invalidated it.
    // So, view manipulation operations like pan/rotate/zoom etc do not require a redraw by addon
    if (m_bIsOutOfDate)
    {
        IUnknown* pUnkDisplayContext = NULL;
        IADAddOnCanvasDisplay* pCanvasDisplay = NULL;

        // Call Begin3DDisplay () and get a context to render my graphics primitives
        m_pCmdSite->Begin3DDisplay(m_bClearViewPort, &pUnkDisplayContext);
        pUnkDisplayContext->QueryInterface(&pCanvasDisplay);
 
        // Draw some example text
        HOOPS_DrawText(pCanvasDisplay, L"X = 1.25", 10, 10);
        HOOPS_DrawText(pCanvasDisplay, L"Y = 0.00", 10, 30);
        HOOPS_DrawText(pCanvasDisplay, L"Z = 2.76", 10, 50);

        pCanvasDisplay->Release();

        // End 3D Display.
        m_pCmdSite->End3DDisplay();

        m_bIsOutOfDate = false;
    }
    return S_OK;
}

Not sure if the font is created and destroyed on every call of DrawScreenText(..) or if the graphics core caches it?? if it's the former then this call will produce a performance hit if called repeatably or with large amounts of text drawn in different positions!
 
Last edited:
Top