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