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