1 // GEOM GEOMGUI : GUI for Geometry component
3 // Copyright (C) 2003 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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : GeometryGUI_Swig.cxx
25 // Author : Nicolas REJNERI, Paul RASCLE
29 #include "GeometryGUI_Swig.hxx"
30 #include "SALOMEGUI_Swig.hxx"
32 #include "QAD_Application.h"
33 #include "QAD_Desktop.h"
34 #include "QAD_Study.h"
35 #include "QAD_StudyFrame.h"
36 #include "QAD_RightFrame.h"
37 #include "SALOMEGUI_ImportOperation.h"
39 #include "OCCViewer_Viewer3d.h"
40 #include "OCCViewer_ViewFrame.h"
41 #include <TopExp_Explorer.hxx>
42 #include <TopTools_MapOfShape.hxx>
43 #include <TopTools_ListOfShape.hxx>
44 #include <TopTools_ListIteratorOfListOfShape.hxx>
45 #include <BRepAdaptor_Surface.hxx>
46 #include <BRepAdaptor_Curve.hxx>
47 #include <GeomAbs_CurveType.hxx>
48 #include <GeomAbs_SurfaceType.hxx>
50 #include <TopoDS_Edge.hxx>
51 #include <TopoDS_Face.hxx>
52 #include <TopoDS_Iterator.hxx>
53 #include <AIS_ListOfInteractive.hxx>
54 #include <AIS_ListIteratorOfListOfInteractive.hxx>
55 #include <V3d_Viewer.hxx>
57 #include "SALOME_Event.hxx"
59 #include "VTKViewer_RenderWindowInteractor.h"
60 #include "VTKViewer_ViewFrame.h"
62 #include "GEOM_Actor.h"
63 #include "GEOM_Client.hxx"
64 #include "GEOM_AISShape.hxx"
65 #include "GEOM_AssemblyBuilder.h"
66 #include "GEOM_InteractiveObject.hxx"
68 #include "utilities.h"
72 static GEOM_Client ShapeReader;
75 template<class TViewFrame> inline TViewFrame* GetFrame(QAD_Study* theStudy){
76 return dynamic_cast<TViewFrame*>(SALOME::GetViewFrame(theStudy));
80 GEOM_Swig::GEOM_Swig()
82 // MESSAGE("Constructeur");
85 GEOM_Swig::~GEOM_Swig()
87 // MESSAGE("Destructeur");
90 void GEOM_Swig::createAndDisplayGO(const char* Entry)
92 // MESSAGE("createAndDisplayGO");
93 QAD_Study* ActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
94 SALOMEDS::Study_var aStudy = ActiveStudy->getStudyDocument();
95 SALOMEDS::StudyBuilder_var aStudyBuilder = aStudy->NewBuilder();
97 Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
98 GEOM::GEOM_Gen_var Geom = GEOM::GEOM_Gen::_narrow(comp);
100 CORBA::String_var aFatherIOR;
101 SALOMEDS::SComponent_var father = aStudy->FindComponent("GEOM");
102 aStudyBuilder->DefineComponentInstance( father, Geom );
103 father->ComponentIOR(aFatherIOR);
105 SALOMEDS::SObject_var fatherSF = aStudy->FindObjectID(ActiveStudy->getActiveStudyFrame()->entry());
107 SALOMEDS::SObject_var obj = aStudy->FindObjectID(Entry);
108 SALOMEDS::GenericAttribute_var anAttr;
109 SALOMEDS::AttributeIOR_var anIOR;
111 if ( !obj->FindAttribute(anAttr, "AttributeIOR"))
113 anIOR = SALOMEDS::AttributeIOR::_narrow(anAttr);
114 CORBA::String_var anIORValue = anIOR->Value();
116 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(anIORValue);
117 TopoDS_Shape Shape = ShapeReader.GetShape(Geom,aShape);
119 if ( !obj->_is_nil() ) {
120 if (obj->FindAttribute(anAttr, "AttributeName")) {
121 SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
122 CORBA::String_var aNameValue = aName->Value();
124 QAD_Operation* op = new SALOMEGUI_ImportOperation( ActiveStudy );
127 SALOMEDS::SObject_var newObj1 = aStudyBuilder->NewObject(fatherSF);
128 aStudyBuilder->Addreference(newObj1, obj);
129 // commit transaction
132 Handle(GEOM_InteractiveObject) anIO = new GEOM_InteractiveObject(const_cast<char*>(anIORValue.in()),
133 const_cast<char*>(aFatherIOR.in()),
135 const_cast<char*>(obj->GetID()));
137 class TEvent: public SALOME_Event{
139 TopoDS_Shape myShape;
140 Handle(SALOME_InteractiveObject) myIO;
143 TEvent(QAD_Study* theStudy, TopoDS_Shape theShape,
144 const Handle(SALOME_InteractiveObject)& theIO,
145 const char* theName):
146 myStudy(theStudy), myShape(theShape),
147 myIO(theIO), myName(theName)
149 virtual void Execute(){
150 if(VTKViewer_ViewFrame* aViewFrame = GetFrame<VTKViewer_ViewFrame>(myStudy)){
151 VTKViewer_RenderWindowInteractor* myRenderInter= aViewFrame->getRWInteractor();
152 int aMode = myRenderInter->GetDisplayMode();
154 vtkActorCollection* theActors = GEOM_AssemblyBuilder::BuildActors(myShape,0,aMode,true);
155 theActors->InitTraversal();
156 while(vtkActor* anActor = theActors->GetNextActor()){
157 GEOM_Actor* GActor = GEOM_Actor::SafeDownCast(anActor);
158 GActor->setName(const_cast<char*>(myName));
160 myRenderInter->Display(GActor);
162 myRenderInter->Update();
163 }else if(OCCViewer_ViewFrame* aViewFrame = GetFrame<OCCViewer_ViewFrame>(myStudy)){
164 Handle(AIS_InteractiveContext) ic = aViewFrame->getViewer()->getAISContext();
165 Handle(GEOM_AISShape) aSh = new GEOM_AISShape(myShape,const_cast<char*>(myName));
166 aSh->setName(const_cast<char*>(myName));
169 ic->AddOrRemoveCurrentObject(aSh,true);
171 myStudy->updateObjBrowser(true);
174 ProcessVoidEvent(new TEvent(ActiveStudy,Shape,anIO,aNameValue.in()));
180 int GEOM_Swig::getIndexTopology(const char* SubIOR, const char* IOR)
182 Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
183 GEOM::GEOM_Gen_var Geom = GEOM::GEOM_Gen::_narrow(comp);
185 GEOM::GEOM_Object_var aMainShape = Geom->GetIORFromString(IOR);
186 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aMainShape);
188 GEOM::GEOM_Object_var aSubShape = Geom->GetIORFromString(SubIOR);
189 TopoDS_Shape subshape = ShapeReader.GetShape(Geom, aSubShape);
192 if(subshape.ShapeType() == TopAbs_COMPOUND) {
194 TopTools_ListOfShape CL;
196 TopTools_ListIteratorOfListOfShape itC;
197 for(itC.Initialize(CL); itC.More(); itC.Next()) {
198 for(it.Initialize(itC.Value()); it.More(); it.Next()) {
199 if (it.Value().ShapeType() == TopAbs_COMPOUND) {
200 if (it.Value().IsSame(subshape))
204 CL.Append(it.Value());
210 TopExp_Explorer Exp(shape, subshape.ShapeType());
211 TopTools_MapOfShape M;
213 if(M.Add(Exp.Current())) {
214 if(Exp.Current().IsSame(subshape))
224 const char* GEOM_Swig::getShapeTypeString(const char* IOR)
226 Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
227 GEOM::GEOM_Gen_var Geom = GEOM::GEOM_Gen::_narrow(comp);
229 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
230 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
232 if( shape.IsNull() ) {
233 return "Null Shape" ;
236 switch (shape.ShapeType() )
238 case TopAbs_COMPOUND:
239 { return "Compound" ;}
240 case TopAbs_COMPSOLID:
241 { return "Compound Solid" ;}
248 BRepAdaptor_Surface surf(TopoDS::Face(shape));
249 if ( surf.GetType() == GeomAbs_Plane ) {
251 } else if ( surf.GetType() == GeomAbs_Cylinder ) {
252 return "Cylindrical Face" ;
253 } else if ( surf.GetType() == GeomAbs_Sphere ) {
254 return "Spherical Face" ;
255 } else if ( surf.GetType() == GeomAbs_Torus ) {
256 return "Toroidal Face" ;
257 } else if ( surf.GetType() == GeomAbs_Cone ) {
258 return "Conical Face" ;
260 return "GEOM::FACE" ;
267 BRepAdaptor_Curve curv(TopoDS::Edge(shape));
268 if ( curv.GetType() == GeomAbs_Line ) {
269 if ( (Abs(curv.FirstParameter()) >= 1E6 ) ||
270 (Abs(curv.LastParameter()) >= 1E6 )) {
274 } else if ( curv.GetType() == GeomAbs_Circle ) {
275 if ( curv.IsClosed() )
292 const char* GEOM_Swig::getShapeTypeIcon(const char* IOR)
294 Engines::Component_var comp = QAD_Application::getDesktop()->getEngine("FactoryServer", "GEOM");
295 GEOM::GEOM_Gen_var Geom = GEOM::GEOM_Gen::_narrow(comp);
297 GEOM::GEOM_Object_var aShape = Geom->GetIORFromString(IOR);
298 TopoDS_Shape shape = ShapeReader.GetShape(Geom, aShape);
300 if( shape.IsNull() ) {
304 switch (shape.ShapeType() )
306 case TopAbs_COMPOUND:
307 { return "ICON_OBJBROWSER_COMPOUND" ;}
308 case TopAbs_COMPSOLID:
309 { return "ICON_OBJBROWSER_COMPSOLID" ;}
311 { return "ICON_OBJBROWSER_SOLID" ;}
313 { return "ICON_OBJBROWSER_SHELL" ;}
315 { return "ICON_OBJBROWSER_FACE" ;}
317 { return "ICON_OBJBROWSER_WIRE" ;}
319 { return "ICON_OBJBROWSER_EDGE" ;}
321 { return "ICON_OBJBROWSER_VERTEX" ;}
326 void GEOM_Swig::setDisplayMode(const char* theEntry, int theMode)
328 QAD_Study* aStudy = QAD_Application::getDesktop()->getActiveStudy();
329 Handle(SALOME_InteractiveObject) anIO = SALOME::FindIObject(aStudy,theEntry);
331 class TEvent: public SALOME_Event{
333 Handle(SALOME_InteractiveObject) myIO;
336 TEvent(QAD_Study* theStudy, const Handle(SALOME_InteractiveObject)& theIO, int theParam):
337 myStudy(theStudy), myIO(theIO), myParam(theParam)
339 virtual void Execute(){
340 if(VTKViewer_ViewFrame* aViewFrame = GetFrame<VTKViewer_ViewFrame>(myStudy)){
341 VTKViewer_RenderWindowInteractor* myRenderInter= aViewFrame->getRWInteractor();
342 myRenderInter->SetDisplayMode(myIO,myParam);
343 myRenderInter->Update();
344 }else if(OCCViewer_ViewFrame* aViewFrame = GetFrame<OCCViewer_ViewFrame>(myStudy)){
345 OCCViewer_Viewer3d* v3d = aViewFrame->getViewer();
346 v3d->SwitchRepresentation(myIO,myParam);
351 ProcessVoidEvent(new TEvent(aStudy,anIO,theMode));
354 void GEOM_Swig::setColor(const char* theEntry, int red, int green, int blue)
356 QAD_Study* aStudy = QAD_Application::getDesktop()->getActiveStudy();
357 Handle(SALOME_InteractiveObject) anIO = SALOME::FindIObject(aStudy,theEntry);
358 QColor aColor(red,green,blue);
360 class TEvent: public SALOME_Event{
362 Handle(SALOME_InteractiveObject) myIO;
365 TEvent(QAD_Study* theStudy, const Handle(SALOME_InteractiveObject)& theIO, const QColor& theParam):
366 myStudy(theStudy), myIO(theIO), myParam(theParam)
368 virtual void Execute(){
369 if(VTKViewer_ViewFrame* aViewFrame = GetFrame<VTKViewer_ViewFrame>(myStudy)){
370 VTKViewer_RenderWindowInteractor* myRenderInter= aViewFrame->getRWInteractor();
371 myRenderInter->SetColor(myIO,myParam);
372 myRenderInter->Update();
373 }else if(OCCViewer_ViewFrame* aViewFrame = GetFrame<OCCViewer_ViewFrame>(myStudy)){
374 OCCViewer_Viewer3d* v3d = aViewFrame->getViewer();
375 Handle(AIS_InteractiveContext) ic = aViewFrame->getViewer()->getAISContext();
376 AIS_ListOfInteractive List;
377 ic->DisplayedObjects(List);
378 AIS_ListIteratorOfListOfInteractive ite(List);
379 for ( ; ite.More(); ite.Next() ) {
380 Handle(SALOME_InteractiveObject) anObj =
381 Handle(SALOME_InteractiveObject)::DownCast( ite.Value()->GetOwner() );
382 if ( !anObj.IsNull() && anObj->hasEntry() && anObj->isSame( myIO ) ) {
383 Quantity_Color CSFColor = Quantity_Color ( myParam.red() / 255.,
384 myParam.green() / 255.,
385 myParam.blue() / 255.,
387 ite.Value()->SetColor( CSFColor );
388 if ( ite.Value()->IsKind( STANDARD_TYPE(GEOM_AISShape) ) )
389 Handle(GEOM_AISShape)::DownCast( ite.Value() )->SetShadingColor( CSFColor );
390 ite.Value()->Redisplay( Standard_True );
391 v3d->getViewer3d()->Update();
399 ProcessVoidEvent(new TEvent(aStudy,anIO,aColor));
402 void GEOM_Swig::setTransparency(const char* theEntry, float transp)
404 QAD_Study* aStudy = QAD_Application::getDesktop()->getActiveStudy();
405 Handle(SALOME_InteractiveObject) anIO = SALOME::FindIObject(aStudy,theEntry);
407 class TEvent: public SALOME_Event{
409 Handle(SALOME_InteractiveObject) myIO;
412 TEvent(QAD_Study* theStudy, const Handle(SALOME_InteractiveObject)& theIO, float theParam):
413 myStudy(theStudy), myIO(theIO), myParam(theParam)
415 virtual void Execute(){
416 if(VTKViewer_ViewFrame* aViewFrame = GetFrame<VTKViewer_ViewFrame>(myStudy)){
417 VTKViewer_RenderWindowInteractor* myRenderInter= aViewFrame->getRWInteractor();
418 myRenderInter->SetTransparency(myIO,myParam);
419 myRenderInter->Update();
420 }else if(OCCViewer_ViewFrame* aViewFrame = GetFrame<OCCViewer_ViewFrame>(myStudy)){
421 OCCViewer_Viewer3d* v3d = aViewFrame->getViewer();
422 v3d->SetTransparency(myIO,myParam);
427 ProcessVoidEvent(new TEvent(aStudy,anIO,transp));