1 // Copyright (C) 2007-2008 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
22 // GEOM GEOMGUI : GUI for Geometry component
23 // File : GEOMBase.cxx
24 // Author : Damien COQUERET, Open CASCADE S.A.S.
28 #include <GeometryGUI.h>
29 #include <GEOM_Client.hxx>
32 #include <GEOM_Actor.h>
33 #include <SVTK_ViewModel.h>
34 #include <SVTK_ViewWindow.h>
35 #include <OCCViewer_ViewPort3d.h>
36 #include <OCCViewer_ViewModel.h>
37 #include <OCCViewer_ViewWindow.h>
39 #include <SALOME_ListIO.hxx>
40 #include <SALOME_ListIteratorOfListIO.hxx>
42 #include <SUIT_Desktop.h>
43 #include <SUIT_Session.h>
44 #include <SUIT_ViewManager.h>
45 #include <SUIT_ViewWindow.h>
46 #include <SUIT_MessageBox.h>
47 #include <SalomeApp_Application.h>
48 #include <SalomeApp_Study.h>
50 // // Open CASCADE Includes
51 #include <BRep_Tool.hxx>
52 #include <BRepAdaptor_Curve.hxx>
53 #include <BRepAdaptor_Surface.hxx>
54 #include <BRepPrimAPI_MakeCone.hxx>
56 #include <AIS_ListIteratorOfListOfInteractive.hxx>
57 #include <AIS_ListOfInteractive.hxx>
61 #include <TopTools_IndexedMapOfShape.hxx>
64 #include <Precision.hxx>
66 #include <vtkRenderer.h>
70 //=====================================================================================
71 // function : GetShapeFromIOR()
72 // purpose : exist also as static method !
73 //=====================================================================================
74 TopoDS_Shape GEOMBase::GetShapeFromIOR(QString IOR)
77 if(IOR.trimmed().isEmpty())
80 CORBA::Object_var obj = SalomeApp_Application::orb()->string_to_object(IOR.toLatin1().data());
81 if(CORBA::is_nil(obj))
83 GEOM::GEOM_Object_var GeomObject = GEOM::GEOM_Object::_narrow( obj );
84 if (GeomObject->_is_nil())
87 result = GEOM_Client().GetShape(GeometryGUI::GetGeomGen(), GeomObject);
92 //=====================================================================================
93 // function : GetIndex()
94 // purpose : Get the index of a sub shape in a main shape : index start at 1
95 //=====================================================================================
96 int GEOMBase::GetIndex(const TopoDS_Shape& subshape, const TopoDS_Shape& shape, int /*ShapeType*/)
98 if(shape.IsNull() || subshape.IsNull())
101 TopTools_IndexedMapOfShape anIndices;
102 TopExp::MapShapes(shape, anIndices);
103 if(anIndices.Contains(subshape)) return anIndices.FindIndex(subshape);
109 //=======================================================================
110 // function : GetTopoFromSelection()
111 // purpose : Define tds from a single selection and retuen true
112 //=======================================================================
113 bool GEOMBase::GetTopoFromSelection(const SALOME_ListIO& aList, TopoDS_Shape& tds)
115 if(aList.Extent() != 1)
118 Handle(SALOME_InteractiveObject) IO = aList.First();
121 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
122 if ( !appStudy ) return false;
123 _PTR(Study) aStudy = appStudy->studyDS();
125 _PTR(SObject) obj ( aStudy->FindObjectID(IO->getEntry()) );
126 _PTR(GenericAttribute) anAttr;
128 if(obj->FindAttribute(anAttr, "AttributeIOR")) {
129 _PTR(AttributeIOR) anIOR ( anAttr );
130 tds = GetShapeFromIOR(anIOR->Value().c_str());
142 //=======================================================================
143 // function : GetNameOfSelectedIObjects()
144 // purpose : Define the name geom++ or other name of mono or multi sel.
145 //=======================================================================
146 int GEOMBase::GetNameOfSelectedIObjects( const SALOME_ListIO& aList,
148 const bool theShapesOnly )
150 if ( !theShapesOnly )
152 int nbSel = aList.Extent();
155 Handle(SALOME_InteractiveObject) anIObj = aList.First();
156 if(anIObj->hasEntry()) {
157 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
158 if ( !appStudy ) return nbSel;
159 _PTR(Study) aStudy = appStudy->studyDS();
161 _PTR(SObject) obj ( aStudy->FindObjectID(anIObj->getEntry()) );
163 _PTR(GenericAttribute) anAttr;
165 if ( obj && obj->FindAttribute( anAttr, "AttributeName") )
167 _PTR(AttributeName) aNameAttr ( anAttr );
168 theName = aNameAttr->Value().c_str();
173 theName = QObject::tr("%1_objects").arg(nbSel);
179 GEOM::ListOfGO anObjs;
180 ConvertListOfIOInListOfGO( aList, anObjs, theShapesOnly );
181 if ( anObjs.length() == 1 ) {
182 theName = GetName( anObjs[ 0 ] );
185 theName = QString( "%1_objects" ).arg( anObjs.length() );
187 return anObjs.length();
192 //=================================================================================
193 // function : GetShapeTypeString()
194 // purpose : for a single shape
195 //=================================================================================
196 bool GEOMBase::GetShapeTypeString(const TopoDS_Shape& aShape, Standard_CString& aTypeString)
198 if(aShape.IsNull()) {
199 aTypeString = "aNullShape";
202 switch(aShape.ShapeType())
204 case TopAbs_COMPOUND:
206 aTypeString = CORBA::string_dup(QObject::tr("GEOM_COMPOUND").toLatin1().constData());
209 case TopAbs_COMPSOLID:
211 aTypeString = CORBA::string_dup(QObject::tr("GEOM_COMPOUNDSOLID").toLatin1().constData()) ;
216 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SOLID").toLatin1().constData()) ;
221 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SHELL").toLatin1().constData()) ;
226 BRepAdaptor_Surface surf(TopoDS::Face(aShape));
227 if(surf.GetType() == GeomAbs_Plane) {
228 aTypeString = CORBA::string_dup(QObject::tr("GEOM_PLANE").toLatin1().constData());
231 else if(surf.GetType() == GeomAbs_Cylinder) {
232 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFCYLINDER").toLatin1().constData());
235 else if(surf.GetType() == GeomAbs_Sphere) {
236 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFSPHERE").toLatin1().constData());
239 else if(surf.GetType() == GeomAbs_Torus) {
240 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFTORUS").toLatin1().constData());
243 else if(surf.GetType() == GeomAbs_Cone) {
244 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SURFCONE").toLatin1().constData());
248 aTypeString = CORBA::string_dup(QObject::tr("GEOM_FACE").toLatin1().constData());
254 aTypeString = CORBA::string_dup(QObject::tr("GEOM_WIRE").toLatin1().constData());
259 BRepAdaptor_Curve curv(TopoDS::Edge(aShape));
260 if(curv.GetType() == GeomAbs_Line) {
261 if((Abs(curv.FirstParameter()) >= 1E6) || (Abs(curv.LastParameter()) >= 1E6))
262 aTypeString = CORBA::string_dup(QObject::tr("GEOM_LINE").toLatin1().constData());
264 aTypeString = CORBA::string_dup(QObject::tr("GEOM_EDGE").toLatin1().constData());
267 else if(curv.GetType() == GeomAbs_Circle) {
269 aTypeString = CORBA::string_dup(QObject::tr("GEOM_CIRCLE").toLatin1().constData());
271 aTypeString = CORBA::string_dup(QObject::tr("GEOM_ARC").toLatin1().constData());
275 aTypeString = CORBA::string_dup(QObject::tr("GEOM_EDGE").toLatin1().constData());
281 aTypeString = CORBA::string_dup(QObject::tr("GEOM_VERTEX").toLatin1().constData());
286 aTypeString = CORBA::string_dup(QObject::tr("GEOM_SHAPE").toLatin1().constData());
294 //=======================================================================
295 // function : ConvertIORinGEOMAISShape()
297 //=======================================================================
298 Handle(GEOM_AISShape) GEOMBase::ConvertIORinGEOMAISShape(const char * IOR, Standard_Boolean& testResult, bool onlyInActiveView)
300 Handle(GEOM_AISShape) resultShape;
303 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
304 if ( !appStudy ) return resultShape;
305 _PTR(Study) aStudy = appStudy->studyDS();
307 _PTR(SObject) anObj ( aStudy->FindObjectIOR( IOR ) );
311 QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
313 QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
314 while ( it.hasNext() ) {
315 SUIT_ViewWindow* aVW = it.next();
316 if (aVW && aVW->getViewManager()->getType() == OCCViewer_Viewer::Type()) {
317 Handle (AIS_InteractiveContext) ic = ((OCCViewer_Viewer*)aVW->getViewManager()->getViewModel())->getAISContext();
319 AIS_ListOfInteractive List;
320 ic->DisplayedObjects(List);
321 AIS_ListOfInteractive List1;
322 ic->ObjectsInCollector(List1);
325 AIS_ListIteratorOfListOfInteractive ite(List);
327 if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape))) {
328 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
330 Handle(SALOME_InteractiveObject) GIO = Handle(SALOME_InteractiveObject)::DownCast(aSh->getIO());
331 if(GIO->hasEntry() && strcmp(GIO->getEntry(), anObj->GetID().c_str()) == 0) {
332 if(!onlyInActiveView ||
333 aVW == SUIT_Session::session()->activeApplication()->desktop()->activeWindow()) {
349 //=======================================================================
350 // function : ConvertIORinGEOMActor()
352 //=======================================================================
353 GEOM_Actor* GEOMBase::ConvertIORinGEOMActor(const char* IOR, Standard_Boolean& testResult, bool onlyInActiveView)
357 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
358 if ( !appStudy ) return GEOM_Actor::New();
359 _PTR(Study) aStudy = appStudy->studyDS();
361 _PTR(SObject) anObj ( aStudy->FindObjectIOR( IOR ) );
363 return GEOM_Actor::New();
365 QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
367 QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
368 while ( it.hasNext() ) {
369 SUIT_ViewWindow* aVW = it.next();
370 if (aVW && aVW->getViewManager()->getType() == SVTK_Viewer::Type()) {
371 SVTK_ViewWindow* aVTKViewWindow = dynamic_cast<SVTK_ViewWindow*>( aVW );
372 if( !aVTKViewWindow )
374 vtkRenderer* Renderer = aVTKViewWindow->getRenderer();
375 vtkActorCollection* theActors = Renderer->GetActors();
376 theActors->InitTraversal();
377 vtkActor *ac = theActors->GetNextActor();
379 if( ac->IsA("GEOM_Actor")) {
380 GEOM_Actor* anActor = GEOM_Actor::SafeDownCast(ac);
381 if(anActor->hasIO()) {
382 Handle(SALOME_InteractiveObject) GIO = Handle(SALOME_InteractiveObject)::DownCast(anActor->getIO());
383 if(GIO->hasEntry() && strcmp(GIO->getEntry(), anObj->GetID().c_str()) == 0) {
384 if(!onlyInActiveView ||
385 aVW == SUIT_Session::session()->activeApplication()->desktop()->activeWindow()) {
392 ac = theActors->GetNextActor();
397 return GEOM_Actor::New();
400 //=======================================================================
401 // function : GetAIS()
403 //=======================================================================
404 Handle(AIS_InteractiveObject) GEOMBase::GetAIS( const Handle(SALOME_InteractiveObject)& theIO,
405 const bool isOnlyInActiveView )
407 if ( theIO.IsNull() || !theIO->hasEntry() )
408 return Handle(AIS_InteractiveObject)();
410 QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
412 QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
413 while ( it.hasNext() ) {
414 SUIT_ViewWindow* aVW = it.next();
415 if (!aVW || aVW->getViewManager()->getType() != OCCViewer_Viewer::Type())
417 Handle (AIS_InteractiveContext) anIC = ((OCCViewer_Viewer*)aVW->getViewManager()->getViewModel())->getAISContext();
419 AIS_ListOfInteractive aList;
420 anIC->DisplayedObjects( aList );
421 anIC->ObjectsInCollector( aList );
423 AIS_ListIteratorOfListOfInteractive anIter( aList );
424 for ( ; anIter.More(); anIter.Next() )
426 Handle(SALOME_InteractiveObject) anObj =
427 Handle(SALOME_InteractiveObject)::DownCast( anIter.Value()->GetOwner() );
429 if( !anObj.IsNull() && strcmp( anObj->getEntry(), theIO->getEntry() ) == 0 )
431 if( isOnlyInActiveView )
433 if ( aVW == SUIT_Session::session()->activeApplication()->desktop()->activeWindow() )
434 return anIter.Value();
437 return anIter.Value();
442 return Handle(AIS_InteractiveObject)();
446 //=======================================================================
447 // function : ConvertIOinGEOMAISShape()
449 //=======================================================================
450 Handle(GEOM_AISShape) GEOMBase::ConvertIOinGEOMAISShape(const Handle(SALOME_InteractiveObject)& IO, Standard_Boolean& testResult, bool onlyInActiveView)
452 Handle(GEOM_AISShape) res;
454 if ( !IO->hasEntry() )
460 QList<SUIT_ViewWindow*> aViewWindowsList = SUIT_Session::session()->activeApplication()->desktop()->windows();
462 QListIterator<SUIT_ViewWindow*> it( aViewWindowsList );
463 while ( it.hasNext() ) {
464 SUIT_ViewWindow* aVW = it.next();
465 if (aVW && aVW->getViewManager()->getType() == OCCViewer_Viewer::Type()) {
466 Handle (AIS_InteractiveContext) ic = ((OCCViewer_Viewer*)aVW->getViewManager()->getViewModel())->getAISContext();
468 AIS_ListOfInteractive List;
469 ic->DisplayedObjects(List);
470 AIS_ListOfInteractive List1;
471 ic->ObjectsInCollector(List1);
474 AIS_ListIteratorOfListOfInteractive ite(List);
477 if(ite.Value()->IsInstance(STANDARD_TYPE(GEOM_AISShape)))
479 Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast(ite.Value());
482 if( strcmp( aSh->getIO()->getEntry(), IO->getEntry() ) == 0 )
486 if(aVW == SUIT_Session::session()->activeApplication()->desktop()->activeWindow())
509 //=======================================================================
510 // function : ConvertIOinGEOMShape()
512 //=======================================================================
513 GEOM::GEOM_Object_ptr GEOMBase::ConvertIOinGEOMShape(const Handle(SALOME_InteractiveObject)& IO,
514 Standard_Boolean& testResult)
516 GEOM::GEOM_Object_var aShape;
521 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
522 (SUIT_Session::session()->activeApplication()->activeStudy());
523 if ( !appStudy ) return GEOM::GEOM_Object::_nil();
524 _PTR(Study) aStudy = appStudy->studyDS();
526 _PTR(SObject) obj ( aStudy->FindObjectID(IO->getEntry()) );
527 _PTR(GenericAttribute) anAttr;
529 if(obj->FindAttribute(anAttr, "AttributeIOR")) {
530 _PTR(AttributeIOR) anIOR ( anAttr );
531 aShape = GeometryGUI::GetGeomGen()->GetIORFromString(anIOR->Value().c_str());
532 if(!CORBA::is_nil(aShape))
534 return aShape._retn();
538 return GEOM::GEOM_Object::_nil();
542 //=======================================================================
543 // function : ConvertListOfIOInListOfIOR()
545 //=======================================================================
546 void GEOMBase::ConvertListOfIOInListOfIOR(const SALOME_ListIO& aList, GEOM::string_array& listIOR)
548 int nbSel = aList.Extent();
549 listIOR.length(nbSel);
551 SALOME_ListIteratorOfListIO It(aList);
552 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
553 ( SUIT_Session::session()->activeApplication()->activeStudy() );
554 if ( !appStudy ) return;
555 _PTR(Study) aStudy = appStudy->studyDS();
557 for (int i=0; It.More(); It.Next(), i++) {
558 Handle(SALOME_InteractiveObject) IObject = It.Value();
559 if (IObject->hasEntry()) {
560 _PTR(SObject) obj ( aStudy->FindObjectID(IObject->getEntry()) );
561 _PTR(GenericAttribute) anAttr;
562 if (obj && obj->FindAttribute(anAttr, "AttributeIOR")) {
563 _PTR(AttributeIOR) anIOR (anAttr);
564 //CORBA::Object_var theObj = dynamic_cast<SALOMEDS_Study*>
565 // (aStudy.get())->ConvertIORToObject(anIOR->Value());
566 CORBA::Object_var theObj = GeometryGUI::ClientSObjectToObject(obj);
567 if (!CORBA::is_nil(theObj) && theObj->_is_a("IDL:GEOM/GEOM_Object:1.0")) {
568 listIOR[j] = CORBA::string_dup(anIOR->Value().c_str());
578 //=======================================================================
579 // function : ConvertIOinGEOMObject()
581 //=======================================================================
582 GEOM::GEOM_Object_ptr GEOMBase::ConvertIOinGEOMObject (const Handle(SALOME_InteractiveObject)& theIO,
583 Standard_Boolean& theResult)
585 theResult = Standard_False;
586 GEOM::GEOM_Object_var aReturnObject;
590 const char* anEntry = theIO->getEntry();
592 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>
593 (SUIT_Session::session()->activeApplication()->activeStudy());
594 if (!appStudy) return GEOM::GEOM_Object::_nil();
595 _PTR(Study) aStudy = appStudy->studyDS();
597 _PTR(SObject) aSObj (aStudy->FindObjectID(anEntry));
601 aReturnObject = GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aSObj));
602 theResult = !CORBA::is_nil(aReturnObject);
605 return aReturnObject._retn();
609 //=======================================================================
610 // function : ConvertListOfIOInListOfGO()
612 //=======================================================================
613 void GEOMBase::ConvertListOfIOInListOfGO( const SALOME_ListIO& theList,
614 GEOM::ListOfGO& theListGO,
615 const bool theShapesOnly )
617 int nbSel = theList.Extent();
618 theListGO.length( nbSel );
619 SALOME_ListIteratorOfListIO anIter( theList );
621 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
622 if ( !appStudy ) return;
623 _PTR(Study) aStudy = appStudy->studyDS();
626 for ( int i=0; anIter.More(); anIter.Next(), i++ )
628 Handle(SALOME_InteractiveObject) anIObj = anIter.Value();
629 _PTR(SObject) aSObj ( aStudy->FindObjectID( anIObj->getEntry() ) );
633 GEOM::GEOM_Object_var aGeomObj =
634 GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aSObj));
635 if ( !CORBA::is_nil( aGeomObj ) && ( !theShapesOnly || IsShape( aGeomObj ) ) )
636 theListGO[ j++ ] = aGeomObj;
640 theListGO.length( j );
643 //=================================================================================
644 // function : CreateArrowForLinearEdge()
645 // purpose : Create a cone topology to be used to display an arrow in the middle
646 // : of an edge showing its orientation. (For simulation and Viewer OCC only)
647 //=================================================================================
648 bool GEOMBase::CreateArrowForLinearEdge(const TopoDS_Shape& tds, TopoDS_Shape& ArrowCone)
650 if(SUIT_Session::session()->activeApplication()->desktop()->activeWindow()->getViewManager()->getType()
651 != OCCViewer_Viewer::Type() || tds.ShapeType() != TopAbs_EDGE)
654 OCCViewer_ViewPort3d* vp3d = ((OCCViewer_ViewWindow*)SUIT_Session::session()->activeApplication()->desktop()->activeWindow())->getViewPort();
655 Handle( V3d_View) view3d = vp3d->getView();
656 Standard_Real Width, Height;
657 view3d->Size(Width, Height);
658 const Standard_Real aHeight = (Width + Height) / 50.0;
661 Standard_Real first, last;
662 Handle(Geom_Curve) curv = BRep_Tool::Curve(TopoDS::Edge(tds), first, last);
666 const Standard_Real param = (first+last) / 2.0;
667 gp_Pnt middleParamPoint;
669 curv->D1( param, middleParamPoint, V1);
670 if(V1.Magnitude() < Precision::Confusion())
673 /* Topology orientation not geom orientation */
674 if(tds.Orientation() == TopAbs_REVERSED)
677 gp_Ax2 anAxis( middleParamPoint, gp_Dir(V1));
678 const Standard_Real radius1 = aHeight / 5.0;
679 if(radius1 > 10.0 * Precision::Confusion() && aHeight > 10.0 * Precision::Confusion()) {
680 ArrowCone = BRepPrimAPI_MakeCone( anAxis, radius1, 0.0, aHeight ).Shape();
684 catch(Standard_Failure) {
685 // OCC failures are hard to catch in GUI.
686 // This because of the position for #include <Standard_ErrorHandler.hxx> that is very critic to find
687 // in SALOME environment : compilation error !
693 //=================================================================================
694 // function : VertexToPoint()
695 // purpose : If S can be converted in a gp_Pnt returns true and the result is P
696 //=================================================================================
697 bool GEOMBase::VertexToPoint(const TopoDS_Shape& S, gp_Pnt& P)
699 if(S.IsNull() || S.ShapeType() != TopAbs_VERTEX)
701 P = BRep_Tool::Pnt(TopoDS::Vertex(S));
706 //=================================================================================
707 // function : GetBipointDxDyDz()
709 //=================================================================================
710 void GEOMBase::GetBipointDxDyDz(gp_Pnt P1, gp_Pnt P2, double& dx, double& dy, double& dz)
712 dx = P2.X() - P1.X();
713 dy = P2.Y() - P1.Y();
714 dz = P2.Z() - P1.Z();
719 //=================================================================================
720 // function : LinearEdgeExtremities()
721 // purpose : If S can be converted in a linear edge and if initial an final points
722 // : distance is sufficient, returns true else returns false.
723 // : Resulting points are respectively P1 and P2
724 //=================================================================================
725 bool GEOMBase::LinearEdgeExtremities(const TopoDS_Shape& S, gp_Pnt& P1, gp_Pnt& P2)
727 if(S.IsNull() || S.ShapeType() != TopAbs_EDGE)
729 BRepAdaptor_Curve curv(TopoDS::Edge(S));
730 if(curv.GetType() != GeomAbs_Line)
733 curv.D0(curv.FirstParameter(), P1);
734 curv.D0(curv.LastParameter(), P2);
736 if(P1.Distance(P2) <= Precision::Confusion())
743 //=======================================================================
744 // function : SelectionByNameInDialogs()
745 // purpose : Called when user has entered a name of object in a LineEdit.
746 // : The selection is changed. Dialog box will receive the
747 // : corresponding signal to manage this event.
748 //=======================================================================
749 bool GEOMBase::SelectionByNameInDialogs(QWidget* aWidget, const QString& objectUserName, const SALOME_ListIO& aList)
751 /* Find SObject with name in component GEOM */
752 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
753 if ( !appStudy ) return false;
754 _PTR(Study) ST = appStudy->studyDS();
756 std::vector<_PTR(SObject)> listSO;
757 listSO = ST->FindObjectByName(objectUserName.toStdString(), "GEOM");
759 if(listSO.size() < 1) {
760 const QString caption = QObject::tr("GEOM_WRN_WARNING");
761 const QString text = QObject::tr("GEOM_NAME_INCORRECT");
762 const QString button0 = QObject::tr("GEOM_BUT_OK");
763 SUIT_MessageBox::critical(aWidget, caption, text, button0);
766 /* More than one object with same name */
767 if(listSO.size() > 1) {
768 const QString caption = QObject::tr("GEOM_WRN_WARNING");
769 const QString text = QObject::tr("GEOM_IDENTICAL_NAMES_SELECT_BY_MOUSE");
770 const QString button0 = QObject::tr("GEOM_BUT_OK") ;
771 SUIT_MessageBox::critical(aWidget, caption, text, button0) ;
776 _PTR(SObject) theObj ( listSO[0] );
777 /* Create a SALOME_InteractiveObject with a SALOME::SObject */
778 char* aCopyobjectUserName = CORBA::string_dup(objectUserName.toLatin1().constData());
779 Handle(SALOME_InteractiveObject) SI = new SALOME_InteractiveObject(theObj->GetID().c_str(), "GEOM", aCopyobjectUserName);
780 delete(aCopyobjectUserName);
782 /* Add as a selected object */
783 /* Clear any previous selection : */
784 /* Warning the LineEdit is purged because of signal currentSelectionChanged ! */
785 // Sel->ClearIObjects(); //mzn
786 // Sel->AddIObject(SI); //mzn
791 //=======================================================================
792 // function : DefineDlgPosition()
793 // purpose : Define x and y the default position for a dialog box
794 //=======================================================================
795 bool GEOMBase::DefineDlgPosition(QWidget* aDlg, int& x, int& y)
797 /* Here the position is on the bottom right corner - 10 */
798 SUIT_Desktop* PP = SUIT_Session::session()->activeApplication()->desktop();
799 x = abs(PP->x() + PP->size().width() - aDlg->size().width() - 10);
800 y = abs(PP->y() + PP->size().height() - aDlg->size().height() - 10);
805 //=======================================================================
806 // function : GetDefaultName()
807 // purpose : Generates default names
808 //=======================================================================
809 QString GEOMBase::GetDefaultName(const QString& theOperation)
813 // collect all object names of GEOM component
814 SalomeApp_Study* appStudy =
815 dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
816 if ( !appStudy ) return aName;
817 _PTR(Study) aStudy = appStudy->studyDS();
819 std::set<std::string> aSet;
820 _PTR(SComponent) aGeomCompo (aStudy->FindComponent("GEOM"));
822 _PTR(ChildIterator) it (aStudy->NewChildIterator(aGeomCompo));
824 for (it->InitEx(true); it->More(); it->Next()) {
826 aSet.insert(obj->GetName());
830 // build a unique name
832 bool isUnique = false;
834 aName = theOperation + "_" + QString::number(++aNumber);
835 isUnique = (aSet.count(aName.toStdString()) == 0);
842 //=======================================================================
843 // function : ShowErrorMessage()
844 // purpose : Shows message box with error code and comment
845 //=======================================================================
846 void GEOMBase::ShowErrorMessage(const char* theErrorCode, const char* theComment)
848 QString anErrorCode(theErrorCode);
849 QString aComment(theComment);
852 if (!anErrorCode.isEmpty())
853 aText.append("\n" + QObject::tr(anErrorCode.toLatin1().constData()));
854 if (!aComment.isEmpty())
855 aText.append("\n" + QString(theComment));
857 SUIT_MessageBox::critical( SUIT_Session::session()->activeApplication()->desktop(), QObject::tr( "GEOM_ERROR" ),
858 QObject::tr("GEOM_PRP_ABORT") + aText, "OK" );
862 //=======================================================================
863 // function : GetObjectFromIOR()
864 // purpose : returns a GEOM_Object by given IOR (string)
865 //=======================================================================
866 GEOM::GEOM_Object_ptr GEOMBase::GetObjectFromIOR( const char* theIOR )
868 GEOM::GEOM_Object_var anObject;
869 if ( theIOR == NULL || strlen( theIOR ) == 0 )
870 return anObject._retn(); // returning nil object
872 anObject = GEOM::GEOM_Object::_narrow( SalomeApp_Application::orb()->string_to_object( theIOR ) );
873 return anObject._retn();
876 //=======================================================================
877 // function : GetIORFromObject()
878 // purpose : returns IOR of a given GEOM_Object
879 //=======================================================================
880 char* GEOMBase::GetIORFromObject( const GEOM::GEOM_Object_ptr& theObject )
882 if ( CORBA::is_nil( theObject ) )
885 return SalomeApp_Application::orb()->object_to_string( theObject );
888 //=======================================================================
889 // function : GetShape()
890 // purpose : returns a TopoDS_Shape stored in GEOM_Object
891 //=======================================================================
892 bool GEOMBase::GetShape( const GEOM::GEOM_Object_ptr& theObject, TopoDS_Shape& theShape, const TopAbs_ShapeEnum theType )
894 if ( !CORBA::is_nil( theObject ) )
896 TopoDS_Shape aTopoDSShape = GEOM_Client().GetShape( GeometryGUI::GetGeomGen(), theObject );
897 if ( !aTopoDSShape.IsNull() && ( theType == TopAbs_SHAPE || theType == aTopoDSShape.ShapeType() ) )
899 theShape = aTopoDSShape;
906 //=======================================================================
907 // function : GetName()
908 // purpose : Get name of object
909 //=======================================================================
910 QString GEOMBase::GetName( GEOM::GEOM_Object_ptr theObj )
912 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
916 CORBA::String_var anIOR = SalomeApp_Application::orb()->object_to_string( theObj );
917 if ( strcmp(anIOR.in(), "") != 0 )
919 _PTR(SObject) aSObj ( appStudy->studyDS()->FindObjectIOR( std::string( anIOR ) ) );
921 _PTR(GenericAttribute) anAttr;
923 if ( aSObj && aSObj->FindAttribute( anAttr, "AttributeName") )
925 _PTR(AttributeName) aNameAttr ( anAttr );
926 return QString( aNameAttr->Value().c_str() );
934 bool GEOMBase::IsShape( GEOM::GEOM_Object_ptr theObj )
936 return !theObj->_is_nil() && theObj->IsShape();