Salome HOME
Copyright update 2021
[modules/geom.git] / src / GEOMBase / GEOMBase.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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, or (at your option) any later version.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : GEOMBase.cxx
25 // Author : Damien COQUERET, Open CASCADE S.A.S.
26 //
27 #include "GEOMBase.h"
28
29 #include <GeometryGUI.h>
30 #include <GEOM_Client.hxx>
31
32 ////  SALOME Includes
33 #include <GEOM_Actor.h>
34 #include <SVTK_ViewModel.h>
35 #include <SVTK_ViewWindow.h>
36 #include <OCCViewer_ViewPort3d.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
39 #include <SOCC_ViewModel.h>
40 #include <SOCC_Prs.h>
41
42 #include <SALOME_ListIO.hxx>
43
44 #include <SUIT_Desktop.h>
45 #include <SUIT_Session.h>
46 #include <SUIT_ViewManager.h>
47 #include <SUIT_ViewWindow.h>
48 #include <SUIT_MessageBox.h>
49 #include <SalomeApp_Application.h>
50 #include <SalomeApp_Study.h>
51
52 // // Open CASCADE Includes
53 #include <BRep_Tool.hxx>
54 #include <BRepAdaptor_Curve.hxx>
55 #include <BRepAdaptor_Surface.hxx>
56 #include <BRepPrimAPI_MakeCone.hxx>
57
58 #include <AIS_ListIteratorOfListOfInteractive.hxx>
59 #include <AIS_ListOfInteractive.hxx>
60
61 #include <TopAbs.hxx>
62 #include <TopExp.hxx>
63 #include <TopTools_IndexedMapOfShape.hxx>
64 #include <TopoDS.hxx>
65
66 #include <Precision.hxx>
67
68 #include <vtkRenderer.h>
69
70 #include <set>
71
72 //=====================================================================================
73 // function : GetShapeFromIOR()
74 // purpose  : Get shape data by the specified IOR
75 //=====================================================================================
76 TopoDS_Shape GEOMBase::GetShapeFromIOR( const QString& IOR )
77 {
78   GEOM::GEOM_Object_var geomObj = GEOMBase::GetObjectFromIOR( IOR );
79   TopoDS_Shape shape;
80   GetShape( geomObj, shape, TopAbs_SHAPE );
81   return shape;
82 }
83
84
85 //=====================================================================================
86 // function : GetIndex()
87 // purpose  : Get the index of a sub-shape in a main shape : index start at 1
88 //=====================================================================================
89 int GEOMBase::GetIndex( const TopoDS_Shape& subshape, const TopoDS_Shape& shape )
90 {
91   int idx = -1;
92   if ( !shape.IsNull() && !subshape.IsNull() ) {
93     TopTools_IndexedMapOfShape anIndices;
94     TopExp::MapShapes( shape, anIndices );
95     if ( anIndices.Contains( subshape ) ) 
96       idx = anIndices.FindIndex( subshape );
97   }
98   return idx;
99 }
100
101
102 //=======================================================================
103 // function : GetTopoFromSelection()
104 // purpose  : Define tds from a single selection and retuen true
105 //=======================================================================
106 TopoDS_Shape GEOMBase::GetTopoFromSelection( const SALOME_ListIO& IObjects )
107 {
108   TopoDS_Shape shape;
109   if ( IObjects.Extent() == 1 ){
110     Handle(SALOME_InteractiveObject) IO = IObjects.First();
111     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
112     if ( IO->hasEntry() && study ) {
113       _PTR(Study) studyDS = study->studyDS();
114       _PTR(SObject) obj( studyDS->FindObjectID( IO->getEntry() ) );
115       _PTR(GenericAttribute) anAttr;
116       if ( obj && obj->FindAttribute( anAttr, "AttributeIOR" ) ) {
117         _PTR(AttributeIOR) anIOR( anAttr );
118         shape = GetShapeFromIOR( anIOR->Value().c_str() );
119       }
120     }
121   }
122   return shape;
123 }
124
125 //=======================================================================
126 // function : GetNameOfSelectedIObjects()
127 // purpose  : Define the name geom++ or other name of mono or multi sel.
128 //=======================================================================
129 int GEOMBase::GetNameOfSelectedIObjects( const SALOME_ListIO& IObjects,
130                                          QString&             name,
131                                          const bool           shapesOnly )
132 {
133   int nbSel = 0;
134   name = ""; // clear output name
135
136   if ( !shapesOnly ) {
137     nbSel = IObjects.Extent();
138     if ( nbSel == 1 ) {
139       Handle(SALOME_InteractiveObject) anIObj = IObjects.First();
140       SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
141       if ( anIObj->hasEntry() && study ) {
142         _PTR(Study) studyDS = study->studyDS();
143         _PTR(SObject) obj( studyDS->FindObjectID( anIObj->getEntry() ) );
144         _PTR(GenericAttribute) anAttr;
145         if ( obj && obj->FindAttribute( anAttr, "AttributeName" ) ) {
146           _PTR(AttributeName) aNameAttr ( anAttr );
147           name = aNameAttr->Value().c_str();
148         }
149       }
150     }
151   }
152   else {
153     GEOM::ListOfGO anObjs;
154     ConvertListOfIOInListOfGO( IObjects, anObjs, shapesOnly );
155     nbSel = anObjs.length();
156     if ( nbSel == 1 )
157       name = GetName( anObjs[ 0 ] );
158   }
159
160   if ( nbSel > 1 )
161     name = QObject::tr( "%1_objects" ).arg( nbSel );
162
163   return nbSel;
164 }
165
166
167 //=================================================================================
168 // function : GetShapeTypeString()
169 // purpose  : for a single shape
170 //=================================================================================
171 QString GEOMBase::GetShapeTypeString(const TopoDS_Shape& shape)
172 {
173   QString aTypeString;
174   if ( !shape.IsNull() ) {
175     switch ( shape.ShapeType() ) {
176     case TopAbs_COMPOUND:
177       {
178         aTypeString = QObject::tr( "GEOM_COMPOUND" );
179         break;
180       }
181     case TopAbs_COMPSOLID:
182       {
183         aTypeString = QObject::tr( "GEOM_COMPOUNDSOLID" );
184         break;
185       }
186     case TopAbs_SOLID:
187       {
188         aTypeString = QObject::tr( "GEOM_SOLID" );
189         break;
190       }
191     case TopAbs_SHELL:
192       {
193         aTypeString = QObject::tr( "GEOM_SHELL" );
194         break;
195       }
196     case TopAbs_FACE:
197       {
198         BRepAdaptor_Surface surf( TopoDS::Face( shape ) );
199         switch ( surf.GetType() ) {
200         case GeomAbs_Plane:
201           {
202             aTypeString = QObject::tr( "GEOM_PLANE" );
203             break;
204           }
205         case GeomAbs_Cylinder:
206           {
207             aTypeString = QObject::tr( "GEOM_SURFCYLINDER" );
208             break;
209           }
210         case GeomAbs_Sphere:
211           {
212             aTypeString = QObject::tr( "GEOM_SURFSPHERE" );
213             break;
214           }
215         case GeomAbs_Torus:
216           {
217             aTypeString = QObject::tr( "GEOM_SURFTORUS" );
218             break;
219           }
220         case GeomAbs_Cone:
221           {
222             aTypeString = QObject::tr( "GEOM_SURFCONE" );
223             break;
224           }
225         default:
226           {
227             aTypeString = QObject::tr( "GEOM_FACE" );
228             break;
229           }
230         }
231         break;
232       }
233     case TopAbs_WIRE:
234       {
235         aTypeString = QObject::tr( "GEOM_WIRE" );
236         break;
237       }
238     case TopAbs_EDGE:
239       {
240         BRepAdaptor_Curve curv( TopoDS::Edge( shape ) );
241         switch ( curv.GetType() ) {
242         case GeomAbs_Line:
243           {
244             aTypeString = ( qAbs( curv.FirstParameter() ) >= 1E6 || qAbs( curv.LastParameter() ) >= 1E6 ) ?
245               QObject::tr( "GEOM_LINE" ) : QObject::tr( "GEOM_EDGE" );
246             break;
247           }
248         case GeomAbs_Circle:
249           {
250             aTypeString = curv.IsClosed() ? QObject::tr( "GEOM_CIRCLE" ) : QObject::tr( "GEOM_ARC" );
251             break;
252           }
253         default:
254           {
255             aTypeString = QObject::tr( "GEOM_EDGE" );
256             break;
257           }
258         }
259         break;
260       }
261     case TopAbs_VERTEX:
262       {
263         aTypeString = QObject::tr( "GEOM_VERTEX" );
264         break;
265       }
266     case TopAbs_SHAPE:
267       {
268         aTypeString = QObject::tr( "GEOM_SHAPE" );
269         break;
270       }
271     default:
272       {
273         break;
274       }
275     }
276   }
277   return aTypeString;
278 }
279
280
281 //=======================================================================
282 // function : ConvertIORinGEOMAISShape()
283 // purpose  :
284 //=======================================================================
285 Handle(GEOM_AISShape) GEOMBase::ConvertIORinGEOMAISShape(const QString& IOR, bool onlyInActiveView)
286 {
287   Handle(GEOM_AISShape) shape;
288
289   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
290   if ( study ) {
291     _PTR(Study) studyDS = study->studyDS();
292     _PTR(SObject) obj( studyDS->FindObjectIOR( IOR.toLatin1().constData() ) );
293     if ( obj ) {
294       QList<SUIT_ViewWindow*> views;
295       if ( onlyInActiveView ) 
296         views.append( SUIT_Session::session()->activeApplication()->desktop()->activeWindow() );
297       else 
298         views = SUIT_Session::session()->activeApplication()->desktop()->windows();
299       foreach ( SUIT_ViewWindow* view, views ) {
300         if ( view && view->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
301           Handle(AIS_InteractiveContext) ic = ((OCCViewer_Viewer*)view->getViewManager()->getViewModel())->getAISContext();
302
303           AIS_ListOfInteractive displayed;
304           ic->DisplayedObjects( displayed );
305           AIS_ListIteratorOfListOfInteractive it( displayed );
306           while ( it.More() && shape.IsNull() ) {
307             if ( it.Value()->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
308               Handle(GEOM_AISShape) sh = Handle(GEOM_AISShape)::DownCast( it.Value() );
309               if ( !sh.IsNull() && sh->hasIO() ) {
310                 Handle(SALOME_InteractiveObject) IO = sh->getIO();
311                 if ( !IO.IsNull() && IO->hasEntry() && obj->GetID() == IO->getEntry() )
312                   shape = sh;
313               }
314             }
315             it.Next();
316           }
317         }
318         if ( !shape.IsNull() ) break;
319       }
320     }
321   }
322   return shape;
323 }
324
325
326 //=======================================================================
327 // function : ConvertIORinGEOMActor()
328 // purpose  :
329 //=======================================================================
330 GEOM_Actor* GEOMBase::ConvertIORinGEOMActor(const QString& IOR, bool onlyInActiveView)
331 {
332   GEOM_Actor* actor = 0;
333
334   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
335   if ( study ) {
336     _PTR(Study) studyDS = study->studyDS();
337     _PTR(SObject) obj( studyDS->FindObjectIOR( IOR.toLatin1().constData() ) );
338     if ( obj ) {
339       QList<SUIT_ViewWindow*> views;
340       if ( onlyInActiveView ) 
341         views.append( SUIT_Session::session()->activeApplication()->desktop()->activeWindow() );
342       else 
343         views = SUIT_Session::session()->activeApplication()->desktop()->windows();
344       foreach ( SUIT_ViewWindow* view, views ) {
345         if ( view && view->getViewManager()->getType() == SVTK_Viewer::Type() ) {
346           SVTK_ViewWindow* aVTKViewWindow = dynamic_cast<SVTK_ViewWindow*>( view );
347           if( !aVTKViewWindow )
348             continue;
349           vtkRenderer* Renderer = aVTKViewWindow->getRenderer();
350           vtkActorCollection* theActors = Renderer->GetActors();
351           theActors->InitTraversal();
352           vtkActor* a = theActors->GetNextActor();
353           while( a && !actor ) {
354             if ( a->IsA( "GEOM_Actor" ) ) {
355               GEOM_Actor* ga = GEOM_Actor::SafeDownCast( a );
356               if ( ga && ga->hasIO() ) {
357                 Handle(SALOME_InteractiveObject) IO = ga->getIO();
358                 if ( !IO.IsNull() && IO->hasEntry() && obj->GetID() == IO->getEntry() )
359                   actor = ga;
360               }
361             }
362             a = theActors->GetNextActor();
363           }
364         }
365         if ( actor ) break;
366       }
367     }
368   }
369   return actor;
370 }
371
372 //=======================================================================
373 // function : GetAIS()
374 // purpose  :
375 //=======================================================================
376 Handle(AIS_InteractiveObject) GEOMBase::GetAIS( const Handle(SALOME_InteractiveObject)& IO,
377                                                 bool onlyInActiveView, bool /*onlyGeom*/ )
378 {
379   Handle(AIS_InteractiveObject) aisObject;
380
381   if ( !IO.IsNull() && IO->hasEntry() ) {
382     QList<SUIT_ViewWindow*> views;
383     if ( onlyInActiveView ) 
384       views.append( SUIT_Session::session()->activeApplication()->desktop()->activeWindow() );
385     else 
386       views = SUIT_Session::session()->activeApplication()->desktop()->windows();
387     
388     foreach ( SUIT_ViewWindow* view, views ) {
389       if ( view && view->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
390         OCCViewer_Viewer* occViewer=(OCCViewer_Viewer*)view->getViewManager()->getViewModel();
391         SOCC_Viewer* soccViewer = dynamic_cast<SOCC_Viewer*>(occViewer);
392         if (soccViewer) {
393           SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( soccViewer->CreatePrs( IO->getEntry() ) );
394           if ( occPrs && !occPrs->IsNull() ) {
395             AIS_ListOfInteractive shapes; occPrs->GetObjects( shapes );
396             if( !shapes.Extent() ) continue;
397             aisObject=shapes.First();
398             delete occPrs;
399           }
400         }
401       }
402       if ( !aisObject.IsNull() ) break;
403     } // foreach
404   }
405
406   return aisObject;
407 }
408
409
410 //=======================================================================
411 // function : ConvertIOinGEOMAISShape()
412 // purpose  :
413 //=======================================================================
414 Handle(GEOM_AISShape) GEOMBase::ConvertIOinGEOMAISShape( const Handle(SALOME_InteractiveObject)& IO, bool onlyInActiveView )
415 {
416   return Handle(GEOM_AISShape)::DownCast( GetAIS( IO, onlyInActiveView, true ) );
417 }
418
419
420 //=======================================================================
421 // function : ConvertListOfIOInListOfIOR()
422 // purpose  :
423 //=======================================================================
424 QStringList GEOMBase::ConvertListOfIOInListOfIOR( const SALOME_ListIO& IObjects )
425 {
426   QStringList iors;
427   SALOME_ListIteratorOfListIO it( IObjects );
428   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
429   if ( study ) {
430     _PTR(Study) studyDS = study->studyDS();
431     for ( ; it.More(); it.Next() ) {
432       GEOM::GEOM_Object_var geomObj = ConvertIOinGEOMObject( it.Value() );
433       if ( !CORBA::is_nil( geomObj ) )
434         iors.append( GetIORFromObject( geomObj ) );
435     }
436   }
437   return iors;
438 }
439
440
441 //=======================================================================
442 // function : ConvertIOinGEOMObject()
443 // purpose  :
444 //=======================================================================
445 GEOM::GEOM_Object_ptr GEOMBase::ConvertIOinGEOMObject( const Handle(SALOME_InteractiveObject)& IO )
446 {
447   GEOM::GEOM_Object_var object;
448
449   if ( !IO.IsNull() && IO->hasEntry() ) {
450     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
451     if ( study ) {
452       _PTR(Study) studyDS = study->studyDS();
453       _PTR(SObject)   obj = studyDS->FindObjectID( IO->getEntry() );
454       if ( GeometryGUI::IsInGeomComponent( obj )) {
455           CORBA::Object_var corbaObj = GeometryGUI::ClientSObjectToObject( obj );
456           object = GEOM::GEOM_Object::_narrow( corbaObj );
457         }
458     }
459   }
460   return object._retn();
461 }
462
463
464 //=======================================================================
465 // function : ConvertListOfIOInListOfGO()
466 // purpose  :
467 //=======================================================================
468 void GEOMBase::ConvertListOfIOInListOfGO( const SALOME_ListIO& IObjects,
469                                           GEOM::ListOfGO&      geomObjects,
470                                           bool                 shapesOnly )
471 {
472   geomObjects.length( 0 );
473
474   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
475   if ( study ) {
476     _PTR(Study) studyDS = study->studyDS();
477
478     geomObjects.length( IObjects.Extent() );
479     SALOME_ListIteratorOfListIO it( IObjects );
480
481     int i = 0;
482     for ( ; it.More(); it.Next() ) {
483       GEOM::GEOM_Object_var geomObj = ConvertIOinGEOMObject( it.Value() );
484       if ( !CORBA::is_nil( geomObj ) && ( !shapesOnly || IsShape( geomObj ) ) )
485         geomObjects[ i++ ] = geomObj;
486     }
487     geomObjects.length( i );
488   }
489 }
490
491 //=================================================================================
492 // function : CreateArrowForLinearEdge()
493 // purpose  : Create a cone topology to be used to display an arrow in the middle
494 //          : of an edge showing its orientation. (For simulation and Viewer OCC only)
495 //=================================================================================
496 TopoDS_Shape GEOMBase::CreateArrowForLinearEdge( const TopoDS_Shape& shape )
497 {
498   TopoDS_Shape ArrowCone;
499
500   SUIT_ViewWindow* view = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
501   if ( view && view->getViewManager()->getType() == OCCViewer_Viewer::Type() && shape.ShapeType() == TopAbs_EDGE ) {
502     Handle(V3d_View) view3d = ((OCCViewer_ViewWindow*)view)->getViewPort()->getView();
503     Standard_Real Width, Height;
504     view3d->Size( Width, Height );
505     const Standard_Real aHeight = (Width + Height) / 50.0;
506
507     try {
508       Standard_Real first, last;
509       Handle(Geom_Curve) curv = BRep_Tool::Curve( TopoDS::Edge( shape ), first, last );
510       if ( !curv.IsNull() && curv->IsCN(1) ) {
511         const Standard_Real param = ( first+last ) / 2.0;
512         gp_Pnt middleParamPoint;
513         gp_Vec V1;
514         curv->D1( param, middleParamPoint, V1 );
515         if ( V1.Magnitude() > Precision::Confusion() ) {
516           /* Topology orientation not geom orientation */
517           if ( shape.Orientation() == TopAbs_REVERSED )
518             V1 *= -1.0;
519
520           gp_Ax2 anAxis( middleParamPoint, gp_Dir( V1 ) );
521           const Standard_Real radius1 = aHeight / 5.0;
522           if ( radius1 > 10.0 * Precision::Confusion() && aHeight > 10.0 * Precision::Confusion() )
523             ArrowCone = BRepPrimAPI_MakeCone( anAxis, radius1, 0.0, aHeight ).Shape();
524         }
525       }
526     }
527     catch ( Standard_Failure& ) {
528       // OCC failures are hard to catch in GUI.
529       // This is because of the position for #include <Standard_ErrorHandler.hxx> that is very critical to find
530       // in SALOME environment : compilation error !
531     }
532   }
533
534   return ArrowCone;
535 }
536
537
538 //=================================================================================
539 // function : VertexToPoint()
540 // purpose  : If S can be converted in a gp_Pnt returns true and the result is P
541 //=================================================================================
542 bool GEOMBase::VertexToPoint( const TopoDS_Shape& shape, gp_Pnt& point )
543 {
544   if ( shape.IsNull() || shape.ShapeType() != TopAbs_VERTEX )
545     return false;
546   point = BRep_Tool::Pnt( TopoDS::Vertex( shape ) );
547   return true;
548 }
549
550
551 //=================================================================================
552 // function : GetBipointDxDyDz()
553 // purpose  :
554 //=================================================================================
555 void GEOMBase::GetBipointDxDyDz( const gp_Pnt& point1, const gp_Pnt& point2, double& dx, double& dy, double& dz )
556 {
557   dx = point2.X() - point1.X();
558   dy = point2.Y() - point1.Y();
559   dz = point2.Z() - point1.Z();
560 }
561
562
563 //=================================================================================
564 // function : LinearEdgeExtremities()
565 // purpose  : If S can be converted in a linear edge and if initial an final points
566 //          : distance is sufficient, returns true else returns false.
567 //          : Resulting points are respectively P1 and P2
568 //=================================================================================
569 bool GEOMBase::LinearEdgeExtremities( const TopoDS_Shape& shape,  gp_Pnt& point1, gp_Pnt& point2 )
570 {
571   if ( shape.IsNull() || shape.ShapeType() != TopAbs_EDGE )
572     return false;
573
574   BRepAdaptor_Curve curv( TopoDS::Edge( shape ) );
575   if ( curv.GetType() != GeomAbs_Line )
576     return false;
577   
578   gp_Pnt p1, p2;
579
580   curv.D0( curv.FirstParameter(), p1 );
581   curv.D0( curv.LastParameter(),  p2 );
582
583   if ( p1.Distance( p2 ) <= Precision::Confusion() )
584     return false;
585
586   point1 = p1;
587   point2 = p2;
588   return true;
589 }
590
591
592 //=======================================================================
593 // function : SelectionByNameInDialogs()
594 // purpose  : Called when user has entered a name of object in a LineEdit.
595 //          : The selection is changed. Dialog box will receive the
596 //          : corresponding signal to manage this event.
597 //=======================================================================
598 bool GEOMBase::SelectionByNameInDialogs( QWidget* widget, const QString& objectUserName, const SALOME_ListIO& /*IObjects*/ )
599 {
600   /* Find SObject with name in component GEOM */
601   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
602   if ( !study ) return false;
603   _PTR(Study) studyDS = study->studyDS();
604
605   std::vector<_PTR(SObject)> listSO = studyDS->FindObjectByName( objectUserName.toStdString(), "GEOM" );
606
607   if ( listSO.size() < 1 ) {
608     SUIT_MessageBox::critical( widget, 
609                                QObject::tr( "GEOM_WRN_WARNING" ),
610                                QObject::tr( "GEOM_NAME_INCORRECT" ),
611                                QObject::tr( "GEOM_BUT_OK" ) );
612     return false;
613   }
614
615   /* More than one object with same name */
616   if ( listSO.size() > 1 ) {
617     SUIT_MessageBox::critical( widget,
618                                QObject::tr("GEOM_WRN_WARNING"),
619                                QObject::tr("GEOM_IDENTICAL_NAMES_SELECT_BY_MOUSE"),
620                                QObject::tr("GEOM_BUT_OK") );
621     return false;
622   }
623
624   /* Create a SALOME_InteractiveObject with a SALOME::SObject */
625   Handle(SALOME_InteractiveObject) IO = new SALOME_InteractiveObject( listSO[0]->GetID().c_str(),
626                                                                       "GEOM",
627                                                                       objectUserName.toUtf8().constData() );
628
629   /* Add as a selected object       */
630   /* Clear any previous selection : */
631   /* Warning the LineEdit is purged because of signal currentSelectionChanged ! */
632   // Sel->ClearIObjects(); //mzn
633   // Sel->AddIObject(SI); //mzn
634   return true;
635 }
636
637
638 //=======================================================================
639 // function : DefineDlgPosition()
640 // purpose  : Define x and y the default position for a dialog box
641 //=======================================================================
642 void GEOMBase::DefineDlgPosition( QWidget* dlg, int& x, int& y )
643 {
644   /* Here the position is on the bottom right corner - 10 */
645   SUIT_Desktop* d = SUIT_Session::session()->activeApplication()->desktop();
646   x = abs( d->x() + d->size().width()  - dlg->size().width()  - 10 );
647   y = abs( d->y() + d->size().height() - dlg->size().height() - 10 );
648 }
649
650
651 //=======================================================================
652 // function : GetDefaultName()
653 // purpose  : Generates default names
654 //=======================================================================
655 QString GEOMBase::GetDefaultName( const QString& operation, bool extractPrefix )
656 {
657   QString aName = "";
658
659   // collect all object names of GEOM component
660   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
661   if ( study ) {
662     _PTR(Study) studyDS = study->studyDS();
663
664     std::set<std::string> names;
665     _PTR(SComponent) component( studyDS->FindComponent( "GEOM" ) );
666     if ( component ) {
667       _PTR(ChildIterator) it( studyDS->NewChildIterator( component ) );
668       for ( it->InitEx( true ); it->More(); it->Next() ) {
669         names.insert( it->Value()->GetName() );
670       }
671     }
672
673     // build a unique name
674     int aNumber = 0;
675     bool isUnique = false;
676     QString prefix = operation;
677
678     if ( extractPrefix ) {
679       QStringList parts = prefix.split( "_", QString::KeepEmptyParts );
680       if ( parts.count() > 1 ) {
681         bool ok;
682         aNumber = parts.last().toLong( &ok );
683         if ( ok ) {
684           parts.removeLast();
685           prefix = parts.join( "_" );
686           aNumber--;
687         }
688       }
689     }
690     
691     while ( !isUnique ) {
692       aName = prefix + "_" + QString::number( ++aNumber );
693       isUnique = ( names.count( aName.toStdString()) == 0 );
694     }
695   }
696   return aName;
697 }
698
699
700 //=======================================================================
701 // function : ShowErrorMessage()
702 // purpose  : Shows message box with error code and comment
703 //=======================================================================
704 void GEOMBase::ShowErrorMessage( const QString& errorCode, const QString& comment )
705 {
706   QStringList text;
707   text << QObject::tr( "GEOM_PRP_ABORT" );
708   if ( !errorCode.isEmpty() )
709     text << QObject::tr( errorCode.toLatin1().constData() );
710   if ( !comment.isEmpty() )
711     text << QObject::tr( comment.toUtf8().constData() );
712
713   SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(),
714                              QObject::tr( "GEOM_ERROR" ),
715                              text.join( "\n" ),
716                              QObject::tr( "GEOM_BUT_OK" ) );
717 }
718
719
720 //=======================================================================
721 // function : GetObjectFromIOR()
722 // purpose  : returns a GEOM_Object by given IOR (string)
723 //=======================================================================
724 GEOM::GEOM_Object_ptr GEOMBase::GetObjectFromIOR( const QString& IOR )
725 {
726   return GeometryGUI::GetObjectFromIOR (IOR);
727 }
728
729 //=======================================================================
730 // function : GetIORFromObject()
731 // purpose  : returns IOR of a given GEOM_Object
732 //=======================================================================
733 QString GEOMBase::GetIORFromObject( GEOM::GEOM_Object_ptr object )
734 {
735   return GeometryGUI::GetIORFromObject (object);
736 }
737
738 //=======================================================================
739 // function : GetShape()
740 // purpose  : returns a TopoDS_Shape stored in GEOM_Object
741 //=======================================================================
742 bool GEOMBase::GetShape( GEOM::GEOM_Object_ptr object, TopoDS_Shape& shape, const TopAbs_ShapeEnum type )
743 {
744   shape = TopoDS_Shape();
745   if ( !CORBA::is_nil( object ) ) {
746     TopAbs_ShapeEnum stype = (TopAbs_ShapeEnum)( object->GetShapeType() );
747     if ( type == TopAbs_SHAPE || type == stype )
748     {
749       GEOM::GEOM_Gen_var gen = object->GetGen();
750       shape = GEOM_Client::get_client().GetShape( gen, object );
751     }
752   }
753   return !shape.IsNull();
754 }
755
756 //=======================================================================
757 // function : GetName()
758 // purpose  : Get name of object
759 //=======================================================================
760 QString GEOMBase::GetName( GEOM::GEOM_Object_ptr object )
761 {
762   QString name;
763   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
764   
765   if ( !CORBA::is_nil( object ) ) {
766     // 1. search if object is already published in the study
767     CORBA::String_var IOR = SalomeApp_Application::orb()->object_to_string( object );
768     if ( study && strcmp( IOR.in(), "" ) != 0 ) {
769       _PTR(SObject) aSObj( study->studyDS()->FindObjectIOR( std::string( IOR.in() ) ) );
770       _PTR(GenericAttribute) anAttr;
771       if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") ) {
772         _PTR(AttributeName) aNameAttr( anAttr );
773         name = aNameAttr->Value().c_str();
774       }
775     }
776     
777     // 2. if object is not found in the study, try default name
778     if ( name.isEmpty() ) {
779       if ( object->IsMainShape() ) {
780         name = GetDefaultName( "geomObj" );
781       }
782       else {
783         GEOM::GEOM_Object_var mainShape = object->GetMainShape();
784         if ( !CORBA::is_nil( mainShape  ) ) { 
785           GEOM::ListOfLong_var indices = object->GetSubShapeIndices();
786           if ( indices->length() > 0 ) {
787             TopAbs_ShapeEnum type = (TopAbs_ShapeEnum)( object->GetShapeType() );
788             name = QString( "%1:%2_%3" ).arg( GetName( mainShape.in() ) )
789               .arg( TypeName( type ) ).arg( indices[0] );
790           }
791         }
792       }
793     }
794   }
795
796   return name;
797 }
798
799 //=======================================================================
800 // function : IsShape()
801 // purpose  : Return TRUE if object is valid and has shape
802 //=======================================================================
803 bool GEOMBase::IsShape( GEOM::GEOM_Object_ptr object )
804 {
805   return !object->_is_nil() && object->IsShape();
806 }
807
808 //=======================================================================
809 // function : TypeName()
810 // purpose  : Get string representation for the shape type
811 //=======================================================================
812 QString GEOMBase::TypeName( TopAbs_ShapeEnum type, bool capitalize )
813 {
814   QString name = "shape";
815   switch( type ) {
816   case TopAbs_COMPSOLID:
817     name = "compsolid"; break;
818   case TopAbs_COMPOUND:
819     name = "compound";  break;
820   case TopAbs_SOLID:
821     name = "solid";     break;
822   case TopAbs_SHELL:
823     name = "shell";     break;
824   case TopAbs_FACE:
825     name = "face";      break;
826   case TopAbs_WIRE:
827     name = "wire";      break;
828   case TopAbs_EDGE:
829     name = "edge";      break;
830   case TopAbs_VERTEX:
831     name = "vertex";    break;
832   default:
833     break;
834   }
835   if ( capitalize && !name.isEmpty() )
836     name = name.left(1).toUpper() + name.mid(1);
837   return name;
838 }
839
840 //================================================================
841 // Function : GetEntry
842 // Purpose  : Get study entry for the given object (if it is published)
843 //================================================================
844 QString GEOMBase::GetEntry( GEOM::GEOM_Object_ptr object )
845 {
846   QString entry;
847   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
848   if ( study && !CORBA::is_nil( object ) ) {
849     QString objIOR = GetIORFromObject( object );
850     if ( !objIOR.isEmpty() ) {
851       _PTR(SObject) SO( study->studyDS()->FindObjectIOR( objIOR.toLatin1().constData() ) );
852       if ( SO )
853         entry = SO->GetID().c_str();
854     }
855   }
856   return entry;
857 }
858
859 //================================================================
860 // Function : PublishSubObject
861 // Purpose  : Publish sub-shape under the main object
862 //================================================================
863 void GEOMBase::PublishSubObject( GEOM::GEOM_Object_ptr object, const QString& name )
864 {
865   SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
866   if ( study && !CORBA::is_nil( object ) ) {
867     _PTR(Study) studyDS = study->studyDS();
868     QString entry = GetEntry( object );
869     GEOM::GEOM_Object_var father = object->GetMainShape();
870     QString fatherEntry = GetEntry( father );
871     if ( entry.isEmpty() && !CORBA::is_nil( father ) && !fatherEntry.isEmpty() ) {
872       QString aName = !name.isEmpty() ? name : GetName( object );
873       GeometryGUI::GetGeomGen()->AddInStudy( object, aName.toUtf8().data(), father.in() );
874     }
875   }
876 }
877
878 //================================================================
879 // Function : synchronize
880 // Purpose  : 
881 //================================================================
882 void GEOMBase::Synchronize( QList<GEOM::GeomObjPtr>& left, QList<GEOM::GeomObjPtr>& right )
883 {
884   // 1. remove items from the "left" list that are not in the "right" list
885   QMutableListIterator<GEOM::GeomObjPtr> it1( left );
886   while ( it1.hasNext() ) {
887     GEOM::GeomObjPtr o1 = it1.next();
888     bool found = false;
889     QMutableListIterator<GEOM::GeomObjPtr> it2( right );
890     while ( it2.hasNext() && !found )
891       found = o1 == it2.next();
892     if ( !found )
893       it1.remove();
894   }
895   // 2. add items from the "right" list that are not in the "left" list (to keep selection order)
896   it1 = right;
897   while ( it1.hasNext() ) {
898     GEOM::GeomObjPtr o1 = it1.next();
899     bool found = false;
900     QMutableListIterator<GEOM::GeomObjPtr> it2( left );
901     while ( it2.hasNext() && !found )
902       found = o1 == it2.next();
903     if ( !found )
904       left << o1;
905   }
906 }