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