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