Open CASCADE, the 3D modelling kernel
3D modeling & numerical simulation

Open CASCADEShowroomGet Open CASCADEDeveloper CornerSupportAbout us
Technical overview
Areas of use
Advantages
Public license
FAQ
Screenshots
Shape factory
Shape gallery
Demonstrations
System requirements
What's new
Download Center
CD-ROM
Documentation
Getting started
Forum
Open Source community
Training and e-learning
A la Carte Support
Value-added software
Client Area
Company Profile
Contacts and Locations
Customers
News
Home / Developer Corner / Forum / Text in the Interactive Context

Forum

Text in the Interactive Context
viviana 2005-07-07 23:27
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 2005-07-08 13:37
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 <qstring.h>

# include <AIS_InteractiveObject.hxx>
# include <Handle_Graphic2d_GraphicObject.hxx>
# include <Handle_Prs3d_Presentation.hxx>
# include <Handle_Prs3d_Projector.hxx>
# include <Handle_PrsMgr_PresentationManager2d.hxx>
# include <Handle_PrsMgr_PresentationManager3d.hxx>
# include <Handle_SelectMgr_Selection.hxx>
# include <Standard_DefineHandle.hxx>
# include <Standard_Macro.hxx>

/*! \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 <Aspect_TypeOfText.hxx>
#include <Quantity_Factor.hxx>
#include <Quantity_PlaneAngle.hxx>
#include <SelectMgr_SelectableObject.hxx>
#include <Standard_CString.hxx>
#include <Standard_IStream.hxx>
#include <Standard_Integer.hxx>
#include <Standard_OStream.hxx>
#include <Standard_Real.hxx>
#include <TCollection_AsciiString.hxx>

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 <Graphic2d_Text.hxx>
#include <Select2D_SensitiveBox.hxx>
#include <Graphic2d_Segment.hxx>
#include <OSD_Environment.hxx>
#include <Graphic2d_View.hxx>
#include <Graphic2d_Drawer.hxx>
#include <PrsMgr_PresentationManager2d.hxx>
#include <SelectMgr_Selection.hxx>
#include <Graphic2d_Array1OfVertex.hxx>
#include <Graphic2d_Polyline.hxx>
#include <Graphic2d_Vertex.hxx>
#include <Graphic2d_DisplayList.hxx>
#include <Prs3d_Text.hxx>
#include <gp_Pnt.hxx>


// ------------------- 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 2005-07-11 09:39
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 2005-07-11 13:43
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 2005-07-18 16:09
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 2005-07-18 18:39
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 2005-07-19 17:41
Thank you very much.
viviana 2005-07-22 15:53
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 2005-07-22 16:00
I suposse... I have to use Prs3D_TextAspect in some way???
Denis TEISSANDIER 2005-07-25 10:03
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 2005-07-25 15:36
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 2005-07-27 09:20
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 2008-10-21 13:55
If the text is chinese or other text? How to do this?
 
 
Latest news
  • SALOME version 4.1.4 Public Release
  • Open CASCADE Technology 6.3
  • SolidWorks Import by Datakit

  • © Open CASCADE 2000 - 2009  |  Search  |  Contacts