Text in the Interactive Context

Hi,

I need to introduce some 3d text in the Interactive context, Can anyone tell me how to do this? I've been trying to obtain the text from Graphic3d_Group::Text, but I don't know how to introduce the text in my scene.

Thanks

Hugues's picture

Hi,

To do this, one means is to inherit from AIS_InteractiveObject and redefine `Compute' routines.

Here is the InteractiveText class I defined, I use it when I want to display text. Indentation of the code is messed in this post, and there is also classes you probably won't have (like Point3D, Integer, Real, QString if you don't work with Qt). But it is easily adaptable.

The header file:

# include "geometry/3d/Point3D.hpp"

# include

# include
# include
# include
# include
# include
# include
# include
# include
# include

/*! \class InteractiveText
* \brief Interactive items specialized in the displaying of texts.
*/

DEFINE_STANDARD_HANDLE (InteractiveText, AIS_InteractiveObject)
class InteractiveText : public AIS_InteractiveObject
{
// ------------------- Initialization and Deletion ---------------------
public:
//! Construct a default instance of InteractiveText.
InteractiveText ();

//! Construct a fully initialized instance of InteractiveText.
InteractiveText (const QString& text, const Point3D& pos,
Real angle = 0, Real slant = 0,
Integer color_id = 1, Integer font_id = 1,
Real scale = 0.1);

//! Destruct the instance and free any allocated resources.
virtual ~InteractiveText ();

DEFINE_STANDARD_RTTI (InteractiveText)

// ----------------------------- Access --------------------------------
public:
//! Position of the text displayed.
const Point3D& position () const
{
return _position;
}

//! Text displated by the current InteractiveText object.
QString text () const
{
return _text;
}

//! User friendly string of the instance.
virtual QString to_string () const
{
return _text;
}

// -------------------------- Element change ---------------------------
public:
void set_text (const QString& v);

void set_position (const Point3D& pos);

// -------------------------- Implementation ---------------------------
private:
//! -- from PrsMgr_PresentableObject.
void Compute (const Handle_PrsMgr_PresentationManager3d& pm,
const Handle_Prs3d_Presentation& pres,
const Integer mode);

//! -- from PrsMgr_PresentableObject.
void Compute (const Handle_Prs3d_Projector& proj,
const Handle_Prs3d_Presentation& pres);

//! -- from PrsMgr_PresentableObject.
void Compute (const Handle_PrsMgr_PresentationManager2d& pres,
const Handle_Graphic2d_GraphicObject& gr_obj,
const Integer mode) ;

//! -- from SelectMgr_SelectableObject.
void ComputeSelection (const Handle_SelectMgr_Selection& sel,
const Integer mode);

// ---------------------------- Attributes -----------------------------
protected:
QString _text;
Point3D _position;
Real _angle;
Real _slant;
Integer _color_id;
Integer _font_id;
Real _scale;

Real _width;
Real _height;

};

Now the implementation file :

#include "InteractiveText.hpp"

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

IMPLEMENT_STANDARD_HANDLE (InteractiveText, AIS_InteractiveObject)
IMPLEMENT_STANDARD_RTTI (InteractiveText, AIS_InteractiveObject)
//
// Foreach ancestors, we add a IMPLEMENT_STANDARD_SUPERTYPE and
// a IMPLEMENT_STANDARD_SUPERTYPE_ARRAY_ENTRY macro.
// We must respect the order: from the direct ancestor class
// to the base class.
//
IMPLEMENT_STANDARD_TYPE (InteractiveText)
IMPLEMENT_STANDARD_SUPERTYPE (AIS_InteractiveObject)
IMPLEMENT_STANDARD_SUPERTYPE (SelectMgr_SelectableObject)
IMPLEMENT_STANDARD_SUPERTYPE (PrsMgr_PresentableObject)
IMPLEMENT_STANDARD_SUPERTYPE (MMgt_TShared)
IMPLEMENT_STANDARD_SUPERTYPE (Standard_Transient)
IMPLEMENT_STANDARD_SUPERTYPE_ARRAY ()
IMPLEMENT_STANDARD_SUPERTYPE_ARRAY_ENTRY (AIS_InteractiveObject)
IMPLEMENT_STANDARD_SUPERTYPE_ARRAY_ENTRY (SelectMgr_SelectableObject)
IMPLEMENT_STANDARD_SUPERTYPE_ARRAY_ENTRY (PrsMgr_PresentableObject)
IMPLEMENT_STANDARD_SUPERTYPE_ARRAY_ENTRY (MMgt_TShared)
IMPLEMENT_STANDARD_SUPERTYPE_ARRAY_ENTRY (Standard_Transient)
IMPLEMENT_STANDARD_SUPERTYPE_ARRAY_END ()
IMPLEMENT_STANDARD_TYPE_END (InteractiveText)

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

// ------------------- Initialization and Deletion ---------------------
InteractiveText::InteractiveText () :
AIS_InteractiveObject (),
_width (0), _height (0)
{
}

InteractiveText::InteractiveText (const QString& text, const Point3D& pos,
Real angle, Real slant,
Integer color_id, Integer font_id,
Real scale) :
AIS_InteractiveObject (),
_text (text), _position (pos),
_angle (angle), _slant (slant),
_color_id (color_id), _font_id (font_id),
_scale (scale),
_width (0), _height (0)
{
}

InteractiveText::~InteractiveText ()
{
}

// -------------------------- Element change ---------------------------

void InteractiveText::set_text (const QString& v)
{
_text = v;
}

void InteractiveText::set_position (const Point3D& pos)
{
_position = pos;
}

// -------------------------- Implementation ---------------------------

void InteractiveText::Compute (const Handle_PrsMgr_PresentationManager3d& pm,
const Handle_Prs3d_Presentation& pres,
const Integer mode)
{
Prs3d_Text::Draw (pres, myDrawer, (Standard_CString) _text.ascii (),
gp_Pnt (_position.x (),
_position.y (),
_position.z ()));
}

void InteractiveText::Compute (const Handle_Prs3d_Projector& proj,
const Handle_Prs3d_Presentation& pres)
{

}

void InteractiveText::Compute (const Handle_PrsMgr_PresentationManager2d& pres,
const Handle_Graphic2d_GraphicObject& gr_obj,
const Integer mode)
{
Handle_Graphic2d_Text text =
new Graphic2d_Text (gr_obj, (Standard_CString) _text.ascii (),
_position.x (), _position.y (),
_angle, Aspect_TOT_SOLID, _scale);
text->SetFontIndex (_font_id);

text->SetColorIndex (_color_id);

text->SetSlant (_slant);
text->SetUnderline (false);
text->SetZoomable (true);
gr_obj->Display ();
Real x_offset;
Real y_offset;
text->TextSize (_width, _height, x_offset, y_offset);
}

void InteractiveText::ComputeSelection (const Handle_SelectMgr_Selection& sel,
const Integer mode)
{
}

khwf's picture

Is it possible to set this text as a constant info text for the viewer. Such that it is displayed at a fixed position in the lower left corner of the window ? What do I have to do to achieve this ?

Hugues's picture

If you are building the GUI with Qt, this can easily be achieved by redefining the procedure `paintEvent' in the widget responsible of the 3D view display.
Use a QPainter object to draw the text at the desired position/color.

viviana's picture

Hi,

I tried something like this:

...

Standard_Real angle=0, slant=0, scale=0.1;
Standard_Integer color_id = 1, font_id=1, mode=1;
Point3D pos(0,0,0);
const QString text1( "This is a Sphere" );

const Handle(PrsMgr_PresentationManager2d) pres;
const Handle(Graphic2d_GraphicObject) gr_obj;
const Handle(PrsMgr_PresentationManager3d) pm;
const Handle(Prs3d_Presentation) pres1;
InteractiveText* myText =new InteractiveText (text1,pos,
angle, slant,
color_id, font_id,
scale);
myText->set_position(pos);
myText->Compute(pres,gr_obj,mode);
...

and it compiles o.k. but when I am going to execute the program a segmentation fault appears.

P.D. I declared Compute() as public so I could use it.

Help please!!!

Hugues's picture

Hi,

Just use InteractiveText objects as InteractiveObject ones, i.e. inside an interactive context. You do not need to declare presentation managers, the interactive context will do this for you.

So assuming you have an initialized 3D view attached to an AIS_InteractiveContext, just do :

Handle_InteractiveText itext = new InteractiveText ("viviana", Point3D (0, 0, 0));
ais_context->Display (itext, true);

That's all. Note you should replace occurences of Point3D by gp_Pnt, i.e. the native OCC class for 3D points. Point3D is a class I defined that you can get rid of.

viviana's picture

Thank you very much.

viviana's picture

Hi,

I would like to display some 3D text with color, angle, font and color attributes, I have change some variables in the new InteractiveText, but the text appears just like before, I also looked in the OCC help about Display() but nothing related.

Thanks in advance for your help.

viviana's picture

I suposse... I have to use Prs3D_TextAspect in some way???

Denis Teissandier's picture

You can do as follows :

TCollection_ExtendedString aTCoText("Text");
gp_Pnt aPnt OrigineDimZT(0,0,0);

aPresentation= new Prs3d_Presentation(aContext->CurrentViewer()->Viewer());
Handle(Prs3d_TextAspect) aTextAspect= new Prs3d_TextAspect();
aTextAspect->SetFont(Graphic3d_NOF_ASCII_COMPLEX);

aTextAspect->SetHeight(6);
aTextAspect->SetColor(Quantity_NOC_SNOW);
aTextAspect->SetSpace(20);
Prs3d_Text::Draw(aPresentation,aComeAspect,aText, OrigineCome);
Prs3d_Text::Draw(aPresentation,aTextAspect,aTCoText, OrigineDimZT);

aPresentation->Display();

Denis

viviana's picture

Hi, I did this

TCollection_ExtendedString aTCoText("Text");
gp_Pnt OrigineDimZT(0,0,0);
Handle(Graphic3d_StructureManager) aStructureManager;
Handle(Prs3d_Presentation) aPresentation= new Prs3d_Presentation(aStructureManager);
Handle(Prs3d_TextAspect) aTextAspect= new Prs3d_TextAspect();
aTextAspect->SetFont(Graphic3d_NOF_ASCII_COMPLEX);

aTextAspect->SetHeight(6);
aTextAspect->SetColor(Quantity_NOC_SNOW);
aTextAspect->SetSpace(20);
//Prs3d_Text::Draw(aPresentation,aComeAspect,aText, OrigineCome);
Prs3d_Text::Draw(aPresentation,aTextAspect,aTCoText, OrigineDimZT);

aPresentation->Display();

because the parameter of Prs3d_Presentation is a Graphic3d_StructureManager not a viewer() like you wrote before...anyway, I did that but a segmentation fault appears, any suggestions?

HELP PLEASE!!!

thank you.

Denis Teissandier's picture

I can not answer this question.
I never use the Prs3d_Text class like that.

I always use an AIS_InteractiveContext to manage my wiewer with one V3d_Viewer.

The Graphic3d_StructureManager is the root class of Visual3d_ViewManager. I always instanciate the Visual3d_ViewManager like that :

AIS_InteractiveContext aContext;
---------
---------
aPresentation= new Prs3d_Presentation(aContext->CurrentViewer()->Viewer());

Denis

cheng's picture

If the text is chinese or other text? How to do this?

ATTIA's picture

Hello,
I have used yhis method but I can't display the coordinates of point in the corner of my Frame.
There is someone who can help me?

Regards

ATTIA's picture

I have used these lines:
Handle_InteractiveText itext = new InteractiveText ("viviana", Point3D (0, 0, 0));
ais_context->Display (itext, true);
but the world viviana displayed in the middle and I would that the world vivana is diplayed in the corner of my Frame.

Thanks

EricThompson's picture

If all you want is to draw some text at a fixed screen location, and the text does not need to be selectable, you might do better working with a Visual3d_Layer.

I create one using:

Handle(Aspect_Window) hWin = mView->Window();
int h,w;
hWin->Size(w,h);
mTextLayer=new Visual3d_Layer(mView->View()->ViewManager(), Aspect_TOL_OVERLAY, Standard_False);
if( mTextLayer.IsNull())
{
assert(0);
return;
}
mTextLayer->SetViewport(w,h);
mTextLayer->SetOrtho(0,w,h,0,(Aspect_TypeOfConstraint) -1);

and draw text on it using:

mTextLayer->Begin();
mTextLayer->SetTextAttributes( Graphic3d_NOF_ASCII_MONO, Aspect_TODT_NORMAL, Quantity_NOC_BLACK );
mTextLayer->SetColor( Quantity_Color( Quantity_NOC_BLACK ));

char* str = "My text";
int xPix = 10;
int yPix = 10;
mTextLayer->DrawText( str, xPix, yPix, 12 );

mTextLayer->End();

This lets you position the text in screen coordinates instead of model coordinates.

ATTIA's picture

I work with myAISContext. For this reason I have not the method window.
when I run your code, an error indicating that appears mview is undefined