1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : GEOM_Displayer.cxx
25 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
27 #include "GEOM_Displayer.h"
29 #include "GeometryGUI.h"
31 #include <GEOM_TypeFilter.h>
32 #include <GEOM_EdgeFilter.h>
33 #include <GEOM_FaceFilter.h>
34 #include <GEOM_CompoundFilter.h>
35 #include <GEOM_PreviewFilter.h>
36 #include <GEOM_LogicalFilter.h>
37 #include <GEOM_OCCFilter.h>
39 #include <GEOM_Actor.h>
40 #include <GEOM_AssemblyBuilder.h>
41 #include <GEOM_AISShape.hxx>
42 #include <GEOM_AISVector.hxx>
43 #include <GEOM_AISTrihedron.hxx>
44 #include <GEOM_VTKTrihedron.hxx>
46 #include <SUIT_Desktop.h>
47 #include <SUIT_ViewWindow.h>
48 #include <SUIT_Session.h>
49 #include <SUIT_ViewManager.h>
50 #include <SUIT_ResourceMgr.h>
52 #include <SalomeApp_Study.h>
53 #include <SalomeApp_Application.h>
54 #include <LightApp_SelectionMgr.h>
55 #include <SalomeApp_TypeFilter.h>
56 #include <SalomeApp_Tools.h>
58 #include <SALOME_ListIteratorOfListIO.hxx>
59 #include <SALOME_ListIO.hxx>
60 #include <SALOME_Prs.h>
63 #include <SOCC_ViewModel.h>
66 #include <SVTK_ViewModel.h>
69 #include <AIS_Drawer.hxx>
70 #include <AIS_ListIteratorOfListOfInteractive.hxx>
71 #include <Prs3d_IsoAspect.hxx>
72 #include <Prs3d_PointAspect.hxx>
73 #include <StdSelect_TypeOfEdge.hxx>
74 #include <StdSelect_TypeOfFace.hxx>
75 #include <TopoDS_Face.hxx>
76 #include <BRep_Tool.hxx>
77 #include <Geom_Plane.hxx>
78 #include <Geom_Axis2Placement.hxx>
80 #include <TColStd_MapOfInteger.hxx>
81 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
82 #include <TopoDS_Iterator.hxx>
83 #include <Graphic3d_AspectMarker3d.hxx>
84 #include <TopTools_MapOfShape.hxx>
85 #include <TopTools_ListOfShape.hxx>
86 #include <TopTools_ListIteratorOfListOfShape.hxx>
89 #include <vtkActorCollection.h>
90 #include <vtkProperty.h>
93 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
95 #include <GEOMImpl_Types.hxx>
96 #include <Graphic3d_HArray1OfBytes.hxx>
99 //================================================================
100 // Function : getActiveStudy
101 // Purpose : Get active study, returns 0 if no open study frame
102 //================================================================
103 static inline SalomeApp_Study* getActiveStudy()
105 SUIT_Session* session = SUIT_Session::session();
106 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
108 return ( SalomeApp_Study* )app->activeStudy();
112 //================================================================
113 // Function : getTopAbsMode
114 // Purpose : Get TopAbs_ShapeEnum value corresponding to the
115 // one from GEOMImpl_Types.h
116 //================================================================
117 static inline int getTopAbsMode( const int implType )
121 case GEOM_COMPOUND : return TopAbs_COMPOUND;
122 case GEOM_SOLID : return TopAbs_SOLID;
123 case GEOM_SHELL : return TopAbs_SHELL;
124 case GEOM_FACE : return TopAbs_FACE;
125 case GEOM_WIRE : return TopAbs_WIRE;
126 case GEOM_EDGE : return TopAbs_EDGE;
127 case GEOM_POINT : return TopAbs_VERTEX;
132 static bool isCompoundOfVertices( const TopoDS_Shape& theShape )
135 if ( !theShape.IsNull() ) {
136 int iType, nbTypes [TopAbs_SHAPE];
137 for (iType = 0; iType < TopAbs_SHAPE; ++iType)
139 nbTypes[theShape.ShapeType()]++;
141 TopTools_MapOfShape aMapOfShape;
142 aMapOfShape.Add(theShape);
143 TopTools_ListOfShape aListOfShape;
144 aListOfShape.Append(theShape);
146 TopTools_ListIteratorOfListOfShape itL (aListOfShape);
147 for (; itL.More(); itL.Next()) {
148 TopoDS_Iterator it (itL.Value());
149 for (; it.More(); it.Next()) {
150 TopoDS_Shape s = it.Value();
151 if (aMapOfShape.Add(s)) {
152 aListOfShape.Append(s);
153 nbTypes[s.ShapeType()]++;
157 ret = nbTypes[TopAbs_VERTEX] > 0 && nbTypes[TopAbs_COMPOUND] == 1;
162 //================================================================
163 // Function : getFilter
164 // Purpose : Get filter corresponding to the type of object
165 // from GEOMImpl_Types.h
166 //================================================================
167 SUIT_SelectionFilter* GEOM_Displayer::getFilter( const int theMode )
169 SUIT_SelectionFilter* aFilter;
171 int aTopAbsMode = getTopAbsMode( theMode );
172 if ( aTopAbsMode != -1 )
173 aFilter = new GEOM_TypeFilter( getStudy(), aTopAbsMode, true ); //@ aFilter = new GEOM_TypeFilter( ( TopAbs_ShapeEnum )aTopAbsMode );
177 case GEOM_LINE : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Line ); break;
178 case GEOM_CIRCLE : aFilter = new GEOM_EdgeFilter( getStudy(), StdSelect_Circle ); break;
180 case GEOM_PLANE : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Plane ); break;
181 case GEOM_CYLINDER : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cylinder ); break;
182 case GEOM_SPHERE : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Sphere ); break;
183 case GEOM_TORUS : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Torus ); break;
184 case GEOM_REVOLUTION: aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Revol ); break;
185 case GEOM_CONE : aFilter = new GEOM_FaceFilter( getStudy(), StdSelect_Cone ); break;
187 case GEOM_PREVIEW : aFilter = new GEOM_PreviewFilter( getStudy() ); break;
189 case GEOM_ALLSHAPES : aFilter = new GEOM_SelectionFilter(getStudy(), true ); break;
190 case GEOM_ALLGEOM : aFilter = new SalomeApp_TypeFilter( getStudy(), "GEOM" ); break;
192 default : aFilter = new GEOM_TypeFilter( getStudy(), theMode ); break;
198 //================================================================
199 // Function : getComplexFilter
200 // Purpose : Get compound filter corresponding to the type of
201 // object from GEOMImpl_Types.h
202 //================================================================
203 SUIT_SelectionFilter* GEOM_Displayer::getComplexFilter( const QList<int>* aSubShapes)
205 GEOM_CompoundFilter* aFilter;
207 if(aSubShapes != NULL ) {
208 aFilter = new GEOM_CompoundFilter(getStudy());
209 QList<int> aTopAbsTypes;
210 QList<int>::const_iterator it;
211 for(it = aSubShapes->constBegin(); it != aSubShapes->constEnd(); ++it ) {
212 int topAbsMode = getTopAbsMode(*it);
213 if(topAbsMode != -1 )
214 aTopAbsTypes.append(topAbsMode);
216 aFilter->addSubTypes(aTopAbsTypes);
222 //================================================================
223 // Function : getEntry
225 //================================================================
226 static std::string getEntry( GEOM::GEOM_Object_ptr object )
228 SUIT_Session* session = SUIT_Session::session();
229 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
232 CORBA::String_var IOR = app->orb()->object_to_string( object );
233 if ( strcmp(IOR.in(), "") != 0 )
235 SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
236 _PTR(SObject) SO ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
244 //================================================================
245 // Function : getName
247 //================================================================
248 static std::string getName( GEOM::GEOM_Object_ptr object )
250 SUIT_Session* session = SUIT_Session::session();
251 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
254 CORBA::String_var IOR = app->orb()->object_to_string( object );
255 if ( strcmp(IOR.in(), "") != 0 )
257 SalomeApp_Study* study = ( SalomeApp_Study* )app->activeStudy();
258 _PTR(SObject) aSObj ( study->studyDS()->FindObjectIOR( std::string(IOR) ) );
260 _PTR(GenericAttribute) anAttr;
262 if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") )
264 _PTR(AttributeName) aNameAttr( anAttr );
265 return aNameAttr->Value();
273 //=================================================================
275 * GEOM_Displayer::GEOM_Displayer
278 //=================================================================
279 GEOM_Displayer::GEOM_Displayer( SalomeApp_Study* st )
282 myApp = dynamic_cast<SalomeApp_Application*>( st->application() );
287 SUIT_Session* session = SUIT_Session::session();
288 SUIT_ResourceMgr* resMgr = session->resourceMgr();
290 QColor col = resMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
291 myShadingColor = SalomeApp_Tools::color( col );
293 myDisplayMode = resMgr->integerValue("Geometry", "display_mode", 0);
294 int aType = resMgr->integerValue("Geometry", "type_of_marker", (int)Aspect_TOM_PLUS);
295 myTypeOfMarker = (Aspect_TypeOfMarker)(std::min((int)Aspect_TOM_RING3, std::max((int)Aspect_TOM_POINT, aType)));
296 myScaleOfMarker = (resMgr->integerValue("Geometry", "marker_scale", 1)-(int)GEOM::MS_10)*0.5 + 1.0;
297 myScaleOfMarker = std::min(7.0, std::max(1., myScaleOfMarker));
300 // This color is used for shape displaying. If it is equal -1 then
301 // default color is used.
307 // This parameter is used for activisation/deactivisation of objects to be displayed
312 //=================================================================
314 * GEOM_Displayer::~GEOM_Displayer
317 //=================================================================
318 GEOM_Displayer::~GEOM_Displayer()
322 //=================================================================
324 * GEOM_Displayer::Display
325 * Display interactive object in the current viewer
327 //=================================================================
328 void GEOM_Displayer::Display( const Handle(SALOME_InteractiveObject)& theIO,
329 const bool updateViewer,
330 SALOME_View* theViewFrame )
332 SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
335 SALOME_Prs* prs = buildPresentation( theIO->getEntry(), vf );
339 vf->BeforeDisplay( this );
341 vf->AfterDisplay( this );
346 delete prs; // delete presentation because displayer is its owner
351 //=================================================================
353 * GEOM_Displayer::Display
354 * This overloaded Display() method can be useful for operations
355 * not using dialog boxes.
357 //=================================================================
358 void GEOM_Displayer::Display( GEOM::GEOM_Object_ptr theObj, const bool updateViewer )
360 if ( theObj->_is_nil() )
363 std::string entry = getEntry( theObj );
365 Display(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
370 //=================================================================
372 * GEOM_Displayer::Erase
373 * Erase interactive object in the current viewer
375 //=================================================================
376 void GEOM_Displayer::Erase( const Handle(SALOME_InteractiveObject)& theIO,
378 const bool updateViewer,
379 SALOME_View* theViewFrame )
381 if ( theIO.IsNull() )
384 SALOME_View* vf = theViewFrame ? theViewFrame : GetActiveView();
387 SALOME_Prs* prs = vf->CreatePrs( theIO->getEntry() );
389 vf->Erase( prs, forced );
392 delete prs; // delete presentation because displayer is its owner
397 //=================================================================
399 * GEOM_Displayer::Erase
400 * Erase geometry object in the current viewer
402 //=================================================================
403 void GEOM_Displayer::Erase( GEOM::GEOM_Object_ptr theObj,
405 const bool updateViewer )
407 std::string entry = getEntry( theObj );
410 Erase(new SALOME_InteractiveObject(entry.c_str(), "GEOM", getName(theObj).c_str()),
411 forced, updateViewer);
415 //=================================================================
417 * GEOM_Displayer::Redisplay
418 * Redisplay (erase and then display again) interactive object
419 * in the current viewer
421 //=================================================================
422 void GEOM_Displayer::Redisplay( const Handle(SALOME_InteractiveObject)& theIO,
423 const bool updateViewer )
425 // Remove the object permanently (<forced> == true)
426 SUIT_Session* ses = SUIT_Session::session();
427 SUIT_Application* app = ses->activeApplication();
430 SUIT_Desktop* desk = app->desktop();
431 QList<SUIT_ViewWindow*> wnds = desk->windows();
432 SUIT_ViewWindow* wnd;
433 QListIterator<SUIT_ViewWindow*> it( wnds );
434 while ( it.hasNext() && (wnd = it.next()) )
436 SUIT_ViewManager* vman = wnd->getViewManager();
439 SUIT_ViewModel* vmodel = vman->getViewModel();
442 SALOME_View* view = dynamic_cast<SALOME_View*>(vmodel);
445 if ( view->isVisible( theIO ) || view == GetActiveView() )
447 Erase( theIO, true, false, view );
448 Display( theIO, updateViewer, view );
457 //=================================================================
459 * GEOM_Displayer::Display
460 * Calls Display() method for each object in the given list
462 //=================================================================
463 void GEOM_Displayer::Display( const SALOME_ListIO& theIOList, const bool updateViewer )
465 SALOME_ListIteratorOfListIO Iter( theIOList );
466 for ( ; Iter.More(); Iter.Next() ) {
467 Display( Iter.Value(), false );
473 //=================================================================
475 * GEOM_Displayer::Erase
476 * Calls Erase() method for each object in the given list
478 //=================================================================
479 void GEOM_Displayer::Erase( const SALOME_ListIO& theIOList,
481 const bool updateViewer )
483 SALOME_ListIteratorOfListIO Iter( theIOList );
484 for ( ; Iter.More(); Iter.Next() )
485 Erase( Iter.Value(), forced, false );
491 //=================================================================
493 * GEOM_Displayer::Redisplay
494 * Calls Redisplay() method for each object in the given list
496 //=================================================================
497 void GEOM_Displayer::Redisplay( const SALOME_ListIO& theIOList, const bool updateViewer )
499 SALOME_ListIteratorOfListIO Iter( theIOList );
500 for ( ; Iter.More(); Iter.Next() )
501 Redisplay( Iter.Value(), false );
507 //=================================================================
509 * GEOM_Displayer::Update
510 * Update OCC presentaion
511 * [ Reimplemented from SALOME_Displayer ]
513 //=================================================================
514 void GEOM_Displayer::Update( SALOME_OCCPrs* prs )
516 SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( prs );
520 if ( myType == GEOM_MARKER && !myShape.IsNull() && myShape.ShapeType() == TopAbs_FACE )
522 TopoDS_Face aFace = TopoDS::Face( myShape );
523 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( aFace ) );
524 if ( !aPlane.IsNull() )
526 gp_Ax3 aPos = aPlane->Pln().Position();
527 Handle(Geom_Axis2Placement) aPlc = new Geom_Axis2Placement( aPos.Ax2() );
529 Handle(GEOM_AISTrihedron) aTrh;
531 if ( occPrs->IsNull() )
533 aTrh = new GEOM_AISTrihedron( aPlc );
536 aTrh->SetColor( (Quantity_NameOfColor)GetColor() );
539 aTrh->SetWidth( GetWidth() );
541 if ( !myIO.IsNull() )
544 aTrh->SetOwner( myIO );
547 occPrs->AddObject( aTrh );
551 AIS_ListOfInteractive aList;
552 occPrs->GetObjects( aList );
553 AIS_ListIteratorOfListOfInteractive anIter( aList );
554 for ( ; anIter.More(); anIter.Next() )
556 aTrh = Handle(GEOM_AISTrihedron)::DownCast( anIter.Value() );
557 if ( !aTrh.IsNull() )
559 aTrh->SetComponent( aPlc );
565 occPrs->SetToActivate( ToActivate() );
570 // if presentation is empty we try to create new one
571 if ( occPrs->IsNull() )
573 if ( !myShape.IsNull() )
575 //Handle(GEOM_AISShape) AISShape = new GEOM_AISShape( myShape, "" );
576 Handle(GEOM_AISShape) AISShape;
577 if (myType == GEOM_VECTOR)
578 AISShape = new GEOM_AISVector (myShape, "");
580 if (myShape.ShapeType() != TopAbs_VERTEX && // fix pb with not displayed points
581 !TopoDS_Iterator(myShape).More())
582 return;// NPAL15983 (Bug when displaying empty groups)
583 AISShape = new GEOM_AISShape (myShape, "");
585 // Temporary staff: vertex must be infinite for correct visualization
586 AISShape->SetInfiniteState( myShape.Infinite() ); // || myShape.ShapeType() == TopAbs_VERTEX // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
588 // Setup shape properties here ..., e.g. display mode, color, transparency, etc
589 AISShape->SetDisplayMode( myDisplayMode );
590 AISShape->SetShadingColor( myShadingColor );
592 // Set color and number for iso lines
593 SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
594 QColor col = aResMgr->colorValue( "Geometry", "isos_color",
595 QColor(int(0.5*255), int(0.5*255), int(0.5*255)) );
596 Quantity_Color aColor = SalomeApp_Tools::color( col );
597 int anUIsoNumber = aResMgr->integerValue("OCCViewer", "iso_number_u", 1);
598 int aVIsoNumber = aResMgr->integerValue("OCCViewer", "iso_number_v", 1);
600 Handle(Prs3d_IsoAspect) anAspect = AISShape->Attributes()->UIsoAspect();
601 anAspect->SetNumber( anUIsoNumber );
602 anAspect->SetColor( aColor );
603 AISShape->Attributes()->SetUIsoAspect( anAspect );
605 anAspect = AISShape->Attributes()->VIsoAspect();
606 anAspect->SetNumber( aVIsoNumber );
607 anAspect->SetColor( aColor );
608 AISShape->Attributes()->SetVIsoAspect( anAspect );
612 AISShape->SetColor( (Quantity_NameOfColor)GetColor() );
613 Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
614 anAspect->SetColor( (Quantity_NameOfColor)GetColor() );
615 if ( myShape.ShapeType() == TopAbs_VERTEX || isCompoundOfVertices( myShape ) )
617 anAspect->SetScale( myScaleOfMarker );
618 anAspect->SetTypeOfMarker( myTypeOfMarker );
620 AISShape->Attributes()->SetPointAspect( anAspect );
624 if ( myShape.ShapeType() == TopAbs_VERTEX || isCompoundOfVertices( myShape ) )
626 col = aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) );
627 aColor = SalomeApp_Tools::color( col );
629 Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
630 anAspect->SetColor( aColor );
631 anAspect->SetScale( myScaleOfMarker );
632 anAspect->SetTypeOfMarker( myTypeOfMarker );
633 AISShape->Attributes()->SetPointAspect( anAspect );
638 col = aResMgr->colorValue( "Geometry", "wireframe_color", QColor( 255, 255, 0 ) );
639 aColor = SalomeApp_Tools::color( col );
641 Handle(Prs3d_LineAspect) anAspect = AISShape->Attributes()->LineAspect();
642 anAspect->SetColor( aColor );
643 AISShape->Attributes()->SetLineAspect( anAspect );
645 // Set unfree boundaries aspect
646 anAspect = AISShape->Attributes()->UnFreeBoundaryAspect();
647 anAspect->SetColor( aColor );
648 AISShape->Attributes()->SetUnFreeBoundaryAspect( anAspect );
650 // Set free boundaries aspect
651 col = aResMgr->colorValue( "Geometry", "free_bound_color", QColor( 0, 255, 0 ) );
652 aColor = SalomeApp_Tools::color( col );
654 anAspect = AISShape->Attributes()->FreeBoundaryAspect();
655 anAspect->SetColor( aColor );
656 AISShape->Attributes()->SetFreeBoundaryAspect( anAspect );
659 col = aResMgr->colorValue( "Geometry", "line_color", QColor( 255, 0, 0 ) );
660 aColor = SalomeApp_Tools::color( col );
662 anAspect = AISShape->Attributes()->WireAspect();
663 anAspect->SetColor( aColor );
664 AISShape->Attributes()->SetWireAspect( anAspect );
666 // bug [SALOME platform 0019868]
667 // Set deviation angle. Default one is 12 degrees (Prs3d_Drawer.cxx:18)
668 //AISShape->SetOwnDeviationAngle( 10*PI/180 );
671 double aDC = aResMgr->doubleValue("Geometry", "deflection_coeff", 0.001);
672 aDC = std::max( aDC, DEFLECTION_MIN ); // to avoid to small values of the coefficient
673 AISShape->SetOwnDeviationCoefficient(aDC);
678 AISShape->SetWidth( GetWidth() );
680 if ( !myIO.IsNull() )
682 AISShape->setIO( myIO );
683 AISShape->SetOwner( myIO );
685 else if ( !myName.empty() )
687 // Workaround to allow selection of temporary objects
688 static int tempId = 0;
690 sprintf( buf, "TEMP_%d", tempId++ );
691 Handle( SALOME_InteractiveObject ) anObj =
692 new SALOME_InteractiveObject( buf, "GEOM", myName.c_str() );
693 AISShape->setIO( anObj );
694 AISShape->SetOwner( anObj );
697 // Get color and other properties from GEOM_Object
698 SUIT_Session* session = SUIT_Session::session();
699 SUIT_Application* app = session->activeApplication();
702 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
705 Handle( SALOME_InteractiveObject ) anIO = AISShape->getIO();
706 if ( !anIO.IsNull() )
708 _PTR(SObject) SO ( study->studyDS()->FindObjectID( anIO->getEntry() ) );
711 // get CORBA reference to data object
712 CORBA::Object_var object = GeometryGUI::ClientSObjectToObject(SO);
713 if ( !CORBA::is_nil( object ) )
715 // downcast to GEOM object
716 GEOM::GEOM_Object_var aGeomObject = GEOM::GEOM_Object::_narrow( object );
717 if ( !aGeomObject->_is_nil() )
719 SALOMEDS::Color aSColor = aGeomObject->GetColor();
720 bool hasColor = aSColor.R >= 0 && aSColor.G >= 0 && aSColor.B >= 0;
721 if( !hasColor && aGeomObject->GetType() == GEOM_GROUP ) // auto color for group
723 GEOM::GEOM_Gen_var aGeomGen = GeometryGUI::GetGeomGen();
724 GEOM::GEOM_IGroupOperations_var anOperations = aGeomGen->GetIGroupOperations( study->id() );
725 GEOM::GEOM_Object_var aMainObject = anOperations->GetMainShape( aGeomObject );
726 if ( !aMainObject->_is_nil() && aMainObject->GetAutoColor() )
728 QList<SALOMEDS::Color> aReservedColors;
730 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( app );
731 CORBA::String_var IOR = app->orb()->object_to_string( aMainObject );
732 if ( strcmp(IOR.in(), "") != 0 )
734 _PTR(Study) aStudy = study->studyDS();
735 _PTR(SObject) aMainSObject( aStudy->FindObjectIOR( std::string(IOR) ) );
736 _PTR(ChildIterator) it( aStudy->NewChildIterator( aMainSObject ) );
737 for( ; it->More(); it->Next() )
739 _PTR(SObject) aChildSObject( it->Value() );
740 GEOM::GEOM_Object_var aChildObject =
741 GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aChildSObject));
742 if( CORBA::is_nil( aChildObject ) )
745 if( aChildObject->GetType() != GEOM_GROUP )
748 SALOMEDS::Color aReservedColor = aChildObject->GetColor();
749 aReservedColors.append( aReservedColor );
753 aSColor = getUniqueColor( aReservedColors );
760 Quantity_Color aQuanColor( aSColor.R, aSColor.G, aSColor.B, Quantity_TOC_RGB );
761 AISShape->SetColor( aQuanColor );
762 AISShape->SetShadingColor( aQuanColor );
763 if ( myShape.ShapeType() == TopAbs_VERTEX || isCompoundOfVertices( myShape ) ) {
764 Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
765 anAspect->SetColor( aQuanColor );
766 anAspect->SetScale( myScaleOfMarker );
767 anAspect->SetTypeOfMarker( myTypeOfMarker );
768 AISShape->Attributes()->SetPointAspect( anAspect );
772 GEOM::marker_type aType = aGeomObject->GetMarkerType();
773 GEOM::marker_size aSize = aGeomObject->GetMarkerSize();
774 if ( aType > GEOM::MT_NONE && aType < GEOM::MT_USER && aSize > GEOM::MS_NONE && aSize <= GEOM::MS_70 ) {
775 Aspect_TypeOfMarker aMType = (Aspect_TypeOfMarker)( (int)aType-1 );
776 double aMSize = ((int)aSize+1)*0.5;
777 Handle(Prs3d_PointAspect) anAspect = AISShape->Attributes()->PointAspect();
778 anAspect->SetScale( aMSize );
779 anAspect->SetTypeOfMarker( aMType );
780 Quantity_Color aQuanColor = SalomeApp_Tools::color( aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) ) );
782 aQuanColor = Quantity_Color( aSColor.R, aSColor.G, aSColor.B, Quantity_TOC_RGB );
783 anAspect->SetColor( aQuanColor );
784 AISShape->Attributes()->SetPointAspect( anAspect );
786 else if ( aType == GEOM::MT_USER ) {
787 int aTextureId = aGeomObject->GetMarkerTexture();
788 Quantity_Color aQuanColor = SalomeApp_Tools::color( aResMgr->colorValue( "Geometry", "point_color", QColor( 255, 255, 0 ) ) );
789 if ( hasColor ) aQuanColor = Quantity_Color( aSColor.R, aSColor.G, aSColor.B, Quantity_TOC_RGB );
790 Standard_Integer aWidth, aHeight;
791 Handle(Graphic3d_HArray1OfBytes) aTexture = GeometryGUI::getTexture( getStudy(), aTextureId, aWidth, aHeight );
792 if ( !aTexture.IsNull() ) {
793 static int TextureId = 0;
794 Handle(Prs3d_PointAspect) aTextureAspect = new Prs3d_PointAspect(aQuanColor,
798 AISShape->Attributes()->SetPointAspect( aTextureAspect );
808 // AISShape->SetName(???); ??? necessary to set name ???
809 occPrs->AddObject( AISShape );
811 // In accordance with ToActivate() value object will be activated/deactivated
812 // when it will be displayed
813 occPrs->SetToActivate( ToActivate() );
816 // if presentation is found -> set again shape for it
819 if ( !myShape.IsNull() )
821 AIS_ListOfInteractive IOList;
822 occPrs->GetObjects( IOList );
823 AIS_ListIteratorOfListOfInteractive Iter( IOList );
824 for ( ; Iter.More(); Iter.Next() )
826 Handle(GEOM_AISShape) AISShape = Handle(GEOM_AISShape)::DownCast( Iter.Value() );
827 if ( AISShape.IsNull() )
829 if ( AISShape->Shape() != myShape )
831 AISShape->Set( myShape );
832 AISShape->UpdateSelection();
833 AISShape->SetToUpdate();
835 if ( !myIO.IsNull() )
837 AISShape->setIO( myIO );
838 AISShape->SetOwner( myIO );
846 //=================================================================
848 * GEOM_Displayer::Update
849 * Update VTK presentaion
850 * [ Reimplemented from SALOME_Displayer ]
852 //=================================================================
853 void GEOM_Displayer::Update( SALOME_VTKPrs* prs )
855 SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( prs );
856 if ( !vtkPrs || myShape.IsNull() )
859 vtkActorCollection* theActors = 0;
861 if ( myType == GEOM_MARKER && myShape.ShapeType() == TopAbs_FACE )
863 //myToActivate = false; // ouv: commented to make the trihedron pickable (see IPAL18657)
864 GEOM_VTKTrihedron* aTrh = GEOM_VTKTrihedron::New();
868 Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
869 aTrh->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
872 Handle(Geom_Plane) aPlane =
873 Handle(Geom_Plane)::DownCast( BRep_Tool::Surface( TopoDS::Face( myShape ) ) );
874 if ( aPlane.IsNull() )
877 gp_Ax2 anAx2 = aPlane->Pln().Position().Ax2();
878 aTrh->SetPlacement( new Geom_Axis2Placement( anAx2 ) );
880 // if ( SVTK_Viewer* vf = dynamic_cast<SVTK_Viewer*>( GetActiveView() ) )
881 // aTrh->SetSize( 0.5 * vf->GetTrihedronSize() );
883 vtkPrs->AddObject( aTrh );
885 theActors = vtkActorCollection::New();
886 theActors->AddItem( aTrh );
890 theActors = GEOM_AssemblyBuilder::BuildActors( myShape, 0, 0, Standard_True, myType == GEOM_VECTOR );
893 theActors->InitTraversal();
895 vtkActor* anActor = (vtkActor*)theActors->GetNextActor();
897 vtkProperty* aProp = 0;
899 if ( HasColor() || HasWidth() )
901 aProp = vtkProperty::New();
902 aProp->SetRepresentationToWireframe();
907 Quantity_Color aColor( (Quantity_NameOfColor)GetColor() );
908 aProp->SetColor( aColor.Red(), aColor.Green(), aColor.Blue() );
913 aProp->SetLineWidth( GetWidth() );
916 while ( anActor != NULL )
918 SALOME_Actor* GActor = SALOME_Actor::SafeDownCast( anActor );
920 GActor->setIO( myIO );
924 GActor->SetProperty( aProp );
925 GActor->SetPreviewProperty( aProp );
927 GEOM_Actor* aGeomGActor = GEOM_Actor::SafeDownCast( anActor );
928 if ( aGeomGActor != 0 )
930 aGeomGActor->SetShadingProperty( aProp );
931 aGeomGActor->SetWireframeProperty( aProp );
936 GActor->PickableOn();
938 GActor->PickableOff();
940 vtkPrs->AddObject( GActor );
942 anActor = (vtkActor*)theActors->GetNextActor();
951 //=================================================================
953 * GEOM_Displayer::BuildPrs
954 * Build presentation accordint to the current viewer type
956 //=================================================================
957 SALOME_Prs* GEOM_Displayer::BuildPrs( GEOM::GEOM_Object_ptr theObj )
959 if ( theObj->_is_nil() )
962 myViewFrame = GetActiveView();
963 if ( myViewFrame == 0 )
966 SALOME_Prs* aPrs = myViewFrame->CreatePrs();
971 setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), theObj ) );
972 myType = theObj->GetType();
974 // Update presentation
980 //=================================================================
982 * GEOM_Displayer::BuildPrs
983 * Build presentation accordint to the current viewer type
985 //=================================================================
986 SALOME_Prs* GEOM_Displayer::BuildPrs( const TopoDS_Shape& theShape )
988 myViewFrame = GetActiveView();
989 if ( theShape.IsNull() || myViewFrame == 0 )
992 SALOME_Prs* aPrs = myViewFrame->CreatePrs();
997 setShape( theShape );
1005 //=================================================================
1007 * GEOM_Displayer::buildPresentation
1008 * Builds/finds object's presentation for the current viewer
1009 * Calls corresponding Update() method by means of double dispatch
1012 //=================================================================
1013 SALOME_Prs* GEOM_Displayer::buildPresentation( const QString& entry,
1014 SALOME_View* theViewFrame )
1016 SALOME_Prs* prs = 0;
1019 myViewFrame = theViewFrame ? theViewFrame : GetActiveView();
1023 prs = LightApp_Displayer::buildPresentation( entry, theViewFrame );
1026 Handle( SALOME_InteractiveObject ) theIO = new SALOME_InteractiveObject();
1027 theIO->setEntry( entry.toLatin1().constData() );
1028 if ( !theIO.IsNull() )
1030 // set interactive object
1032 // Find SOBject (because shape should be published previously)
1033 SUIT_Session* session = SUIT_Session::session();
1034 SUIT_Application* app = session->activeApplication();
1037 SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
1040 _PTR(SObject) SO ( study->studyDS()->FindObjectID( theIO->getEntry() ) );
1043 // get CORBA reference to data object
1044 CORBA::Object_var object = GeometryGUI::ClientSObjectToObject(SO);
1045 if ( !CORBA::is_nil( object ) )
1047 // downcast to GEOM object
1048 GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( object );
1049 if ( !GeomObject->_is_nil() )
1051 // finally set shape
1052 setShape( GEOM_Client::get_client().GetShape( GeometryGUI::GetGeomGen(), GeomObject ) );
1053 myType = GeomObject->GetType();
1060 UpdatePrs( prs ); // Update presentation by using of the double dispatch
1066 //=================================================================
1068 * GEOM_Displayer::buildSubshapePresentation
1069 * Builds/finds object's presentation for the current viewer
1070 * Calls corresponding Update() method by means of double dispatch
1071 * For not published objects (for Mantis issue 0020435)
1073 //=================================================================
1074 SALOME_Prs* GEOM_Displayer::buildSubshapePresentation(const TopoDS_Shape& aShape,
1075 const QString& entry,
1076 SALOME_View* theViewFrame)
1078 SALOME_Prs* prs = 0;
1081 myViewFrame = theViewFrame ? theViewFrame : GetActiveView();
1085 prs = LightApp_Displayer::buildPresentation(entry, theViewFrame);
1088 Handle(SALOME_InteractiveObject) theIO = new SALOME_InteractiveObject();
1089 theIO->setEntry(entry.toLatin1().constData());
1090 if (!theIO.IsNull())
1092 // set interactive object
1094 // finally set shape
1096 myType = GEOM_SUBSHAPE;
1098 UpdatePrs(prs); // Update presentation by using of the double dispatch
1104 //=================================================================
1106 * GEOM_Displayer::internalReset
1107 * Resets internal data
1110 //=================================================================
1111 void GEOM_Displayer::internalReset()
1117 //=================================================================
1119 * GEOM_Displayer::LocalSelection
1120 * Activate selection of CAD shapes with activisation of selection
1121 * of their sub-shapes (with opened local context for OCC viewer)
1123 //=================================================================
1124 void GEOM_Displayer::LocalSelection( const Handle(SALOME_InteractiveObject)& theIO, const int theMode )
1126 SUIT_Session* session = SUIT_Session::session();
1127 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1131 LightApp_SelectionMgr* sm = app->selectionMgr();
1133 // remove all filters from selection
1136 SALOME_View* vf = GetActiveView();
1138 if (!theIO.IsNull() && !vf->isVisible(theIO))
1140 SALOME_Prs* prs = vf->CreatePrs( theIO.IsNull() ? 0 : theIO->getEntry() );
1141 vf->LocalSelection( prs, theMode );
1142 delete prs; // delete presentation because displayer is its owner
1146 //=================================================================
1148 * GEOM_Displayer::globalSelection
1149 * Activate selection of CAD shapes without activisation of selection
1150 * of their sub-shapes (without opened local context for OCC viewer)
1152 //=================================================================
1153 void GEOM_Displayer::GlobalSelection( const int theMode, const bool update )
1155 TColStd_MapOfInteger aModes;
1156 aModes.Add( theMode );
1157 GlobalSelection( aModes, update );
1160 //=================================================================
1162 * GEOM_Displayer::globalSelection
1163 * Activate selection of CAD shapes without activisation of selection
1164 * of their sub-shapes (without opened local context for OCC viewer)
1166 //=================================================================
1167 void GEOM_Displayer::GlobalSelection( const TColStd_MapOfInteger& theModes,
1168 const bool update, const QList<int>* theSubShapes )
1170 SUIT_Session* session = SUIT_Session::session();
1171 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
1175 SALOME_View* vf = GetActiveView();
1179 // Close local context
1180 vf->GlobalSelection( update );
1182 // Set selection filters in accordance with current mode
1183 LightApp_SelectionMgr* sm = app->selectionMgr();
1187 // Remove from selection temporary objects if necessary
1188 if ( !theModes.Contains( GEOM_PREVIEW ) )
1189 clearTemporary( sm );
1191 //@ aSel->ClearIndex();
1195 // Remove filters from AIS_InteractiveContext
1196 Handle(AIS_InteractiveContext) ic;
1197 SOCC_Viewer* viewer = dynamic_cast<SOCC_Viewer*>( vf );
1200 ic = viewer->getAISContext();
1202 ic->RemoveFilters();
1205 if ( theModes.Contains( GEOM_ALLOBJECTS ) )
1208 SUIT_SelectionFilter* aFilter;
1209 if ( theModes.Extent() == 1 )
1211 int aMode = TColStd_MapIteratorOfMapOfInteger( theModes ).Key();
1213 if ( aMode == GEOM_COMPOUNDFILTER )
1214 aFilter = getComplexFilter( theSubShapes );
1216 aFilter = getFilter( aMode );
1218 else if ( theModes.Extent() > 1 )
1220 TColStd_MapOfInteger aTopAbsModes;
1221 TColStd_MapIteratorOfMapOfInteger anIter( theModes );
1222 QList<SUIT_SelectionFilter*> aListOfFilters;
1223 for ( ; anIter.More(); anIter.Next() )
1225 SUIT_SelectionFilter* aFilter;
1226 int aMode = anIter.Key();
1227 if ( aMode == GEOM_COMPOUNDFILTER )
1228 aFilter = getComplexFilter( theSubShapes );
1230 aFilter = getFilter( aMode );
1233 aListOfFilters.append( aFilter );
1236 aFilter = new GEOM_LogicalFilter( aListOfFilters, GEOM_LogicalFilter::LO_OR );
1243 sm->installFilter( aFilter );
1246 Handle(GEOM_OCCFilter) anOCCFilter = new GEOM_OCCFilter( sm );
1247 ic->AddFilter( anOCCFilter );
1252 //=================================================================
1254 * GEOM_Displayer::LocalSelection
1255 * Activate selection of CAD shapes with activisation of selection
1256 * of their sub-shapes (with opened local context for OCC viewer)
1258 //=================================================================
1259 void GEOM_Displayer::LocalSelection( const SALOME_ListIO& theIOList, const int theMode )
1261 SALOME_ListIteratorOfListIO Iter( theIOList );
1262 for ( ; Iter.More(); Iter.Next() )
1263 LocalSelection( Iter.Value(), theMode );
1266 //=================================================================
1268 * GEOM_Displayer::BeforeDisplay
1269 * Called before displaying of pars. Close local context
1270 * [ Reimplemented from SALOME_Displayer ]
1272 //=================================================================
1273 void GEOM_Displayer::BeforeDisplay( SALOME_View* v, const SALOME_OCCViewType& )
1275 SOCC_Viewer* vf = dynamic_cast<SOCC_Viewer*>( v );
1278 Handle(AIS_InteractiveContext) ic = vf->getAISContext();
1281 if ( ic->HasOpenedContext() )
1282 ic->CloseAllContexts();
1287 void GEOM_Displayer::AfterDisplay( SALOME_View*, const SALOME_OCCViewType& )
1292 //=================================================================
1294 * GEOM_Displayer::SetColor
1295 * Set color for shape displaying. If it is equal -1 then default color is used.
1296 * Available values are from Quantity_NameOfColor enumeration
1298 //=================================================================
1299 void GEOM_Displayer::SetColor( const int color )
1306 myShadingColor = Quantity_Color( (Quantity_NameOfColor)color );
1310 int GEOM_Displayer::GetColor() const
1315 bool GEOM_Displayer::HasColor() const
1317 return myColor != -1;
1320 void GEOM_Displayer::UnsetColor()
1324 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1325 QColor col = resMgr->colorValue( "Geometry", "shading_color", QColor( 255, 0, 0 ) );
1326 myShadingColor = SalomeApp_Tools::color( col );
1329 //=================================================================
1331 * GEOM_Displayer::SetWidth
1332 * Set width of shape displaying. If it is equal -1 then default width is used.
1334 //=================================================================
1335 void GEOM_Displayer::SetWidth( const double width )
1340 double GEOM_Displayer::GetWidth() const
1345 bool GEOM_Displayer::HasWidth() const
1347 return myWidth != -1;
1350 void GEOM_Displayer::UnsetWidth()
1355 //=================================================================
1357 * GEOM_Displayer::SetToActivate
1358 * This method is used for activisation/deactivisation of objects to be displayed
1360 //=================================================================
1361 void GEOM_Displayer::SetToActivate( const bool toActivate )
1363 myToActivate = toActivate;
1365 bool GEOM_Displayer::ToActivate() const
1367 return myToActivate;
1370 //=================================================================
1372 * GEOM_Displayer::clearTemporary
1373 * Removes from selection temporary objects
1375 //=================================================================
1376 void GEOM_Displayer::clearTemporary( LightApp_SelectionMgr* theSelMgr )
1378 SALOME_ListIO selected, toSelect;
1379 theSelMgr->selectedObjects( selected );
1381 for ( SALOME_ListIteratorOfListIO it( selected ) ; it.More(); it.Next() ) {
1382 Handle(SALOME_InteractiveObject) io = it.Value();
1383 if ( !io.IsNull() && io->hasEntry() && strncmp( io->getEntry(), "TEMP_", 5 ) != 0 )
1384 toSelect.Append( it.Value() );
1387 theSelMgr->setSelectedObjects( toSelect, true );
1390 void GEOM_Displayer::SetName( const char* theName )
1395 void GEOM_Displayer::UnsetName()
1400 SalomeApp_Study* GEOM_Displayer::getStudy() const
1402 return dynamic_cast<SalomeApp_Study*>( myApp->activeStudy() );
1405 void GEOM_Displayer::setIO( const Handle(SALOME_InteractiveObject)& theIO )
1410 void GEOM_Displayer::setShape( const TopoDS_Shape& theShape )
1415 bool GEOM_Displayer::canBeDisplayed( const QString& /*entry*/, const QString& viewer_type ) const
1417 return viewer_type==SOCC_Viewer::Type() || viewer_type==SVTK_Viewer::Type();
1420 int GEOM_Displayer::SetDisplayMode( const int theMode )
1422 int aPrevMode = myDisplayMode;
1423 if ( theMode != -1 )
1424 myDisplayMode = theMode;
1427 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1428 myDisplayMode = resMgr->integerValue( "Geometry", "display_mode", 0 );
1433 int GEOM_Displayer::GetDisplayMode() const
1435 return myDisplayMode;
1438 int GEOM_Displayer::UnsetDisplayMode()
1440 int aPrevMode = myDisplayMode;
1441 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
1442 myDisplayMode = resMgr->integerValue( "Geometry", "display_mode", 0 );
1446 SALOMEDS::Color GEOM_Displayer::getUniqueColor( const QList<SALOMEDS::Color>& theReservedColors )
1449 int aTolerance = 64;
1450 int anIterations = 0;
1456 if( anIterations % aPeriod == 0 )
1459 if( aTolerance < 1 )
1462 //std::cout << "Iteration N" << anIterations << " (tolerance=" << aTolerance << ")"<< std::endl;
1464 aHue = (int)( 360.0 * rand() / RAND_MAX );
1465 //std::cout << "Hue = " << aHue << std::endl;
1467 //std::cout << "Auto colors : ";
1469 QList<SALOMEDS::Color>::const_iterator it = theReservedColors.constBegin();
1470 QList<SALOMEDS::Color>::const_iterator itEnd = theReservedColors.constEnd();
1471 for( ; it != itEnd; ++it )
1473 SALOMEDS::Color anAutoColor = *it;
1474 QColor aQColor( (int)( anAutoColor.R * 255.0 ), (int)( anAutoColor.G * 255.0 ), (int)( anAutoColor.B * 255.0 ) );
1477 aQColor.getHsv( &h, &s, &v );
1478 //std::cout << h << " ";
1479 if( abs( h - aHue ) < aTolerance )
1482 //std::cout << "break (diff = " << abs( h - aHue ) << ")";
1486 //std::cout << std::endl;
1492 //std::cout << "Hue of the returned color = " << aHue << std::endl;
1494 aColor.setHsv( aHue, 255, 255 );
1496 SALOMEDS::Color aSColor;
1497 aSColor.R = (double)aColor.red() / 255.0;
1498 aSColor.G = (double)aColor.green() / 255.0;
1499 aSColor.B = (double)aColor.blue() / 255.0;