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.
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?
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.
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