[Solved] How to get the pointer of an existing TechDraw balloon ?

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
edi
Posts: 481
Joined: Fri Jan 17, 2020 1:32 pm

[Solved] How to get the pointer of an existing TechDraw balloon ?

Post by edi »

The name (as a string) of an existing TD balloon is known. How can I get its pointer ? I use the following lines:

Code: Select all

void CmdTechDrawExtensionTestroutine::activated(int iMsg)
// Testroutine
{
    Q_UNUSED(iMsg);
    Base::Console().Message("Testtool started\n");

    // returns the pointer of a dimension:

    std::string dimName = "Dimension";
    TechDraw::DrawViewDimension* dim = 0;
    dim = dynamic_cast<TechDraw::DrawViewDimension *>(this->getDocument()->getObject(dimName.c_str()));
    dim->X.setValue(0.0);

    // should return the pointer of a balloon. Compiler error: dynamic cast not possible 
    
    std::string balloonName = "Balloon";
    TechDraw::DrawViewBalloon* balloon = 0;
    balloon = dynamic_cast<TechDraw::DrawViewBalloon *>(this->getDocument()->getObject(balloonName.c_str()));
    balloon->X.setValue(0.0);
}
To comparison: The first 4 lines return the pointer of a dimension object. The next 4 lines should do the same with a balloon. But a compilation error occures.

Can anybody help ?
Last edited by edi on Sun Jan 23, 2022 7:36 am, edited 1 time in total.
aapo
Posts: 615
Joined: Mon Oct 29, 2018 6:41 pm

Re: How to get the pointer of an existing TechDraw balloon ?

Post by aapo »

Generally, dynamic_cast is not possible, if the base class has no virtual functions, because then there would not be vtable present in the object, which is a requirement for the dynamic_cast to work. In this case both classes DrawViewDimension and DrawViewBalloon are derived from DrawView, which derives all the way from App::DocumentObject. All these classes have virtual functions, and hence vtables. So, dynamic_cast should indeed work. Thus, it leads me to suspect that in your case somehow the compiler doesn't know that DrawViewBalloon is derived from DrawView, but knows that DrawViewDimension is. Have you #included DrawViewBalloon.h and DrawViewDimension.h at the same place?
edi
Posts: 481
Joined: Fri Jan 17, 2020 1:32 pm

Re: [Solved] How to get the pointer of an existing TechDraw balloon ?

Post by edi »

Tank you very much. That is exactly the solution.
I had included neither DrawViewDimension.h nor DrawViewBalloon.h, but by pure chance one included haeder file had included DrawViewDimension.h.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: [Solved] How to get the pointer of an existing TechDraw balloon ?

Post by wmayer »

Btw, when doing

Code: Select all

dim = dynamic_cast<TechDraw::DrawViewDimension *>(this->getDocument()->getObject(dimName.c_str()));
then you should check if dim is null because:
  • there is no guarantee that in the document there is an object called Dimension. So, getObject() will return null
  • in case there is an object called Dimension then it's still possible that it's not of the expected type TechDraw::DrawViewDimension. The dynamic_cast then returns null
Furthermore:

Code: Select all

TechDraw::DrawViewDimension* dim = 0;
In modern C++ code you should avoid using 0 or NULL for pointers. Instead use nullptr;
Post Reply