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