1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "VISU_ClippingPlaneMgr.hxx"
21 #include "VISU_ColoredPrs3dHolder_i.hh"
23 //#include CORBA_SERVER_HEADER(SALOMEDS)
24 //#include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
26 #include "SALOMEDSClient_GenericAttribute.hxx"
27 #include "SALOMEDSClient_AttributeName.hxx"
28 #include "SALOMEDSClient_AttributeSequenceOfReal.hxx"
29 #include "SALOMEDSClient_AttributeInteger.hxx"
32 #include <vtkImplicitFunctionCollection.h>
35 #define CLIP_PLANES_FOLDER "Clipping Planes"
39 //*************************************************************
40 VISU_ClippingPlaneMgr::VISU_ClippingPlaneMgr()
42 myPlanes = vtkImplicitFunctionCollection::New();
45 //*************************************************************
46 VISU_ClippingPlaneMgr::~VISU_ClippingPlaneMgr()
51 //*************************************************************
52 void VISU_ClippingPlaneMgr::SetStudy(_PTR(Study) theStudy, bool reinitStudy)
54 if (myStudy == theStudy && !reinitStudy) return;
56 myPlanes->RemoveAllItems();
59 _PTR(SObject) aFolder = GetClippingPlanesFolder(false);
61 _PTR(ChildIterator) aIter = myStudy->NewChildIterator(aFolder);
63 for (i = 0; aIter->More(); aIter->Next(), i++) { // For each plane
64 _PTR(SObject) aSObject = aIter->Value();
65 VISU_CutPlaneFunction* aPlane = VISU_CutPlaneFunction::New();
66 aPlane->setPlaneObject(aSObject);
67 aPlane->setName(aSObject->GetName());
69 _PTR(GenericAttribute) anAttr;
70 if (aSObject->FindAttribute(anAttr, "AttributeSequenceOfReal")) {
71 _PTR(AttributeSequenceOfReal) aArray(anAttr);
72 aPlane->SetOrigin(aArray->Value(1), aArray->Value(2), aArray->Value(3));
73 aPlane->SetNormal(aArray->Value(4), aArray->Value(5), aArray->Value(6));
75 if (aSObject->FindAttribute(anAttr, "AttributeInteger")) {
76 _PTR(AttributeInteger) aFlag(anAttr);
77 aPlane->setAuto(aFlag->Value() == 1);
79 aPlane->setAuto(false);
81 applyPlaneToAll(aPlane);
83 myPlanes->AddItem(aPlane);
89 void VISU_ClippingPlaneMgr::applyPlaneToAll(VISU_CutPlaneFunction* thePlane)
91 _PTR(SComponent) aVisuSO = myStudy->FindComponent("VISU");
92 _PTR(ChildIterator) aChildIter = myStudy->NewChildIterator(aVisuSO);
93 for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
94 _PTR(SObject) aSObject = aChildIter->Value();
95 CORBA::Object_var anObject = VISU::ClientSObjectToObject(aSObject);
96 if(VISU::Base_i* aBase = dynamic_cast<VISU::Base_i*>(VISU::GetServant(anObject).in())) {
98 if(aBase->GetType() == VISU::TCOLOREDPRS3DHOLDER){
99 CORBA::Object_var anObject = aBase->_this();
100 VISU::ColoredPrs3dHolder_var aHolder = VISU::ColoredPrs3dHolder::_narrow(anObject);
101 VISU::Prs3d_var aPrs3d = aHolder->GetDevice();
102 aPrs = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(aPrs3d).in());
104 aPrs = dynamic_cast<VISU::Prs3d_i*>(aBase);
107 if (!ContainsPlane(aPrs, thePlane)) {
108 if (thePlane->isAuto())
109 aPrs->AddClippingPlane(thePlane);
111 string aPrsEntry = aPrs->GetEntry();
112 if (aPrsEntry.length() == 0) {
113 VISU::ColoredPrs3d_i* aColPrs = dynamic_cast<VISU::ColoredPrs3d_i*>(aPrs);
115 aPrsEntry = aColPrs->GetHolderEntry();
118 _PTR(SObject) aSObject = thePlane->getPlaneObject();
119 _PTR(ChildIterator) aRefIter = myStudy->NewChildIterator(aSObject);
120 for (; aRefIter->More(); aRefIter->Next()) {
121 _PTR(SObject) aObj = aRefIter->Value();
122 _PTR(SObject) aRefPrsObject;
123 if (aObj->ReferencedObject(aRefPrsObject)) { // If it is referenced on current plane
124 if (aRefPrsObject->GetID() == aPrsEntry) {
125 aPrs->AddClippingPlane(thePlane);
136 //*************************************************************
137 long VISU_ClippingPlaneMgr::CreateClippingPlane(double X,double Y, double Z,
138 double dX, double dY, double dZ,
139 bool isAuto, const char* name)
141 _PTR(SObject) aObjPtr = CreateClippingPlaneObject(X, Y, Z, dX, dY, dZ, isAuto, name);
142 return myPlanes->GetNumberOfItems() - 1;
146 //*************************************************************
147 _PTR(SObject) VISU_ClippingPlaneMgr::CreateClippingPlaneObject(double X,double Y, double Z,
148 double dX, double dY, double dZ,
149 bool isAuto, const char* name)
151 _PTR(SObject) aPlaneObj;
152 if(!myStudy->GetProperties()->IsLocked()) {
153 _PTR(SObject) aFolder = GetClippingPlanesFolder(true);
155 _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
156 aPlaneObj = aBuilder->NewObject(aFolder);
159 _PTR(GenericAttribute) anAttr;
160 anAttr = aBuilder->FindOrCreateAttribute(aPlaneObj,"AttributeName");
161 _PTR(AttributeName) aName(anAttr);
162 aName->SetValue(name);
173 anAttr = aBuilder->FindOrCreateAttribute(aPlaneObj,"AttributeSequenceOfReal");
174 _PTR(AttributeSequenceOfReal) aArray(anAttr);
175 if (aArray->Length() == 6) {
176 for (int i = 0; i < 6; i++)
177 aArray->ChangeValue(i+1, aParams[i]);
179 for (int i = 0; i < 6; i++)
180 aArray->Add(aParams[i]);
183 anAttr = aBuilder->FindOrCreateAttribute(aPlaneObj,"AttributeInteger");
184 _PTR(AttributeInteger) aFlag(anAttr);
185 aFlag->SetValue(isAuto? 1 : 0);
187 vtkSmartPointer<VISU_CutPlaneFunction> aPlane = VISU_CutPlaneFunction::New();
188 aPlane->Delete(); //vtkSmartPointer specific
189 aPlane->setPlaneObject(aPlaneObj);
190 aPlane->SetOrigin(X, Y, Z);
191 aPlane->SetNormal(dX, dY, dZ);
192 aPlane->setName(name);
193 aPlane->setAuto(isAuto);
194 applyPlaneToAll(aPlane);
195 myPlanes->AddItem(aPlane.GetPointer());
202 //*************************************************************
203 void VISU_ClippingPlaneMgr::EditClippingPlane(long id, double X,double Y, double Z,
204 double dX, double dY, double dZ,
205 bool isAuto, const char* name)
207 VISU_CutPlaneFunction* aPlane = GetClippingPlane(id);
208 if (aPlane != NULL) {
209 _PTR(SObject) aSObj = aPlane->getPlaneObject();
210 aPlane->SetOrigin(X, Y, Z);
211 aPlane->SetNormal(dX, dY, dZ);
212 aPlane->setName(name);
213 aPlane->setAuto(isAuto);
215 if(!myStudy->GetProperties()->IsLocked()) {
216 _PTR(GenericAttribute) anAttr;
217 if (aSObj->FindAttribute(anAttr, "AttributeSequenceOfReal")) {
218 _PTR(AttributeSequenceOfReal) aArray(anAttr);
219 aArray->ChangeValue(1, X);
220 aArray->ChangeValue(2, Y);
221 aArray->ChangeValue(3, Z);
222 aArray->ChangeValue(4, dX);
223 aArray->ChangeValue(5, dY);
224 aArray->ChangeValue(6, dZ);
226 if (aSObj->FindAttribute(anAttr, "AttributeInteger")) {
227 _PTR(AttributeInteger) aFlag(anAttr);
228 aFlag->SetValue(isAuto? 1 : 0);
230 if (aSObj->FindAttribute(anAttr, "AttributeName")) {
231 _PTR(AttributeName) aName(anAttr);
232 aName->SetValue(name);
234 // Remove references on presentations if it becomes Auto plane
235 _PTR(SObject) aPlaneSObj = aPlane->getPlaneObject();
236 if (aPlane->isAuto()) {
237 _PTR(ChildIterator) aIter = myStudy->NewChildIterator(aPlaneSObj);
238 _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
239 for (; aIter->More(); aIter->Next()) {
240 _PTR(SObject) aObj = aIter->Value();
241 aBuilder->RemoveObject(aObj);
250 //*************************************************************
251 /* Returns clipping plane by its Id */
252 VISU_CutPlaneFunction* VISU_ClippingPlaneMgr::GetClippingPlane(long id)
254 if ((id < 0) || (id >= GetClippingPlanesNb()))
256 return (VISU_CutPlaneFunction*) myPlanes->GetItemAsObject(id);
259 //*************************************************************
260 /* Returns -1 if Plane is not exists */
261 int VISU_ClippingPlaneMgr::GetPlaneId(VISU_CutPlaneFunction* thePlane)
263 int aTag = thePlane->getPlaneObject()->Tag();
265 VISU_CutPlaneFunction* aPlane;
266 for (int i = 0; i < GetClippingPlanesNb(); i++) {
267 aPlane = GetClippingPlane(i);
268 if (aPlane->getPlaneObject()->Tag() == aTag) {
277 //*************************************************************
278 /* Deletes clipping plane by its Id */
279 bool VISU_ClippingPlaneMgr::DeleteClippingPlane(long id)
281 _PTR(SObject) aFolder = GetClippingPlanesFolder(false);
283 VISU_CutPlaneFunction* aPlane = GetClippingPlane(id);
284 if (aPlane != NULL) {
285 _PTR(SComponent) aVisuSO = myStudy->FindComponent("VISU");
286 _PTR(ChildIterator) aChildIter = myStudy->NewChildIterator(aVisuSO);
287 for (aChildIter->InitEx(true); aChildIter->More(); aChildIter->Next()) {
288 _PTR(SObject) aSObject = aChildIter->Value();
289 CORBA::Object_var anObject = VISU::ClientSObjectToObject(aSObject);
290 if(VISU::Base_i* aBase = dynamic_cast<VISU::Base_i*>(VISU::GetServant(anObject).in())) {
292 if(aBase->GetType() == VISU::TCOLOREDPRS3DHOLDER){
293 CORBA::Object_var anObject = aBase->_this();
294 VISU::ColoredPrs3dHolder_var aHolder = VISU::ColoredPrs3dHolder::_narrow(anObject);
295 VISU::Prs3d_var aPrs3d = aHolder->GetDevice();
296 aPrs = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(aPrs3d).in());
298 aPrs = dynamic_cast<VISU::Prs3d_i*>(aBase);
301 if (ContainsPlane(aPrs, aPlane)) {
302 short aTag1 = aPlane->getPlaneObject()->Tag();
303 for (int j = aPrs->GetNumberOfClippingPlanes()-1; j > -1; j--) {
304 VISU_CutPlaneFunction* aPln = dynamic_cast<VISU_CutPlaneFunction*>
305 (aPrs->GetClippingPlane(j));
307 short aTag2 = aPln->getPlaneObject()->Tag();
308 if (aTag1 == aTag2) {
309 aPrs->RemoveClippingPlane(j);
318 _PTR(SObject) aSObj = aPlane->getPlaneObject();
320 _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
321 aBuilder->RemoveObject(aSObj);
323 myPlanes->RemoveItem(id);
330 //*************************************************************
331 bool VISU_ClippingPlaneMgr::ContainsPlane(VISU::Prs3d_ptr thePrs, VISU_CutPlaneFunction* thePlane)
333 VISU::Prs3d_i* aPrs = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(thePrs).in());
334 return ContainsPlane(aPrs, thePlane);
337 //*************************************************************
338 bool VISU_ClippingPlaneMgr::ContainsPlane(VISU::Prs3d_i* thePrs, VISU_CutPlaneFunction* thePlane)
340 VISU::Prs3d_i* aPrs = thePrs;
341 if (thePrs->GetType() == VISU::TCOLOREDPRS3DHOLDER) {
342 VISU::ColoredPrs3dHolder_i* aHolder = dynamic_cast<VISU::ColoredPrs3dHolder_i*>(thePrs);
343 if (!aHolder) return false;
344 aPrs = aHolder->GetPrs3dDevice();
346 string aEntry = thePlane->getPlaneObject()->GetID();
347 for (int i = 0; i < thePrs->GetNumberOfClippingPlanes(); i++) {
348 VISU_CutPlaneFunction* aPlane = dynamic_cast<VISU_CutPlaneFunction*>(thePrs->GetClippingPlane(i));
350 if (aPlane->getPlaneObject()->GetID() == aEntry) {
359 //*************************************************************
360 /* Applyes a clipping plane with Id to presentation thePrs */
361 bool VISU_ClippingPlaneMgr::ApplyClippingPlane(VISU::Prs3d_i* thePrs, long id)
363 //VISU::Prs3d_i* aPrs = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(thePrs).in());
364 if (!thePrs) return false;
366 VISU_CutPlaneFunction* aPlane = GetClippingPlane(id);
367 if (!aPlane) return false;
368 if (!ContainsPlane(thePrs, aPlane)) {
369 thePrs->AddClippingPlane(aPlane);
370 if (!aPlane->isAuto()) {
371 string aEntry = thePrs->GetEntry();
372 if (aEntry.length() == 0) {
373 VISU::ColoredPrs3d_i* aColPrs = dynamic_cast<VISU::ColoredPrs3d_i*>(thePrs);
375 aEntry = aColPrs->GetHolderEntry();
377 if(!myStudy->GetProperties()->IsLocked()) {
378 _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
379 _PTR(SObject) aPrsSObj = myStudy->FindObjectID(aEntry);
380 _PTR(SObject) aSObject = aPlane->getPlaneObject();
381 _PTR(SObject) aNewObj = aBuilder->NewObject(aSObject);
382 aBuilder->Addreference(aNewObj, aPrsSObj);
390 //*************************************************************
391 bool VISU_ClippingPlaneMgr::DetachClippingPlane(VISU::Prs3d_i* thePrs, long id)
393 VISU_CutPlaneFunction* aPlane = GetClippingPlane(id);
394 //VISU::Prs3d_i* aPrs = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(thePrs).in());
395 if (aPlane && thePrs) {
396 if (ContainsPlane(thePrs, aPlane)) {
397 bool isRemoved = false;
398 short aTag1 = aPlane->getPlaneObject()->Tag();
399 for (int j = thePrs->GetNumberOfClippingPlanes()-1; j > -1; j--) {
400 VISU_CutPlaneFunction* aPln = dynamic_cast<VISU_CutPlaneFunction*>
401 (thePrs->GetClippingPlane(j));
403 short aTag2 = aPln->getPlaneObject()->Tag();
404 if (aTag1 == aTag2) {
405 thePrs->RemoveClippingPlane(j);
411 if(!myStudy->GetProperties()->IsLocked()) {
412 _PTR(SObject) aSObject = aPlane->getPlaneObject();
413 _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
415 string aEntry = thePrs->GetEntry();
416 if (aEntry.length() == 0) {
417 VISU::ColoredPrs3d_i* aColPrs = dynamic_cast<VISU::ColoredPrs3d_i*>(thePrs);
419 aEntry = aColPrs->GetHolderEntry();
421 _PTR(ChildIterator) aIter = myStudy->NewChildIterator(aSObject);
422 for (; aIter->More(); aIter->Next()) {
423 _PTR(SObject) aRefObj = aIter->Value();
425 _PTR(SObject) aRefPrsObject;
426 if (aRefObj->ReferencedObject(aRefPrsObject)) {
427 if (aRefPrsObject->GetID() == aEntry) {
428 aBuilder->RemoveObject(aRefObj);
442 //*************************************************************
443 /* Get number of clipping planes */
444 long VISU_ClippingPlaneMgr::GetClippingPlanesNb()
446 return myPlanes->GetNumberOfItems();
450 //*************************************************************
451 _PTR(SObject) VISU_ClippingPlaneMgr::GetClippingPlanesFolder(bool toCreate)
453 _PTR(SObject) aFolder;
454 _PTR(SComponent) aVisuSO = myStudy->FindComponent("VISU");
455 if (!aVisuSO) return aFolder;
457 aFolder = myStudy->FindObject(CLIP_PLANES_FOLDER);
458 if (!aFolder && toCreate) {
459 _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
460 aFolder = aBuilder->NewObject(aVisuSO);
462 _PTR(GenericAttribute) anAttr;
463 anAttr = aBuilder->FindOrCreateAttribute(aFolder,"AttributeName");
464 _PTR(AttributeName) aName(anAttr);
465 aName->SetValue(CLIP_PLANES_FOLDER);
473 //****************************************************************
474 //****************************************************************
475 //****************************************************************
476 VISU_CutPlaneFunction* VISU_CutPlaneFunction::New()
478 return new VISU_CutPlaneFunction();
481 void VISU_CutPlaneFunction::setActive(bool theActive)
483 myIsActive = theActive;
487 double VISU_CutPlaneFunction::EvaluateFunction(double x[3])
490 return vtkPlane::EvaluateFunction(x);
495 double VISU_CutPlaneFunction::EvaluateFunction(double x, double y, double z)
498 return vtkPlane::EvaluateFunction(x,y,z);
503 VISU_CutPlaneFunction::VISU_CutPlaneFunction():
508 VISU_CutPlaneFunction::~VISU_CutPlaneFunction()