]> SALOME platform Git repositories - modules/visu.git/blob - src/VISU_I/VISU_ClippingPlaneMgr.cxx
Salome HOME
d1346758877fa226c2873c76e2196e09b826ee92
[modules/visu.git] / src / VISU_I / VISU_ClippingPlaneMgr.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "VISU_ClippingPlaneMgr.hxx"
23 #include "VISU_ColoredPrs3dHolder_i.hh"
24
25 //#include CORBA_SERVER_HEADER(SALOMEDS)
26 //#include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
27
28 #include "SALOMEDSClient_GenericAttribute.hxx"
29 #include "SALOMEDSClient_AttributeName.hxx"
30 #include "SALOMEDSClient_AttributeSequenceOfReal.hxx"
31 #include "SALOMEDSClient_AttributeInteger.hxx"
32
33
34 #include <vtkImplicitFunctionCollection.h>
35
36
37 #define CLIP_PLANES_FOLDER "Clipping Planes"
38
39 using namespace std;
40
41 //*************************************************************
42 VISU_ClippingPlaneMgr::VISU_ClippingPlaneMgr()
43 {
44   myPlanes = vtkImplicitFunctionCollection::New();
45 }
46
47 //*************************************************************
48 VISU_ClippingPlaneMgr::~VISU_ClippingPlaneMgr()
49 {
50   myPlanes->Delete();
51 }
52
53 //*************************************************************
54 void VISU_ClippingPlaneMgr::SetStudy(_PTR(Study) theStudy)
55 {
56   if (myStudy == theStudy) return;
57   myStudy = theStudy;
58   myPlanes->RemoveAllItems();
59   if (!myStudy) return;
60
61   _PTR(SObject) aFolder = GetClippingPlanesFolder(false);
62   if (aFolder) {
63     _PTR(ChildIterator) aIter = myStudy->NewChildIterator(aFolder);
64     int i;
65     for (i = 0; aIter->More(); aIter->Next(), i++) { // For each plane
66       _PTR(SObject) aSObject = aIter->Value();
67       VISU_CutPlaneFunction* aPlane = VISU_CutPlaneFunction::New();
68       aPlane->setPlaneObject(aSObject);
69       aPlane->setName(aSObject->GetName());
70       
71       _PTR(GenericAttribute) anAttr;
72       if (aSObject->FindAttribute(anAttr, "AttributeSequenceOfReal")) {
73         _PTR(AttributeSequenceOfReal) aArray(anAttr);
74         aPlane->SetOrigin(aArray->Value(1), aArray->Value(2), aArray->Value(3));
75         aPlane->SetNormal(aArray->Value(4), aArray->Value(5), aArray->Value(6));
76       }
77       if (aSObject->FindAttribute(anAttr, "AttributeInteger")) {
78         _PTR(AttributeInteger) aFlag(anAttr);
79         aPlane->setAuto(aFlag->Value() == 1);
80       } else
81         aPlane->setAuto(false);
82
83       applyPlaneToAll(aPlane);
84       myPlanes->AddItem(aPlane);
85     }
86   }
87 }
88
89
90 void VISU_ClippingPlaneMgr::applyPlaneToAll(VISU_CutPlaneFunction* thePlane)
91 {
92   _PTR(SComponent) aVisuSO = myStudy->FindComponent("VISU");
93   _PTR(ChildIterator) aChildIter = myStudy->NewChildIterator(aVisuSO);
94   for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
95     _PTR(SObject) aSObject = aChildIter->Value();
96     CORBA::Object_var anObject = VISU::ClientSObjectToObject(aSObject);
97     if(VISU::Base_i* aBase = dynamic_cast<VISU::Base_i*>(VISU::GetServant(anObject).in())) {
98       VISU::Prs3d_i* aPrs;
99       if(aBase->GetType() == VISU::TCOLOREDPRS3DHOLDER){
100         CORBA::Object_var anObject = aBase->_this();
101         VISU::ColoredPrs3dHolder_var aHolder = VISU::ColoredPrs3dHolder::_narrow(anObject);
102         VISU::Prs3d_var aPrs3d = aHolder->GetDevice();
103         aPrs = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(aPrs3d).in());
104       } else {
105         aPrs = dynamic_cast<VISU::Prs3d_i*>(aBase);
106       }
107       if (aPrs) {
108         if (!ContainsPlane(aPrs, thePlane)) {
109           if (thePlane->isAuto())
110             aPrs->AddClippingPlane(thePlane);
111           else {
112             string aPrsEntry = aPrs->GetEntry();
113             if (aPrsEntry.length() == 0) {
114               VISU::ColoredPrs3d_i* aColPrs = dynamic_cast<VISU::ColoredPrs3d_i*>(aPrs);
115               if (aColPrs)
116                 aPrsEntry = aColPrs->GetHolderEntry();
117             }
118             
119             _PTR(SObject) aSObject = thePlane->getPlaneObject();
120             _PTR(ChildIterator) aRefIter = myStudy->NewChildIterator(aSObject);   
121             for (; aRefIter->More(); aRefIter->Next()) {
122               _PTR(SObject) aObj = aRefIter->Value();
123               _PTR(SObject) aRefPrsObject;
124               if (aObj->ReferencedObject(aRefPrsObject)) { // If it is referenced on current plane
125                 if (aRefPrsObject->GetID() == aPrsEntry) {
126                   aPrs->AddClippingPlane(thePlane);
127                 }
128               }
129             }
130           }
131         }
132       }
133     }
134   } 
135 }
136
137 //*************************************************************
138 long VISU_ClippingPlaneMgr::CreateClippingPlane(double X,double  Y, double Z, 
139                                                 double dX, double dY, double dZ, 
140                                                 bool isAuto, const char* name)
141 {
142   _PTR(SObject) aObjPtr = CreateClippingPlaneObject(X, Y, Z, dX, dY, dZ, isAuto, name);
143   return myPlanes->GetNumberOfItems() - 1;
144 }
145
146
147 //*************************************************************
148 _PTR(SObject) VISU_ClippingPlaneMgr::CreateClippingPlaneObject(double X,double  Y, double Z, 
149                                                                double dX, double dY, double dZ, 
150                                                                bool isAuto, const char* name)
151 {
152   _PTR(SObject) aPlaneObj;
153   if(!myStudy->GetProperties()->IsLocked()) {
154     _PTR(SObject) aFolder = GetClippingPlanesFolder(true);
155     if (aFolder) {
156       _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
157       aPlaneObj = aBuilder->NewObject(aFolder);
158
159       // Save Name
160       _PTR(GenericAttribute) anAttr;
161       anAttr = aBuilder->FindOrCreateAttribute(aPlaneObj,"AttributeName");
162       _PTR(AttributeName) aName(anAttr);
163       aName->SetValue(name);
164
165       //Save Parameters
166       double aParams[6];
167       aParams[0] = X;
168       aParams[1] = Y;
169       aParams[2] = Z;
170       aParams[3] = dX;
171       aParams[4] = dY;
172       aParams[5] = dZ;
173
174       anAttr = aBuilder->FindOrCreateAttribute(aPlaneObj,"AttributeSequenceOfReal");
175       _PTR(AttributeSequenceOfReal) aArray(anAttr);
176       if (aArray->Length() == 6) {
177         for (int i = 0; i < 6; i++)
178           aArray->ChangeValue(i+1, aParams[i]);
179       } else {
180         for (int i = 0; i < 6; i++)
181           aArray->Add(aParams[i]);
182       }
183       // Save Bool Flag
184       anAttr = aBuilder->FindOrCreateAttribute(aPlaneObj,"AttributeInteger");
185       _PTR(AttributeInteger) aFlag(anAttr);
186       aFlag->SetValue(isAuto? 1 : 0);
187
188       vtkSmartPointer<VISU_CutPlaneFunction> aPlane = VISU_CutPlaneFunction::New();
189       aPlane->Delete(); //vtkSmartPointer specific
190       aPlane->setPlaneObject(aPlaneObj);
191       aPlane->SetOrigin(X, Y, Z);
192       aPlane->SetNormal(dX, dY, dZ);
193       aPlane->setName(name);
194       aPlane->setAuto(isAuto);
195       applyPlaneToAll(aPlane);
196       myPlanes->AddItem(aPlane.GetPointer());
197     }
198   }
199   return aPlaneObj;
200 }
201   
202
203 //*************************************************************
204 void VISU_ClippingPlaneMgr::EditClippingPlane(long id, double X,double  Y, double Z, 
205                                               double dX, double dY, double dZ, 
206                                               bool isAuto, const char* name)
207 {
208   VISU_CutPlaneFunction* aPlane = GetClippingPlane(id);
209   if (aPlane != NULL) {
210     _PTR(SObject) aSObj = aPlane->getPlaneObject();
211     aPlane->SetOrigin(X, Y, Z);
212     aPlane->SetNormal(dX, dY, dZ);
213     aPlane->setName(name);
214     aPlane->setAuto(isAuto);
215
216     if(!myStudy->GetProperties()->IsLocked()) {
217       _PTR(GenericAttribute) anAttr;
218       if (aSObj->FindAttribute(anAttr, "AttributeSequenceOfReal")) {
219         _PTR(AttributeSequenceOfReal) aArray(anAttr);
220         aArray->ChangeValue(1, X);
221         aArray->ChangeValue(2, Y);
222         aArray->ChangeValue(3, Z);
223         aArray->ChangeValue(4, dX);
224         aArray->ChangeValue(5, dY);
225         aArray->ChangeValue(6, dZ);
226       }
227       if (aSObj->FindAttribute(anAttr, "AttributeInteger")) {
228         _PTR(AttributeInteger) aFlag(anAttr);
229         aFlag->SetValue(isAuto? 1 : 0);
230       }
231       if (aSObj->FindAttribute(anAttr, "AttributeName")) {
232         _PTR(AttributeName) aName(anAttr);
233         aName->SetValue(name);
234       }
235       // Remove references on presentations if it becomes Auto plane
236       _PTR(SObject) aPlaneSObj = aPlane->getPlaneObject();
237       if (aPlane->isAuto()) {
238         _PTR(ChildIterator) aIter = myStudy->NewChildIterator(aPlaneSObj);
239         _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
240         for (; aIter->More(); aIter->Next()) {
241           _PTR(SObject) aObj = aIter->Value();
242           aBuilder->RemoveObject(aObj);
243         }
244       } 
245     }
246   }
247 }
248
249
250
251 //*************************************************************
252   /* Returns clipping plane by its Id */
253 VISU_CutPlaneFunction* VISU_ClippingPlaneMgr::GetClippingPlane(long id)
254 {
255   if ((id < 0) || (id >= GetClippingPlanesNb()))
256     return NULL;
257   return (VISU_CutPlaneFunction*) myPlanes->GetItemAsObject(id);
258 }
259
260 //*************************************************************
261   /* Returns -1 if Plane is not exists */
262 int VISU_ClippingPlaneMgr::GetPlaneId(VISU_CutPlaneFunction* thePlane)
263 {
264   int aTag = thePlane->getPlaneObject()->Tag();
265   int aRes = -1;
266   VISU_CutPlaneFunction* aPlane;
267   for (int i = 0; i < GetClippingPlanesNb(); i++) {
268     aPlane = GetClippingPlane(i);
269     if (aPlane->getPlaneObject()->Tag() == aTag) {
270       aRes = i;
271       break;
272     }
273   }
274   return aRes;
275 }
276
277   
278 //*************************************************************
279   /* Deletes clipping plane by its Id */
280 bool VISU_ClippingPlaneMgr::DeleteClippingPlane(long id)
281 {
282   _PTR(SObject) aFolder = GetClippingPlanesFolder(false);
283   if (aFolder) {
284     VISU_CutPlaneFunction* aPlane = GetClippingPlane(id);
285     if (aPlane != NULL) {
286       _PTR(SComponent) aVisuSO = myStudy->FindComponent("VISU");
287       _PTR(ChildIterator) aChildIter = myStudy->NewChildIterator(aVisuSO);
288       for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
289         _PTR(SObject) aSObject = aChildIter->Value();
290         CORBA::Object_var anObject = VISU::ClientSObjectToObject(aSObject);
291         if(VISU::Base_i* aBase = dynamic_cast<VISU::Base_i*>(VISU::GetServant(anObject).in())) {
292           VISU::Prs3d_i* aPrs;
293           if(aBase->GetType() == VISU::TCOLOREDPRS3DHOLDER){
294             CORBA::Object_var anObject = aBase->_this();
295             VISU::ColoredPrs3dHolder_var aHolder = VISU::ColoredPrs3dHolder::_narrow(anObject);
296             VISU::Prs3d_var aPrs3d = aHolder->GetDevice();
297             aPrs = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(aPrs3d).in());
298           } else
299             aPrs = dynamic_cast<VISU::Prs3d_i*>(aBase);
300
301           if (aPrs) {
302             if (ContainsPlane(aPrs, aPlane)) {
303               short aTag1 = aPlane->getPlaneObject()->Tag();
304               for (int j = aPrs->GetNumberOfClippingPlanes()-1; j > -1; j--) {
305                 VISU_CutPlaneFunction* aPln = dynamic_cast<VISU_CutPlaneFunction*>
306                   (aPrs->GetClippingPlane(j));
307                 if (aPln) {
308                   short aTag2 = aPln->getPlaneObject()->Tag();
309                   if (aTag1 == aTag2) {
310                     aPrs->RemoveClippingPlane(j);
311                     break;
312                   }
313                 }
314               }
315             }
316           }
317         }
318       }   
319       _PTR(SObject) aSObj = aPlane->getPlaneObject();
320       if (aSObj) {
321         _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
322         aBuilder->RemoveObject(aSObj);
323       }
324       myPlanes->RemoveItem(id);
325       return true;
326     }
327   }
328   return false;
329 }
330
331 //*************************************************************
332 bool VISU_ClippingPlaneMgr::ContainsPlane(VISU::Prs3d_ptr thePrs, VISU_CutPlaneFunction* thePlane)
333 {
334   VISU::Prs3d_i* aPrs = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(thePrs).in());
335   return ContainsPlane(aPrs, thePlane);
336 }
337
338 //*************************************************************
339 bool VISU_ClippingPlaneMgr::ContainsPlane(VISU::Prs3d_i* thePrs, VISU_CutPlaneFunction* thePlane)
340 {
341   VISU::Prs3d_i* aPrs = thePrs;
342   if (thePrs->GetType() == VISU::TCOLOREDPRS3DHOLDER) {
343     VISU::ColoredPrs3dHolder_i* aHolder = dynamic_cast<VISU::ColoredPrs3dHolder_i*>(thePrs);
344     if (!aHolder) return false;
345     aPrs = aHolder->GetPrs3dDevice();
346   }
347   string aEntry = thePlane->getPlaneObject()->GetID();
348   for (int i = 0; i < thePrs->GetNumberOfClippingPlanes(); i++) {
349     VISU_CutPlaneFunction* aPlane = dynamic_cast<VISU_CutPlaneFunction*>(thePrs->GetClippingPlane(i));
350     if (aPlane) {
351       if (aPlane->getPlaneObject()->GetID() == aEntry) {
352         return true;
353       }
354     }
355   }
356   return false;
357 }
358
359   
360 //*************************************************************
361   /* Applyes a clipping plane with Id to presentation thePrs */
362 bool VISU_ClippingPlaneMgr::ApplyClippingPlane(VISU::Prs3d_i* thePrs, long id) 
363 {
364   //VISU::Prs3d_i* aPrs = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(thePrs).in());
365   if (!thePrs) return false;
366
367   VISU_CutPlaneFunction* aPlane = GetClippingPlane(id);
368   if (!aPlane) return false;
369   if (!ContainsPlane(thePrs, aPlane)) {
370     thePrs->AddClippingPlane(aPlane);
371     if (!aPlane->isAuto()) {
372       string aEntry = thePrs->GetEntry();
373       if (aEntry.length() == 0) {
374         VISU::ColoredPrs3d_i* aColPrs = dynamic_cast<VISU::ColoredPrs3d_i*>(thePrs);
375         if (aColPrs)
376           aEntry = aColPrs->GetHolderEntry();
377       }
378       if(!myStudy->GetProperties()->IsLocked()) {
379         _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
380         _PTR(SObject) aPrsSObj = myStudy->FindObjectID(aEntry);
381         _PTR(SObject) aSObject = aPlane->getPlaneObject();
382         _PTR(SObject) aNewObj = aBuilder->NewObject(aSObject);
383         aBuilder->Addreference(aNewObj, aPrsSObj);
384       }
385     }
386     return true;
387   }
388   return false;
389 }
390
391 //*************************************************************
392 bool VISU_ClippingPlaneMgr::DetachClippingPlane(VISU::Prs3d_i* thePrs, long id) 
393 {
394   VISU_CutPlaneFunction* aPlane = GetClippingPlane(id);
395   //VISU::Prs3d_i* aPrs = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(thePrs).in());
396   if (aPlane && thePrs) {
397     if (ContainsPlane(thePrs, aPlane)) {
398       bool isRemoved = false;
399       short aTag1 = aPlane->getPlaneObject()->Tag();
400       for (int j = thePrs->GetNumberOfClippingPlanes()-1; j > -1; j--) {
401         VISU_CutPlaneFunction* aPln = dynamic_cast<VISU_CutPlaneFunction*>
402           (thePrs->GetClippingPlane(j));
403         if (aPln) {
404           short aTag2 = aPln->getPlaneObject()->Tag();
405           if (aTag1 == aTag2) {
406             thePrs->RemoveClippingPlane(j);
407             isRemoved = true;
408             break;
409           }
410         }
411       }
412       if(!myStudy->GetProperties()->IsLocked()) {
413         _PTR(SObject) aSObject = aPlane->getPlaneObject();
414         _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
415
416         string aEntry = thePrs->GetEntry();
417         if (aEntry.length() == 0) {
418           VISU::ColoredPrs3d_i* aColPrs = dynamic_cast<VISU::ColoredPrs3d_i*>(thePrs);
419           if (aColPrs)
420             aEntry = aColPrs->GetHolderEntry();
421         }
422         _PTR(ChildIterator) aIter = myStudy->NewChildIterator(aSObject);
423         for (; aIter->More(); aIter->Next()) {
424           _PTR(SObject) aRefObj = aIter->Value();
425           if(aRefObj) {
426             _PTR(SObject) aRefPrsObject;
427             if (aRefObj->ReferencedObject(aRefPrsObject)) {
428               if (aRefPrsObject->GetID() == aEntry) {
429                 aBuilder->RemoveObject(aRefObj);
430                 break;
431               }
432             }
433           }
434         }
435       }
436       return isRemoved;
437     }
438   }
439   return false;
440 }
441
442   
443 //*************************************************************
444   /* Get number of clipping planes */
445 long VISU_ClippingPlaneMgr::GetClippingPlanesNb() 
446 {
447   return myPlanes->GetNumberOfItems();
448 }
449
450
451 //*************************************************************
452 _PTR(SObject) VISU_ClippingPlaneMgr::GetClippingPlanesFolder(bool toCreate)
453 {
454   _PTR(SObject) aFolder;
455   _PTR(SComponent) aVisuSO = myStudy->FindComponent("VISU");
456   if (!aVisuSO) return aFolder;
457
458   aFolder = myStudy->FindObject(CLIP_PLANES_FOLDER);
459   if (!aFolder && toCreate) {
460     _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
461     aFolder = aBuilder->NewObject(aVisuSO);
462     
463     _PTR(GenericAttribute) anAttr;
464     anAttr = aBuilder->FindOrCreateAttribute(aFolder,"AttributeName");
465     _PTR(AttributeName) aName(anAttr);
466     aName->SetValue(CLIP_PLANES_FOLDER);
467   }
468   return aFolder;
469 }
470
471
472
473
474 //****************************************************************
475 //****************************************************************
476 //****************************************************************
477 VISU_CutPlaneFunction* VISU_CutPlaneFunction::New()
478 {
479   return new VISU_CutPlaneFunction();
480 }
481
482 void VISU_CutPlaneFunction::setActive(bool theActive) 
483
484   myIsActive = theActive; 
485   Modified();
486 }
487
488 double VISU_CutPlaneFunction::EvaluateFunction(double x[3])
489 {
490   if (myIsActive)
491     return vtkPlane::EvaluateFunction(x);
492   else 
493     return -1;
494 }
495
496 double VISU_CutPlaneFunction::EvaluateFunction(double x, double y, double z)
497 {
498   if (myIsActive)
499     return vtkPlane::EvaluateFunction(x,y,z);
500   else 
501     return -1;
502 }
503   
504 VISU_CutPlaneFunction::VISU_CutPlaneFunction():
505   myIsActive(true)
506 {
507 }
508
509 VISU_CutPlaneFunction::~VISU_CutPlaneFunction()
510 {
511 }
512