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