1 // Copyright (C) 2007-2010 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : GEOMBase.cxx
25 // Author : Damien COQUERET, Open CASCADE S.A.S.
29 #include <GeometryGUI.h>
30 #include <GEOM_Client.hxx>
33 #include <GEOM_Actor.h>
34 #include <SVTK_ViewModel.h>
35 #include <SVTK_ViewWindow.h>
36 #include <OCCViewer_ViewPort3d.h>
37 #include <OCCViewer_ViewModel.h>
38 #include <OCCViewer_ViewWindow.h>
40 #include <SALOME_ListIO.hxx>
41 #include <SALOME_ListIteratorOfListIO.hxx>
43 #include <SUIT_Desktop.h>
44 #include <SUIT_Session.h>
45 #include <SUIT_ViewManager.h>
46 #include <SUIT_ViewWindow.h>
47 #include <SUIT_MessageBox.h>
48 #include <SalomeApp_Application.h>
49 #include <SalomeApp_Study.h>
51 // // Open CASCADE Includes
52 #include <BRep_Tool.hxx>
53 #include <BRepAdaptor_Curve.hxx>
54 #include <BRepAdaptor_Surface.hxx>
55 #include <BRepPrimAPI_MakeCone.hxx>
57 #include <AIS_ListIteratorOfListOfInteractive.hxx>
58 #include <AIS_ListOfInteractive.hxx>
62 #include <TopTools_IndexedMapOfShape.hxx>
65 #include <Precision.hxx>
67 #include <vtkRenderer.h>
71 //=====================================================================================
72 // function : GetShapeFromIOR()
73 // purpose : exist also as static method !
74 //=====================================================================================
75 TopoDS_Shape GEOMBase::GetShapeFromIOR(QString IOR)
78 if(IOR.trimmed().isEmpty())
81 CORBA::Object_var obj = SalomeApp_Application::orb()->string_to_object(IOR.toLatin1().data());
82 if(CORBA::is_nil(obj))
84 GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( obj );
85 if (GeomObject->_is_nil())
88 result = GEOM_Client().GetShape(GeometryGUI::GetGeomGen(), GeomObject);
93 //=====================================================================================
94 // function : GetIndex()
95 // purpose : Get the index of a sub shape in a main shape : index start at 1
96 //=====================================================================================
97 int GEOMBase::GetIndex(const TopoDS_Shape& subshape, const TopoDS_Shape& shape, int /*ShapeType*/)
99 if(shape.IsNull() || subshape.IsNull())
102 TopTools_IndexedMapOfShape anIndices;
103 TopExp::MapShapes(shape, anIndices);
104 if(anIndices.Contains(subshape)) return anIndices.FindIndex(subshape);
110 //=======================================================================
111 // function : GetTopoFromSelection()
112 // purpose : Define tds from a single selection and retuen true
113 //=======================================================================
114 bool GEOMBase::GetTopoFromSelection(const SALOME_ListIO& aList, TopoDS_Shape& tds)
116 if(aList.Extent() != 1)
119 Handle(SALOME_InteractiveObject) IO = aList.First();
122 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
123 if ( !appStudy ) return false;
124 _PTR(Study) aStudy = appStudy->studyDS();
126 _PTR(SObject) obj ( aStudy->FindObjectID(IO->getEntry()) );
127 _PTR(GenericAttribute) anAttr;
129 if(obj->FindAttribute(anAttr, "AttributeIOR")) {
130 _PTR(AttributeIOR) anIOR ( anAttr );
131 tds = GetShapeFromIOR(anIOR->Value().c_str());
143 //=======================================================================
144 // function : GetNameOfSelectedIObjects()
145 // purpose : Define the name geom++ or other name of mono or multi sel.
146 //=======================================================================
147 int GEOMBase::GetNameOfSelectedIObjects( const SALOME_ListIO& aList,
149 const bool theShapesOnly )
151 if ( !theShapesOnly )
153 int nbSel = aList.Extent();
156 Handle(SALOME_InteractiveObject) anIObj = aList.First();
157 if(anIObj->hasEntry()) {
158 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
159 if ( !appStudy ) return nbSel;
160 _PTR(Study) aStudy = appStudy->studyDS();
162 _PTR(SObject) obj ( aStudy->FindObjectID(anIObj->getEntry()) );
164 _PTR(GenericAttribute) anAttr;
166 if ( obj && obj->FindAttribute( anAttr, "AttributeName") )
168 _PTR(AttributeName) aNameAttr ( anAttr );
169 theName = aNameAttr->Value().c_str();
174 theName = QObject::tr("%1_objects").arg(nbSel);
180 GEOM::ListOfGO anObjs;
181 ConvertListOfIOInListOfGO( aList, anObjs, theShapesOnly );
182 if ( anObjs.length() == 1 ) {
183 theName = GetName( anObjs[ 0 ] );
186 theName = QString( "%1_objects" ).arg( anObjs.length() );
188 return anObjs.length();
193 //=================================================================================
194 // function : GetShapeTypeString()
195 // purpose : for a single shape
196 //=================================================================================
197 bool GEOMBase::GetShapeTypeString(const TopoDS_Shape& aShape, Standard_CString& aTypeString)
199 if(aShape.IsNull()) {
200 aTypeString = "aNullShape";
203 switch(aShape.ShapeType())
205 case TopAbs_COMPOUND:
207 aTypeString = CORBA::string_dup(QObject::tr("GEOM_COMPOUND").toLatin1().constData());
210 case TopAbs_COMPSOLID:
212 aTypeString = CORBA::string_dup(QObject::tr("GEOM_COMPOUNDSOLID").toLatin1().constData()) ;
217 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SOLID").toLatin1().constData()) ;
222 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SHELL").toLatin1().constData()) ;
227 BRepAdaptor_Surface surf(TopoDS::Face(aShape));
228 if(surf.GetType() == GeomAbs_Plane) {
229 aTypeString = CORBA::string_dup(QObject::tr("GEOM_PLANE").toLatin1().constData());
232 else if(surf.GetType() == GeomAbs_Cylinder) {
233 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFCYLINDER").toLatin1().constData());
236 else if(surf.GetType() == GeomAbs_Sphere) {
237 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFSPHERE").toLatin1().constData());
240 else if(surf.GetType() == GeomAbs_Torus) {
241 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFTORUS").toLatin1().constData());
244 else if(surf.GetType() == GeomAbs_Cone) {
245 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFCONE").toLatin1().constData());
249 aTypeString = CORBA::string_dup(QObject::tr("GEOM_FACE").toLatin1().constData());
255 aTypeString = CORBA::string_dup(QObject::tr("GEOM_WIRE").toLatin1().constData());
260 BRepAdaptor_Curve curv(TopoDS::Edge(aShape));
261 if(curv.GetType() == GeomAbs_Line) {
262 if((Abs(curv.FirstParameter()) >= 1E6) || (Abs(curv.LastParameter()) >= 1E6))
263 aTypeString = CORBA::string_dup(QObject::tr("GEOM_LINE").toLatin1().constData());
265 aTypeString = CORBA::string_dup(QObject::tr("GEOM_EDGE").toLatin1().constData());
268 else if(curv.GetType() == GeomAbs_Circle) {
270 aTypeString = CORBA::string_dup(QObject::tr("GEOM_CIRCLE").toLatin1().constData());
272 aTypeString = CORBA::string_dup(QObject::tr("GEOM_ARC").toLatin1().constData());
276 aTypeString = CORBA::string_dup(QObject::tr("GEOM_EDGE").toLatin1().constData());
282 aTypeString = CORBA::string_dup(QObject::tr("GEOM_VERTEX").toLatin1().constData());
287 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SHAPE").toLatin1().constData());
295 //=======================================================================
296 // function : ConvertIORinGEOMAISShape()
298 //=======================================================================
299 Handle(GEOM_AISShape) GEOMBase::ConvertIORinGEOMAISShape(const char * IOR, Standard_Boolean& testResult, bool onlyInActiveView)
301 Handle(GEOM_AISShape) resultShape;
304 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
305 if ( !appStudy ) return resultShape;
306 _PTR(Study) aStudy = appStudy->studyDS();
308 _PTR(SObject) anObj ( aStudy->FindObjectIOR( IOR ) );
312 QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
314 QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
315 while ( it.hasNext() ) {
316 SUIT_ViewWindow* aVW = it.next();
317 if (aVW && aVW->getViewManager()->getType() == OCCViewer_Viewer::Type()) {
318 Handle (AIS_InteractiveContext) ic = ((OCCViewer_Viewer*)aVW->getViewManager()->getViewModel())->getAISContext();
320 AIS_ListOfInteractive List;
321 ic->DisplayedObjects(List);
322 AIS_ListOfInteractive List1;
323 ic->ObjectsInCollector(List1);
326 AIS_ListIteratorOfListOfInteractive ite(List);
328 if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
329 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
331 Handle(SALOME_InteractiveObject) GIO = Handle(SALOME_InteractiveObject)::DownCast(aSh->getIO());
332 if(GIO->hasEntry() && strcmp(GIO->getEntry(), anObj->GetID().c_str()) == 0) {
333 if(!onlyInActiveView ||
334 aVW == SUIT_Session::session()->activeApplication()->desktop()->activeWindow()) {
350 //=======================================================================
351 // function : ConvertIORinGEOMActor()
353 //=======================================================================
354 GEOM_Actor* GEOMBase::ConvertIORinGEOMActor(const char* IOR, Standard_Boolean& testResult, bool onlyInActiveView)
358 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
359 if ( !appStudy ) return GEOM_Actor::New();
360 _PTR(Study) aStudy = appStudy->studyDS();
362 _PTR(SObject) anObj ( aStudy->FindObjectIOR( IOR ) );
364 return GEOM_Actor::New();
366 QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
368 QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
369 while ( it.hasNext() ) {
370 SUIT_ViewWindow* aVW = it.next();
371 if (aVW && aVW->getViewManager()->getType() == SVTK_Viewer::Type()) {
372 SVTK_ViewWindow* aVTKViewWindow = dynamic_cast<SVTK_ViewWindow*>( aVW );
373 if( !aVTKViewWindow )
375 vtkRenderer* Renderer = aVTKViewWindow->getRenderer();
376 vtkActorCollection* theActors = Renderer->GetActors();
377 theActors->InitTraversal();
378 vtkActor *ac = theActors->GetNextActor();
380 if( ac->IsA("GEOM_Actor")) {
381 GEOM_Actor* anActor = GEOM_Actor::SafeDownCast(ac);
382 if(anActor->hasIO()) {
383 Handle(SALOME_InteractiveObject) GIO = Handle(SALOME_InteractiveObject)::DownCast(anActor->getIO());
384 if(GIO->hasEntry() && strcmp(GIO->getEntry(), anObj->GetID().c_str()) == 0) {
385 if(!onlyInActiveView ||
386 aVW == SUIT_Session::session()->activeApplication()->desktop()->activeWindow()) {
393 ac = theActors->GetNextActor();
398 return GEOM_Actor::New();
401 //=======================================================================
402 // function : GetAIS()
404 //=======================================================================
405 Handle(AIS_InteractiveObject) GEOMBase::GetAIS( const Handle(SALOME_InteractiveObject)& theIO,
406 const bool isOnlyInActiveView )
408 if ( theIO.IsNull() || !theIO->hasEntry() )
409 return Handle(AIS_InteractiveObject)();
411 QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
413 QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
414 while ( it.hasNext() ) {
415 SUIT_ViewWindow* aVW = it.next();
416 if (!aVW || aVW->getViewManager()->getType() != OCCViewer_Viewer::Type())
418 Handle (AIS_InteractiveContext) anIC = ((OCCViewer_Viewer*)aVW->getViewManager()->getViewModel())->getAISContext();
420 AIS_ListOfInteractive aList;
421 anIC->DisplayedObjects( aList );
422 anIC->ObjectsInCollector( aList );
424 AIS_ListIteratorOfListOfInteractive anIter( aList );
425 for ( ; anIter.More(); anIter.Next() )
427 Handle(SALOME_InteractiveObject) anObj =
428 Handle(SALOME_InteractiveObject)::DownCast( anIter.Value()->GetOwner() );
430 if( !anObj.IsNull() && strcmp( anObj->getEntry(), theIO->getEntry() ) == 0 )
432 if( isOnlyInActiveView )
434 if ( aVW == SUIT_Session::session()->activeApplication()->desktop()->activeWindow() )
435 return anIter.Value();
438 return anIter.Value();
443 return Handle(AIS_InteractiveObject)();
447 //=======================================================================
448 // function : ConvertIOinGEOMAISShape()
450 //=======================================================================
451 Handle(GEOM_AISShape) GEOMBase::ConvertIOinGEOMAISShape(const Handle(SALOME_InteractiveObject)& IO, Standard_Boolean& testResult, bool onlyInActiveView)
453 Handle(GEOM_AISShape) res;
455 if ( !IO->hasEntry() )
461 QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
463 QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
464 while ( it.hasNext() ) {
465 SUIT_ViewWindow* aVW = it.next();
466 if (aVW && aVW->getViewManager()->getType() == OCCViewer_Viewer::Type()) {
467 Handle (AIS_InteractiveContext) ic = ((OCCViewer_Viewer*)aVW->getViewManager()->getViewModel())->getAISContext();
469 AIS_ListOfInteractive List;
470 ic->DisplayedObjects(List);
471 AIS_ListOfInteractive List1;
472 ic->ObjectsInCollector(List1);
475 AIS_ListIteratorOfListOfInteractive ite(List);
478 if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape)))
480 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
483 if( strcmp( aSh->getIO()->getEntry(), IO->getEntry() ) == 0 )
487 if(aVW == SUIT_Session::session()->activeApplication()->desktop()->activeWindow())
510 //=======================================================================
511 // function : ConvertIOinGEOMShape()
513 //=======================================================================
514 GEOM::GEOM_Object_ptr GEOMBase::ConvertIOinGEOMShape(const Handle(SALOME_InteractiveObject)& IO,
515 Standard_Boolean& testResult)
517 GEOM::GEOM_Object_var aShape;
522 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
523 (SUIT_Session::session()->activeApplication()->activeStudy());
524 if ( !appStudy ) return GEOM::GEOM_Object::_nil();
525 _PTR(Study) aStudy = appStudy->studyDS();
527 _PTR(SObject) obj ( aStudy->FindObjectID(IO->getEntry()) );
528 _PTR(GenericAttribute) anAttr;
530 if(obj->FindAttribute(anAttr, "AttributeIOR")) {
531 _PTR(AttributeIOR) anIOR ( anAttr );
532 aShape = GeometryGUI::GetGeomGen()->GetIORFromString(anIOR->Value().c_str());
533 if(!CORBA::is_nil(aShape))
535 return aShape._retn();
539 return GEOM::GEOM_Object::_nil();
543 //=======================================================================
544 // function : ConvertListOfIOInListOfIOR()
546 //=======================================================================
547 void GEOMBase::ConvertListOfIOInListOfIOR(const SALOME_ListIO& aList, GEOM::string_array& listIOR)
549 int nbSel = aList.Extent();
550 listIOR.length(nbSel);
552 SALOME_ListIteratorOfListIO It(aList);
553 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
554 ( SUIT_Session::session()->activeApplication()->activeStudy() );
555 if ( !appStudy ) return;
556 _PTR(Study) aStudy = appStudy->studyDS();
558 for (int i=0; It.More(); It.Next(), i++) {
559 Handle(SALOME_InteractiveObject) IObject = It.Value();
560 if (IObject->hasEntry()) {
561 _PTR(SObject) obj ( aStudy->FindObjectID(IObject->getEntry()) );
562 _PTR(GenericAttribute) anAttr;
563 if (obj && obj->FindAttribute(anAttr, "AttributeIOR")) {
564 _PTR(AttributeIOR) anIOR (anAttr);
565 //CORBA::Object_var theObj = dynamic_cast<SALOMEDS_Study*>
566 // (aStudy.get())->ConvertIORToObject(anIOR->Value());
567 CORBA::Object_var theObj = GeometryGUI::ClientSObjectToObject(obj);
568 if (!CORBA::is_nil(theObj) && theObj->_is_a("IDL:GEOM/GEOM_Object:1.0")) {
569 listIOR[j] = CORBA::string_dup(anIOR->Value().c_str());
579 //=======================================================================
580 // function : ConvertIOinGEOMObject()
582 //=======================================================================
583 GEOM::GEOM_Object_ptr GEOMBase::ConvertIOinGEOMObject (const Handle(SALOME_InteractiveObject)& theIO,
584 Standard_Boolean& theResult)
586 theResult = Standard_False;
587 GEOM::GEOM_Object_var aReturnObject;
591 const char* anEntry = theIO->getEntry();
593 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
594 (SUIT_Session::session()->activeApplication()->activeStudy());
595 if (!appStudy) return GEOM::GEOM_Object::_nil();
596 _PTR(Study) aStudy = appStudy->studyDS();
598 _PTR(SObject) aSObj (aStudy->FindObjectID(anEntry));
602 aReturnObject = GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aSObj));
603 theResult = !CORBA::is_nil(aReturnObject);
606 return aReturnObject._retn();
610 //=======================================================================
611 // function : ConvertListOfIOInListOfGO()
613 //=======================================================================
614 void GEOMBase::ConvertListOfIOInListOfGO( const SALOME_ListIO& theList,
615 GEOM::ListOfGO& theListGO,
616 const bool theShapesOnly )
618 int nbSel = theList.Extent();
619 theListGO.length( nbSel );
620 SALOME_ListIteratorOfListIO anIter( theList );
622 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
623 if ( !appStudy ) return;
624 _PTR(Study) aStudy = appStudy->studyDS();
627 for ( int i=0; anIter.More(); anIter.Next(), i++ )
629 Handle(SALOME_InteractiveObject) anIObj = anIter.Value();
630 _PTR(SObject) aSObj ( aStudy->FindObjectID( anIObj->getEntry() ) );
634 GEOM::GEOM_Object_var aGeomObj =
635 GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aSObj));
636 if ( !CORBA::is_nil( aGeomObj ) && ( !theShapesOnly || IsShape( aGeomObj ) ) )
637 theListGO[ j++ ] = aGeomObj;
641 theListGO.length( j );
644 //=================================================================================
645 // function : CreateArrowForLinearEdge()
646 // purpose : Create a cone topology to be used to display an arrow in the middle
647 // : of an edge showing its orientation. (For simulation and Viewer OCC only)
648 //=================================================================================
649 bool GEOMBase::CreateArrowForLinearEdge(const TopoDS_Shape& tds, TopoDS_Shape& ArrowCone)
651 if(SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType()
652 != OCCViewer_Viewer::Type() || tds.ShapeType() != TopAbs_EDGE)
655 OCCViewer_ViewPort3d* vp3d = ((OCCViewer_ViewWindow*)SUIT_Session::session()->activeApplication()->desktop()->activeWindow())->getViewPort();
656 Handle( V3d_View) view3d = vp3d->getView();
657 Standard_Real Width, Height;
658 view3d->Size(Width, Height);
659 const Standard_Real aHeight = (Width + Height) / 50.0;
662 Standard_Real first, last;
663 Handle(Geom_Curve) curv = BRep_Tool::Curve(TopoDS::Edge(tds), first, last);
667 const Standard_Real param = (first+last) / 2.0;
668 gp_Pnt middleParamPoint;
670 curv->D1( param, middleParamPoint, V1);
671 if(V1.Magnitude() < Precision::Confusion())
674 /* Topology orientation not geom orientation */
675 if(tds.Orientation() == TopAbs_REVERSED)
678 gp_Ax2 anAxis( middleParamPoint, gp_Dir(V1));
679 const Standard_Real radius1 = aHeight / 5.0;
680 if(radius1 > 10.0 * Precision::Confusion() && aHeight > 10.0 * Precision::Confusion()) {
681 ArrowCone = BRepPrimAPI_MakeCone( anAxis, radius1, 0.0, aHeight ).Shape();
685 catch(Standard_Failure) {
686 // OCC failures are hard to catch in GUI.
687 // This because of the position for #include <Standard_ErrorHandler.hxx> that is very critic to find
688 // in SALOME environment : compilation error !
694 //=================================================================================
695 // function : VertexToPoint()
696 // purpose : If S can be converted in a gp_Pnt returns true and the result is P
697 //=================================================================================
698 bool GEOMBase::VertexToPoint(const TopoDS_Shape& S, gp_Pnt& P)
700 if(S.IsNull() || S.ShapeType() != TopAbs_VERTEX)
702 P = BRep_Tool::Pnt(TopoDS::Vertex(S));
707 //=================================================================================
708 // function : GetBipointDxDyDz()
710 //=================================================================================
711 void GEOMBase::GetBipointDxDyDz(gp_Pnt P1, gp_Pnt P2, double& dx, double& dy, double& dz)
713 dx = P2.X() - P1.X();
714 dy = P2.Y() - P1.Y();
715 dz = P2.Z() - P1.Z();
720 //=================================================================================
721 // function : LinearEdgeExtremities()
722 // purpose : If S can be converted in a linear edge and if initial an final points
723 // : distance is sufficient, returns true else returns false.
724 // : Resulting points are respectively P1 and P2
725 //=================================================================================
726 bool GEOMBase::LinearEdgeExtremities(const TopoDS_Shape& S, gp_Pnt& P1, gp_Pnt& P2)
728 if(S.IsNull() || S.ShapeType() != TopAbs_EDGE)
730 BRepAdaptor_Curve curv(TopoDS::Edge(S));
731 if(curv.GetType() != GeomAbs_Line)
734 curv.D0(curv.FirstParameter(), P1);
735 curv.D0(curv.LastParameter(), P2);
737 if(P1.Distance(P2) <= Precision::Confusion())
744 //=======================================================================
745 // function : SelectionByNameInDialogs()
746 // purpose : Called when user has entered a name of object in a LineEdit.
747 // : The selection is changed. Dialog box will receive the
748 // : corresponding signal to manage this event.
749 //=======================================================================
750 bool GEOMBase::SelectionByNameInDialogs(QWidget* aWidget, const QString& objectUserName, const SALOME_ListIO& aList)
752 /* Find SObject with name in component GEOM */
753 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
754 if ( !appStudy ) return false;
755 _PTR(Study) ST = appStudy->studyDS();
757 std::vector<_PTR(SObject)> listSO;
758 listSO = ST->FindObjectByName(objectUserName.toStdString(), "GEOM");
760 if(listSO.size() < 1) {
761 const QString caption = QObject::tr("GEOM_WRN_WARNING");
762 const QString text = QObject::tr("GEOM_NAME_INCORRECT");
763 const QString button0 = QObject::tr("GEOM_BUT_OK");
764 SUIT_MessageBox::critical(aWidget, caption, text, button0);
767 /* More than one object with same name */
768 if(listSO.size() > 1) {
769 const QString caption = QObject::tr("GEOM_WRN_WARNING");
770 const QString text = QObject::tr("GEOM_IDENTICAL_NAMES_SELECT_BY_MOUSE");
771 const QString button0 = QObject::tr("GEOM_BUT_OK") ;
772 SUIT_MessageBox::critical(aWidget, caption, text, button0) ;
777 _PTR(SObject) theObj ( listSO[0] );
778 /* Create a SALOME_InteractiveObject with a SALOME::SObject */
779 char* aCopyobjectUserName = CORBA::string_dup(objectUserName.toLatin1().constData());
780 Handle(SALOME_InteractiveObject) SI = new SALOME_InteractiveObject(theObj->GetID().c_str(), "GEOM", aCopyobjectUserName);
781 delete(aCopyobjectUserName);
783 /* Add as a selected object */
784 /* Clear any previous selection : */
785 /* Warning the LineEdit is purged because of signal currentSelectionChanged ! */
786 // Sel->ClearIObjects(); //mzn
787 // Sel->AddIObject(SI); //mzn
792 //=======================================================================
793 // function : DefineDlgPosition()
794 // purpose : Define x and y the default position for a dialog box
795 //=======================================================================
796 bool GEOMBase::DefineDlgPosition(QWidget* aDlg, int& x, int& y)
798 /* Here the position is on the bottom right corner - 10 */
799 SUIT_Desktop* PP = SUIT_Session::session()->activeApplication()->desktop();
800 x = abs(PP->x() + PP->size().width() - aDlg->size().width() - 10);
801 y = abs(PP->y() + PP->size().height() - aDlg->size().height() - 10);
806 //=======================================================================
807 // function : GetDefaultName()
808 // purpose : Generates default names
809 //=======================================================================
810 QString GEOMBase::GetDefaultName(const QString& theOperation, const bool extractPrefix)
814 // collect all object names of GEOM component
815 SalomeApp_Study* appStudy =
816 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
817 if ( !appStudy ) return aName;
818 _PTR(Study) aStudy = appStudy->studyDS();
820 std::set<std::string> aSet;
821 _PTR(SComponent) aGeomCompo (aStudy->FindComponent("GEOM"));
823 _PTR(ChildIterator) it (aStudy->NewChildIterator(aGeomCompo));
825 for (it->InitEx(true); it->More(); it->Next()) {
827 aSet.insert(obj->GetName());
831 // build a unique name
833 bool isUnique = false;
834 QString prefix = theOperation;
836 if ( extractPrefix ) {
837 QStringList parts = prefix.split( "_", QString::KeepEmptyParts );
838 if ( parts.count() > 1 ) {
840 aNumber = parts.last().toLong(&ok);
843 prefix = parts.join( "_" );
850 aName = prefix + "_" + QString::number(++aNumber);
851 isUnique = (aSet.count(aName.toStdString()) == 0);
858 //=======================================================================
859 // function : ShowErrorMessage()
860 // purpose : Shows message box with error code and comment
861 //=======================================================================
862 void GEOMBase::ShowErrorMessage(const char* theErrorCode, const char* theComment)
864 QString anErrorCode(theErrorCode);
865 QString aComment(theComment);
868 if (!anErrorCode.isEmpty())
869 aText.append("\n" + QObject::tr(anErrorCode.toLatin1().constData()));
870 if (!aComment.isEmpty())
871 aText.append("\n" + QString(theComment));
873 SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(), QObject::tr( "GEOM_ERROR" ),
874 QObject::tr("GEOM_PRP_ABORT") + aText, "OK" );
878 //=======================================================================
879 // function : GetObjectFromIOR()
880 // purpose : returns a GEOM_Object by given IOR (string)
881 //=======================================================================
882 GEOM::GEOM_Object_ptr GEOMBase::GetObjectFromIOR( const char* theIOR )
884 GEOM::GEOM_Object_var anObject;
885 if ( theIOR == NULL || strlen( theIOR ) == 0 )
886 return anObject._retn(); // returning nil object
888 anObject = GEOM::GEOM_Object::_narrow( SalomeApp_Application::orb()->string_to_object( theIOR ) );
889 return anObject._retn();
892 //=======================================================================
893 // function : GetIORFromObject()
894 // purpose : returns IOR of a given GEOM_Object
895 //=======================================================================
896 char* GEOMBase::GetIORFromObject( const GEOM::GEOM_Object_ptr& theObject )
898 if ( CORBA::is_nil( theObject ) )
901 return SalomeApp_Application::orb()->object_to_string( theObject );
904 //=======================================================================
905 // function : GetShape()
906 // purpose : returns a TopoDS_Shape stored in GEOM_Object
907 //=======================================================================
908 bool GEOMBase::GetShape( const GEOM::GEOM_Object_ptr& theObject, TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType )
910 if ( !CORBA::is_nil( theObject ) )
912 TopoDS_Shape aTopoDSShape = GEOM_Client().GetShape( GeometryGUI::GetGeomGen(), theObject );
913 if ( !aTopoDSShape.IsNull() && ( theType == TopAbs_SHAPE || theType == aTopoDSShape.ShapeType() ) )
915 theShape = aTopoDSShape;
922 //=======================================================================
923 // function : GetName()
924 // purpose : Get name of object
925 //=======================================================================
926 QString GEOMBase::GetName( GEOM::GEOM_Object_ptr theObj )
928 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
932 CORBA::String_var anIOR = SalomeApp_Application::orb()->object_to_string( theObj );
933 if ( strcmp(anIOR.in(), "") != 0 )
935 _PTR(SObject) aSObj ( appStudy->studyDS()->FindObjectIOR( std::string( anIOR ) ) );
937 _PTR(GenericAttribute) anAttr;
939 if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") )
941 _PTR(AttributeName) aNameAttr ( anAttr );
942 return QString( aNameAttr->Value().c_str() );
950 bool GEOMBase::IsShape( GEOM::GEOM_Object_ptr theObj )
952 return !theObj->_is_nil() && theObj->IsShape();