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