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 "GEOMBase_aParameterDlg.h"
29
30 #include <GeometryGUI.h>
31 #include <GEOM_Client.hxx>
32
33 ////  SALOME Includes
34 #include <GEOM_Actor.h>
35 #include <SVTK_ViewModel.h>
36 #include <SVTK_ViewWindow.h>
37 #include <OCCViewer_ViewPort3d.h>
38 #include <OCCViewer_ViewModel.h>
39 #include <OCCViewer_ViewWindow.h>
40
41 #include <SALOME_ListIO.hxx>
42 #include <SALOME_ListIteratorOfListIO.hxx>
43
44 #include <SUIT_Desktop.h>
45 #include <SUIT_Session.h>
46 #include <SUIT_ViewManager.h>
47 #include <SUIT_ViewWindow.h>
48 #include <SUIT_MessageBox.h>
49 #include <SalomeApp_Application.h>
50 #include <SalomeApp_Study.h>
51
52 // // Open CASCADE Includes
53 #include <BRep_Tool.hxx>
54 #include <BRepAdaptor_Curve.hxx>
55 #include <BRepAdaptor_Surface.hxx>
56 #include <BRepPrimAPI_MakeCone.hxx>
57
58 #include <AIS_ListIteratorOfListOfInteractive.hxx>
59 #include <AIS_ListOfInteractive.hxx>
60
61 #include <TopAbs.hxx>
62 #include <TopExp.hxx>
63 #include <TopTools_IndexedMapOfShape.hxx>
64
65 #include <Precision.hxx>
66
67 #include <vtkRenderer.h>
68
69 #include <set>
70
71 using namespace std;
72
73
74 //=====================================================================================
75 // function : GetShapeFromIOR()
76 // purpose  : exist also as static method !
77 //=====================================================================================
78 TopoDS_Shape GEOMBase::GetShapeFromIOR(QString IOR)
79 {
80   TopoDS_Shape result;
81   if(IOR.trimmed().isEmpty())
82     return result;
83
84   CORBA::Object_var obj = SalomeApp_Application::orb()->string_to_object(IOR.toLatin1().data());
85   if(CORBA::is_nil(obj))
86     return result;
87   GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( obj );
88   if (GeomObject->_is_nil())
89     return result;
90
91   result = GEOM_Client().GetShape(GeometryGUI::GetGeomGen(), GeomObject);
92   return result;
93 }
94
95
96 //=====================================================================================
97 // function : GetIndex()
98 // purpose  : Get the index of a sub shape in a main shape : index start at 1
99 //=====================================================================================
100 int GEOMBase::GetIndex(const TopoDS_Shape& subshape, const TopoDS_Shape& shape, int /*ShapeType*/)
101 {
102   if(shape.IsNull() || subshape.IsNull())
103     return -1;
104
105   TopTools_IndexedMapOfShape anIndices;
106   TopExp::MapShapes(shape, anIndices);
107   if(anIndices.Contains(subshape)) return anIndices.FindIndex(subshape);
108
109   return -1;
110 }
111
112
113 //=======================================================================
114 // function : GetTopoFromSelection()
115 // purpose  : Define tds from a single selection and retuen true
116 //=======================================================================
117 bool GEOMBase::GetTopoFromSelection(const SALOME_ListIO& aList, TopoDS_Shape& tds)
118 {
119   if(aList.Extent() != 1)
120     return false;
121
122   Handle(SALOME_InteractiveObject) IO = aList.First();
123   /* case SObject */
124   if(IO->hasEntry()) {
125     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
126     if ( !appStudy ) return false;
127     _PTR(Study) aStudy = appStudy->studyDS();
128
129     _PTR(SObject) obj ( aStudy->FindObjectID(IO->getEntry()) );
130     _PTR(GenericAttribute) anAttr;
131     if( obj ) {
132       if(obj->FindAttribute(anAttr, "AttributeIOR")) {
133         _PTR(AttributeIOR) anIOR ( anAttr );
134         tds = GetShapeFromIOR(anIOR->Value().c_str());
135         if(tds.IsNull())
136           return false;
137         else
138           return true;
139       }
140     }
141   }
142
143   return false;
144 }
145
146 //=======================================================================
147 // function : GetNameOfSelectedIObjects()
148 // purpose  : Define the name geom++ or other name of mono or multi sel.
149 //=======================================================================
150 int GEOMBase::GetNameOfSelectedIObjects( const SALOME_ListIO& aList,
151                                          QString&          theName,
152                                          const bool        theShapesOnly )
153 {
154   if ( !theShapesOnly )
155     {
156       int nbSel = aList.Extent();
157       if ( nbSel == 1 )
158         {
159           Handle(SALOME_InteractiveObject) anIObj = aList.First();
160           if(anIObj->hasEntry()) {
161             SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
162             if ( !appStudy ) return nbSel;
163             _PTR(Study) aStudy = appStudy->studyDS();
164
165             _PTR(SObject) obj ( aStudy->FindObjectID(anIObj->getEntry()) );
166
167             _PTR(GenericAttribute) anAttr;
168
169             if ( obj && obj->FindAttribute( anAttr, "AttributeName") )
170               {
171                 _PTR(AttributeName) aNameAttr ( anAttr );
172                 theName = aNameAttr->Value().c_str();
173               }
174           }
175         }
176       else
177         theName = QObject::tr("%1_objects").arg(nbSel);
178
179       return nbSel;
180     }
181   else
182     {
183       GEOM::ListOfGO anObjs;
184       ConvertListOfIOInListOfGO( aList, anObjs, theShapesOnly );
185       if ( anObjs.length() == 1 ) {
186         theName = GetName( anObjs[ 0 ] );
187       }
188       else
189         theName = QString( "%1_objects" ).arg( anObjs.length() );
190
191       return anObjs.length();
192     }
193 }
194
195
196 //=================================================================================
197 // function : GetShapeTypeString()
198 // purpose  : for a single shape
199 //=================================================================================
200 bool GEOMBase::GetShapeTypeString(const TopoDS_Shape& aShape, Standard_CString& aTypeString)
201 {
202   if(aShape.IsNull()) {
203     aTypeString = "aNullShape";
204     return false;
205   }
206   switch(aShape.ShapeType())
207     {
208     case TopAbs_COMPOUND:
209       {
210         aTypeString = CORBA::string_dup(QObject::tr("GEOM_COMPOUND").toLatin1().constData());
211         return true;
212       }
213     case  TopAbs_COMPSOLID:
214       {
215         aTypeString = CORBA::string_dup(QObject::tr("GEOM_COMPOUNDSOLID").toLatin1().constData()) ;
216         return true ;
217       }
218     case TopAbs_SOLID:
219       {
220         aTypeString = CORBA::string_dup(QObject::tr("GEOM_SOLID").toLatin1().constData()) ;
221         return true ;
222       }
223     case TopAbs_SHELL:
224       {
225         aTypeString = CORBA::string_dup(QObject::tr("GEOM_SHELL").toLatin1().constData()) ;
226         return true ;
227       }
228     case TopAbs_FACE:
229       {
230         BRepAdaptor_Surface surf(TopoDS::Face(aShape));
231         if(surf.GetType() == GeomAbs_Plane) {
232           aTypeString = CORBA::string_dup(QObject::tr("GEOM_PLANE").toLatin1().constData());
233           return true;
234         }
235         else if(surf.GetType() == GeomAbs_Cylinder) {
236           aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFCYLINDER").toLatin1().constData());
237           return true;
238         }
239         else if(surf.GetType() == GeomAbs_Sphere) {
240           aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFSPHERE").toLatin1().constData());
241           return true ;
242         }
243         else if(surf.GetType() == GeomAbs_Torus) {
244           aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFTORUS").toLatin1().constData());
245           return true ;
246         }
247         else if(surf.GetType() == GeomAbs_Cone) {
248           aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFCONE").toLatin1().constData());
249           return true ;
250         }
251         else {
252           aTypeString = CORBA::string_dup(QObject::tr("GEOM_FACE").toLatin1().constData());
253           return true;
254         }
255       }
256     case TopAbs_WIRE:
257       {
258         aTypeString = CORBA::string_dup(QObject::tr("GEOM_WIRE").toLatin1().constData());
259         return true;
260       }
261     case TopAbs_EDGE:
262       {
263         BRepAdaptor_Curve curv(TopoDS::Edge(aShape));
264         if(curv.GetType() == GeomAbs_Line) {
265           if((Abs(curv.FirstParameter()) >= 1E6) || (Abs(curv.LastParameter()) >= 1E6))
266             aTypeString = CORBA::string_dup(QObject::tr("GEOM_LINE").toLatin1().constData());
267           else
268             aTypeString = CORBA::string_dup(QObject::tr("GEOM_EDGE").toLatin1().constData());
269           return true;
270         }
271         else if(curv.GetType() == GeomAbs_Circle) {
272           if(curv.IsClosed())
273             aTypeString = CORBA::string_dup(QObject::tr("GEOM_CIRCLE").toLatin1().constData());
274           else
275             aTypeString = CORBA::string_dup(QObject::tr("GEOM_ARC").toLatin1().constData());
276         return true;
277       }
278         else {
279           aTypeString = CORBA::string_dup(QObject::tr("GEOM_EDGE").toLatin1().constData());
280           return true;
281         }
282       }
283     case TopAbs_VERTEX:
284       {
285         aTypeString = CORBA::string_dup(QObject::tr("GEOM_VERTEX").toLatin1().constData());
286         return true;
287       }
288     case TopAbs_SHAPE:
289       {
290         aTypeString = CORBA::string_dup(QObject::tr("GEOM_SHAPE").toLatin1().constData());
291         return true;
292       }
293     }
294   return false;
295 }
296
297
298 //=======================================================================
299 // function : ConvertIORinGEOMAISShape()
300 // purpose  :
301 //=======================================================================
302 Handle(GEOM_AISShape) GEOMBase::ConvertIORinGEOMAISShape(const char * IOR, Standard_Boolean& testResult, bool onlyInActiveView)
303 {
304   Handle(GEOM_AISShape) resultShape;
305   testResult = false;
306
307   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
308   if ( !appStudy ) return resultShape;
309   _PTR(Study) aStudy = appStudy->studyDS();
310
311   _PTR(SObject) anObj ( aStudy->FindObjectIOR( IOR ) );
312   if ( !anObj )
313     return resultShape;
314
315   QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
316
317   QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
318   while ( it.hasNext() ) {
319     SUIT_ViewWindow* aVW = it.next();
320     if (aVW && aVW->getViewManager()->getType() == OCCViewer_Viewer::Type()) {
321       Handle (AIS_InteractiveContext) ic = ((OCCViewer_Viewer*)aVW->getViewManager()->getViewModel())->getAISContext();
322
323       AIS_ListOfInteractive List;
324       ic->DisplayedObjects(List);
325       AIS_ListOfInteractive List1;
326       ic->ObjectsInCollector(List1);
327       List.Append(List1);
328
329       AIS_ListIteratorOfListOfInteractive ite(List);
330       while(ite.More()) {
331         if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
332           Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
333           if(aSh->hasIO()) {
334             Handle(SALOME_InteractiveObject) GIO = Handle(SALOME_InteractiveObject)::DownCast(aSh->getIO());
335             if(GIO->hasEntry() && strcmp(GIO->getEntry(), anObj->GetID().c_str()) == 0) {
336               if(!onlyInActiveView ||
337                  aVW == SUIT_Session::session()->activeApplication()->desktop()->activeWindow()) {
338                 testResult = true;
339                 resultShape = aSh;
340                 return resultShape;
341               }
342             }
343           }
344         }
345         ite.Next();
346       }
347     }
348   }
349   return  resultShape;
350 }
351
352
353 //=======================================================================
354 // function : ConvertIORinGEOMActor()
355 // purpose  :
356 //=======================================================================
357 GEOM_Actor* GEOMBase::ConvertIORinGEOMActor(const char* IOR, Standard_Boolean& testResult, bool onlyInActiveView)
358 {
359   testResult = false;
360
361   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
362   if ( !appStudy ) return GEOM_Actor::New();
363   _PTR(Study) aStudy = appStudy->studyDS();
364
365   _PTR(SObject) anObj ( aStudy->FindObjectIOR( IOR ) );
366   if ( !anObj )
367     return GEOM_Actor::New();
368
369   QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
370
371   QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
372   while ( it.hasNext() ) {
373     SUIT_ViewWindow* aVW = it.next();
374     if (aVW && aVW->getViewManager()->getType() == SVTK_Viewer::Type()) {
375       SVTK_ViewWindow* aVTKViewWindow = dynamic_cast<SVTK_ViewWindow*>( aVW );
376       if( !aVTKViewWindow )
377         continue;
378       vtkRenderer* Renderer = aVTKViewWindow->getRenderer();
379       vtkActorCollection* theActors = Renderer->GetActors();
380       theActors->InitTraversal();
381       vtkActor *ac = theActors->GetNextActor();
382       while(!(ac==NULL)) {
383         if( ac->IsA("GEOM_Actor")) {
384           GEOM_Actor* anActor = GEOM_Actor::SafeDownCast(ac);
385           if(anActor->hasIO()) {
386             Handle(SALOME_InteractiveObject) GIO = Handle(SALOME_InteractiveObject)::DownCast(anActor->getIO());
387             if(GIO->hasEntry() && strcmp(GIO->getEntry(), anObj->GetID().c_str()) == 0) {
388               if(!onlyInActiveView ||
389                  aVW == SUIT_Session::session()->activeApplication()->desktop()->activeWindow()) {
390                 testResult = true;
391                 return anActor;
392               }
393             }
394           }
395         }
396         ac = theActors->GetNextActor();
397       }
398     }
399   }
400   testResult = false;
401   return GEOM_Actor::New();
402 }
403
404 //=======================================================================
405 // function : GetAIS()
406 // purpose  :
407 //=======================================================================
408 Handle(AIS_InteractiveObject) GEOMBase::GetAIS( const Handle(SALOME_InteractiveObject)& theIO,
409                                                 const bool                              isOnlyInActiveView )
410 {
411   if ( theIO.IsNull() || !theIO->hasEntry() )
412     return Handle(AIS_InteractiveObject)();
413
414   QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
415
416   QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
417   while ( it.hasNext() ) {
418     SUIT_ViewWindow* aVW = it.next();
419     if (!aVW || aVW->getViewManager()->getType() != OCCViewer_Viewer::Type())
420       continue;
421     Handle (AIS_InteractiveContext) anIC = ((OCCViewer_Viewer*)aVW->getViewManager()->getViewModel())->getAISContext();
422
423     AIS_ListOfInteractive aList;
424     anIC->DisplayedObjects( aList );
425     anIC->ObjectsInCollector( aList );
426
427     AIS_ListIteratorOfListOfInteractive anIter( aList );
428     for ( ; anIter.More(); anIter.Next() )
429     {
430       Handle(SALOME_InteractiveObject) anObj =
431         Handle(SALOME_InteractiveObject)::DownCast( anIter.Value()->GetOwner() );
432
433       if( !anObj.IsNull() && strcmp( anObj->getEntry(), theIO->getEntry() ) == 0 )
434       {
435         if( isOnlyInActiveView )
436         {
437           if ( aVW == SUIT_Session::session()->activeApplication()->desktop()->activeWindow() )
438             return anIter.Value();
439         }
440         else
441           return anIter.Value();
442       }
443     }
444   }
445
446   return Handle(AIS_InteractiveObject)();
447 }
448
449
450 //=======================================================================
451 // function : ConvertIOinGEOMAISShape()
452 // purpose  :
453 //=======================================================================
454 Handle(GEOM_AISShape) GEOMBase::ConvertIOinGEOMAISShape(const Handle(SALOME_InteractiveObject)& IO, Standard_Boolean& testResult, bool onlyInActiveView)
455 {
456   Handle(GEOM_AISShape) res;
457
458   if ( !IO->hasEntry() )
459   {
460     testResult = false;
461     return res;
462   }
463
464   QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
465
466   QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
467   while ( it.hasNext() ) {
468     SUIT_ViewWindow* aVW = it.next();
469     if (aVW && aVW->getViewManager()->getType() == OCCViewer_Viewer::Type()) {
470       Handle (AIS_InteractiveContext) ic = ((OCCViewer_Viewer*)aVW->getViewManager()->getViewModel())->getAISContext();
471
472       AIS_ListOfInteractive List;
473       ic->DisplayedObjects(List);
474       AIS_ListOfInteractive List1;
475       ic->ObjectsInCollector(List1);
476       List.Append(List1);
477
478       AIS_ListIteratorOfListOfInteractive ite(List);
479       while(ite.More())
480       {
481         if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape)))
482         {
483           Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
484           if( aSh->hasIO() )
485           {
486             if( strcmp( aSh->getIO()->getEntry(), IO->getEntry() ) == 0 )
487             {
488               if(onlyInActiveView)
489               {
490                 if(aVW == SUIT_Session::session()->activeApplication()->desktop()->activeWindow())
491                 {
492                   testResult = true;
493                   return aSh;
494                 }
495               }
496               else
497               {
498                 testResult = true;
499                 return aSh;
500               }
501             }
502           }
503         }
504         ite.Next();
505       }
506     }
507   }
508   testResult = false;
509   return res;
510 }
511
512
513 //=======================================================================
514 // function : ConvertIOinGEOMShape()
515 // purpose  :
516 //=======================================================================
517 GEOM::GEOM_Object_ptr GEOMBase::ConvertIOinGEOMShape(const Handle(SALOME_InteractiveObject)& IO, Standard_Boolean& testResult)
518 {
519   GEOM::GEOM_Object_var aShape;
520   testResult = false;
521
522   /* case SObject */
523   if(IO->hasEntry()) {
524     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
525     if ( !appStudy ) return GEOM::GEOM_Object::_nil();
526     _PTR(Study) aStudy = appStudy->studyDS();
527
528     _PTR(SObject) obj ( aStudy->FindObjectID(IO->getEntry()) );
529     _PTR(GenericAttribute) anAttr;
530     if(obj) {
531       if(obj->FindAttribute(anAttr, "AttributeIOR")) {
532         _PTR(AttributeIOR) anIOR ( anAttr );
533         aShape = GeometryGUI::GetGeomGen()->GetIORFromString(anIOR->Value().c_str());
534         if(!CORBA::is_nil(aShape))
535           testResult = true;
536         return aShape._retn();
537       }
538     }
539   }
540   return GEOM::GEOM_Object::_nil();
541 }
542
543
544 //=======================================================================
545 // function : ConvertListOfIOInListOfIOR()
546 // purpose  :
547 //=======================================================================
548 void GEOMBase::ConvertListOfIOInListOfIOR(const SALOME_ListIO& aList, GEOM::string_array& listIOR)
549 {
550   int nbSel = aList.Extent();
551   listIOR.length(nbSel);
552   int j=0;
553   SALOME_ListIteratorOfListIO It(aList);
554   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
555     ( SUIT_Session::session()->activeApplication()->activeStudy() );
556   if ( !appStudy ) return;
557   _PTR(Study) aStudy = appStudy->studyDS();
558
559   for (int i=0; It.More(); It.Next(), i++) {
560     Handle(SALOME_InteractiveObject) IObject = It.Value();
561     if (IObject->hasEntry()) {
562       _PTR(SObject) obj ( aStudy->FindObjectID(IObject->getEntry()) );
563       _PTR(GenericAttribute) anAttr;
564       if (obj && obj->FindAttribute(anAttr, "AttributeIOR")) {
565         _PTR(AttributeIOR) anIOR (anAttr);
566         //CORBA::Object_var theObj = dynamic_cast<SALOMEDS_Study*>
567         //  (aStudy.get())->ConvertIORToObject(anIOR->Value());
568         CORBA::Object_var theObj = GeometryGUI::ClientSObjectToObject(obj);
569         if (!CORBA::is_nil(theObj) && theObj->_is_a("IDL:GEOM/GEOM_Object:1.0")) {
570           listIOR[j] = CORBA::string_dup(anIOR->Value().c_str());
571           j++;
572         }
573       }
574     }
575   }
576   listIOR.length(j);
577 }
578
579
580 //=======================================================================
581 // function : ConvertIOinGEOMObject()
582 // purpose  :
583 //=======================================================================
584 GEOM::GEOM_Object_ptr GEOMBase::ConvertIOinGEOMObject( const Handle(SALOME_InteractiveObject)& theIO,
585                                                        Standard_Boolean& theResult )
586 {
587   theResult = Standard_False;
588   GEOM::GEOM_Object_var aReturnObject;
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 : Parameter()
746 // purpose  : return a parameter (float) from a dialog box
747 //
748 //  avalue1    : is a float or integer used as a default value displayed
749 //  aTitle1    : is the title for aValue1
750 //  aTitle     : is the main title
751 //  bottom     : maximum value to be entered
752 //  top        : minimum value to be entered
753 //  decimals   : number of decimals
754 //=======================================================================
755 double GEOMBase::Parameter(Standard_Boolean& res, const char* aValue1, const char* aTitle1, const char* aTitle, const double bottom, const double top, const int decimals)
756 {
757   GEOMBase_aParameterDlg * Dialog = new GEOMBase_aParameterDlg(aValue1, aTitle1, SUIT_Session::session()->activeApplication()->desktop(),
758                                                                aTitle, TRUE, 0, bottom, top, decimals);
759   int r = Dialog->exec();
760   float X = 0.0;
761   if(r == QDialog::Accepted) {
762     res = Standard_True;
763     X = Dialog->getValue();
764   }
765   else
766     res = Standard_False;
767   delete Dialog;
768   return X;
769 }
770
771
772 //=======================================================================
773 // function : SelectionByNameInDialogs()
774 // purpose  : Called when user has entered a name of object in a LineEdit.
775 //          : The selection is changed. Dialog box will receive the
776 //          : corresponding signal to manage this event.
777 //=======================================================================
778 bool GEOMBase::SelectionByNameInDialogs(QWidget* aWidget, const QString& objectUserName, const SALOME_ListIO& aList)
779 {
780   /* Find SObject with name in component GEOM */
781   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
782   if ( !appStudy ) return false;
783   _PTR(Study) ST = appStudy->studyDS();
784
785   std::vector<_PTR(SObject)> listSO;
786   listSO = ST->FindObjectByName(objectUserName.toStdString(), "GEOM");
787
788   if(listSO.size() < 1) {
789     const QString caption  = QObject::tr("GEOM_WRN_WARNING");
790     const QString text = QObject::tr("GEOM_NAME_INCORRECT");
791     const QString button0  = QObject::tr("GEOM_BUT_OK");
792     SUIT_MessageBox::critical(aWidget, caption, text, button0);
793     return false;
794   }
795   /* More than one object with same name */
796   if(listSO.size() > 1) {
797     const QString caption  = QObject::tr("GEOM_WRN_WARNING");
798     const QString text = QObject::tr("GEOM_IDENTICAL_NAMES_SELECT_BY_MOUSE");
799     const QString button0  = QObject::tr("GEOM_BUT_OK") ;
800     SUIT_MessageBox::critical(aWidget, caption, text, button0) ;
801     listSO.clear();
802     return false;
803   }
804
805   _PTR(SObject) theObj ( listSO[0] );
806   /* Create a SALOME_InteractiveObject with a SALOME::SObject */
807   char* aCopyobjectUserName = CORBA::string_dup(objectUserName.toLatin1().constData());
808   Handle(SALOME_InteractiveObject) SI = new SALOME_InteractiveObject(theObj->GetID().c_str(), "GEOM", aCopyobjectUserName);
809   delete(aCopyobjectUserName);
810
811   /* Add as a selected object       */
812   /* Clear any previous selection : */
813   /* Warning the LineEdit is purged because of signal currentSelectionChanged ! */
814   // Sel->ClearIObjects(); //mzn
815   // Sel->AddIObject(SI); //mzn
816   return true;
817 }
818
819
820 //=======================================================================
821 // function : DefineDlgPosition()
822 // purpose  : Define x and y the default position for a dialog box
823 //=======================================================================
824 bool GEOMBase::DefineDlgPosition(QWidget* aDlg, int& x, int& y)
825 {
826   /* Here the position is on the bottom right corner - 10 */
827   SUIT_Desktop* PP = SUIT_Session::session()->activeApplication()->desktop();
828   x = abs(PP->x() + PP->size().width() - aDlg->size().width() - 10);
829   y = abs(PP->y() + PP->size().height() - aDlg->size().height() - 10);
830   return true;
831 }
832
833
834 //=======================================================================
835 // function : GetDefaultName()
836 // purpose  : Generates default names
837 //=======================================================================
838 QString GEOMBase::GetDefaultName(const QString& theOperation)
839 {
840   QString aName = "";
841
842   // collect all object names of GEOM component
843   SalomeApp_Study* appStudy =
844     dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
845   if ( !appStudy ) return aName;
846   _PTR(Study) aStudy = appStudy->studyDS();
847
848   std::set<std::string> aSet;
849   _PTR(SComponent) aGeomCompo (aStudy->FindComponent("GEOM"));
850   if (aGeomCompo) {
851     _PTR(ChildIterator) it (aStudy->NewChildIterator(aGeomCompo));
852     _PTR(SObject) obj;
853     for (it->InitEx(true); it->More(); it->Next()) {
854       obj = it->Value();
855       aSet.insert(obj->GetName());
856     }
857   }
858
859   // build a unique name
860   int aNumber = 0;
861   bool isUnique = false;
862   while (!isUnique) {
863     aName = theOperation + "_" + QString::number(++aNumber);
864     isUnique = (aSet.count(aName.toStdString()) == 0);
865   }
866
867   return aName;
868 }
869
870
871 //=======================================================================
872 // function : ShowErrorMessage()
873 // purpose  : Shows message box with error code and comment
874 //=======================================================================
875 void GEOMBase::ShowErrorMessage(const char* theErrorCode, const char* theComment)
876 {
877   QString anErrorCode(theErrorCode);
878   QString aComment(theComment);
879
880   QString aText = "";
881   if (!anErrorCode.isEmpty())
882     aText.append("\n" + QObject::tr(anErrorCode.toLatin1().constData()));
883   if (!aComment.isEmpty())
884     aText.append("\n" + QString(theComment));
885
886   SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(), QObject::tr( "GEOM_ERROR" ),
887                              QObject::tr("GEOM_PRP_ABORT") + aText, "OK" );
888 }
889
890
891 //=======================================================================
892 // function : GetObjectFromIOR()
893 // purpose  : returns a GEOM_Object by given IOR (string)
894 //=======================================================================
895 GEOM::GEOM_Object_ptr GEOMBase::GetObjectFromIOR( const char* theIOR )
896 {
897   GEOM::GEOM_Object_var anObject;
898   if ( theIOR == NULL || strlen( theIOR ) == 0 )
899     return anObject._retn(); // returning nil object
900
901   anObject = GEOM::GEOM_Object::_narrow( SalomeApp_Application::orb()->string_to_object( theIOR ) );
902   return anObject._retn();
903 }
904
905 //=======================================================================
906 // function : GetIORFromObject()
907 // purpose  : returns IOR of a given GEOM_Object
908 //=======================================================================
909 char* GEOMBase::GetIORFromObject( const GEOM::GEOM_Object_ptr& theObject )
910 {
911   if ( CORBA::is_nil( theObject ) )
912     return NULL;
913
914   return SalomeApp_Application::orb()->object_to_string( theObject );
915 }
916
917 //=======================================================================
918 // function : GetShape()
919 // purpose  : returns a TopoDS_Shape stored in GEOM_Object
920 //=======================================================================
921 bool GEOMBase::GetShape( const GEOM::GEOM_Object_ptr& theObject, TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType )
922 {
923   if ( !CORBA::is_nil( theObject ) )
924   {
925     TopoDS_Shape aTopoDSShape = GEOM_Client().GetShape(  GeometryGUI::GetGeomGen(), theObject );
926     if ( !aTopoDSShape.IsNull() && ( theType == TopAbs_SHAPE || theType == aTopoDSShape.ShapeType() ) )
927     {
928        theShape = aTopoDSShape;
929        return true;
930     }
931   }
932   return false;
933 }
934
935 //=======================================================================
936 // function : GetName()
937 // purpose  : Get name of object
938 //=======================================================================
939 QString GEOMBase::GetName( GEOM::GEOM_Object_ptr theObj )
940 {
941   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
942
943   if ( appStudy )
944   {
945     CORBA::String_var anIOR = SalomeApp_Application::orb()->object_to_string( theObj );
946     if ( strcmp(anIOR.in(), "") != 0 )
947     {
948       _PTR(SObject) aSObj ( appStudy->studyDS()->FindObjectIOR( string( anIOR ) ) );
949
950       _PTR(GenericAttribute) anAttr;
951
952       if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") )
953       {
954         _PTR(AttributeName) aNameAttr ( anAttr );
955         return QString( aNameAttr->Value().c_str() );
956       }
957     }
958   }
959
960   return QString("");
961 }
962
963 bool GEOMBase::IsShape( GEOM::GEOM_Object_ptr theObj )
964 {
965   return !theObj->_is_nil() && theObj->IsShape();
966 }