]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMBase/GEOMBase.cxx
Salome HOME
SALOME PAL V1_4_1
[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         GActor->setName(strdup(Name.latin1()));
680       // OCC
681       else if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC)
682         GAISShape->setName(strdup(Name.latin1()));
683     } 
684     else
685       return false;
686   }
687
688   // open transaction
689   QAD_Operation* op = new SALOMEGUI_ImportOperation(QAD_Application::getDesktop()->getActiveStudy());
690   op->start();
691
692   SALOMEDS::SObject_var newObj = aStudyBuilder->NewObject(father);
693
694   GEOM::GEOM_Shape_var aShape = myGeom->GetIORFromString(GIO->getIOR());
695
696   /* For the shape inserted into the study we set its field 'studyshapeid'    */
697   /* so the shape will contain its corresponding entry in the study Ocaf doc. */
698   aShape->StudyShapeId(newObj->GetID());
699
700   GIO->setEntry(newObj->GetID());
701
702   anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeIOR");
703   anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
704   anIOR->SetValue(aShape->Name());
705
706   anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributeName");
707   aName = SALOMEDS::AttributeName::_narrow(anAttr);
708
709   anAttr = aStudyBuilder->FindOrCreateAttribute(newObj, "AttributePixMap");
710   aPixmap = SALOMEDS::AttributePixMap::_narrow(anAttr);
711   if(aShape->ShapeType() == GEOM::COMPOUND)
712     aPixmap->SetPixMap("ICON_OBJBROWSER_COMPOUND");
713   else if(aShape->ShapeType() == GEOM::COMPSOLID)
714     aPixmap->SetPixMap("ICON_OBJBROWSER_COMPSOLID");
715   else if(aShape->ShapeType() == GEOM::SOLID)
716     aPixmap->SetPixMap("ICON_OBJBROWSER_SOLID");
717   else if(aShape->ShapeType() == GEOM::SHELL)
718     aPixmap->SetPixMap("ICON_OBJBROWSER_SHELL");
719   else if(aShape->ShapeType() == GEOM::FACE)
720     aPixmap->SetPixMap("ICON_OBJBROWSER_FACE");
721   else if(aShape->ShapeType() == GEOM::WIRE)
722     aPixmap->SetPixMap("ICON_OBJBROWSER_WIRE");
723   else if(aShape->ShapeType() == GEOM::EDGE)
724     aPixmap->SetPixMap("ICON_OBJBROWSER_EDGE");
725   else if(aShape->ShapeType() == GEOM::VERTEX)
726     aPixmap->SetPixMap("ICON_OBJBROWSER_VERTEX");
727
728   // VTK
729   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_VTK) {
730     GActor->setIO(GIO);
731     aName->SetValue(GActor->getName());
732   }
733   // OCC
734   else if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() == VIEW_OCC) {
735     GAISShape->setIO(GIO);
736     aName->SetValue(GAISShape->getName());
737   }
738
739   SALOMEDS::SObject_var newObj1 = aStudyBuilder->NewObject(fatherSF);
740   aStudyBuilder->Addreference(newObj1,newObj);
741
742   GEOM::GEOM_Gen::ListOfIOR_var listIOR = new GEOM::GEOM_Gen::ListOfIOR;
743   listIOR = myGeom->GetReferencedObjects(aShape);
744
745   if(listIOR->length()>0) {
746     SALOMEDS::SObject_var Arguments = aStudyBuilder->NewObject(newObj);
747     anAttr = aStudyBuilder->FindOrCreateAttribute(Arguments, "AttributeName");
748     aName = SALOMEDS::AttributeName::_narrow(anAttr);
749     aName->SetValue(tr("GEOM_ARGUMENTS"));
750     anAttr = aStudyBuilder->FindOrCreateAttribute(Arguments, "AttributeSelectable");
751     aSelAttr = SALOMEDS::AttributeSelectable::_narrow(anAttr);
752     aSelAttr->SetSelectable(false);
753
754     bool ObjectReferenced = false;
755     for (unsigned int ind = 0; ind < listIOR->length();ind++) {
756       SALOMEDS::SObject_var theObj = aStudy->FindObjectIOR(listIOR[ind]);
757
758       if(!theObj->_is_nil()) {
759         SALOMEDS::SObject_var RefObject = aStudyBuilder->NewObject(Arguments);
760         aStudyBuilder->Addreference(RefObject, theObj);
761         ObjectReferenced = true;
762       }
763     }
764
765     if(!ObjectReferenced)
766       aStudyBuilder->RemoveObject(Arguments);
767   }
768   op->finish();
769
770   if(!mySettings_AddInStudy || selection)
771     QAD_Application::getDesktop()->getActiveStudy()->updateObjBrowser();
772   else {
773     QAD_Application::getDesktop()->getActiveStudy()->updateObjBrowser(false);
774     // DCQ Sel->AddIObject(GIO);
775   }
776   return true;
777 }
778
779
780 //=====================================================================================
781 // function : GetShapeFromIOR()
782 // purpose  : exist also as static method !
783 //=====================================================================================
784 TopoDS_Shape GEOMBase::GetShapeFromIOR(QString IOR)
785 {
786   TopoDS_Shape result;
787   if(IOR.stripWhiteSpace().isEmpty())
788     return result;
789   ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
790   CORBA::ORB_var& _orb = init.orb() ;
791   CORBA::Object_var obj = _orb->string_to_object((char*)(IOR.latin1()));
792   if(CORBA::is_nil(obj))
793     return result;
794   GEOM::GEOM_Shape_var aShape = myGeom->GetIORFromString(IOR);
795   if(!aShape->_is_nil())
796     result = myGeomGUI->GetShapeReader().GetShape(myGeom, aShape);
797   return result;
798 }
799
800
801 //=====================================================================================
802 // function : GetIndex()
803 // purpose  : Get the index of a sub shape in a main shape : index start at 1
804 //=====================================================================================
805 int GEOMBase::GetIndex(const TopoDS_Shape& subshape, const TopoDS_Shape& shape, int /*ShapeType*/) 
806
807   if(shape.IsNull() || subshape.IsNull()) 
808     return -1; 
809   
810   int index = 1; 
811   if(subshape.ShapeType() == TopAbs_COMPOUND) { 
812     TopoDS_Iterator it; 
813     TopTools_ListOfShape CL; 
814     CL.Append(shape); 
815     TopTools_ListIteratorOfListOfShape itC; 
816     for(itC.Initialize(CL); itC.More(); itC.Next()) { 
817       for(it.Initialize(itC.Value()); it.More(); it.Next()) { 
818         if(it.Value().ShapeType() == TopAbs_COMPOUND) {
819           if(it.Value().IsSame(subshape)) 
820             return index; 
821           else 
822             index++; 
823           CL.Append(it.Value()); 
824         }
825       } 
826     }
827   }
828   else { 
829     TopExp_Explorer Exp(shape, subshape.ShapeType()); 
830     TopTools_MapOfShape M; 
831     while(Exp.More()) {
832       if(M.Add(Exp.Current())) { 
833         if(Exp.Current().IsSame(subshape)) 
834           return index; 
835         index++; 
836       } 
837       Exp.Next(); 
838     } 
839   } 
840   return -1; 
841 }
842
843
844 //=======================================================================
845 // function : GetTopoFromSelection()
846 // purpose  : Define tds from a single selection and retuen true
847 //=======================================================================
848 bool GEOMBase::GetTopoFromSelection(SALOME_Selection *Sel, TopoDS_Shape& tds) 
849 {
850   if(Sel->IObjectCount() != 1)
851     return false;
852   
853   Handle(SALOME_InteractiveObject) IO = Sel->firstIObject();
854   /* case SObject */
855   if(IO->hasEntry()) {
856     SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
857     SALOMEDS::SObject_var obj = aStudy->FindObjectID(IO->getEntry());
858     SALOMEDS::GenericAttribute_var anAttr;
859     SALOMEDS::AttributeIOR_var anIOR;
860     if(!obj->_is_nil()) {
861       if(obj->FindAttribute(anAttr, "AttributeIOR")) {
862         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
863         tds = this->GetShapeFromIOR(anIOR->Value());
864         if(tds.IsNull())
865           return false;
866         else
867           return true;
868       }
869     }
870   }
871
872   if(IO->IsInstance(STANDARD_TYPE(GEOM_InteractiveObject))) {
873     Standard_CString ior = "";
874     Handle(GEOM_InteractiveObject) GIObject = Handle(GEOM_InteractiveObject)::DownCast(IO);
875     ior = GIObject->getIOR();
876     tds = this->GetShapeFromIOR(ior);
877     if(tds.IsNull())
878       return false;
879     else
880       return true;
881   }
882   
883   return false;
884 }
885
886
887 //=======================================================================
888 // function : GetNameOfSelectedIObjects()
889 // purpose  : Define the name geom++ or other name of mono or multi sel.
890 //=======================================================================
891 int GEOMBase::GetNameOfSelectedIObjects(SALOME_Selection* Sel, QString& aName)
892 {
893   int nbSel = Sel->IObjectCount();
894   if(nbSel == 1) {
895     Handle(SALOME_InteractiveObject) IObject = Sel->firstIObject();
896     aName = IObject->getName();
897   } 
898   else {
899     aName = tr("%1_objects").arg(nbSel);
900   }
901   return nbSel;
902 }
903
904
905 //=================================================================================
906 // function : GetShapeTypeString()
907 // purpose  : for a single shape
908 //=================================================================================
909 bool GEOMBase::GetShapeTypeString(const TopoDS_Shape& aShape, Standard_CString& aTypeString)
910 {
911   if(aShape.IsNull()) {
912     aTypeString = "aNullShape";
913     return false;
914   }
915   switch(aShape.ShapeType())
916     {
917     case TopAbs_COMPOUND:
918       {
919         aTypeString = strdup(tr("GEOM_COMPOUND"));
920         return true;
921       }
922     case  TopAbs_COMPSOLID:
923       {
924         aTypeString = strdup(tr("GEOM_COMPOUNDSOLID")) ; 
925         return true ;
926       }
927     case TopAbs_SOLID:
928       {
929         aTypeString = strdup(tr("GEOM_SOLID")) ; 
930         return true ;
931       }
932     case TopAbs_SHELL:
933       {
934         aTypeString = strdup(tr("GEOM_SHELL")) ; 
935         return true ;
936       }
937     case TopAbs_FACE:
938       { 
939         BRepAdaptor_Surface surf(TopoDS::Face(aShape));
940         if(surf.GetType() == GeomAbs_Plane) {
941           aTypeString = strdup(tr("GEOM_PLANE"));
942           return true;
943         }
944         else if(surf.GetType() == GeomAbs_Cylinder) {
945           aTypeString = strdup(tr("GEOM_SURFCYLINDER"));
946           return true;
947         }
948         else if(surf.GetType() == GeomAbs_Sphere) {
949           aTypeString = strdup(tr("GEOM_SURFSPHERE"));
950           return true ;
951         }
952         else if(surf.GetType() == GeomAbs_Torus) {
953           aTypeString = strdup(tr("GEOM_SURFTORUS"));
954           return true ;
955         }
956         else if(surf.GetType() == GeomAbs_Cone) {
957           aTypeString = strdup(tr("GEOM_SURFCONE"));
958           return true ;
959         }
960         else {
961           aTypeString = strdup(tr("GEOM_FACE"));
962           return true;
963         }
964       }
965     case TopAbs_WIRE:
966       {
967         aTypeString = strdup(tr("GEOM_WIRE")); 
968         return true;
969       }
970     case TopAbs_EDGE:
971       {
972         BRepAdaptor_Curve curv(TopoDS::Edge(aShape));
973         if(curv.GetType() == GeomAbs_Line) {
974           if((Abs(curv.FirstParameter()) >= 1E6) || (Abs(curv.LastParameter()) >= 1E6))
975             aTypeString = strdup(tr("GEOM_LINE"));
976           else
977             aTypeString = strdup(tr("GEOM_EDGE"));
978           return true;
979         }
980         else if(curv.GetType() == GeomAbs_Circle) {
981           if(curv.IsClosed())
982             aTypeString = strdup(tr("GEOM_CIRCLE"));
983           else
984             aTypeString = strdup(tr("GEOM_ARC"));
985         return true;
986       } 
987         else {
988           aTypeString = strdup(tr("GEOM_EDGE"));
989           return true;
990         }
991       }
992     case TopAbs_VERTEX:
993       {
994         aTypeString = strdup(tr("GEOM_VERTEX"));
995         return true;
996       }
997     case TopAbs_SHAPE:
998       {
999         aTypeString = strdup(tr("GEOM_SHAPE"));
1000         return true;
1001       }
1002     }
1003   return false;
1004 }
1005
1006
1007 //=======================================================================
1008 // function : ConvertIORinGEOMAISShape()
1009 // purpose  :
1010 //=======================================================================
1011 Handle(GEOM_AISShape) GEOMBase::ConvertIORinGEOMAISShape(const char * IOR, Standard_Boolean& testResult, bool onlyInActiveView)
1012 {
1013   Handle(GEOM_AISShape) resultShape;
1014   testResult = false;
1015   int nbSf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFramesCount();
1016   for(int i = 0; i < nbSf; i++) {
1017     QAD_StudyFrame* sf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i);
1018     if(sf->getTypeView() == VIEW_OCC) {
1019       OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)sf->getRightFrame()->getViewFrame())->getViewer();
1020       Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
1021
1022       AIS_ListOfInteractive List;
1023       ic->DisplayedObjects(List);
1024       AIS_ListOfInteractive List1;
1025       ic->ObjectsInCollector(List1);
1026       List.Append(List1);
1027
1028       AIS_ListIteratorOfListOfInteractive ite(List);
1029       while(ite.More()) {
1030         if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
1031           Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
1032           if(aSh->hasIO()) {
1033             Handle(GEOM_InteractiveObject) GIO = Handle(GEOM_InteractiveObject)::DownCast(aSh->getIO());
1034             Standard_CString theIOR = GIO->getIOR();
1035             if(strcmp(IOR, theIOR) == 0) {
1036               if(onlyInActiveView) {
1037                 if(sf == QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()) {
1038                   testResult = true;
1039                   resultShape = aSh;
1040                   return resultShape; 
1041                 }
1042               } 
1043               else {
1044                 testResult = true;
1045                 resultShape = aSh;
1046                 return resultShape; 
1047               }
1048             }
1049           }
1050         }
1051         ite.Next();
1052       }  
1053     }
1054   }
1055   return  resultShape;
1056 }
1057
1058
1059 //=======================================================================
1060 // function : ConvertIORinGEOMActor()
1061 // purpose  :
1062 //=======================================================================
1063 GEOM_Actor* GEOMBase::ConvertIORinGEOMActor(const char* IOR, Standard_Boolean& testResult, bool onlyInActiveView)
1064 {
1065   int nbSf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFramesCount();
1066   for(int i = 0; i < nbSf; i++) {
1067     QAD_StudyFrame* sf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i);
1068     if(sf->getTypeView() == VIEW_VTK) {
1069       vtkRenderer* Renderer = ((VTKViewer_ViewFrame*)sf->getRightFrame()->getViewFrame())->getRenderer();
1070       vtkActorCollection* theActors = Renderer->GetActors();
1071       theActors->InitTraversal();
1072       vtkActor *ac = theActors->GetNextActor();
1073       while(!(ac==NULL)) {
1074         if( ac->IsA("GEOM_Actor")) {
1075           GEOM_Actor* anActor = GEOM_Actor::SafeDownCast(ac);
1076           if(anActor->hasIO()) {
1077             Handle(GEOM_InteractiveObject) GIO = Handle(GEOM_InteractiveObject)::DownCast(anActor->getIO());
1078             Standard_CString theIOR = GIO->getIOR();
1079             if(strcmp(IOR, theIOR) == 0) {
1080               if(onlyInActiveView) {
1081                 if(sf == QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()) {
1082                   testResult = true;
1083                   return anActor;
1084                 }
1085               } 
1086               else {
1087                 testResult = true;
1088                 return anActor;
1089               }
1090             }
1091           }
1092         }
1093         ac = theActors->GetNextActor();
1094       }
1095     }
1096   }
1097   testResult = false;
1098   return GEOM_Actor::New();
1099 }
1100
1101
1102 //=======================================================================
1103 // function : ConvertIOinGEOMAISShape()
1104 // purpose  :
1105 //=======================================================================
1106 Handle(GEOM_AISShape) GEOMBase::ConvertIOinGEOMAISShape(const Handle(SALOME_InteractiveObject)& IO, Standard_Boolean& testResult, bool onlyInActiveView)
1107 {
1108   Handle(GEOM_AISShape) res;
1109   int nbSf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFramesCount();
1110   for(int i = 0; i < nbSf; i++) {
1111     QAD_StudyFrame* sf = QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i);
1112     if(sf->getTypeView() == VIEW_OCC) {
1113       OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)sf->getRightFrame()->getViewFrame())->getViewer();
1114       Handle (AIS_InteractiveContext) ic = v3d->getAISContext();
1115
1116       AIS_ListOfInteractive List;
1117       ic->DisplayedObjects(List);
1118       AIS_ListOfInteractive List1;
1119       ic->ObjectsInCollector(List1);
1120       List.Append(List1);
1121       
1122       AIS_ListIteratorOfListOfInteractive ite(List);
1123       while(ite.More()) {
1124         if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
1125           Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
1126           if(aSh->hasIO()) {
1127             Handle(GEOM_InteractiveObject) GIO = Handle(GEOM_InteractiveObject)::DownCast(aSh->getIO());
1128             if(GIO->isSame(IO)) {
1129               if(onlyInActiveView) {
1130                 if(sf == QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()) {
1131                   testResult = true;
1132                   return aSh;
1133                 }
1134               } 
1135               else {
1136                 testResult = true;
1137                 return aSh;
1138               }
1139             }
1140           }
1141         }
1142         ite.Next();
1143       }  
1144     }
1145   }
1146   testResult = false;
1147   return res;
1148 }
1149
1150
1151 //=======================================================================
1152 // function : ConvertIOinGEOMShape()
1153 // purpose  :
1154 //=======================================================================
1155 GEOM::GEOM_Shape_ptr GEOMBase::ConvertIOinGEOMShape(const Handle(SALOME_InteractiveObject)& IO, Standard_Boolean& testResult)
1156 {
1157   GEOM::GEOM_Shape_var aShape;
1158   testResult = false;
1159   
1160   /* case SObject */
1161   if(IO->hasEntry()) {
1162     SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
1163     SALOMEDS::SObject_var obj = aStudy->FindObjectID(IO->getEntry());
1164     SALOMEDS::GenericAttribute_var anAttr;
1165     SALOMEDS::AttributeIOR_var anIOR;
1166     if(!obj->_is_nil()) {
1167       if(obj->FindAttribute(anAttr, "AttributeIOR")) {
1168         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1169         aShape = myGeom->GetIORFromString(anIOR->Value());
1170         if(!CORBA::is_nil(aShape))
1171           testResult = true;
1172         return aShape._retn();
1173       }
1174     }
1175   }
1176   /* case Graphical Object */
1177   if(IO->IsInstance(STANDARD_TYPE(GEOM_InteractiveObject))) {
1178     Handle(GEOM_InteractiveObject) GIObject = Handle(GEOM_InteractiveObject)::DownCast(IO);
1179     Standard_CString ior = GIObject->getIOR();
1180
1181     aShape = myGeom->GetIORFromString(ior);
1182     if(!CORBA::is_nil(aShape))
1183       testResult = true;
1184     return aShape._retn();
1185   }
1186   return aShape._retn();
1187 }
1188
1189
1190 //=======================================================================
1191 // function : ConvertListOfIOInListOfIOR()
1192 // purpose  : 
1193 //=======================================================================
1194 void GEOMBase::ConvertListOfIOInListOfIOR(const SALOME_ListIO& aList, GEOM::GEOM_Gen::ListOfIOR& listIOR)
1195 {
1196   int nbSel = aList.Extent();  
1197   listIOR.length(nbSel);
1198   int j=0;
1199   SALOME_ListIteratorOfListIO It(aList);
1200   for(int i=0; It.More(); It.Next(), i++) {
1201     Handle(SALOME_InteractiveObject) IObject = It.Value();
1202     if(IObject->hasEntry()) {
1203       SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
1204       SALOMEDS::SObject_var obj = aStudy->FindObjectID(IObject->getEntry());
1205       SALOMEDS::GenericAttribute_var anAttr;
1206       SALOMEDS::AttributeIOR_var anIOR;
1207       if(!obj->_is_nil() && obj->FindAttribute(anAttr, "AttributeIOR")) {
1208         anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
1209         ORB_INIT &init = *SINGLETON_<ORB_INIT>::Instance();
1210         CORBA::ORB_var& _orb = init.orb();
1211         CORBA::String_var theValue = anIOR->Value();
1212         CORBA::Object_var theObj = _orb->string_to_object(theValue);
1213         if(theObj->_is_a("IDL:GEOM/GEOM_Shape:1.0")) {
1214           listIOR[j] = strdup(theValue);
1215           j++;
1216         }
1217       }
1218     } 
1219     else if(IObject->IsInstance(STANDARD_TYPE(GEOM_InteractiveObject))) {
1220       Handle(GEOM_InteractiveObject) GIObject = Handle(GEOM_InteractiveObject)::DownCast(IObject);
1221       Standard_CString ior = GIObject->getIOR();
1222       listIOR[j] = strdup(ior);
1223       j++;
1224     }
1225   }
1226   listIOR.length(j);
1227 }
1228
1229
1230 //================================================================================
1231 // function : SetDisplayedObjectList()
1232 // purpose  :
1233 //================================================================================
1234 // void GEOMBase::SetDisplayedObjectList()
1235 // {
1236 //   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
1237 //     return;
1238
1239 //   OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
1240 //   myGeomGUI->GetListDisplayedObject().Clear();
1241
1242 //   Handle (AIS_InteractiveContext) aContext = v3d->getAISContext();
1243 //   aContext->DisplayedObjects(myGeomGUI->GetListDisplayedObject());
1244 // }
1245
1246
1247 //=====================================================================================
1248 // function : DisplaySimulationShape() 
1249 // purpose  : Displays 'this->mySimulationShape' a pure graphical shape from a TopoDS_Shape
1250 //=====================================================================================
1251 void GEOMBase::DisplaySimulationShape(const TopoDS_Shape& S) 
1252 {
1253   if(S.IsNull())
1254     return;
1255         
1256   //NRI DEBUG : 14/02/2002
1257   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() > VIEW_OCC)
1258     return;
1259         
1260   OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewer();
1261   Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
1262   try {
1263     /* erase any previous */
1264     ic->Erase(mySimulationShape, Standard_True, Standard_False);
1265     ic->ClearPrs(mySimulationShape);
1266     mySimulationShape = new AIS_Shape(TopoDS_Shape());
1267     mySimulationShape->Set(S);
1268     mySimulationShape->SetColor(Quantity_NOC_VIOLET);
1269     ic->Deactivate(mySimulationShape);
1270     ic->Display(mySimulationShape, Standard_False);
1271     ic->UpdateCurrentViewer();
1272   }
1273   catch(Standard_Failure) {
1274     MESSAGE("Exception catched in GEOMBase::DisplaySimulationShape ");
1275   } 
1276   mySimulationShape->UnsetColor();
1277   return;
1278 }
1279
1280
1281 //==================================================================================
1282 // function : EraseSimulationShape()
1283 // purpose  : Clears the display of 'mySimulationShape' a pure graphical shape
1284 //==================================================================================
1285 void GEOMBase::EraseSimulationShape()
1286 {
1287   int count = QAD_Application::getDesktop()->getActiveStudy()->getStudyFramesCount();
1288   for(int i = 0; i < count; i++) {
1289     if(QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i)->getTypeView() == VIEW_OCC) {
1290       OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getStudyFrame(i)->getRightFrame()->getViewFrame())->getViewer();
1291       Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
1292       ic->Erase(mySimulationShape, Standard_True, Standard_False);
1293       ic->ClearPrs(mySimulationShape);
1294       ic->UpdateCurrentViewer();
1295     } 
1296   }
1297 }
1298
1299
1300 //=================================================================================
1301 // function : CreateArrowForLinearEdge()
1302 // purpose  : Create a cone topology to be used to display an arrow in the middle
1303 //          : of an edge showing its orientation. (For simulation and Viewer OCC only)
1304 //=================================================================================
1305 bool GEOMBase::CreateArrowForLinearEdge(const TopoDS_Shape& tds, TopoDS_Shape& ArrowCone)
1306 {
1307   if(QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getTypeView() != VIEW_OCC || tds.ShapeType() != TopAbs_EDGE)
1308     return false;
1309   
1310   OCCViewer_ViewPort* vp = ((OCCViewer_ViewFrame*)QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame())->getViewPort();
1311   Handle( V3d_View) view3d = ((OCCViewer_ViewPort3d*)vp)->getView();
1312   Standard_Real Width, Height;
1313   view3d->Size(Width, Height);
1314   const Standard_Real aHeight = (Width + Height) / 50.0;
1315   
1316   try {
1317     Standard_Real first, last;
1318     Handle(Geom_Curve) curv = BRep_Tool::Curve(TopoDS::Edge(tds), first, last);
1319     if(!curv->IsCN(1))
1320       return false;
1321
1322     const Standard_Real param = (first+last) / 2.0;
1323     gp_Pnt middleParamPoint;
1324     gp_Vec V1;
1325     curv->D1( param, middleParamPoint, V1);
1326     if(V1.Magnitude() < Precision::Confusion())
1327       return false;
1328     
1329     /* Topology orientation not geom orientation */
1330     if(tds.Orientation() == TopAbs_REVERSED)
1331       V1 *= -1.0;
1332     
1333     gp_Ax2 anAxis( middleParamPoint, gp_Dir(V1));
1334     const Standard_Real radius1 = aHeight / 5.0;
1335     if(radius1 > 10.0 * Precision::Confusion() && aHeight > 10.0 * Precision::Confusion()) {
1336       ArrowCone = BRepPrimAPI_MakeCone( anAxis, radius1, 0.0, aHeight ).Shape();
1337       return true;
1338     }
1339   }
1340   catch(Standard_Failure) {
1341     // OCC failures are hard to catch in GUI.
1342     // This  because of the position for  #include <Standard_ErrorHandler.hxx> that is very critic to find 
1343     // in SALOME environment : compilation error !
1344   }
1345   return false;
1346 }
1347
1348
1349 //=================================================================================
1350 // function : VertexToPoint()
1351 // purpose  : If S can be converted in a gp_Pnt returns true and the result is P
1352 //=================================================================================
1353 bool GEOMBase::VertexToPoint(const TopoDS_Shape& S, gp_Pnt& P)
1354 {
1355   if(S.IsNull() || S.ShapeType() != TopAbs_VERTEX)
1356     return false;
1357   P = BRep_Tool::Pnt(TopoDS::Vertex(S));
1358   return true;
1359 }
1360
1361
1362 //=================================================================================
1363 // function : GetBipointDxDyDz()
1364 // purpose  : 
1365 //=================================================================================
1366 void GEOMBase::GetBipointDxDyDz(gp_Pnt P1, gp_Pnt P2, double& dx, double& dy, double& dz)
1367 {
1368   dx = P2.X() - P1.X();
1369   dy = P2.Y() - P1.Y();
1370   dz = P2.Z() - P1.Z();
1371   return;
1372 }
1373
1374
1375 //=================================================================================
1376 // function : LinearEdgeExtremities()
1377 // purpose  : If S can be converted in a linear edge and if initial an final points
1378 //          : distance is sufficient, returns true else returns false.
1379 //          : Resulting points are respectively P1 and P2
1380 //=================================================================================
1381 bool GEOMBase::LinearEdgeExtremities(const TopoDS_Shape& S,  gp_Pnt& P1, gp_Pnt& P2)
1382 {
1383   if(S.IsNull() || S.ShapeType() != TopAbs_EDGE)
1384     return false;
1385   BRepAdaptor_Curve curv(TopoDS::Edge(S));
1386   if(curv.GetType() != GeomAbs_Line)
1387     return false;
1388
1389   curv.D0(curv.FirstParameter(), P1);
1390   curv.D0(curv.LastParameter(), P2);
1391
1392   if(P1.Distance(P2) <= Precision::Confusion())
1393     return false;
1394
1395   return true;
1396 }
1397
1398
1399 //=======================================================================
1400 // function : Parameter()
1401 // purpose  : return a parameter (float) from a dialog box
1402 //
1403 //  avalue1    : is a float or integer used as a default value displayed
1404 //  aTitle1    : is the title for aValue1
1405 //  aTitle     : is the main title
1406 //  bottom     : maximum value to be entered
1407 //  top        : minimum value to be entered
1408 //  decimals   : number of decimals
1409 //=======================================================================
1410 double GEOMBase::Parameter(Standard_Boolean& res, const char* aValue1, const char* aTitle1, const char* aTitle, const double bottom, const double top, const int decimals)
1411 {
1412   GEOMBase_aParameterDlg * Dialog = new GEOMBase_aParameterDlg(aValue1, aTitle1, QAD_Application::getDesktop(), aTitle, TRUE, 0, bottom, top, decimals);
1413   int r = Dialog->exec();
1414   float X = 0.0;
1415   if(r == QDialog::Accepted) {
1416     res = Standard_True;
1417     X = Dialog->getValue();
1418   } 
1419   else
1420     res = Standard_False;
1421   delete Dialog;
1422   return X;
1423 }
1424
1425
1426 //=======================================================================
1427 // function : SelectionByNameInDialogs()
1428 // purpose  : Called when user has entered a name of object in a LineEdit.
1429 //          : The selection is changed. Dialog box will receive the 
1430 //          : corresponding signal to manage this event.
1431 //=======================================================================
1432 bool GEOMBase::SelectionByNameInDialogs(QWidget* aWidget, const QString& objectUserName, SALOME_Selection* Sel)
1433 {
1434   /* Find SObject with name in component GEOM */
1435   SALOMEDS::Study_var ST = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
1436   SALOMEDS::Study::ListOfSObject_var listSO = new SALOMEDS::Study::ListOfSObject;
1437   listSO = ST->FindObjectByName(objectUserName, "GEOM");
1438   
1439   if(listSO->length() < 1) {
1440     const QString caption  = tr("GEOM_WRN_WARNING");
1441     const QString text = tr("GEOM_NAME_INCORRECT");
1442     const QString button0  = tr("GEOM_BUT_OK");
1443     QMessageBox::warning(aWidget, caption, text, button0);
1444     return false;
1445   }
1446   /* More than one object with same name */
1447   if(listSO->length() > 1) {
1448     const QString caption  = tr("GEOM_WRN_WARNING");
1449     const QString text = tr("GEOM_IDENTICAL_NAMES_SELECT_BY_MOUSE");
1450     const QString button0  = tr("GEOM_BUT_OK") ;
1451     QMessageBox::warning(aWidget, caption, text, button0) ;
1452     return false;
1453   }
1454
1455   SALOMEDS::SObject_var theObj = listSO[0];
1456   /* Create a SALOME_InteractiveObject with a SALOME::SObject */
1457   Standard_CString anEntry = theObj->GetID();
1458   Handle(SALOME_InteractiveObject) SI = new SALOME_InteractiveObject(anEntry, "GEOM", strdup(objectUserName));
1459
1460   /* Add as a selected object       */
1461   /* Clear any previous selection : */
1462   /* Warning the LineEdit is purged because of signal currentSelectionChanged ! */ 
1463   Sel->ClearIObjects(); 
1464   Sel->AddIObject(SI);
1465   return true;
1466 }
1467
1468
1469 //=======================================================================
1470 // function : DefineDlgPosition()
1471 // purpose  : Define x and y the default position for a dialog box
1472 //=======================================================================
1473 bool GEOMBase::DefineDlgPosition(QWidget* aDlg, int& x, int& y)
1474 {
1475   /* Here the position is on the bottom right corner - 10 */
1476   QAD_Desktop* PP = QAD_Application::getDesktop();
1477   x = abs(PP->x() + PP->size().width() - aDlg->size().width() - 10);
1478   y = abs(PP->y() + PP->size().height() - aDlg->size().height() - 10);
1479   return true;  
1480 }
1481
1482
1483 //=====================================================================================
1484 // EXPORTED METHODS
1485 //=====================================================================================
1486 extern "C"
1487 {
1488   bool CustomPopup(QAD_Desktop* parent, QPopupMenu* popup, const QString & theContext,
1489                    const QString & theParent, const QString & theObject)
1490   {return GEOMBase::CustomPopup(parent, popup, theContext, theParent, theObject);}
1491 }