Salome HOME
Bug 0020043: EDF 865 GEOM: Object is non valid after translation. Fix object after...
[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 #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, Standard_Boolean& testResult)
514 {
515   GEOM::GEOM_Object_var aShape;
516   testResult = false;
517
518   /* case SObject */
519   if(IO->hasEntry()) {
520     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
521     if ( !appStudy ) return GEOM::GEOM_Object::_nil();
522     _PTR(Study) aStudy = appStudy->studyDS();
523
524     _PTR(SObject) obj ( aStudy->FindObjectID(IO->getEntry()) );
525     _PTR(GenericAttribute) anAttr;
526     if(obj) {
527       if(obj->FindAttribute(anAttr, "AttributeIOR")) {
528         _PTR(AttributeIOR) anIOR ( anAttr );
529         aShape = GeometryGUI::GetGeomGen()->GetIORFromString(anIOR->Value().c_str());
530         if(!CORBA::is_nil(aShape))
531           testResult = true;
532         return aShape._retn();
533       }
534     }
535   }
536   return GEOM::GEOM_Object::_nil();
537 }
538
539
540 //=======================================================================
541 // function : ConvertListOfIOInListOfIOR()
542 // purpose  :
543 //=======================================================================
544 void GEOMBase::ConvertListOfIOInListOfIOR(const SALOME_ListIO& aList, GEOM::string_array& listIOR)
545 {
546   int nbSel = aList.Extent();
547   listIOR.length(nbSel);
548   int j=0;
549   SALOME_ListIteratorOfListIO It(aList);
550   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
551     ( SUIT_Session::session()->activeApplication()->activeStudy() );
552   if ( !appStudy ) return;
553   _PTR(Study) aStudy = appStudy->studyDS();
554
555   for (int i=0; It.More(); It.Next(), i++) {
556     Handle(SALOME_InteractiveObject) IObject = It.Value();
557     if (IObject->hasEntry()) {
558       _PTR(SObject) obj ( aStudy->FindObjectID(IObject->getEntry()) );
559       _PTR(GenericAttribute) anAttr;
560       if (obj && obj->FindAttribute(anAttr, "AttributeIOR")) {
561         _PTR(AttributeIOR) anIOR (anAttr);
562         //CORBA::Object_var theObj = dynamic_cast<SALOMEDS_Study*>
563         //  (aStudy.get())->ConvertIORToObject(anIOR->Value());
564         CORBA::Object_var theObj = GeometryGUI::ClientSObjectToObject(obj);
565         if (!CORBA::is_nil(theObj) && theObj->_is_a("IDL:GEOM/GEOM_Object:1.0")) {
566           listIOR[j] = CORBA::string_dup(anIOR->Value().c_str());
567           j++;
568         }
569       }
570     }
571   }
572   listIOR.length(j);
573 }
574
575
576 //=======================================================================
577 // function : ConvertIOinGEOMObject()
578 // purpose  :
579 //=======================================================================
580 GEOM::GEOM_Object_ptr GEOMBase::ConvertIOinGEOMObject( const Handle(SALOME_InteractiveObject)& theIO,
581                                                        Standard_Boolean& theResult )
582 {
583   theResult = Standard_False;
584   GEOM::GEOM_Object_var aReturnObject;
585   if ( !theIO.IsNull() )
586   {
587     const char* anEntry = theIO->getEntry();
588
589     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
590       ( SUIT_Session::session()->activeApplication()->activeStudy() );
591     if ( !appStudy ) return  GEOM::GEOM_Object::_nil();
592     _PTR(Study) aStudy = appStudy->studyDS();
593
594     _PTR(SObject) aSObj ( aStudy->FindObjectID( anEntry ) );
595
596     if (aSObj)
597     {
598       aReturnObject = GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aSObj));
599       theResult = !CORBA::is_nil( aReturnObject );
600     }
601   }
602   return aReturnObject._retn();
603 }
604
605
606 //=======================================================================
607 // function : ConvertListOfIOInListOfGO()
608 // purpose  :
609 //=======================================================================
610 void GEOMBase::ConvertListOfIOInListOfGO( const SALOME_ListIO& theList,
611                                           GEOM::ListOfGO&      theListGO,
612                                           const bool           theShapesOnly )
613 {
614   int nbSel = theList.Extent();
615   theListGO.length( nbSel );
616   SALOME_ListIteratorOfListIO anIter( theList );
617
618   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
619   if ( !appStudy ) return;
620   _PTR(Study) aStudy = appStudy->studyDS();
621
622   int j = 0;
623   for ( int i=0; anIter.More(); anIter.Next(), i++ )
624   {
625     Handle(SALOME_InteractiveObject) anIObj = anIter.Value();
626     _PTR(SObject) aSObj ( aStudy->FindObjectID( anIObj->getEntry() ) );
627
628     if ( aSObj )
629     {
630       GEOM::GEOM_Object_var aGeomObj =
631         GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aSObj));
632       if ( !CORBA::is_nil( aGeomObj ) && ( !theShapesOnly || IsShape( aGeomObj ) ) )
633         theListGO[ j++ ] = aGeomObj;
634     }
635   }
636
637   theListGO.length( j );
638 }
639
640 //=================================================================================
641 // function : CreateArrowForLinearEdge()
642 // purpose  : Create a cone topology to be used to display an arrow in the middle
643 //          : of an edge showing its orientation. (For simulation and Viewer OCC only)
644 //=================================================================================
645 bool GEOMBase::CreateArrowForLinearEdge(const TopoDS_Shape& tds, TopoDS_Shape& ArrowCone)
646 {
647   if(SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType()
648      != OCCViewer_Viewer::Type() || tds.ShapeType() != TopAbs_EDGE)
649     return false;
650
651   OCCViewer_ViewPort3d* vp3d = ((OCCViewer_ViewWindow*)SUIT_Session::session()->activeApplication()->desktop()->activeWindow())->getViewPort();
652   Handle( V3d_View) view3d = vp3d->getView();
653   Standard_Real Width, Height;
654   view3d->Size(Width, Height);
655   const Standard_Real aHeight = (Width + Height) / 50.0;
656
657   try {
658     Standard_Real first, last;
659     Handle(Geom_Curve) curv = BRep_Tool::Curve(TopoDS::Edge(tds), first, last);
660     if(!curv->IsCN(1))
661       return false;
662
663     const Standard_Real param = (first+last) / 2.0;
664     gp_Pnt middleParamPoint;
665     gp_Vec V1;
666     curv->D1( param, middleParamPoint, V1);
667     if(V1.Magnitude() < Precision::Confusion())
668       return false;
669
670     /* Topology orientation not geom orientation */
671     if(tds.Orientation() == TopAbs_REVERSED)
672       V1 *= -1.0;
673
674     gp_Ax2 anAxis( middleParamPoint, gp_Dir(V1));
675     const Standard_Real radius1 = aHeight / 5.0;
676     if(radius1 > 10.0 * Precision::Confusion() && aHeight > 10.0 * Precision::Confusion()) {
677       ArrowCone = BRepPrimAPI_MakeCone( anAxis, radius1, 0.0, aHeight ).Shape();
678       return true;
679     }
680   }
681   catch(Standard_Failure) {
682     // OCC failures are hard to catch in GUI.
683     // This  because of the position for  #include <Standard_ErrorHandler.hxx> that is very critic to find
684     // in SALOME environment : compilation error !
685   }
686   return false;
687 }
688
689
690 //=================================================================================
691 // function : VertexToPoint()
692 // purpose  : If S can be converted in a gp_Pnt returns true and the result is P
693 //=================================================================================
694 bool GEOMBase::VertexToPoint(const TopoDS_Shape& S, gp_Pnt& P)
695 {
696   if(S.IsNull() || S.ShapeType() != TopAbs_VERTEX)
697     return false;
698   P = BRep_Tool::Pnt(TopoDS::Vertex(S));
699   return true;
700 }
701
702
703 //=================================================================================
704 // function : GetBipointDxDyDz()
705 // purpose  :
706 //=================================================================================
707 void GEOMBase::GetBipointDxDyDz(gp_Pnt P1, gp_Pnt P2, double& dx, double& dy, double& dz)
708 {
709   dx = P2.X() - P1.X();
710   dy = P2.Y() - P1.Y();
711   dz = P2.Z() - P1.Z();
712   return;
713 }
714
715
716 //=================================================================================
717 // function : LinearEdgeExtremities()
718 // purpose  : If S can be converted in a linear edge and if initial an final points
719 //          : distance is sufficient, returns true else returns false.
720 //          : Resulting points are respectively P1 and P2
721 //=================================================================================
722 bool GEOMBase::LinearEdgeExtremities(const TopoDS_Shape& S,  gp_Pnt& P1, gp_Pnt& P2)
723 {
724   if(S.IsNull() || S.ShapeType() != TopAbs_EDGE)
725     return false;
726   BRepAdaptor_Curve curv(TopoDS::Edge(S));
727   if(curv.GetType() != GeomAbs_Line)
728     return false;
729
730   curv.D0(curv.FirstParameter(), P1);
731   curv.D0(curv.LastParameter(), P2);
732
733   if(P1.Distance(P2) <= Precision::Confusion())
734     return false;
735
736   return true;
737 }
738
739
740 //=======================================================================
741 // function : SelectionByNameInDialogs()
742 // purpose  : Called when user has entered a name of object in a LineEdit.
743 //          : The selection is changed. Dialog box will receive the
744 //          : corresponding signal to manage this event.
745 //=======================================================================
746 bool GEOMBase::SelectionByNameInDialogs(QWidget* aWidget, const QString& objectUserName, const SALOME_ListIO& aList)
747 {
748   /* Find SObject with name in component GEOM */
749   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
750   if ( !appStudy ) return false;
751   _PTR(Study) ST = appStudy->studyDS();
752
753   std::vector<_PTR(SObject)> listSO;
754   listSO = ST->FindObjectByName(objectUserName.toStdString(), "GEOM");
755
756   if(listSO.size() < 1) {
757     const QString caption  = QObject::tr("GEOM_WRN_WARNING");
758     const QString text = QObject::tr("GEOM_NAME_INCORRECT");
759     const QString button0  = QObject::tr("GEOM_BUT_OK");
760     SUIT_MessageBox::critical(aWidget, caption, text, button0);
761     return false;
762   }
763   /* More than one object with same name */
764   if(listSO.size() > 1) {
765     const QString caption  = QObject::tr("GEOM_WRN_WARNING");
766     const QString text = QObject::tr("GEOM_IDENTICAL_NAMES_SELECT_BY_MOUSE");
767     const QString button0  = QObject::tr("GEOM_BUT_OK") ;
768     SUIT_MessageBox::critical(aWidget, caption, text, button0) ;
769     listSO.clear();
770     return false;
771   }
772
773   _PTR(SObject) theObj ( listSO[0] );
774   /* Create a SALOME_InteractiveObject with a SALOME::SObject */
775   char* aCopyobjectUserName = CORBA::string_dup(objectUserName.toLatin1().constData());
776   Handle(SALOME_InteractiveObject) SI = new SALOME_InteractiveObject(theObj->GetID().c_str(), "GEOM", aCopyobjectUserName);
777   delete(aCopyobjectUserName);
778
779   /* Add as a selected object       */
780   /* Clear any previous selection : */
781   /* Warning the LineEdit is purged because of signal currentSelectionChanged ! */
782   // Sel->ClearIObjects(); //mzn
783   // Sel->AddIObject(SI); //mzn
784   return true;
785 }
786
787
788 //=======================================================================
789 // function : DefineDlgPosition()
790 // purpose  : Define x and y the default position for a dialog box
791 //=======================================================================
792 bool GEOMBase::DefineDlgPosition(QWidget* aDlg, int& x, int& y)
793 {
794   /* Here the position is on the bottom right corner - 10 */
795   SUIT_Desktop* PP = SUIT_Session::session()->activeApplication()->desktop();
796   x = abs(PP->x() + PP->size().width() - aDlg->size().width() - 10);
797   y = abs(PP->y() + PP->size().height() - aDlg->size().height() - 10);
798   return true;
799 }
800
801
802 //=======================================================================
803 // function : GetDefaultName()
804 // purpose  : Generates default names
805 //=======================================================================
806 QString GEOMBase::GetDefaultName(const QString& theOperation)
807 {
808   QString aName = "";
809
810   // collect all object names of GEOM component
811   SalomeApp_Study* appStudy =
812     dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
813   if ( !appStudy ) return aName;
814   _PTR(Study) aStudy = appStudy->studyDS();
815
816   std::set<std::string> aSet;
817   _PTR(SComponent) aGeomCompo (aStudy->FindComponent("GEOM"));
818   if (aGeomCompo) {
819     _PTR(ChildIterator) it (aStudy->NewChildIterator(aGeomCompo));
820     _PTR(SObject) obj;
821     for (it->InitEx(true); it->More(); it->Next()) {
822       obj = it->Value();
823       aSet.insert(obj->GetName());
824     }
825   }
826
827   // build a unique name
828   int aNumber = 0;
829   bool isUnique = false;
830   while (!isUnique) {
831     aName = theOperation + "_" + QString::number(++aNumber);
832     isUnique = (aSet.count(aName.toStdString()) == 0);
833   }
834
835   return aName;
836 }
837
838
839 //=======================================================================
840 // function : ShowErrorMessage()
841 // purpose  : Shows message box with error code and comment
842 //=======================================================================
843 void GEOMBase::ShowErrorMessage(const char* theErrorCode, const char* theComment)
844 {
845   QString anErrorCode(theErrorCode);
846   QString aComment(theComment);
847
848   QString aText = "";
849   if (!anErrorCode.isEmpty())
850     aText.append("\n" + QObject::tr(anErrorCode.toLatin1().constData()));
851   if (!aComment.isEmpty())
852     aText.append("\n" + QString(theComment));
853
854   SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(), QObject::tr( "GEOM_ERROR" ),
855                              QObject::tr("GEOM_PRP_ABORT") + aText, "OK" );
856 }
857
858
859 //=======================================================================
860 // function : GetObjectFromIOR()
861 // purpose  : returns a GEOM_Object by given IOR (string)
862 //=======================================================================
863 GEOM::GEOM_Object_ptr GEOMBase::GetObjectFromIOR( const char* theIOR )
864 {
865   GEOM::GEOM_Object_var anObject;
866   if ( theIOR == NULL || strlen( theIOR ) == 0 )
867     return anObject._retn(); // returning nil object
868
869   anObject = GEOM::GEOM_Object::_narrow( SalomeApp_Application::orb()->string_to_object( theIOR ) );
870   return anObject._retn();
871 }
872
873 //=======================================================================
874 // function : GetIORFromObject()
875 // purpose  : returns IOR of a given GEOM_Object
876 //=======================================================================
877 char* GEOMBase::GetIORFromObject( const GEOM::GEOM_Object_ptr& theObject )
878 {
879   if ( CORBA::is_nil( theObject ) )
880     return NULL;
881
882   return SalomeApp_Application::orb()->object_to_string( theObject );
883 }
884
885 //=======================================================================
886 // function : GetShape()
887 // purpose  : returns a TopoDS_Shape stored in GEOM_Object
888 //=======================================================================
889 bool GEOMBase::GetShape( const GEOM::GEOM_Object_ptr& theObject, TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType )
890 {
891   if ( !CORBA::is_nil( theObject ) )
892   {
893     TopoDS_Shape aTopoDSShape = GEOM_Client().GetShape(  GeometryGUI::GetGeomGen(), theObject );
894     if ( !aTopoDSShape.IsNull() && ( theType == TopAbs_SHAPE || theType == aTopoDSShape.ShapeType() ) )
895     {
896        theShape = aTopoDSShape;
897        return true;
898     }
899   }
900   return false;
901 }
902
903 //=======================================================================
904 // function : GetName()
905 // purpose  : Get name of object
906 //=======================================================================
907 QString GEOMBase::GetName( GEOM::GEOM_Object_ptr theObj )
908 {
909   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
910
911   if ( appStudy )
912   {
913     CORBA::String_var anIOR = SalomeApp_Application::orb()->object_to_string( theObj );
914     if ( strcmp(anIOR.in(), "") != 0 )
915     {
916       _PTR(SObject) aSObj ( appStudy->studyDS()->FindObjectIOR( std::string( anIOR ) ) );
917
918       _PTR(GenericAttribute) anAttr;
919
920       if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") )
921       {
922         _PTR(AttributeName) aNameAttr ( anAttr );
923         return QString( aNameAttr->Value().c_str() );
924       }
925     }
926   }
927
928   return QString("");
929 }
930
931 bool GEOMBase::IsShape( GEOM::GEOM_Object_ptr theObj )
932 {
933   return !theObj->_is_nil() && theObj->IsShape();
934 }