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