Visual3d_View::OverLayer()

Hi, I want to draw 2d text or polygon to a layer that is always front and visible. So I tried using Visual3d_View's OverLayer but could not succeed.
mview->View()->OverLayer()->SetColor(aColor);
mview->View()->OverLayer()->DrawText(text,50,50,10);
mview->View()->OverLayer()->DrawRectangle(50,100,100,100);
//mview->Update();
The code above didn't work. Where am I doing mistake?
Thanks

Francois Lauzon's picture

Hello Maili,
one way to do it is to create a layer over a given view:

Handle_V3d_View myView=...;
Handle(Aspect_Window) hWin = myView->Window();
hWin->Size(w,h);
myLayer=new Visual3d_Layer(myView->View()->ViewManager(), Aspect_TOL_OVERLAY, Standard_False);
myLayer->SetViewport(w,h);
myLayer->SetOrtho(0,w,h,0,(Aspect_TypeOfConstraint) -1);

// then you could draw on the given layer
myLayer->Begin();

// draw some text
myLayer->DrawText(aString,xPos, yPos,textSize);

// draw a polyline
myLayer->BeginPolyline();
myLayer->AddVertex(x1, y1, Standard_False);
myLayer->AddVertex(x2, y2);
myLayer->ClosePrimitive();

// draw a filled box
myLayer->SetColor(aColor);
myLayer->DrawRectangle(xPos,yPos,width,height);

...

// and close the layer and redraw
myLayer->End();
myView->Redraw();

Good Luck

Maili's picture

Thanks but I get segmentation fault after creating a new layer. (I can call Begin() Draw..() End() but whether I call them or not, I get segmentation fault after the function that creates layer is terminated)

Jun WANG's picture

Your code works well but a small error in my program: myLayer->DrawText(aString,xPos, yPos,textSize);
My program crash when it run here. I do not konw why? can anybody give me some hints.
Below is my code, please help me the line: "DrawText(...)".

-----------------------------------------------
Handle_V3d_View myView=theApp.GetCurrentView()->GetView();;
Handle(Aspect_Window) hWin = myView->Window();
Standard_Integer w, h;
hWin->Size(w,h);
Visual3d_Layer* myLayer=new Visual3d_Layer(myView->View()->ViewManager(), Aspect_TOL_OVERLAY, Standard_False);
myLayer->SetViewport(w,h);
myLayer->SetOrtho(0,w,h,0,(Aspect_TypeOfConstraint) -1);

// then you could draw on the given layer
myLayer->Begin();

// draw some text
Standard_CString aString = "TEST";
int xPos = w/2;
int yPos = h/2;
int textSize = 1;
myLayer->DrawText(aString, xPos, yPos, textSize);// !!!!! CRASH ERROR !!!!!!!!

// draw a polyline
Standard_Real wn = 2.0, hn = (2.0/Standard_Real(w))*Standard_Real(h);
Standard_Real x1 = -0.9, x2= x1+wn-.2, y1 = -0.9, y2=y1+hn-.2;
myLayer->BeginPolyline();
myLayer->AddVertex(x1, y1, Standard_False);
myLayer->AddVertex(x2, y2);
myLayer->ClosePrimitive();

// draw a filled box
myLayer->SetColor(Quantity_NOC_RED);
myLayer->DrawRectangle(xPos,yPos,12,12);

// and close the layer and redraw
myLayer->End();
myView->Redraw();

-----------------------------------------------

Kazumasa Uno's picture

Before the call DrawText

myLayer->SetTextAttributes( Graphic3d_NOF_ASCII_ITALIC_COMPLEX, Aspect_TODT_NORMAL, Quantity_NOC_ORANGE );

will draw text properly.

but I do not know why the color does not take effect.

Jun WANG's picture

The color setting depends on the color of Layer, instead of the color setted with SetTextAttributes(.,., Quantity_NOC_ORANGE). So you can set the color like:
myLayer->SetColor(Quantity_NOC_ORANGE). Hope it works.