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