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