Fusion API Custom Graphic Text Billboard Problem

Fusion API Custom Graphic Text Billboard Problem

manfred_weinert
Contributor Contributor
51 Views
0 Replies
Message 1 of 1

Fusion API Custom Graphic Text Billboard Problem

manfred_weinert
Contributor
Contributor

 

I am using the personal free version of fusion on a Win11 computer and visual studio 2022.

I don't understand how the billboarding of Text is done within a custom graphic group of the fusion API.

"ScreenBillBoardStyle" is used!

I created the following script for testing it:

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

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

#include <windows.h>

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






bool DrawFrameConnectionline(Ptr<CustomGraphicsGroup> cgGroup)
{
    Ptr <CustomGraphicsLines> cgFrameLines = NULL;
    for (int iVQSentityIndex = 0; iVQSentityIndex < cgGroup->count(); iVQSentityIndex++)
    {
        if (cgGroup->item(iVQSentityIndex)->id().find("Frame") != std::string::npos)
        {
            cgFrameLines = cgGroup->item(iVQSentityIndex);
        }
    }
    if (!cgFrameLines)
        return false;

    std::vector <double> vdvecCoords;
    std::vector <int>    viindexList;

    vdvecCoords.insert(vdvecCoords.end(),
        {
        0
        , 0
        , 0
        , cgFrameLines->coordinates()->getCoordinate(0)->x()
        , cgFrameLines->coordinates()->getCoordinate(0)->y()
        , cgFrameLines->coordinates()->getCoordinate(0)->z()
        });

    Ptr<CustomGraphicsCoordinates> Coordinates = CustomGraphicsCoordinates::create(vdvecCoords);
    cgFrameLines = cgGroup->addLines(Coordinates, viindexList, false);

    return true;
}

bool DrawFrame(Ptr<CustomGraphicsGroup> cgGroup)
{

    std::vector <double> vdvecCoords;
    std::vector <int>    viindexList;

    vdvecCoords.insert(vdvecCoords.end(),
        { 
          -2
        , -1
        , 0
        , 8
        , -1
        , 0
        , 8
        , 7
        , 0
        , -2
        , 7
        , 0
        , -2
        , -1
        , 0
        });

    Ptr<CustomGraphicsCoordinates> Coordinates = CustomGraphicsCoordinates::create(vdvecCoords);
    Ptr <CustomGraphicsLines> cgLines = cgGroup->addLines(Coordinates, viindexList, true);
    cgLines->id("Frame");
    {
        char buffer[64] = { '\0' };
        ZeroMemory(buffer, sizeof(buffer));
        sprintf_s(buffer, "Frame X=%.4f Y=%.4f Z=%.4f",
            cgLines->transform()->translation()->x()
            , cgLines->transform()->translation()->y()
            , cgLines->transform()->translation()->z()
        );
        app->log(buffer);
    }
    {
        char buffer[64] = { '\0' };
        ZeroMemory(buffer, sizeof(buffer));
        sprintf_s(buffer, "Frame edge 0 X=%.4f Y=%.4f Z=%.4f",
            cgLines->coordinates()->getCoordinate(0)->x()
            , cgLines->coordinates()->getCoordinate(0)->y()
            , cgLines->coordinates()->getCoordinate(0)->z()
        );
        app->log(buffer);
    }



    return true;
}

bool DrawText(Ptr<CustomGraphicsGroup> cgGroup)
{
    Ptr <CustomGraphicsLines> cgFrameLines = NULL;
    for (int iVQSentityIndex = 0; iVQSentityIndex < cgGroup->count(); iVQSentityIndex++)
    {
        if (cgGroup->item(iVQSentityIndex)->id().find("Frame") != std::string::npos)
        {
            cgFrameLines = cgGroup->item(iVQSentityIndex);
        }
    }
    if (!cgFrameLines)
        return false;

    double txtsize = 0.5;
    std::string text = "upper left corner of Frame";

    Ptr<Vector3D> vector = adsk::core::Vector3D::create(
        cgFrameLines->coordinates()->getCoordinate(3)->x()
        , cgFrameLines->coordinates()->getCoordinate(3)->y() - txtsize
        , cgFrameLines->coordinates()->getCoordinate(3)->z()
    );

    Ptr<Matrix3D> transform = adsk::core::Matrix3D::create();
    transform->translation(vector);
    Ptr<CustomGraphicsText> cgText = cgGroup->addText(text, "Arial", txtsize, transform);

    if (!cgText)
    {
        std::string desc;
        app->getLastError(&desc);
        app->log("cg Text Error" + desc);

    }
    {
        char buffer[64] = { '\0' };
        ZeroMemory(buffer, sizeof(buffer));
        sprintf_s(buffer, "Text ULC X=%.4f Y=%.4f Z=%.4f",
            cgText->transform()->translation()->x()
            , cgText->transform()->translation()->y()
            , cgText->transform()->translation()->z()
        );
        app->log(buffer);
    }





    text = "Anchorpoint of BillboardGroup";
    vector = adsk::core::Vector3D::create(
        0
        , 0
        , 0
    );

    transform = adsk::core::Matrix3D::create();
    transform->translation(vector);
    cgText = cgGroup->addText(text, "Arial", txtsize, transform);

    return true;
}






void createCGObject(Ptr<CustomGraphicsGroup> cgGroup)
{    




    Ptr<CustomGraphicsGroup> cgGroupP = cgGroup->parent();  
    Ptr<CustomGraphicsLines> cgLines = cgGroupP->item(0);
        //   Setup  CG anchor- and billboarding- Point


	Ptr<Vector3D> vectorcg = adsk::core::Vector3D::create(
        cgLines->coordinates()->getCoordinate(2)->x() + cgLines->transform()->translation()->x()
		, cgLines->coordinates()->getCoordinate(2)->y() + cgLines->transform()->translation()->y()
		, cgLines->coordinates()->getCoordinate(2)->z() + cgLines->transform()->translation()->z()
	);


	Ptr<Matrix3D> transformcg = adsk::core::Matrix3D::create();
	transformcg->translation(vectorcg);

	cgGroup->transform(transformcg);

    Ptr <CustomGraphicsBillBoard> billBoard = CustomGraphicsBillBoard::create(Point3D::create(0, 0, 0));
	billBoard->billBoardStyle(CustomGraphicsBillBoardStyles::ScreenBillBoardStyle);
    cgGroup->billBoarding(billBoard);
    Ptr<Vector3D> vAxis = adsk::core::Vector3D::create(1, 0, 0);

    {
        char buffer[64] = { '\0' };
        ZeroMemory(buffer, sizeof(buffer));
        sprintf_s(buffer, "Group X=%.4f Y=%.4f Z=%.4f",
            cgGroup->transform()->translation()->x()
            , cgGroup->transform()->translation()->y()
            , cgGroup->transform()->translation()->z()
        );
        app->log(buffer);
    }



    if (DrawFrame(cgGroup) == false)
        return;


    if (DrawFrameConnectionline(cgGroup) == false)
        return;


    if (DrawText(cgGroup) == false)
        return;
    

    return;
}


void Object(Ptr<CustomGraphicsGroup> cgGroup)
{

    std::vector <int>    viindexList;
    std::vector <int>    viNormalindexList;
    std::vector <double> vdvecNormal;
    std::vector <double> vdvecCoords;

    vdvecCoords.insert(vdvecCoords.end(),
        { 0
        , 0
        , 0
        , 40
        , 0
        , 0
        , 0
        , 20
        , 0
        , 0
        , 0
        , 0
        });

    Ptr<CustomGraphicsCoordinates> LinesCoords = CustomGraphicsCoordinates::create(vdvecCoords);

    // Create the mesh.
    Ptr< CustomGraphicsLines> TriangleLines = cgGroup->addLines(LinesCoords, viindexList, true);

    Ptr <Color> diffuse = Color::create(0, 255, 0, 255);
    Ptr <Color> ambient = Color::create(0, 255, 0, 255);
    Ptr <Color> specular = Color::create(255, 255, 255, 255);
    Ptr <Color> emissive = Color::create(0, 255, 0, 255);
    double glossy = 60;
    double opacity = 1.0;
    Ptr <CustomGraphicsBasicMaterialColorEffect> greenBasicMaterial = CustomGraphicsBasicMaterialColorEffect::create(diffuse,
        ambient,
        specular,
        emissive,
        glossy,
        opacity);

    Ptr <Color> solid = Color::create(0, 255, 0, 255);
    Ptr <CustomGraphicsSolidColorEffect> greenSolidColor = CustomGraphicsSolidColorEffect::create(solid);
    

    TriangleLines->color(greenBasicMaterial);

    Ptr<Vector3D> vector = adsk::core::Vector3D::create(-10, -10, -10);
    Ptr<Matrix3D> transform = adsk::core::Matrix3D::create();
    transform->translation(vector);
    TriangleLines->transform(transform);
    return;
}



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

    ui = app->userInterface();
    if (!ui)
        return false;

    ui->messageBox("Hello script");

    Ptr<Document> doc = app->activeDocument();
    if (!doc)
        return false;

    Ptr<Products> prods = doc->products();
    if (!prods)
        return false;

    Ptr<Product> prod = prods->itemByProductType("DesignProductType");
    if (!prod)
        return false;

    Ptr<Design> des = prod->cast<Design>();
    if (!des)
        return false;

    // get the entry for custom graphics
    Ptr<Product> activeProd = app->activeProduct();
    if (!activeProd)
        return false;

    Ptr<CustomGraphicsGroups> cgGroups = NULL;

    Ptr<CAM> cam = activeProd->cast<CAM>();
    if (cam)
    {
        Ptr<CustomGraphicsGroups> cgGroups = cam->customGraphicsGroups();
    }
    else
    {
        Ptr<Component> rootComp = des->rootComponent();
        if (!rootComp)
            return false;
        cgGroups = rootComp->customGraphicsGroups();
    }
    if (!cgGroups)
        return false;

    Ptr<CustomGraphicsGroup> cgGroupP = cgGroups->add();
    Object(cgGroupP);

    Ptr<CustomGraphicsGroup> cgGroup = cgGroupP->addGroup();
    createCGObject(cgGroup);

    app->activeViewport()->refresh();

    return true;
}

 

The following screen shows the result after starting the script:

startup screenstartup screen

After zoom and rotation to the X-Y Plane it will look like:

graphic and text fit to screengraphic and text fit to screen

Now rotate the coordinate system 90 degrees around the Y axis clockwise:

turn around 90 degree clockwise.jpg

You will see the "upper left corner of Frame" does not more fit to the left line of the Frame it is moved to the right.

Positioning the mouse pointer over the text results in the display of a blue text copy at the expected position.

Blue TextBlue Text

Does anybody know why it is as it is?

Can anybody help so that the original black Text will be displayed at the position of the blue faded in Text?

 

Many thanks in advance for your help!  

0 Likes
52 Views
0 Replies
Replies (0)