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