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