]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMBase/GEOMBase.cxx
Salome HOME
Using files from package LightApp instead of SalomeApp
[modules/geom.git] / src / GEOMBase / GEOMBase.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : GEOMBase.cxx
25 //  Author : Damien COQUERET
26 //  Module : GEOM
27 //  $Header$
28
29 #include "GEOMBase.h"
30 #include "GeometryGUI.h"
31 #include "GEOMBase_aParameterDlg.h"
32
33 #include "GEOM_Client.hxx"
34
35 ////  SALOME Includes
36 #include "Utils_ORB_INIT.hxx"
37 #include "Utils_SINGLETON.hxx"
38
39 #include "SALOME_LifeCycleCORBA.hxx"
40
41 #include "GEOM_AssemblyBuilder.h"
42 #include "GEOM_Actor.h"
43 #include "SVTK_RenderWindowInteractor.h"
44 #include "SVTK_ViewModel.h"
45 #include "SVTK_ViewWindow.h"
46 #include "OCCViewer_ViewPort3d.h"
47 #include "OCCViewer_ViewModel.h"
48 #include "OCCViewer_ViewWindow.h"
49
50 #include "SALOME_ListIO.hxx"
51 #include "SALOME_ListIteratorOfListIO.hxx"
52 #include "GEOM_AISTrihedron.hxx"
53
54 #include "SUIT_Session.h"
55 #include "SUIT_ViewWindow.h"
56 #include "SUIT_MessageBox.h"
57 #include "SalomeApp_Application.h"
58 #include "SalomeApp_Study.h"
59
60 // // Open CASCADE Includes
61 #include <BRep_Tool.hxx>
62 #include <BRepAdaptor_Curve.hxx>
63 #include <BRepAdaptor_Surface.hxx>
64 #include <BRepPrimAPI_MakeCone.hxx>
65
66 #include <Geom_Circle.hxx>
67 #include <AIS_ListIteratorOfListOfInteractive.hxx>
68 #include <AIS_ListOfInteractive.hxx>
69
70 #include <TopAbs.hxx>
71 #include <TopExp.hxx>
72 #include <TopExp_Explorer.hxx>
73 #include <TopoDS_Iterator.hxx>
74 #include <TopoDS_Compound.hxx>
75 #include <TopTools_MapOfShape.hxx>
76 #include <TopTools_ListIteratorOfListOfShape.hxx>
77 #include <TopTools_IndexedMapOfShape.hxx>
78
79 #include <Precision.hxx>
80
81 #include <vtkRenderer.h>
82 #include <qvaluelist.h>
83 #include <qstringlist.h>
84
85 #include "GEOMImpl_Types.hxx"
86
87 using namespace std;
88
89 #include "SALOMEDSClient.hxx"
90
91
92 //=======================================================================
93 // function : GEOMBase()
94 // purpose  : Constructor
95 //=======================================================================
96 GEOMBase::GEOMBase()
97 {
98 }
99
100
101 //=======================================================================
102 // function : ~GEOMBase()
103 // purpose  : Destructor
104 //=======================================================================
105 GEOMBase::~GEOMBase()
106 {
107 }
108
109 //=====================================================================================
110 // function : GetShapeFromIOR()
111 // purpose  : exist also as static method !
112 //=====================================================================================
113 TopoDS_Shape GEOMBase::GetShapeFromIOR(QString IOR)
114 {
115   TopoDS_Shape result;
116   if(IOR.stripWhiteSpace().isEmpty())
117     return result;
118
119   CORBA::Object_var obj = SalomeApp_Application::orb()->string_to_object((char*)(IOR.latin1()));
120   if(CORBA::is_nil(obj))
121     return result;
122   GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( obj );
123   if (GeomObject->_is_nil())
124     return result;
125
126   result = GEOM_Client().GetShape(GeometryGUI::GetGeomGen(), GeomObject);
127   return result;
128 }
129
130
131 //=====================================================================================
132 // function : GetIndex()
133 // purpose  : Get the index of a sub shape in a main shape : index start at 1
134 //=====================================================================================
135 int GEOMBase::GetIndex(const TopoDS_Shape& subshape, const TopoDS_Shape& shape, int /*ShapeType*/)
136 {
137   if(shape.IsNull() || subshape.IsNull())
138     return -1;
139
140   TopTools_IndexedMapOfShape anIndices;
141   TopExp::MapShapes(shape, anIndices);
142   if(anIndices.Contains(subshape)) return anIndices.FindIndex(subshape);
143
144   return -1;
145 }
146
147
148 //=======================================================================
149 // function : GetTopoFromSelection()
150 // purpose  : Define tds from a single selection and retuen true
151 //=======================================================================
152 bool GEOMBase::GetTopoFromSelection(const SALOME_ListIO& aList, TopoDS_Shape& tds)
153 {
154   if(aList.Extent() != 1)
155     return false;
156
157   Handle(SALOME_InteractiveObject) IO = aList.First();
158   /* case SObject */
159   if(IO->hasEntry()) {
160     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
161     if ( !appStudy ) return false;
162     _PTR(Study) aStudy = appStudy->studyDS();
163
164     _PTR(SObject) obj ( aStudy->FindObjectID(IO->getEntry()) );
165     _PTR(GenericAttribute) anAttr;
166     if( obj ) {
167       if(obj->FindAttribute(anAttr, "AttributeIOR")) {
168         _PTR(AttributeIOR) anIOR ( anAttr );
169         tds = GetShapeFromIOR(anIOR->Value().c_str());
170         if(tds.IsNull())
171           return false;
172         else
173           return true;
174       }
175     }
176   }
177
178   return false;
179 }
180
181 //=======================================================================
182 // function : GetNameOfSelectedIObjects()
183 // purpose  : Define the name geom++ or other name of mono or multi sel.
184 //=======================================================================
185 int GEOMBase::GetNameOfSelectedIObjects( const SALOME_ListIO& aList,
186                                          QString&          theName,
187                                          const bool        theShapesOnly )
188 {
189   if ( !theShapesOnly )
190     {
191       int nbSel = aList.Extent();
192       if ( nbSel == 1 )
193         {
194           Handle(SALOME_InteractiveObject) anIObj = aList.First();
195           if(anIObj->hasEntry()) {
196             SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
197             if ( !appStudy ) return nbSel;
198             _PTR(Study) aStudy = appStudy->studyDS();
199
200             _PTR(SObject) obj ( aStudy->FindObjectID(anIObj->getEntry()) );
201
202             _PTR(GenericAttribute) anAttr;
203
204             if ( obj && obj->FindAttribute( anAttr, "AttributeName") )
205               {
206                 _PTR(AttributeName) aNameAttr ( anAttr );
207                 theName = aNameAttr->Value().c_str();
208               }
209           }
210         }
211       else
212         theName = QObject::tr("%1_objects").arg(nbSel);
213
214       return nbSel;
215     }
216   else
217     {
218       GEOM::ListOfGO anObjs;
219       ConvertListOfIOInListOfGO( aList, anObjs, theShapesOnly );
220       if ( anObjs.length() == 1 ) {
221         theName = GetName( anObjs[ 0 ] );
222       }
223       else
224         theName = QString( "%1_objects" ).arg( anObjs.length() );
225
226       return anObjs.length();
227     }
228 }
229
230
231 //=================================================================================
232 // function : GetShapeTypeString()
233 // purpose  : for a single shape
234 //=================================================================================
235 bool GEOMBase::GetShapeTypeString(const TopoDS_Shape& aShape, Standard_CString& aTypeString)
236 {
237   if(aShape.IsNull()) {
238     aTypeString = "aNullShape";
239     return false;
240   }
241   switch(aShape.ShapeType())
242     {
243     case TopAbs_COMPOUND:
244       {
245         aTypeString = CORBA::string_dup(QObject::tr("GEOM_COMPOUND"));
246         return true;
247       }
248     case  TopAbs_COMPSOLID:
249       {
250         aTypeString = CORBA::string_dup(QObject::tr("GEOM_COMPOUNDSOLID")) ;
251         return true ;
252       }
253     case TopAbs_SOLID:
254       {
255         aTypeString = CORBA::string_dup(QObject::tr("GEOM_SOLID")) ;
256         return true ;
257       }
258     case TopAbs_SHELL:
259       {
260         aTypeString = CORBA::string_dup(QObject::tr("GEOM_SHELL")) ;
261         return true ;
262       }
263     case TopAbs_FACE:
264       {
265         BRepAdaptor_Surface surf(TopoDS::Face(aShape));
266         if(surf.GetType() == GeomAbs_Plane) {
267           aTypeString = CORBA::string_dup(QObject::tr("GEOM_PLANE"));
268           return true;
269         }
270         else if(surf.GetType() == GeomAbs_Cylinder) {
271           aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFCYLINDER"));
272           return true;
273         }
274         else if(surf.GetType() == GeomAbs_Sphere) {
275           aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFSPHERE"));
276           return true ;
277         }
278         else if(surf.GetType() == GeomAbs_Torus) {
279           aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFTORUS"));
280           return true ;
281         }
282         else if(surf.GetType() == GeomAbs_Cone) {
283           aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFCONE"));
284           return true ;
285         }
286         else {
287           aTypeString = CORBA::string_dup(QObject::tr("GEOM_FACE"));
288           return true;
289         }
290       }
291     case TopAbs_WIRE:
292       {
293         aTypeString = CORBA::string_dup(QObject::tr("GEOM_WIRE"));
294         return true;
295       }
296     case TopAbs_EDGE:
297       {
298         BRepAdaptor_Curve curv(TopoDS::Edge(aShape));
299         if(curv.GetType() == GeomAbs_Line) {
300           if((Abs(curv.FirstParameter()) >= 1E6) || (Abs(curv.LastParameter()) >= 1E6))
301             aTypeString = CORBA::string_dup(QObject::tr("GEOM_LINE"));
302           else
303             aTypeString = CORBA::string_dup(QObject::tr("GEOM_EDGE"));
304           return true;
305         }
306         else if(curv.GetType() == GeomAbs_Circle) {
307           if(curv.IsClosed())
308             aTypeString = CORBA::string_dup(QObject::tr("GEOM_CIRCLE"));
309           else
310             aTypeString = CORBA::string_dup(QObject::tr("GEOM_ARC"));
311         return true;
312       }
313         else {
314           aTypeString = CORBA::string_dup(QObject::tr("GEOM_EDGE"));
315           return true;
316         }
317       }
318     case TopAbs_VERTEX:
319       {
320         aTypeString = CORBA::string_dup(QObject::tr("GEOM_VERTEX"));
321         return true;
322       }
323     case TopAbs_SHAPE:
324       {
325         aTypeString = CORBA::string_dup(QObject::tr("GEOM_SHAPE"));
326         return true;
327       }
328     }
329   return false;
330 }
331
332
333 //=======================================================================
334 // function : ConvertIORinGEOMAISShape()
335 // purpose  :
336 //=======================================================================
337 Handle(GEOM_AISShape) GEOMBase::ConvertIORinGEOMAISShape(const char * IOR, Standard_Boolean& testResult, bool onlyInActiveView)
338 {
339   Handle(GEOM_AISShape) resultShape;
340   testResult = false;
341
342   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
343   if ( !appStudy ) return resultShape;
344   _PTR(Study) aStudy = appStudy->studyDS();
345
346   _PTR(SObject) anObj ( aStudy->FindObjectIOR( IOR ) );
347   if ( !anObj )
348     return resultShape;
349
350   QPtrList<SUIT_ViewWindow> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
351
352   for ( QPtrListIterator<SUIT_ViewWindow> it( aViewWindowsList ); it.current(); ++it ) {
353     if (it.current()->getViewManager()->getType() == OCCViewer_Viewer::Type()) {
354       Handle (AIS_InteractiveContext) ic = ((OCCViewer_Viewer*)it.current()->getViewManager()->getViewModel())->getAISContext();
355
356       AIS_ListOfInteractive List;
357       ic->DisplayedObjects(List);
358       AIS_ListOfInteractive List1;
359       ic->ObjectsInCollector(List1);
360       List.Append(List1);
361
362       AIS_ListIteratorOfListOfInteractive ite(List);
363       while(ite.More()) {
364         if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
365           Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
366           if(aSh->hasIO()) {
367             Handle(SALOME_InteractiveObject) GIO = Handle(SALOME_InteractiveObject)::DownCast(aSh->getIO());
368             if(GIO->hasEntry() && strcmp(GIO->getEntry(), anObj->GetID().c_str()) == 0) {
369               if(!onlyInActiveView ||
370                  it.current() == SUIT_Session::session()->activeApplication()->desktop()->activeWindow()) {
371                 testResult = true;
372                 resultShape = aSh;
373                 return resultShape;
374               }
375             }
376           }
377         }
378         ite.Next();
379       }
380     }
381   }
382   return  resultShape;
383 }
384
385
386 //=======================================================================
387 // function : ConvertIORinGEOMActor()
388 // purpose  :
389 //=======================================================================
390 GEOM_Actor* GEOMBase::ConvertIORinGEOMActor(const char* IOR, Standard_Boolean& testResult, bool onlyInActiveView)
391 {
392   testResult = false;
393
394   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
395   if ( !appStudy ) return GEOM_Actor::New();
396   _PTR(Study) aStudy = appStudy->studyDS();
397
398   _PTR(SObject) anObj ( aStudy->FindObjectIOR( IOR ) );
399   if ( !anObj )
400     return GEOM_Actor::New();
401
402   QPtrList<SUIT_ViewWindow> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
403
404   for ( QPtrListIterator<SUIT_ViewWindow> it( aViewWindowsList ); it.current(); ++it ) {
405     if (it.current()->getViewManager()->getType() == SVTK_Viewer::Type()) {
406       SVTK_ViewWindow* aVTKViewWindow = dynamic_cast<SVTK_ViewWindow*>( it.current() );
407       if( !aVTKViewWindow )
408         continue;
409       vtkRenderer* Renderer = aVTKViewWindow->getRenderer();
410       vtkActorCollection* theActors = Renderer->GetActors();
411       theActors->InitTraversal();
412       vtkActor *ac = theActors->GetNextActor();
413       while(!(ac==NULL)) {
414         if( ac->IsA("GEOM_Actor")) {
415           GEOM_Actor* anActor = GEOM_Actor::SafeDownCast(ac);
416           if(anActor->hasIO()) {
417             Handle(SALOME_InteractiveObject) GIO = Handle(SALOME_InteractiveObject)::DownCast(anActor->getIO());
418             if(GIO->hasEntry() && strcmp(GIO->getEntry(), anObj->GetID().c_str()) == 0) {
419               if(!onlyInActiveView ||
420                  it.current() == SUIT_Session::session()->activeApplication()->desktop()->activeWindow()) {
421                 testResult = true;
422                 return anActor;
423               }
424             }
425           }
426         }
427         ac = theActors->GetNextActor();
428       }
429     }
430   }
431   testResult = false;
432   return GEOM_Actor::New();
433 }
434
435 //=======================================================================
436 // function : GetAIS()
437 // purpose  :
438 //=======================================================================
439 Handle(AIS_InteractiveObject) GEOMBase::GetAIS( const Handle(SALOME_InteractiveObject)& theIO,
440                                                 const bool                              isOnlyInActiveView )
441 {
442   if ( theIO.IsNull() || !theIO->hasEntry() )
443     return Handle(AIS_InteractiveObject)();
444
445   QPtrList<SUIT_ViewWindow> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
446
447   for ( QPtrListIterator<SUIT_ViewWindow> it( aViewWindowsList ); it.current(); ++it ) {
448     if (it.current()->getViewManager()->getType() != OCCViewer_Viewer::Type())
449       continue;
450     Handle (AIS_InteractiveContext) anIC = ((OCCViewer_Viewer*)it.current()->getViewManager()->getViewModel())->getAISContext();
451
452     AIS_ListOfInteractive aList;
453     anIC->DisplayedObjects( aList );
454     anIC->ObjectsInCollector( aList );
455
456     AIS_ListIteratorOfListOfInteractive anIter( aList );
457     for ( ; anIter.More(); anIter.Next() )
458     {
459       Handle(SALOME_InteractiveObject) anObj =
460         Handle(SALOME_InteractiveObject)::DownCast( anIter.Value()->GetOwner() );
461
462       if( !anObj.IsNull() && strcmp( anObj->getEntry(), theIO->getEntry() ) == 0 )
463       {
464         if( isOnlyInActiveView )
465         {
466           if ( it.current() == SUIT_Session::session()->activeApplication()->desktop()->activeWindow() )
467             return anIter.Value();
468         }
469         else
470           return anIter.Value();
471       }
472     }
473   }
474
475   return Handle(AIS_InteractiveObject)();
476 }
477
478
479 //=======================================================================
480 // function : ConvertIOinGEOMAISShape()
481 // purpose  :
482 //=======================================================================
483 Handle(GEOM_AISShape) GEOMBase::ConvertIOinGEOMAISShape(const Handle(SALOME_InteractiveObject)& IO, Standard_Boolean& testResult, bool onlyInActiveView)
484 {
485   Handle(GEOM_AISShape) res;
486
487   if ( !IO->hasEntry() )
488   {
489     testResult = false;
490     return res;
491   }
492
493   QPtrList<SUIT_ViewWindow> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
494
495   for ( QPtrListIterator<SUIT_ViewWindow> it( aViewWindowsList ); it.current(); ++it ) {
496     if (it.current()->getViewManager()->getType() == OCCViewer_Viewer::Type()) {
497       Handle (AIS_InteractiveContext) ic = ((OCCViewer_Viewer*)it.current()->getViewManager()->getViewModel())->getAISContext();
498
499       AIS_ListOfInteractive List;
500       ic->DisplayedObjects(List);
501       AIS_ListOfInteractive List1;
502       ic->ObjectsInCollector(List1);
503       List.Append(List1);
504
505       AIS_ListIteratorOfListOfInteractive ite(List);
506       while(ite.More())
507       {
508         if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape)))
509         {
510           Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
511           if( aSh->hasIO() )
512           {
513             if( strcmp( aSh->getIO()->getEntry(), IO->getEntry() ) == 0 )
514             {
515               if(onlyInActiveView)
516               {
517                 if(it.current() == SUIT_Session::session()->activeApplication()->desktop()->activeWindow())
518                 {
519                   testResult = true;
520                   return aSh;
521                 }
522               }
523               else
524               {
525                 testResult = true;
526                 return aSh;
527               }
528             }
529           }
530         }
531         ite.Next();
532       }
533     }
534   }
535   testResult = false;
536   return res;
537 }
538
539
540 //=======================================================================
541 // function : ConvertIOinGEOMShape()
542 // purpose  :
543 //=======================================================================
544 GEOM::GEOM_Object_ptr GEOMBase::ConvertIOinGEOMShape(const Handle(SALOME_InteractiveObject)& IO, Standard_Boolean& testResult)
545 {
546   GEOM::GEOM_Object_var aShape;
547   testResult = false;
548
549   /* case SObject */
550   if(IO->hasEntry()) {
551     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
552     if ( !appStudy ) return GEOM::GEOM_Object::_nil();
553     _PTR(Study) aStudy = appStudy->studyDS();
554
555     _PTR(SObject) obj ( aStudy->FindObjectID(IO->getEntry()) );
556     _PTR(GenericAttribute) anAttr;
557     if(obj) {
558       if(obj->FindAttribute(anAttr, "AttributeIOR")) {
559         _PTR(AttributeIOR) anIOR ( anAttr );
560         aShape = GeometryGUI::GetGeomGen()->GetIORFromString(anIOR->Value().c_str());
561         if(!CORBA::is_nil(aShape))
562           testResult = true;
563         return aShape._retn();
564       }
565     }
566   }
567   return GEOM::GEOM_Object::_nil();
568 }
569
570
571 //=======================================================================
572 // function : ConvertListOfIOInListOfIOR()
573 // purpose  :
574 //=======================================================================
575 void GEOMBase::ConvertListOfIOInListOfIOR(const SALOME_ListIO& aList, GEOM::string_array& listIOR)
576 {
577   int nbSel = aList.Extent();
578   listIOR.length(nbSel);
579   int j=0;
580   SALOME_ListIteratorOfListIO It(aList);
581   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
582     ( SUIT_Session::session()->activeApplication()->activeStudy() );
583   if ( !appStudy ) return;
584   _PTR(Study) aStudy = appStudy->studyDS();
585
586   for (int i=0; It.More(); It.Next(), i++) {
587     Handle(SALOME_InteractiveObject) IObject = It.Value();
588     if (IObject->hasEntry()) {
589       _PTR(SObject) obj ( aStudy->FindObjectID(IObject->getEntry()) );
590       _PTR(GenericAttribute) anAttr;
591       if (obj && obj->FindAttribute(anAttr, "AttributeIOR")) {
592         _PTR(AttributeIOR) anIOR (anAttr);
593         //CORBA::Object_var theObj = dynamic_cast<SALOMEDS_Study*>
594         //  (aStudy.get())->ConvertIORToObject(anIOR->Value());
595         CORBA::Object_var theObj = GeometryGUI::ClientSObjectToObject(obj);
596         if (!CORBA::is_nil(theObj) && theObj->_is_a("IDL:GEOM/GEOM_Object:1.0")) {
597           listIOR[j] = CORBA::string_dup(anIOR->Value().c_str());
598           j++;
599         }
600       }
601     }
602   }
603   listIOR.length(j);
604 }
605
606
607 //=======================================================================
608 // function : ConvertIOinGEOMObject()
609 // purpose  :
610 //=======================================================================
611 GEOM::GEOM_Object_ptr GEOMBase::ConvertIOinGEOMObject( const Handle(SALOME_InteractiveObject)& theIO,
612                                                        Standard_Boolean& theResult )
613 {
614   theResult = Standard_False;
615   GEOM::GEOM_Object_var aReturnObject;
616   if ( !theIO.IsNull() )
617   {
618     const char* anEntry = theIO->getEntry();
619
620     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
621       ( SUIT_Session::session()->activeApplication()->activeStudy() );
622     if ( !appStudy ) return  GEOM::GEOM_Object::_nil();
623     _PTR(Study) aStudy = appStudy->studyDS();
624
625     _PTR(SObject) aSObj ( aStudy->FindObjectID( anEntry ) );
626
627     if (aSObj)
628     {
629       aReturnObject = GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aSObj));
630       theResult = !CORBA::is_nil( aReturnObject );
631     }
632   }
633   return aReturnObject._retn();
634 }
635
636
637 //=======================================================================
638 // function : ConvertListOfIOInListOfGO()
639 // purpose  :
640 //=======================================================================
641 void GEOMBase::ConvertListOfIOInListOfGO( const SALOME_ListIO& theList,
642                                           GEOM::ListOfGO&      theListGO,
643                                           const bool           theShapesOnly )
644 {
645   int nbSel = theList.Extent();
646   theListGO.length( nbSel );
647   SALOME_ListIteratorOfListIO anIter( theList );
648
649   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
650   if ( !appStudy ) return;
651   _PTR(Study) aStudy = appStudy->studyDS();
652
653   int j = 0;
654   for ( int i=0; anIter.More(); anIter.Next(), i++ )
655   {
656     Handle(SALOME_InteractiveObject) anIObj = anIter.Value();
657     _PTR(SObject) aSObj ( aStudy->FindObjectID( anIObj->getEntry() ) );
658
659     if ( aSObj )
660     {
661       GEOM::GEOM_Object_var aGeomObj =
662         GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aSObj));
663       if ( !CORBA::is_nil( aGeomObj ) && ( !theShapesOnly || IsShape( aGeomObj ) ) )
664         theListGO[ j++ ] = aGeomObj;
665     }
666   }
667
668   theListGO.length( j );
669 }
670
671 //=================================================================================
672 // function : CreateArrowForLinearEdge()
673 // purpose  : Create a cone topology to be used to display an arrow in the middle
674 //          : of an edge showing its orientation. (For simulation and Viewer OCC only)
675 //=================================================================================
676 bool GEOMBase::CreateArrowForLinearEdge(const TopoDS_Shape& tds, TopoDS_Shape& ArrowCone)
677 {
678   if(SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType()
679      != OCCViewer_Viewer::Type() || tds.ShapeType() != TopAbs_EDGE)
680     return false;
681
682   OCCViewer_ViewPort3d* vp3d = ((OCCViewer_ViewWindow*)SUIT_Session::session()->activeApplication()->desktop()->activeWindow())->getViewPort();
683   Handle( V3d_View) view3d = vp3d->getView();
684   Standard_Real Width, Height;
685   view3d->Size(Width, Height);
686   const Standard_Real aHeight = (Width + Height) / 50.0;
687
688   try {
689     Standard_Real first, last;
690     Handle(Geom_Curve) curv = BRep_Tool::Curve(TopoDS::Edge(tds), first, last);
691     if(!curv->IsCN(1))
692       return false;
693
694     const Standard_Real param = (first+last) / 2.0;
695     gp_Pnt middleParamPoint;
696     gp_Vec V1;
697     curv->D1( param, middleParamPoint, V1);
698     if(V1.Magnitude() < Precision::Confusion())
699       return false;
700
701     /* Topology orientation not geom orientation */
702     if(tds.Orientation() == TopAbs_REVERSED)
703       V1 *= -1.0;
704
705     gp_Ax2 anAxis( middleParamPoint, gp_Dir(V1));
706     const Standard_Real radius1 = aHeight / 5.0;
707     if(radius1 > 10.0 * Precision::Confusion() && aHeight > 10.0 * Precision::Confusion()) {
708       ArrowCone = BRepPrimAPI_MakeCone( anAxis, radius1, 0.0, aHeight ).Shape();
709       return true;
710     }
711   }
712   catch(Standard_Failure) {
713     // OCC failures are hard to catch in GUI.
714     // This  because of the position for  #include <Standard_ErrorHandler.hxx> that is very critic to find
715     // in SALOME environment : compilation error !
716   }
717   return false;
718 }
719
720
721 //=================================================================================
722 // function : VertexToPoint()
723 // purpose  : If S can be converted in a gp_Pnt returns true and the result is P
724 //=================================================================================
725 bool GEOMBase::VertexToPoint(const TopoDS_Shape& S, gp_Pnt& P)
726 {
727   if(S.IsNull() || S.ShapeType() != TopAbs_VERTEX)
728     return false;
729   P = BRep_Tool::Pnt(TopoDS::Vertex(S));
730   return true;
731 }
732
733
734 //=================================================================================
735 // function : GetBipointDxDyDz()
736 // purpose  :
737 //=================================================================================
738 void GEOMBase::GetBipointDxDyDz(gp_Pnt P1, gp_Pnt P2, double& dx, double& dy, double& dz)
739 {
740   dx = P2.X() - P1.X();
741   dy = P2.Y() - P1.Y();
742   dz = P2.Z() - P1.Z();
743   return;
744 }
745
746
747 //=================================================================================
748 // function : LinearEdgeExtremities()
749 // purpose  : If S can be converted in a linear edge and if initial an final points
750 //          : distance is sufficient, returns true else returns false.
751 //          : Resulting points are respectively P1 and P2
752 //=================================================================================
753 bool GEOMBase::LinearEdgeExtremities(const TopoDS_Shape& S,  gp_Pnt& P1, gp_Pnt& P2)
754 {
755   if(S.IsNull() || S.ShapeType() != TopAbs_EDGE)
756     return false;
757   BRepAdaptor_Curve curv(TopoDS::Edge(S));
758   if(curv.GetType() != GeomAbs_Line)
759     return false;
760
761   curv.D0(curv.FirstParameter(), P1);
762   curv.D0(curv.LastParameter(), P2);
763
764   if(P1.Distance(P2) <= Precision::Confusion())
765     return false;
766
767   return true;
768 }
769
770
771 //=======================================================================
772 // function : Parameter()
773 // purpose  : return a parameter (float) from a dialog box
774 //
775 //  avalue1    : is a float or integer used as a default value displayed
776 //  aTitle1    : is the title for aValue1
777 //  aTitle     : is the main title
778 //  bottom     : maximum value to be entered
779 //  top        : minimum value to be entered
780 //  decimals   : number of decimals
781 //=======================================================================
782 double GEOMBase::Parameter(Standard_Boolean& res, const char* aValue1, const char* aTitle1, const char* aTitle, const double bottom, const double top, const int decimals)
783 {
784   GEOMBase_aParameterDlg * Dialog = new GEOMBase_aParameterDlg(aValue1, aTitle1, SUIT_Session::session()->activeApplication()->desktop(),
785                                                                aTitle, TRUE, 0, bottom, top, decimals);
786   int r = Dialog->exec();
787   float X = 0.0;
788   if(r == QDialog::Accepted) {
789     res = Standard_True;
790     X = Dialog->getValue();
791   }
792   else
793     res = Standard_False;
794   delete Dialog;
795   return X;
796 }
797
798
799 //=======================================================================
800 // function : SelectionByNameInDialogs()
801 // purpose  : Called when user has entered a name of object in a LineEdit.
802 //          : The selection is changed. Dialog box will receive the
803 //          : corresponding signal to manage this event.
804 //=======================================================================
805 bool GEOMBase::SelectionByNameInDialogs(QWidget* aWidget, const QString& objectUserName, const SALOME_ListIO& aList)
806 {
807   /* Find SObject with name in component GEOM */
808   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
809   if ( !appStudy ) return false;
810   _PTR(Study) ST = appStudy->studyDS();
811
812   std::vector<_PTR(SObject)> listSO;
813   listSO = ST->FindObjectByName(objectUserName.latin1(), "GEOM");
814
815   if(listSO.size() < 1) {
816     const QString caption  = QObject::tr("GEOM_WRN_WARNING");
817     const QString text = QObject::tr("GEOM_NAME_INCORRECT");
818     const QString button0  = QObject::tr("GEOM_BUT_OK");
819     SUIT_MessageBox::error1(aWidget, caption, text, button0);
820     return false;
821   }
822   /* More than one object with same name */
823   if(listSO.size() > 1) {
824     const QString caption  = QObject::tr("GEOM_WRN_WARNING");
825     const QString text = QObject::tr("GEOM_IDENTICAL_NAMES_SELECT_BY_MOUSE");
826     const QString button0  = QObject::tr("GEOM_BUT_OK") ;
827     SUIT_MessageBox::error1(aWidget, caption, text, button0) ;
828     listSO.clear();
829     return false;
830   }
831
832   _PTR(SObject) theObj ( listSO[0] );
833   /* Create a SALOME_InteractiveObject with a SALOME::SObject */
834   char* aCopyobjectUserName = CORBA::string_dup(objectUserName);
835   Handle(SALOME_InteractiveObject) SI = new SALOME_InteractiveObject(theObj->GetID().c_str(), "GEOM", aCopyobjectUserName);
836   delete(aCopyobjectUserName);
837
838   /* Add as a selected object       */
839   /* Clear any previous selection : */
840   /* Warning the LineEdit is purged because of signal currentSelectionChanged ! */
841   // Sel->ClearIObjects(); //mzn
842   // Sel->AddIObject(SI); //mzn
843   return true;
844 }
845
846
847 //=======================================================================
848 // function : DefineDlgPosition()
849 // purpose  : Define x and y the default position for a dialog box
850 //=======================================================================
851 bool GEOMBase::DefineDlgPosition(QWidget* aDlg, int& x, int& y)
852 {
853   /* Here the position is on the bottom right corner - 10 */
854   SUIT_Desktop* PP = SUIT_Session::session()->activeApplication()->desktop();
855   x = abs(PP->x() + PP->size().width() - aDlg->size().width() - 10);
856   y = abs(PP->y() + PP->size().height() - aDlg->size().height() - 10);
857   return true;
858 }
859
860
861 //=======================================================================
862 // function : GetDefaultName()
863 // purpose  : Generates default names
864 //=======================================================================
865 QString GEOMBase::GetDefaultName(const QString& theOperation)
866 {
867   QString aName = "";
868
869   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
870   if ( !appStudy ) return aName;
871   _PTR(Study) aStudy = appStudy->studyDS();
872
873   int aNumber = 0;
874   _PTR(SObject) obj;
875   do
876     {
877       aName = theOperation+"_"+QString::number(++aNumber);
878       obj = aStudy->FindObject(aName.latin1());
879     }
880   while (obj);
881
882   return aName;
883 }
884
885
886 //=======================================================================
887 // function : ShowErrorMessage()
888 // purpose  : Shows message box with error code and comment
889 //=======================================================================
890 void GEOMBase::ShowErrorMessage(const char* theErrorCode, const char* theComment)
891 {
892   QString anErrorCode(theErrorCode);
893   QString aComment(theComment);
894
895   QString aText = "";
896   if (!anErrorCode.isEmpty())
897     aText.append("\n" + QObject::tr(anErrorCode));
898   if (!aComment.isEmpty())
899     aText.append("\n" + QString(theComment));
900
901   SUIT_MessageBox::error1( SUIT_Session::session()->activeApplication()->desktop(), QObject::tr( "GEOM_ERROR" ),
902                            QObject::tr("GEOM_PRP_ABORT") + aText, "OK" );
903 }
904
905
906 //=======================================================================
907 // function : GetObjectFromIOR()
908 // purpose  : returns a GEOM_Object by given IOR (string)
909 //=======================================================================
910 GEOM::GEOM_Object_ptr GEOMBase::GetObjectFromIOR( const char* theIOR )
911 {
912   GEOM::GEOM_Object_var anObject;
913   if ( theIOR == NULL || strlen( theIOR ) == 0 )
914     return anObject._retn(); // returning nil object
915
916   anObject = GEOM::GEOM_Object::_narrow( SalomeApp_Application::orb()->string_to_object( theIOR ) );
917   return anObject._retn();
918 }
919
920 //=======================================================================
921 // function : GetIORFromObject()
922 // purpose  : returns IOR of a given GEOM_Object
923 //=======================================================================
924 char* GEOMBase::GetIORFromObject( const GEOM::GEOM_Object_ptr& theObject )
925 {
926   if ( CORBA::is_nil( theObject ) )
927     return NULL;
928
929   return SalomeApp_Application::orb()->object_to_string( theObject );
930 }
931
932 //=======================================================================
933 // function : GetShape()
934 // purpose  : returns a TopoDS_Shape stored in GEOM_Object
935 //=======================================================================
936 bool GEOMBase::GetShape( const GEOM::GEOM_Object_ptr& theObject, TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType )
937 {
938   if ( !CORBA::is_nil( theObject ) )
939   {
940     TopoDS_Shape aTopoDSShape = GEOM_Client().GetShape(  GeometryGUI::GetGeomGen(), theObject );
941     if ( !aTopoDSShape.IsNull() && ( theType == TopAbs_SHAPE || theType == aTopoDSShape.ShapeType() ) )
942     {
943        theShape = aTopoDSShape;
944        return true;
945     }
946   }
947   return false;
948 }
949
950 //=======================================================================
951 // function : GetName()
952 // purpose  : Get name of object
953 //=======================================================================
954 QString GEOMBase::GetName( GEOM::GEOM_Object_ptr theObj )
955 {
956   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
957
958   if ( appStudy )
959   {
960     string anIOR = SalomeApp_Application::orb()->object_to_string( theObj );
961     if ( anIOR != "" )
962     {
963       _PTR(SObject) aSObj ( appStudy->studyDS()->FindObjectIOR( anIOR ) );
964
965       _PTR(GenericAttribute) anAttr;
966
967       if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") )
968       {
969         _PTR(AttributeName) aNameAttr ( anAttr );
970         return QString( aNameAttr->Value().c_str() );
971       }
972     }
973   }
974
975   return QString("");
976 }
977
978 bool GEOMBase::IsShape( GEOM::GEOM_Object_ptr theObj )
979 {
980   return !theObj->_is_nil() && theObj->IsShape();
981 }