Salome HOME
DCQ : New Architecture
[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 using namespace std;
30 #include "GEOMBase.h"
31
32 ////  SALOME Includes
33 # include "Utils_ORB_INIT.hxx"
34 # include "Utils_SINGLETON.hxx"
35
36 #include "QAD_RightFrame.h"
37 #include "QAD_MessageBox.h"
38 #include "QAD_Resource.h"
39
40 #include "GEOM_AssemblyBuilder.h"
41 #include "VTKViewer_ViewFrame.h"
42 #include "OCCViewer_ViewPort3d.h"
43 #include "OCCViewer_Viewer3d.h"
44
45 #include "SALOME_ListIteratorOfListIO.hxx"
46 #include "SALOMEGUI_ImportOperation.h"
47 #include "SALOMEGUI_NameDlg.h"            
48
49 // // Open CASCADE Includes
50 #include <BRep_Tool.hxx>
51 #include <BRepAdaptor_Curve.hxx>
52 #include <BRepAdaptor_Surface.hxx>
53 #include <BRepPrimAPI_MakeCone.hxx>
54
55 #include <Geom_Circle.hxx>
56 #include <AIS_ListIteratorOfListOfInteractive.hxx>
57
58 #include <TopAbs.hxx>
59 #include <TopExp.hxx>
60 #include <TopExp_Explorer.hxx>
61 #include <TopoDS_Iterator.hxx>
62 #include <TopoDS_Compound.hxx>
63 #include <TopTools_MapOfShape.hxx>
64 #include <TopTools_ListIteratorOfListOfShape.hxx>
65
66 #include <Precision.hxx>
67
68 // // QT Includes
69 #include <qmessagebox.h>
70
71 #include "GEOMBase_aParameterDlg.h"
72
73 //=================================================================================
74 // class   : CustomItem
75 // purpose : Set Font to a text.
76 //=================================================================================
77 class CustomItem : public QCustomMenuItem
78 {
79 public:
80     CustomItem(const QString& s, const QFont& f)
81       :string(s), font(f){};
82     ~CustomItem(){}
83   
84     void paint(QPainter* p, const QColorGroup& /*cg*/, bool /*act*/,
85                bool /*enabled*/, int x, int y, int w, int h)
86     {
87       p->setFont(font);
88       p->drawText(x, y, w, h, AlignHCenter | AlignVCenter | ShowPrefix | DontClip, string);
89     }
90
91     QSize sizeHint()
92     {
93       return QFontMetrics(font).size(AlignHCenter | AlignVCenter | ShowPrefix | DontClip, string);
94     }
95
96 private:
97   QString string;
98   QFont font;
99
100 };
101
102
103 //=======================================================================
104 // function : GEOMBase()
105 // purpose  : Constructor
106 //=======================================================================
107 GEOMBase::GEOMBase() :
108   QObject()
109 {
110   myGeomGUI = GEOMContext::GetGeomGUI();
111   myGeom = myGeomGUI->myComponentGeom;
112
113   /* Shading Color */
114   QString SCr = QAD_CONFIG->getSetting("Geometry:SettingsShadingColorRed");
115   QString SCg = QAD_CONFIG->getSetting("Geometry:SettingsShadingColorGreen");
116   QString SCb = QAD_CONFIG->getSetting("Geometry:SettingsShadingColorBlue");
117   if(!SCr.isEmpty() && !SCg.isEmpty() && !SCb.isEmpty())   
118     myShadingColor = Quantity_Color(SCr.toInt()/255., SCg.toInt()/255., SCb.toInt()/255., Quantity_TOC_RGB);
119   else
120     myShadingColor = Quantity_Color(Quantity_NOC_GOLDENROD);
121
122   QString AddInStudy = QAD_CONFIG->getSetting("Geometry:SettingsAddInStudy");
123   if(!AddInStudy.isEmpty())
124     mySettings_AddInStudy = AddInStudy.toInt();
125   else
126     mySettings_AddInStudy = 1;
127 }
128
129
130 //=======================================================================
131 // function : ~GEOMBase()
132 // purpose  : Destructor
133 //=======================================================================
134 GEOMBase::~GEOMBase()
135 {
136 }
137
138
139 //=================================================================================
140 // function : CustomPopup()
141 // purpose  : [static]
142 //=================================================================================
143 bool GEOMBase::CustomPopup(QAD_Desktop* parent, QPopupMenu* popup, const QString& theContext,
144                            const QString& theParent, const QString& theObject)
145 {
146   GEOMBase* myGeomBase = new GEOMBase();
147   QAD_Study* ActiveStudy = parent->getActiveStudy();
148
149   /* Deactivate any non modal dialog box to get the neutral point */
150   myGeomBase->myGeomGUI->EmitSignalDeactivateDialog();
151
152   SALOME_Selection* Sel = SALOME_Selection::Selection(ActiveStudy->getSelection());
153   int nbSel = Sel->IObjectCount();
154   
155   if((nbSel == 0))
156     return false;
157
158   if(ActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
159     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)ActiveStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
160     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
161     if(theParent.compare("Viewer") == 0) {
162       if(theObject.compare("Component") == 0) {
163         popup->removeItem(QAD_DisplayOnly_Popup_ID);
164         return true;
165       }
166       else {
167         QFont f = QApplication::font();
168         f.setBold(TRUE);
169         if(nbSel == 1) {
170           Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
171           popup->removeItem(QAD_TopLabel_Popup_ID);
172           popup->insertItem(new CustomItem (QString(IObject->getName()), f), QAD_TopLabel_Popup_ID, 0);
173           if(IObject->hasEntry())
174             popup->setItemEnabled(804, false); //Add in Study Menu
175           else
176             popup->setItemEnabled(804, true);  //Add in Study Menu
177     
178           if(IObject->IsKind(STANDARD_TYPE(GEOM_InteractiveObject))) {
179             Standard_Boolean found;
180             Handle(GEOM_AISShape) Result = myGeomBase->ConvertIOinGEOMAISShape(IObject, found, true);
181
182             if(found) {
183               if(Result->DisplayMode() == 1)
184                 popup->changeItem(8031, tr("GEOM_MEN_WIREFRAME")); //Shading/Wireframe Menu
185               else
186                 popup->changeItem(8031, tr("GEOM_MEN_SHADING"));   //Shading/Wireframe Menu
187             }
188           }
189
190           if(!(v3d->isInViewer(IObject) && v3d->isVisible(IObject)))
191             popup->removeItem(QAD_Erase_Popup_ID);
192           else
193             popup->removeItem(QAD_Display_Popup_ID);    
194         }
195         else {
196           popup->removeItem(QAD_DisplayOnly_Popup_ID);
197           popup->removeItem(QAD_TopLabel_Popup_ID);
198           popup->insertItem(new CustomItem (tr("GEOM_MEN_POPUP_NAME").arg(nbSel), f), QAD_TopLabel_Popup_ID, 0);
199           popup->setItemEnabled(804, false); //Add in Study Menu
200         }
201       }
202       return true;
203     }
204     else if(theParent.compare("ObjectBrowser") == 0) {
205       popup->removeItem(QAD_TopLabel_Popup_ID);
206       int id = popup->idAt(0); // separator
207       if(id < 0)
208         popup->removeItem(id);
209             
210       // checking for GEOM label in the selected list
211       SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
212       Handle(SALOME_InteractiveObject) anIObject;
213
214       bool useSubItems = false;
215       bool needOpen = false;
216       bool needDisplay = false;
217       bool needErase = false;
218       SALOMEDS::GenericAttribute_var aTmpAttr;
219       for(;It.More();It.Next()) {
220         anIObject = It.Value();
221         if(!anIObject->hasEntry())
222           continue;
223
224         if(v3d->isInViewer(anIObject) && v3d->isVisible(anIObject)) 
225           needErase = true; 
226         else 
227           needDisplay = true;
228         SALOMEDS::SObject_var obj = ActiveStudy->getStudyDocument()->FindObjectID(anIObject->getEntry());
229         if(!obj->_is_nil()) {
230           GEOM::GEOM_Shape_var aShape;
231           if(obj->FindAttribute(aTmpAttr, "AttributeIOR")) {
232             char *str = SALOMEDS::AttributeIOR::_narrow(aTmpAttr)->Value();
233             if(str && strlen(str))
234               aShape = myGeomBase->myGeom->GetIORFromString(str);
235           }
236           else if(obj->FindAttribute(aTmpAttr, "AttributePersistentRef"))
237             needOpen = true;
238           
239           if(aShape->_is_nil()) {
240             SALOMEDS::ChildIterator_var anIter = ActiveStudy->getStudyDocument()->NewChildIterator(obj);
241             while(anIter->More()) {
242               SALOMEDS::SObject_var subobj = anIter->Value();
243               if(subobj->FindAttribute(aTmpAttr, "AttributeIOR")) {
244                 useSubItems = true;
245                 needErase = true;
246                 needDisplay = true;
247               }
248               else if(subobj->FindAttribute(aTmpAttr, "AttributePersistentRef")) {
249                 needOpen = true;
250                 useSubItems = true;
251               }
252               anIter->Next();
253             }
254           }
255         }
256       }
257       // remove useless popup items
258       if(nbSel != 1)
259         popup->removeItem(901); // rename
260       if(useSubItems)
261         popup->removeItem(QAD_DisplayOnly_Popup_ID); // display only
262
263       if(!needOpen) {
264         int index = popup->indexOf(9024);  // open
265         popup->removeItem(9024);
266         popup->removeItemAt(index); // separator under Open
267
268         if (!needDisplay)
269           popup->removeItem(QAD_Display_Popup_ID);
270         if (!needErase)
271           popup->removeItem(QAD_Erase_Popup_ID);
272
273         if(!needDisplay && !needErase) {
274           int id = popup->idAt(popup->count()-1); // last item
275           popup->removeItem(id); // separator
276         }
277       }
278       else {
279         popup->removeItem(QAD_DisplayOnly_Popup_ID); // display only
280         popup->removeItem(QAD_Display_Popup_ID);
281         popup->removeItem(QAD_Erase_Popup_ID);
282         if (nbSel != 1) {
283           int id = popup->idAt(popup->count()-1); // last item
284           popup->removeItem(id); // separator
285         }
286       }
287       return true;
288     }
289   }
290   else if(ActiveStudy->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
291     // MESSAGE ( " CUSTOM POPUP VIEWER VTK ")
292     if((theParent.compare("Viewer") == 0)) {
293       popup->setItemEnabled(8034, false); //Isos Menu
294       if(theObject.compare("Component") == 0) {
295         popup->removeItem(QAD_DisplayOnly_Popup_ID);
296         return true;
297       }
298
299       //int id = popup->idAt(0);
300       QFont f = QApplication::font();
301       f.setBold(TRUE);
302   
303       if(nbSel == 1) {
304         Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
305         popup->removeItem(QAD_TopLabel_Popup_ID);
306         popup->insertItem(new CustomItem (QString(IObject->getName()), f), QAD_TopLabel_Popup_ID, 0);
307         if(IObject->hasEntry()) {
308           popup->setItemEnabled(804, false); //Add in Study Menu
309           SALOMEDS::Study_var aStudy = ActiveStudy->getStudyDocument();
310           SALOMEDS::SObject_var SO = aStudy->FindObjectID(IObject->getEntry());
311           SALOMEDS::GenericAttribute_var anAttr;
312           SALOMEDS::AttributeIOR_var anIOR;
313           if(!SO->_is_nil()) {
314             if(SO->FindAttribute(anAttr, "AttributeIOR")) {
315               anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
316               Standard_Boolean found;
317               GEOM_Actor* Result = myGeomBase->ConvertIORinGEOMActor(anIOR->Value(), found, true);
318               if(found) {
319                 if(Result->getDisplayMode() == 1)
320                   popup->changeItem(8031, tr("GEOM_MEN_WIREFRAME")); //Shading/Wireframe Menu
321                 else
322                   popup->changeItem(8031, tr("GEOM_MEN_SHADING"));   //Shading/Wireframe Menu
323                               
324                 if(!Result->GetVisibility())
325                   popup->removeItem(QAD_Erase_Popup_ID);
326                 else
327                   popup->removeItem(QAD_Display_Popup_ID);                
328               }
329               else
330                 popup->removeItem(QAD_Erase_Popup_ID);
331             }
332           }
333         }
334         else
335           popup->setItemEnabled(804, true); //Add in Study Menu
336       }
337       else {
338         popup->removeItem(QAD_DisplayOnly_Popup_ID);
339         popup->removeItem(QAD_TopLabel_Popup_ID);
340         popup->insertItem( new CustomItem (tr("GEOM_MEN_POPUP_NAME").arg(nbSel), f), QAD_TopLabel_Popup_ID, 0);
341         popup->setItemEnabled(804, false); //Add in Study Menu
342       }
343       return true;
344     }
345     else if(theParent.compare("ObjectBrowser") == 0) {
346       popup->removeItem(QAD_TopLabel_Popup_ID);
347       int id = popup->idAt(0); // separator
348       if(id < 0)
349         popup->removeItem(id);
350
351       // checking for GEOM label in the selected list
352       SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
353       Handle(SALOME_InteractiveObject) anIObject;
354       
355       bool useSubItems = false;
356       bool needOpen = false;
357       bool needDisplay = false;
358       bool needErase = false;
359       SALOMEDS::GenericAttribute_var aTmpAttr;
360       VTKViewer_RenderWindowInteractor* myRenderInter = ((VTKViewer_ViewFrame*)ActiveStudy->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRWInteractor();
361       for(;It.More();It.Next()) {
362         anIObject = It.Value();
363         if(!anIObject->hasEntry())
364           continue;
365
366         if(myRenderInter->isInViewer(anIObject) && myRenderInter->isVisible(anIObject)) 
367           needErase = true; 
368         else 
369           needDisplay = true;
370
371         SALOMEDS::SObject_var obj = ActiveStudy->getStudyDocument()->FindObjectID(anIObject->getEntry());
372         if(!obj->_is_nil()) {
373           GEOM::GEOM_Shape_var aShape;
374           if(obj->FindAttribute(aTmpAttr, "AttributeIOR")) {
375             char *str = SALOMEDS::AttributeIOR::_narrow(aTmpAttr)->Value();
376             if(str && strlen(str))
377               aShape = myGeomBase->myGeom->GetIORFromString(str);
378           }
379           else if(obj->FindAttribute(aTmpAttr, "AttributePersistentRef"))
380             needOpen = true;
381
382           if(aShape->_is_nil()) {
383             SALOMEDS::ChildIterator_var anIter = ActiveStudy->getStudyDocument()->NewChildIterator(obj);
384             while(anIter->More()) {
385               SALOMEDS::SObject_var subobj = anIter->Value();
386               if(subobj->FindAttribute(aTmpAttr, "AttributeIOR")) {
387                 useSubItems = true;
388                 needDisplay = true;
389                 needErase = true;
390               }
391               else if(subobj->FindAttribute(aTmpAttr, "AttributePersistentRef")) {
392                 needOpen = true;
393                 useSubItems = true;
394               }
395               anIter->Next();
396             }
397           }
398         }
399       }
400       // remove useless popup items
401       if(nbSel != 1)
402         popup->removeItem(901); // rename
403       if(useSubItems)
404         popup->removeItem(QAD_DisplayOnly_Popup_ID); // display only
405
406       if(!needOpen) {
407         int index = popup->indexOf(9024); // open
408         popup->removeItem(9024);
409         popup->removeItemAt(index); // separator under Open
410           
411         if(!needDisplay)
412           popup->removeItem(QAD_Display_Popup_ID);
413         if(!needErase)
414           popup->removeItem(QAD_Erase_Popup_ID);
415         if(!needDisplay && !needErase) {
416           int id = popup->idAt(popup->count()-1); // last item
417           popup->removeItem(id); // separator
418         }
419       }
420       else {
421         popup->removeItem(QAD_DisplayOnly_Popup_ID); // display only
422         popup->removeItem(QAD_Display_Popup_ID);
423         popup->removeItem(QAD_Erase_Popup_ID);
424         if(nbSel!=1) {
425           int id = popup->idAt(popup->count()-1); // last item
426           popup->removeItem(id); // separator
427         }
428       }
429       return true;
430     }
431   } 
432   else { // other viewer types not supported.
433     while(1) {
434       int id = popup->idAt(0);
435       if(id <= QAD_TopLabel_Popup_ID && id != -1)
436         popup->removeItemAt(0);
437       else
438         break;
439     }
440     popup->removeItem(QAD_DisplayOnly_Popup_ID);        
441     popup->removeItem(QAD_Display_Popup_ID);
442     popup->removeItem(QAD_Erase_Popup_ID);
443     int id = popup->idAt(popup->count() - 1); // last item
444     if(id < 0 && id != -1)
445       popup->removeItem(id); // separator
446     return false;
447   }
448   return false;
449 }
450
451
452 //=====================================================================================
453 // function : Display()
454 // purpose  : Displays a CORBA shape
455 //=====================================================================================
456 bool GEOMBase::Display(GEOM::GEOM_Shape_ptr aShape, Standard_CString name)
457 {
458   // MESSAGE ( "GEOMBase::Display init ") 
459   Handle(GEOM_InteractiveObject) IO;
460   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
461   Sel->ClearIObjects();
462
463   if(aShape->_is_nil()) {
464     QAD_MessageBox::warn1 (QAD_Application::getDesktop(), tr("GEOM_WRN_WARNING"), tr("GEOM_PRP_ABORT"), tr("GEOM_BUT_OK"));
465     return false;
466   }
467   
468   TopoDS_Shape shape = myGeomGUI->GetShapeReader().GetShape(myGeom, aShape);
469   if(shape.IsNull())
470     return false;
471
472   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
473   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
474   SALOMEDS::GenericAttribute_var anAttr;
475   SALOMEDS::AttributeName_var aName;
476   SALOMEDS::AttributePixMap_var aPixmap;
477
478   SALOMEDS::SComponent_var father = aStudy->FindComponent("GEOM");
479   int aLocked = false;
480   if(father->_is_nil()) {
481     QAD_Operation* op = new SALOMEGUI_ImportOperation(QAD_Application::getDesktop()->getActiveStudy());
482     op->start();
483     aLocked = aStudy->GetProperties()->IsLocked();
484     if(aLocked) 
485       aStudy->GetProperties()->SetLocked(false);
486     father = aStudyBuilder->NewComponent("GEOM");
487     anAttr = aStudyBuilder->FindOrCreateAttribute(father, "AttributeName");
488     aName = SALOMEDS::AttributeName::_narrow(anAttr);
489     aName->SetValue(QAD_Application::getDesktop()->getComponentUserName("GEOM"));
490     anAttr = aStudyBuilder->FindOrCreateAttribute(father, "AttributePixMap");
491     aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
492     aPixmap->SetPixMap("ICON_OBJBROWSER_Geometry");
493     QAD_Application::getDesktop()->getActiveStudy()->updateObjBrowser();
494     if(aLocked)
495       aStudy->GetProperties()->SetLocked(true);
496     op->finish();
497   }
498
499   aStudyBuilder->DefineComponentInstance(father, myGeom);
500   father->ComponentIOR(myGeomGUI->GetFatherior());
501
502   TCollection_AsciiString nameG("");
503   Standard_CString Type;
504   if(TCollection_AsciiString(name).IsEqual(Standard_CString(""))) {
505     if(TCollection_AsciiString(aShape->NameType()).IsEqual(Standard_CString(""))) {
506       Standard_CString type;
507       GetShapeTypeString(shape,type);
508       aShape->NameType(type);
509       nameG += TCollection_AsciiString(type) + TCollection_AsciiString("_") + TCollection_AsciiString(myGeomGUI->GetNbGeom()++) + TCollection_AsciiString("\0");
510     } 
511     else
512       nameG += TCollection_AsciiString(aShape->NameType()) + TCollection_AsciiString("_") + TCollection_AsciiString(myGeomGUI->GetNbGeom()++) + TCollection_AsciiString("\0");
513   } 
514   else 
515     nameG = TCollection_AsciiString(name);
516   
517   // VTK
518   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
519     VTKViewer_RenderWindowInteractor* myRenderInter = ((VTKViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRWInteractor();
520     
521     vtkRenderer *theRenderer = ((VTKViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRenderer();
522     int themode = myRenderInter->GetDisplayMode();
523
524     vtkActorCollection* theActors = GEOM_AssemblyBuilder::BuildActors(shape, 0, themode, Standard_True);
525     theActors->InitTraversal();
526     vtkActor* anActor = (vtkActor*)theActors->GetNextActor();
527
528     IO = new GEOM_InteractiveObject(aShape->Name(), myGeomGUI->GetFatherior(), "GEOM");
529     while(!(anActor==NULL)) {
530       GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
531       GActor->setIO(IO);
532       GActor->setName(nameG.ToCString());
533       
534       theRenderer->AddActor(GActor);
535       anActor = (vtkActor*)theActors->GetNextActor();
536     }
537   } 
538   // OCC
539   else if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
540     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
541     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
542     Handle(GEOM_AISShape) theResult = new GEOM_AISShape(shape, nameG.ToCString());
543     theResult->SetShadingColor(myShadingColor);
544     IO = new GEOM_InteractiveObject(aShape->Name(), myGeomGUI->GetFatherior(), "GEOM");
545     theResult->setIO(IO);
546     theResult->setName(nameG.ToCString());
547     
548     /* Precaution : close any local context to permit the display */
549     if(ic->HasOpenedContext())
550       ic->CloseAllContexts();
551
552     ic->Display(theResult);
553   }
554
555   // DCQ Sel->AddIObject(IO, false);
556   QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame()->Repaint();
557   if(mySettings_AddInStudy)
558     AddInStudy(false, IO);
559
560   return true;
561 }
562
563
564 //=====================================================================================
565 // function : AddInStudy() 
566 // purpose  : anIOShape or a selected shape
567 //=====================================================================================
568 bool GEOMBase::AddInStudy(bool selection, const Handle(SALOME_InteractiveObject)& anIO)
569 {
570   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
571   if (!(!mySettings_AddInStudy || selection)) {
572     Sel->ClearIObjects();
573   }
574
575   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
576   SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
577   SALOMEDS::GenericAttribute_var anAttr;
578   SALOMEDS::AttributeName_var aName;
579   SALOMEDS::AttributePixMap_var aPixmap;
580   SALOMEDS::AttributeIOR_var anIOR;
581   SALOMEDS::AttributeSelectable_var aSelAttr;
582   
583   SALOMEDS::SComponent_var father = aStudy->FindComponent("GEOM");
584   int aLocked = false;
585   if(father->_is_nil()) {
586     QAD_Operation* op = new SALOMEGUI_ImportOperation(QAD_Application::getDesktop()->getActiveStudy());
587     op->start();
588     aLocked = aStudy->GetProperties()->IsLocked();
589     if(aLocked)
590       aStudy->GetProperties()->SetLocked(false);
591     father = aStudyBuilder->NewComponent("GEOM");
592     anAttr = aStudyBuilder->FindOrCreateAttribute(father, "AttributeName");
593     aName = SALOMEDS::AttributeName::_narrow(anAttr);
594     aName->SetValue( QAD_Application::getDesktop()->getComponentUserName("GEOM"));
595     anAttr = aStudyBuilder->FindOrCreateAttribute(father, "AttributePixMap");
596     aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
597     aPixmap->SetPixMap("ICON_OBJBROWSER_Geometry");
598     if (aLocked)
599       aStudy->GetProperties()->SetLocked(true);
600     op->finish();
601   }
602
603   aStudyBuilder->DefineComponentInstance(father, myGeom);
604   father->ComponentIOR(myGeomGUI->GetFatherior());
605   
606   SALOMEDS::SObject_var fatherSF = aStudy->FindObjectID(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->entry());
607   
608   Handle(GEOM_AISShape) GAISShape;
609   GEOM_Actor* GActor;
610   Handle(GEOM_InteractiveObject) GIO;
611   bool found = false;
612
613   // VTK
614   if (QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
615     vtkRenderer *Renderer = ((VTKViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getRenderer();
616
617     vtkActorCollection* theActors = Renderer->GetActors();
618     theActors->InitTraversal();
619     vtkActor *ac = theActors->GetNextActor();
620     while(!(ac==NULL)) {
621       if(ac->IsA("GEOM_Actor")) {
622         GEOM_Actor* anActor = GEOM_Actor::SafeDownCast(ac);
623         if(anActor->hasIO()) {
624           Handle(SALOME_InteractiveObject) IO = anActor->getIO();
625           if(IO->IsKind(STANDARD_TYPE(GEOM_InteractiveObject))) {
626             GIO = Handle(GEOM_InteractiveObject)::DownCast(IO);
627             if(anIO->isSame(GIO)) {
628               found = true;
629               GActor = anActor;
630               break;
631             }
632           }
633         }
634       }
635       ac = theActors->GetNextActor();
636     }
637     
638     if(!found)
639       return false;
640   }
641   // OCC
642   else if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
643     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
644     Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
645     
646     AIS_ListOfInteractive List;
647     ic->DisplayedObjects(List);
648     AIS_ListIteratorOfListOfInteractive ite(List);
649     while(ite.More()) {
650       if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
651         Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
652         if(aSh->hasIO()) {
653           Handle(SALOME_InteractiveObject) IO = aSh->getIO();
654           if ( IO->IsKind(STANDARD_TYPE(GEOM_InteractiveObject))) {
655             GIO = Handle(GEOM_InteractiveObject)::DownCast(IO);
656             if(anIO->isSame(GIO)) {
657               found = true;
658               GAISShape = aSh;
659               break;
660             }
661           }
662         }
663       }
664       ite.Next();
665     }
666
667     if(!found)
668       return false;
669   }
670
671   if(!mySettings_AddInStudy || selection) {
672     QString Name = SALOMEGUI_NameDlg::getName(QAD_Application::getDesktop(), anIO->getName());
673     if(!Name.isEmpty()) {
674       // VTK
675       if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK)
676         GActor->setName(strdup(Name.latin1()));
677       // OCC
678       else if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC)
679         GAISShape->setName(strdup(Name.latin1()));
680     } 
681     else
682       return false;
683   }
684
685   // open transaction
686   QAD_Operation* op = new SALOMEGUI_ImportOperation(QAD_Application::getDesktop()->getActiveStudy());
687   op->start();
688
689   SALOMEDS::SObject_var newObj = aStudyBuilder->NewObject(father);
690
691   GEOM::GEOM_Shape_var aShape = myGeom->GetIORFromString(GIO->getIOR());
692
693   /* For the shape inserted into the study we set its field 'studyshapeid'    */
694   /* so the shape will contain its corresponding entry in the study Ocaf doc. */
695   aShape->StudyShapeId(newObj->GetID());
696
697   GIO->setEntry(newObj->GetID());
698
699   anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
700   anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
701   anIOR->SetValue(aShape->Name());
702
703   anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
704   aName = SALOMEDS::AttributeName::_narrow(anAttr);
705
706   anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePixMap");
707   aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
708   if(aShape->ShapeType() == GEOM::COMPOUND)
709     aPixmap->SetPixMap("ICON_OBJBROWSER_COMPOUND");
710   else if(aShape->ShapeType() == GEOM::COMPSOLID)
711     aPixmap->SetPixMap("ICON_OBJBROWSER_COMPSOLID");
712   else if(aShape->ShapeType() == GEOM::SOLID)
713     aPixmap->SetPixMap("ICON_OBJBROWSER_SOLID");
714   else if(aShape->ShapeType() == GEOM::SHELL)
715     aPixmap->SetPixMap("ICON_OBJBROWSER_SHELL");
716   else if(aShape->ShapeType() == GEOM::FACE)
717     aPixmap->SetPixMap("ICON_OBJBROWSER_FACE");
718   else if(aShape->ShapeType() == GEOM::WIRE)
719     aPixmap->SetPixMap("ICON_OBJBROWSER_WIRE");
720   else if(aShape->ShapeType() == GEOM::EDGE)
721     aPixmap->SetPixMap("ICON_OBJBROWSER_EDGE");
722   else if(aShape->ShapeType() == GEOM::VERTEX)
723     aPixmap->SetPixMap("ICON_OBJBROWSER_VERTEX");
724
725   // VTK
726   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
727     GActor->setIO(GIO);
728     aName->SetValue(GActor->getName());
729   }
730   // OCC
731   else if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
732     GAISShape->setIO(GIO);
733     aName->SetValue(GAISShape->getName());
734   }
735
736   SALOMEDS::SObject_var newObj1 = aStudyBuilder->NewObject(fatherSF);
737   aStudyBuilder->Addreference(newObj1,newObj);
738
739   GEOM::GEOM_Gen::ListOfIOR_var listIOR = new GEOM::GEOM_Gen::ListOfIOR;
740   listIOR = myGeom->GetReferencedObjects(aShape);
741
742   if(listIOR->length()>0) {
743     SALOMEDS::SObject_var Arguments = aStudyBuilder->NewObject(newObj);
744     anAttr = aStudyBuilder->FindOrCreateAttribute(Arguments, "AttributeName");
745     aName = SALOMEDS::AttributeName::_narrow(anAttr);
746     aName->SetValue(tr("GEOM_ARGUMENTS"));
747     anAttr = aStudyBuilder->FindOrCreateAttribute(Arguments, "AttributeSelectable");
748     aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
749     aSelAttr->SetSelectable(false);
750
751     bool ObjectReferenced = false;
752     for (unsigned int ind = 0; ind < listIOR->length();ind++) {
753       SALOMEDS::SObject_var theObj = aStudy->FindObjectIOR(listIOR[ind]);
754
755       if(!theObj->_is_nil()) {
756         SALOMEDS::SObject_var RefObject = aStudyBuilder->NewObject(Arguments);
757         aStudyBuilder->Addreference(RefObject, theObj);
758         ObjectReferenced = true;
759       }
760     }
761
762     if(!ObjectReferenced)
763       aStudyBuilder->RemoveObject(Arguments);
764   }
765   op->finish();
766
767   if(!mySettings_AddInStudy || selection)
768     QAD_Application::getDesktop()->getActiveStudy()->updateObjBrowser();
769   else {
770     QAD_Application::getDesktop()->getActiveStudy()->updateObjBrowser(false);
771     // DCQ Sel->AddIObject(GIO);
772   }
773   return true;
774 }
775
776
777 //=====================================================================================
778 // function : GetShapeFromIOR()
779 // purpose  : exist also as static method !
780 //=====================================================================================
781 TopoDS_Shape GEOMBase::GetShapeFromIOR(QString IOR)
782 {
783   TopoDS_Shape result;
784   if(IOR.stripWhiteSpace().isEmpty())
785     return result;
786   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
787   CORBA::ORB_var& _orb = init.orb() ;
788   CORBA::Object_var obj = _orb->string_to_object((char*)(IOR.latin1()));
789   if(CORBA::is_nil(obj))
790     return result;
791   GEOM::GEOM_Shape_var aShape = myGeom->GetIORFromString(IOR);
792   if(!aShape->_is_nil())
793     result = myGeomGUI->GetShapeReader().GetShape(myGeom, aShape);
794   return result;
795 }
796
797
798 //=====================================================================================
799 // function : GetIndex()
800 // purpose  : Get the index of a sub shape in a main shape : index start at 1
801 //=====================================================================================
802 int GEOMBase::GetIndex(const TopoDS_Shape& subshape, const TopoDS_Shape& shape, int /*ShapeType*/) 
803
804   if(shape.IsNull() || subshape.IsNull()) 
805     return -1; 
806   
807   int index = 1; 
808   if(subshape.ShapeType() == TopAbs_COMPOUND) { 
809     TopoDS_Iterator it; 
810     TopTools_ListOfShape CL; 
811     CL.Append(shape); 
812     TopTools_ListIteratorOfListOfShape itC; 
813     for(itC.Initialize(CL); itC.More(); itC.Next()) { 
814       for(it.Initialize(itC.Value()); it.More(); it.Next()) { 
815         if(it.Value().ShapeType() == TopAbs_COMPOUND) {
816           if(it.Value().IsSame(subshape)) 
817             return index; 
818           else 
819             index++; 
820           CL.Append(it.Value()); 
821         }
822       } 
823     }
824   }
825   else { 
826     TopExp_Explorer Exp(shape, subshape.ShapeType()); 
827     TopTools_MapOfShape M; 
828     while(Exp.More()) {
829       if(M.Add(Exp.Current())) { 
830         if(Exp.Current().IsSame(subshape)) 
831           return index; 
832         index++; 
833       } 
834       Exp.Next(); 
835     } 
836   } 
837   return -1; 
838 }
839
840
841 //=======================================================================
842 // function : GetTopoFromSelection()
843 // purpose  : Define tds from a single selection and retuen true
844 //=======================================================================
845 bool GEOMBase::GetTopoFromSelection(SALOME_Selection *Sel, TopoDS_Shape& tds) 
846 {
847   if(Sel->IObjectCount() != 1)
848     return false;
849   
850   Handle(SALOME_InteractiveObject) IO = Sel->firstIObject();
851   /* case SObject */
852   if(IO->hasEntry()) {
853     SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
854     SALOMEDS::SObject_var obj = aStudy->FindObjectID(IO->getEntry());
855     SALOMEDS::GenericAttribute_var anAttr;
856     SALOMEDS::AttributeIOR_var anIOR;
857     if(!obj->_is_nil()) {
858       if(obj->FindAttribute(anAttr, "AttributeIOR")) {
859         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
860         tds = this->GetShapeFromIOR(anIOR->Value());
861         if(tds.IsNull())
862           return false;
863         else
864           return true;
865       }
866     }
867   }
868
869   if(IO->IsInstance(STANDARD_TYPE(GEOM_InteractiveObject))) {
870     Standard_CString ior = "";
871     Handle(GEOM_InteractiveObject) GIObject = Handle(GEOM_InteractiveObject)::DownCast(IO);
872     ior = GIObject->getIOR();
873     tds = this->GetShapeFromIOR(ior);
874     if(tds.IsNull())
875       return false;
876     else
877       return true;
878   }
879   
880   return false;
881 }
882
883
884 //=======================================================================
885 // function : GetNameOfSelectedIObjects()
886 // purpose  : Define the name geom++ or other name of mono or multi sel.
887 //=======================================================================
888 int GEOMBase::GetNameOfSelectedIObjects(SALOME_Selection* Sel, QString& aName)
889 {
890   int nbSel = Sel->IObjectCount();
891   if(nbSel == 1) {
892     Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
893     aName = IObject->getName();
894   } 
895   else {
896     aName = tr("%1_objects").arg(nbSel);
897   }
898   return nbSel;
899 }
900
901
902 //=================================================================================
903 // function : GetShapeTypeString()
904 // purpose  : for a single shape
905 //=================================================================================
906 bool GEOMBase::GetShapeTypeString(const TopoDS_Shape& aShape, Standard_CString& aTypeString)
907 {
908   if(aShape.IsNull()) {
909     aTypeString = "aNullShape";
910     return false;
911   }
912   switch(aShape.ShapeType())
913     {
914     case TopAbs_COMPOUND:
915       {
916         aTypeString = strdup(tr("GEOM_COMPOUND"));
917         return true;
918       }
919     case  TopAbs_COMPSOLID:
920       {
921         aTypeString = strdup(tr("GEOM_COMPOUNDSOLID")) ; 
922         return true ;
923       }
924     case TopAbs_SOLID:
925       {
926         aTypeString = strdup(tr("GEOM_SOLID")) ; 
927         return true ;
928       }
929     case TopAbs_SHELL:
930       {
931         aTypeString = strdup(tr("GEOM_SHELL")) ; 
932         return true ;
933       }
934     case TopAbs_FACE:
935       { 
936         BRepAdaptor_Surface surf(TopoDS::Face(aShape));
937         if(surf.GetType() == GeomAbs_Plane) {
938           aTypeString = strdup(tr("GEOM_PLANE"));
939           return true;
940         }
941         else if(surf.GetType() == GeomAbs_Cylinder) {
942           aTypeString = strdup(tr("GEOM_SURFCYLINDER"));
943           return true;
944         }
945         else if(surf.GetType() == GeomAbs_Sphere) {
946           aTypeString = strdup(tr("GEOM_SURFSPHERE"));
947           return true ;
948         }
949         else if(surf.GetType() == GeomAbs_Torus) {
950           aTypeString = strdup(tr("GEOM_SURFTORUS"));
951           return true ;
952         }
953         else if(surf.GetType() == GeomAbs_Cone) {
954           aTypeString = strdup(tr("GEOM_SURFCONE"));
955           return true ;
956         }
957         else {
958           aTypeString = strdup(tr("GEOM_FACE"));
959           return true;
960         }
961       }
962     case TopAbs_WIRE:
963       {
964         aTypeString = strdup(tr("GEOM_WIRE")); 
965         return true;
966       }
967     case TopAbs_EDGE:
968       {
969         BRepAdaptor_Curve curv(TopoDS::Edge(aShape));
970         if(curv.GetType() == GeomAbs_Line) {
971           if((Abs(curv.FirstParameter()) >= 1E6) || (Abs(curv.LastParameter()) >= 1E6))
972             aTypeString = strdup(tr("GEOM_LINE"));
973           else
974             aTypeString = strdup(tr("GEOM_EDGE"));
975           return true;
976         }
977         else if(curv.GetType() == GeomAbs_Circle) {
978           if(curv.IsClosed())
979             aTypeString = strdup(tr("GEOM_CIRCLE"));
980           else
981             aTypeString = strdup(tr("GEOM_ARC"));
982         return true;
983       } 
984         else {
985           aTypeString = strdup(tr("GEOM_EDGE"));
986           return true;
987         }
988       }
989     case TopAbs_VERTEX:
990       {
991         aTypeString = strdup(tr("GEOM_VERTEX"));
992         return true;
993       }
994     case TopAbs_SHAPE:
995       {
996         aTypeString = strdup(tr("GEOM_SHAPE"));
997         return true;
998       }
999     }
1000   return false;
1001 }
1002
1003
1004 //=======================================================================
1005 // function : ConvertIORinGEOMAISShape()
1006 // purpose  :
1007 //=======================================================================
1008 Handle(GEOM_AISShape) GEOMBase::ConvertIORinGEOMAISShape(const char * IOR, Standard_Boolean& testResult, bool onlyInActiveView)
1009 {
1010   Handle(GEOM_AISShape) resultShape;
1011   testResult = false;
1012   int nbSf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFramesCount();
1013   for(int i = 0; i < nbSf; i++) {
1014     QAD_StudyFrame* sf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i);
1015     if(sf->getTypeView() == VIEW_OCC) {
1016       OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)sf->getRightFrame()->getViewFrame())->getViewer();
1017       Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
1018
1019       AIS_ListOfInteractive List;
1020       ic->DisplayedObjects(List);
1021       AIS_ListOfInteractive List1;
1022       ic->ObjectsInCollector(List1);
1023       List.Append(List1);
1024
1025       AIS_ListIteratorOfListOfInteractive ite(List);
1026       while(ite.More()) {
1027         if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
1028           Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
1029           if(aSh->hasIO()) {
1030             Handle(GEOM_InteractiveObject) GIO = Handle(GEOM_InteractiveObject)::DownCast(aSh->getIO());
1031             Standard_CString theIOR = GIO->getIOR();
1032             if(strcmp(IOR, theIOR) == 0) {
1033               if(onlyInActiveView) {
1034                 if(sf == QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()) {
1035                   testResult = true;
1036                   resultShape = aSh;
1037                   return resultShape; 
1038                 }
1039               } 
1040               else {
1041                 testResult = true;
1042                 resultShape = aSh;
1043                 return resultShape; 
1044               }
1045             }
1046           }
1047         }
1048         ite.Next();
1049       }  
1050     }
1051   }
1052   return  resultShape;
1053 }
1054
1055
1056 //=======================================================================
1057 // function : ConvertIORinGEOMActor()
1058 // purpose  :
1059 //=======================================================================
1060 GEOM_Actor* GEOMBase::ConvertIORinGEOMActor(const char* IOR, Standard_Boolean& testResult, bool onlyInActiveView)
1061 {
1062   int nbSf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFramesCount();
1063   for(int i = 0; i < nbSf; i++) {
1064     QAD_StudyFrame* sf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i);
1065     if(sf->getTypeView() == VIEW_VTK) {
1066       vtkRenderer* Renderer = ((VTKViewer_ViewFrame*)sf->getRightFrame()->getViewFrame())->getRenderer();
1067       vtkActorCollection* theActors = Renderer->GetActors();
1068       theActors->InitTraversal();
1069       vtkActor *ac = theActors->GetNextActor();
1070       while(!(ac==NULL)) {
1071         if( ac->IsA("GEOM_Actor")) {
1072           GEOM_Actor* anActor = GEOM_Actor::SafeDownCast(ac);
1073           if(anActor->hasIO()) {
1074             Handle(GEOM_InteractiveObject) GIO = Handle(GEOM_InteractiveObject)::DownCast(anActor->getIO());
1075             Standard_CString theIOR = GIO->getIOR();
1076             if(strcmp(IOR, theIOR) == 0) {
1077               if(onlyInActiveView) {
1078                 if(sf == QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()) {
1079                   testResult = true;
1080                   return anActor;
1081                 }
1082               } 
1083               else {
1084                 testResult = true;
1085                 return anActor;
1086               }
1087             }
1088           }
1089         }
1090         ac = theActors->GetNextActor();
1091       }
1092     }
1093   }
1094   testResult = false;
1095   return GEOM_Actor::New();
1096 }
1097
1098
1099 //=======================================================================
1100 // function : ConvertIOinGEOMAISShape()
1101 // purpose  :
1102 //=======================================================================
1103 Handle(GEOM_AISShape) GEOMBase::ConvertIOinGEOMAISShape(const Handle(SALOME_InteractiveObject)& IO, Standard_Boolean& testResult, bool onlyInActiveView)
1104 {
1105   Handle(GEOM_AISShape) res;
1106   int nbSf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFramesCount();
1107   for(int i = 0; i < nbSf; i++) {
1108     QAD_StudyFrame* sf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i);
1109     if(sf->getTypeView() == VIEW_OCC) {
1110       OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)sf->getRightFrame()->getViewFrame())->getViewer();
1111       Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
1112
1113       AIS_ListOfInteractive List;
1114       ic->DisplayedObjects(List);
1115       AIS_ListOfInteractive List1;
1116       ic->ObjectsInCollector(List1);
1117       List.Append(List1);
1118       
1119       AIS_ListIteratorOfListOfInteractive ite(List);
1120       while(ite.More()) {
1121         if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
1122           Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
1123           if(aSh->hasIO()) {
1124             Handle(GEOM_InteractiveObject) GIO = Handle(GEOM_InteractiveObject)::DownCast(aSh->getIO());
1125             if(GIO->isSame(IO)) {
1126               if(onlyInActiveView) {
1127                 if(sf == QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()) {
1128                   testResult = true;
1129                   return aSh;
1130                 }
1131               } 
1132               else {
1133                 testResult = true;
1134                 return aSh;
1135               }
1136             }
1137           }
1138         }
1139         ite.Next();
1140       }  
1141     }
1142   }
1143   testResult = false;
1144   return res;
1145 }
1146
1147
1148 //=======================================================================
1149 // function : ConvertIOinGEOMShape()
1150 // purpose  :
1151 //=======================================================================
1152 GEOM::GEOM_Shape_ptr GEOMBase::ConvertIOinGEOMShape(const Handle(SALOME_InteractiveObject)& IO, Standard_Boolean& testResult)
1153 {
1154   GEOM::GEOM_Shape_var aShape;
1155   testResult = false;
1156   
1157   /* case SObject */
1158   if(IO->hasEntry()) {
1159     SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
1160     SALOMEDS::SObject_var obj = aStudy->FindObjectID(IO->getEntry());
1161     SALOMEDS::GenericAttribute_var anAttr;
1162     SALOMEDS::AttributeIOR_var anIOR;
1163     if(!obj->_is_nil()) {
1164       if(obj->FindAttribute(anAttr, "AttributeIOR")) {
1165         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1166         aShape = myGeom->GetIORFromString(anIOR->Value());
1167         if(!CORBA::is_nil(aShape))
1168           testResult = true;
1169         return aShape._retn();
1170       }
1171     }
1172   }
1173   /* case Graphical Object */
1174   if(IO->IsInstance(STANDARD_TYPE(GEOM_InteractiveObject))) {
1175     Handle(GEOM_InteractiveObject) GIObject = Handle(GEOM_InteractiveObject)::DownCast(IO);
1176     Standard_CString ior = GIObject->getIOR();
1177
1178     aShape = myGeom->GetIORFromString(ior);
1179     if(!CORBA::is_nil(aShape))
1180       testResult = true;
1181     return aShape._retn();
1182   }
1183   return aShape._retn();
1184 }
1185
1186
1187 //=======================================================================
1188 // function : ConvertListOfIOInListOfIOR()
1189 // purpose  : 
1190 //=======================================================================
1191 void GEOMBase::ConvertListOfIOInListOfIOR(const SALOME_ListIO& aList, GEOM::GEOM_Gen::ListOfIOR& listIOR)
1192 {
1193   int nbSel = aList.Extent();  
1194   listIOR.length(nbSel);
1195   int j=0;
1196   SALOME_ListIteratorOfListIO It(aList);
1197   for(int i=0; It.More(); It.Next(), i++) {
1198     Handle(SALOME_InteractiveObject) IObject = It.Value();
1199     if(IObject->hasEntry()) {
1200       SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
1201       SALOMEDS::SObject_var obj = aStudy->FindObjectID(IObject->getEntry());
1202       SALOMEDS::GenericAttribute_var anAttr;
1203       SALOMEDS::AttributeIOR_var anIOR;
1204       if(!obj->_is_nil() && obj->FindAttribute(anAttr, "AttributeIOR")) {
1205         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1206         ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
1207         CORBA::ORB_var& _orb = init.orb();
1208         CORBA::String_var theValue = anIOR->Value();
1209         CORBA::Object_var theObj = _orb->string_to_object(theValue);
1210         if(theObj->_is_a("IDL:GEOM/GEOM_Shape:1.0")) {
1211           listIOR[j] = strdup(theValue);
1212           j++;
1213         }
1214       }
1215     } 
1216     else if(IObject->IsInstance(STANDARD_TYPE(GEOM_InteractiveObject))) {
1217       Handle(GEOM_InteractiveObject) GIObject = Handle(GEOM_InteractiveObject)::DownCast(IObject);
1218       Standard_CString ior = GIObject->getIOR();
1219       listIOR[j] = strdup(ior);
1220       j++;
1221     }
1222   }
1223   listIOR.length(j);
1224 }
1225
1226
1227 //================================================================================
1228 // function : SetDisplayedObjectList()
1229 // purpose  :
1230 //================================================================================
1231 // void GEOMBase::SetDisplayedObjectList()
1232 // {
1233 //   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
1234 //     return;
1235
1236 //   OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
1237 //   myGeomGUI->GetListDisplayedObject().Clear();
1238
1239 //   Handle (AIS_InteractiveContext) aContext = v3d->getAISContext();
1240 //   aContext->DisplayedObjects(myGeomGUI->GetListDisplayedObject());
1241 // }
1242
1243
1244 //=====================================================================================
1245 // function : DisplaySimulationShape() 
1246 // purpose  : Displays 'this->mySimulationShape' a pure graphical shape from a TopoDS_Shape
1247 //=====================================================================================
1248 void GEOMBase::DisplaySimulationShape(const TopoDS_Shape& S) 
1249 {
1250   if(S.IsNull())
1251     return;
1252         
1253   //NRI DEBUG : 14/02/2002
1254   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
1255     return;
1256         
1257   OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
1258   Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
1259   try {
1260     /* erase any previous */
1261     ic->Erase(mySimulationShape, Standard_True, Standard_False);
1262     ic->ClearPrs(mySimulationShape);
1263     mySimulationShape = new AIS_Shape(TopoDS_Shape());
1264     mySimulationShape->Set(S);
1265     mySimulationShape->SetColor(Quantity_NOC_VIOLET);
1266     ic->Deactivate(mySimulationShape);
1267     ic->Display(mySimulationShape, Standard_False);
1268     ic->UpdateCurrentViewer();
1269   }
1270   catch(Standard_Failure) {
1271     MESSAGE("Exception catched in GEOMBase::DisplaySimulationShape ");
1272   } 
1273   mySimulationShape->UnsetColor();
1274   return;
1275 }
1276
1277
1278 //==================================================================================
1279 // function : EraseSimulationShape()
1280 // purpose  : Clears the display of 'mySimulationShape' a pure graphical shape
1281 //==================================================================================
1282 void GEOMBase::EraseSimulationShape()
1283 {
1284   int count = QAD_Application::getDesktop()->getActiveStudy()->getStudyFramesCount();
1285   for(int i = 0; i < count; i++) {
1286     if(QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i)->getTypeView() == VIEW_OCC) {
1287       OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i)->getRightFrame()->getViewFrame())->getViewer();
1288       Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
1289       ic->Erase(mySimulationShape, Standard_True, Standard_False);
1290       ic->ClearPrs(mySimulationShape);
1291       ic->UpdateCurrentViewer();
1292     } 
1293   }
1294 }
1295
1296
1297 //=================================================================================
1298 // function : CreateArrowForLinearEdge()
1299 // purpose  : Create a cone topology to be used to display an arrow in the middle
1300 //          : of an edge showing its orientation. (For simulation and Viewer OCC only)
1301 //=================================================================================
1302 bool GEOMBase::CreateArrowForLinearEdge(const TopoDS_Shape& tds, TopoDS_Shape& ArrowCone)
1303 {
1304   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() != VIEW_OCC || tds.ShapeType() != TopAbs_EDGE)
1305     return false;
1306   
1307   OCCViewer_ViewPort* vp = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewPort();
1308   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)vp)->getView();
1309   Standard_Real Width, Height;
1310   view3d->Size(Width, Height);
1311   const Standard_Real aHeight = (Width + Height) / 50.0;
1312   
1313   try {
1314     Standard_Real first, last;
1315     Handle(Geom_Curve) curv = BRep_Tool::Curve(TopoDS::Edge(tds), first, last);
1316     if(!curv->IsCN(1))
1317       return false;
1318
1319     const Standard_Real param = (first+last) / 2.0;
1320     gp_Pnt middleParamPoint;
1321     gp_Vec V1;
1322     curv->D1( param, middleParamPoint, V1);
1323     if(V1.Magnitude() < Precision::Confusion())
1324       return false;
1325     
1326     /* Topology orientation not geom orientation */
1327     if(tds.Orientation() == TopAbs_REVERSED)
1328       V1 *= -1.0;
1329     
1330     gp_Ax2 anAxis( middleParamPoint, gp_Dir(V1));
1331     const Standard_Real radius1 = aHeight / 5.0;
1332     if(radius1 > 10.0 * Precision::Confusion() && aHeight > 10.0 * Precision::Confusion()) {
1333       ArrowCone = BRepPrimAPI_MakeCone( anAxis, radius1, 0.0, aHeight ).Shape();
1334       return true;
1335     }
1336   }
1337   catch(Standard_Failure) {
1338     // OCC failures are hard to catch in GUI.
1339     // This  because of the position for  #include <Standard_ErrorHandler.hxx> that is very critic to find 
1340     // in SALOME environment : compilation error !
1341   }
1342   return false;
1343 }
1344
1345
1346 //=================================================================================
1347 // function : VertexToPoint()
1348 // purpose  : If S can be converted in a gp_Pnt returns true and the result is P
1349 //=================================================================================
1350 bool GEOMBase::VertexToPoint(const TopoDS_Shape& S, gp_Pnt& P)
1351 {
1352   if(S.IsNull() || S.ShapeType() != TopAbs_VERTEX)
1353     return false;
1354   P = BRep_Tool::Pnt(TopoDS::Vertex(S));
1355   return true;
1356 }
1357
1358
1359 //=================================================================================
1360 // function : GetBipointDxDyDz()
1361 // purpose  : 
1362 //=================================================================================
1363 void GEOMBase::GetBipointDxDyDz(gp_Pnt P1, gp_Pnt P2, double& dx, double& dy, double& dz)
1364 {
1365   dx = P2.X() - P1.X();
1366   dy = P2.Y() - P1.Y();
1367   dz = P2.Z() - P1.Z();
1368   return;
1369 }
1370
1371
1372 //=================================================================================
1373 // function : LinearEdgeExtremities()
1374 // purpose  : If S can be converted in a linear edge and if initial an final points
1375 //          : distance is sufficient, returns true else returns false.
1376 //          : Resulting points are respectively P1 and P2
1377 //=================================================================================
1378 bool GEOMBase::LinearEdgeExtremities(const TopoDS_Shape& S,  gp_Pnt& P1, gp_Pnt& P2)
1379 {
1380   if(S.IsNull() || S.ShapeType() != TopAbs_EDGE)
1381     return false;
1382   BRepAdaptor_Curve curv(TopoDS::Edge(S));
1383   if(curv.GetType() != GeomAbs_Line)
1384     return false;
1385
1386   curv.D0(curv.FirstParameter(), P1);
1387   curv.D0(curv.LastParameter(), P2);
1388
1389   if(P1.Distance(P2) <= Precision::Confusion())
1390     return false;
1391
1392   return true;
1393 }
1394
1395
1396 //=======================================================================
1397 // function : Parameter()
1398 // purpose  : return a parameter (float) from a dialog box
1399 //
1400 //  avalue1    : is a float or integer used as a default value displayed
1401 //  aTitle1    : is the title for aValue1
1402 //  aTitle     : is the main title
1403 //  bottom     : maximum value to be entered
1404 //  top        : minimum value to be entered
1405 //  decimals   : number of decimals
1406 //=======================================================================
1407 double GEOMBase::Parameter(Standard_Boolean& res, const char* aValue1, const char* aTitle1, const char* aTitle, const double bottom, const double top, const int decimals)
1408 {
1409   GEOMBase_aParameterDlg * Dialog = new GEOMBase_aParameterDlg(aValue1, aTitle1, QAD_Application::getDesktop(), aTitle, TRUE, 0, bottom, top, decimals);
1410   int r = Dialog->exec();
1411   float X = 0.0;
1412   if(r == QDialog::Accepted) {
1413     res = Standard_True;
1414     X = Dialog->getValue();
1415   } 
1416   else
1417     res = Standard_False;
1418   delete Dialog;
1419   return X;
1420 }
1421
1422
1423 //=======================================================================
1424 // function : SelectionByNameInDialogs()
1425 // purpose  : Called when user has entered a name of object in a LineEdit.
1426 //          : The selection is changed. Dialog box will receive the 
1427 //          : corresponding signal to manage this event.
1428 //=======================================================================
1429 bool GEOMBase::SelectionByNameInDialogs(QWidget* aWidget, const QString& objectUserName, SALOME_Selection* Sel)
1430 {
1431   /* Find SObject with name in component GEOM */
1432   SALOMEDS::Study_var ST = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
1433   SALOMEDS::Study::ListOfSObject_var listSO = new SALOMEDS::Study::ListOfSObject;
1434   listSO = ST->FindObjectByName(objectUserName, "GEOM");
1435   
1436   if(listSO->length() < 1) {
1437     const QString caption  = tr("GEOM_WRN_WARNING");
1438     const QString text = tr("GEOM_NAME_INCORRECT");
1439     const QString button0  = tr("GEOM_BUT_OK");
1440     QMessageBox::warning(aWidget, caption, text, button0);
1441     return false;
1442   }
1443   /* More than one object with same name */
1444   if(listSO->length() > 1) {
1445     const QString caption  = tr("GEOM_WRN_WARNING");
1446     const QString text = tr("GEOM_IDENTICAL_NAMES_SELECT_BY_MOUSE");
1447     const QString button0  = tr("GEOM_BUT_OK") ;
1448     QMessageBox::warning(aWidget, caption, text, button0) ;
1449     return false;
1450   }
1451
1452   SALOMEDS::SObject_var theObj = listSO[0];
1453   /* Create a SALOME_InteractiveObject with a SALOME::SObject */
1454   Standard_CString anEntry = theObj->GetID();
1455   Handle(SALOME_InteractiveObject) SI = new SALOME_InteractiveObject(anEntry, "GEOM", strdup(objectUserName));
1456
1457   /* Add as a selected object       */
1458   /* Clear any previous selection : */
1459   /* Warning the LineEdit is purged because of signal currentSelectionChanged ! */ 
1460   Sel->ClearIObjects(); 
1461   Sel->AddIObject(SI);
1462   return true;
1463 }
1464
1465
1466 //=======================================================================
1467 // function : DefineDlgPosition()
1468 // purpose  : Define x and y the default position for a dialog box
1469 //=======================================================================
1470 bool GEOMBase::DefineDlgPosition(QWidget* aDlg, int& x, int& y)
1471 {
1472   /* Here the position is on the bottom right corner - 10 */
1473   QAD_Desktop* PP = QAD_Application::getDesktop();
1474   x = abs(PP->x() + PP->size().width() - aDlg->size().width() - 10);
1475   y = abs(PP->y() + PP->size().height() - aDlg->size().height() - 10);
1476   return true;  
1477 }
1478
1479
1480 //=====================================================================================
1481 // EXPORTED METHODS
1482 //=====================================================================================
1483 extern "C"
1484 {
1485   bool CustomPopup(QAD_Desktop* parent, QPopupMenu* popup, const QString & theContext,
1486                    const QString & theParent, const QString & theObject)
1487   {return GEOMBase::CustomPopup(parent, popup, theContext, theParent, theObject);}
1488 }