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 #include "VISU_ClippingPlaneMgr.hxx"
23 #include "VISU_ColoredPrs3dHolder_i.hh"
25 //#include CORBA_SERVER_HEADER(SALOMEDS)
26 //#include CORBA_SERVER_HEADER(SALOMEDS_Attributes)
28 #include "SALOMEDSClient_GenericAttribute.hxx"
29 #include "SALOMEDSClient_AttributeName.hxx"
30 #include "SALOMEDSClient_AttributeSequenceOfReal.hxx"
31 #include "SALOMEDSClient_AttributeInteger.hxx"
34 #include <vtkImplicitFunctionCollection.h>
37 #define CLIP_PLANES_FOLDER "Clipping Planes"
41 //*************************************************************
42 VISU_ClippingPlaneMgr::VISU_ClippingPlaneMgr()
44 myPlanes = vtkImplicitFunctionCollection::New();
47 //*************************************************************
48 VISU_ClippingPlaneMgr::~VISU_ClippingPlaneMgr()
53 //*************************************************************
54 void VISU_ClippingPlaneMgr::SetStudy(_PTR(Study) theStudy)
56 if (myStudy == theStudy) return;
58 myPlanes->RemoveAllItems();
61 _PTR(SObject) aFolder = GetClippingPlanesFolder(false);
63 _PTR(ChildIterator) aIter = myStudy->NewChildIterator(aFolder);
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());
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));
77 if (aSObject->FindAttribute(anAttr, "AttributeInteger")) {
78 _PTR(AttributeInteger) aFlag(anAttr);
79 aPlane->setAuto(aFlag->Value() == 1);
81 aPlane->setAuto(false);
83 applyPlaneToAll(aPlane);
84 myPlanes->AddItem(aPlane);
90 void VISU_ClippingPlaneMgr::applyPlaneToAll(VISU_CutPlaneFunction* thePlane)
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())) {
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());
105 aPrs = dynamic_cast<VISU::Prs3d_i*>(aBase);
108 if (!ContainsPlane(aPrs, thePlane)) {
109 if (thePlane->isAuto())
110 aPrs->AddClippingPlane(thePlane);
112 string aPrsEntry = aPrs->GetEntry();
113 if (aPrsEntry.length() == 0) {
114 VISU::ColoredPrs3d_i* aColPrs = dynamic_cast<VISU::ColoredPrs3d_i*>(aPrs);
116 aPrsEntry = aColPrs->GetHolderEntry();
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);
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)
142 _PTR(SObject) aObjPtr = CreateClippingPlaneObject(X, Y, Z, dX, dY, dZ, isAuto, name);
143 return myPlanes->GetNumberOfItems() - 1;
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)
152 _PTR(SObject) aPlaneObj;
153 if(!myStudy->GetProperties()->IsLocked()) {
154 _PTR(SObject) aFolder = GetClippingPlanesFolder(true);
156 _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
157 aPlaneObj = aBuilder->NewObject(aFolder);
160 _PTR(GenericAttribute) anAttr;
161 anAttr = aBuilder->FindOrCreateAttribute(aPlaneObj,"AttributeName");
162 _PTR(AttributeName) aName(anAttr);
163 aName->SetValue(name);
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]);
180 for (int i = 0; i < 6; i++)
181 aArray->Add(aParams[i]);
184 anAttr = aBuilder->FindOrCreateAttribute(aPlaneObj,"AttributeInteger");
185 _PTR(AttributeInteger) aFlag(anAttr);
186 aFlag->SetValue(isAuto? 1 : 0);
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());
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)
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);
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);
227 if (aSObj->FindAttribute(anAttr, "AttributeInteger")) {
228 _PTR(AttributeInteger) aFlag(anAttr);
229 aFlag->SetValue(isAuto? 1 : 0);
231 if (aSObj->FindAttribute(anAttr, "AttributeName")) {
232 _PTR(AttributeName) aName(anAttr);
233 aName->SetValue(name);
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);
251 //*************************************************************
252 /* Returns clipping plane by its Id */
253 VISU_CutPlaneFunction* VISU_ClippingPlaneMgr::GetClippingPlane(long id)
255 if ((id < 0) || (id >= GetClippingPlanesNb()))
257 return (VISU_CutPlaneFunction*) myPlanes->GetItemAsObject(id);
260 //*************************************************************
261 /* Returns -1 if Plane is not exists */
262 int VISU_ClippingPlaneMgr::GetPlaneId(VISU_CutPlaneFunction* thePlane)
264 int aTag = thePlane->getPlaneObject()->Tag();
266 VISU_CutPlaneFunction* aPlane;
267 for (int i = 0; i < GetClippingPlanesNb(); i++) {
268 aPlane = GetClippingPlane(i);
269 if (aPlane->getPlaneObject()->Tag() == aTag) {
278 //*************************************************************
279 /* Deletes clipping plane by its Id */
280 bool VISU_ClippingPlaneMgr::DeleteClippingPlane(long id)
282 _PTR(SObject) aFolder = GetClippingPlanesFolder(false);
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())) {
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());
299 aPrs = dynamic_cast<VISU::Prs3d_i*>(aBase);
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));
308 short aTag2 = aPln->getPlaneObject()->Tag();
309 if (aTag1 == aTag2) {
310 aPrs->RemoveClippingPlane(j);
319 _PTR(SObject) aSObj = aPlane->getPlaneObject();
321 _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
322 aBuilder->RemoveObject(aSObj);
324 myPlanes->RemoveItem(id);
331 //*************************************************************
332 bool VISU_ClippingPlaneMgr::ContainsPlane(VISU::Prs3d_ptr thePrs, VISU_CutPlaneFunction* thePlane)
334 VISU::Prs3d_i* aPrs = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(thePrs).in());
335 return ContainsPlane(aPrs, thePlane);
338 //*************************************************************
339 bool VISU_ClippingPlaneMgr::ContainsPlane(VISU::Prs3d_i* thePrs, VISU_CutPlaneFunction* thePlane)
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();
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));
351 if (aPlane->getPlaneObject()->GetID() == aEntry) {
360 //*************************************************************
361 /* Applyes a clipping plane with Id to presentation thePrs */
362 bool VISU_ClippingPlaneMgr::ApplyClippingPlane(VISU::Prs3d_i* thePrs, long id)
364 //VISU::Prs3d_i* aPrs = dynamic_cast<VISU::Prs3d_i*>(VISU::GetServant(thePrs).in());
365 if (!thePrs) return false;
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);
376 aEntry = aColPrs->GetHolderEntry();
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);
391 //*************************************************************
392 bool VISU_ClippingPlaneMgr::DetachClippingPlane(VISU::Prs3d_i* thePrs, long id)
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));
404 short aTag2 = aPln->getPlaneObject()->Tag();
405 if (aTag1 == aTag2) {
406 thePrs->RemoveClippingPlane(j);
412 if(!myStudy->GetProperties()->IsLocked()) {
413 _PTR(SObject) aSObject = aPlane->getPlaneObject();
414 _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
416 string aEntry = thePrs->GetEntry();
417 if (aEntry.length() == 0) {
418 VISU::ColoredPrs3d_i* aColPrs = dynamic_cast<VISU::ColoredPrs3d_i*>(thePrs);
420 aEntry = aColPrs->GetHolderEntry();
422 _PTR(ChildIterator) aIter = myStudy->NewChildIterator(aSObject);
423 for (; aIter->More(); aIter->Next()) {
424 _PTR(SObject) aRefObj = aIter->Value();
426 _PTR(SObject) aRefPrsObject;
427 if (aRefObj->ReferencedObject(aRefPrsObject)) {
428 if (aRefPrsObject->GetID() == aEntry) {
429 aBuilder->RemoveObject(aRefObj);
443 //*************************************************************
444 /* Get number of clipping planes */
445 long VISU_ClippingPlaneMgr::GetClippingPlanesNb()
447 return myPlanes->GetNumberOfItems();
451 //*************************************************************
452 _PTR(SObject) VISU_ClippingPlaneMgr::GetClippingPlanesFolder(bool toCreate)
454 _PTR(SObject) aFolder;
455 _PTR(SComponent) aVisuSO = myStudy->FindComponent("VISU");
456 if (!aVisuSO) return aFolder;
458 aFolder = myStudy->FindObject(CLIP_PLANES_FOLDER);
459 if (!aFolder && toCreate) {
460 _PTR(StudyBuilder) aBuilder = myStudy->NewBuilder();
461 aFolder = aBuilder->NewObject(aVisuSO);
463 _PTR(GenericAttribute) anAttr;
464 anAttr = aBuilder->FindOrCreateAttribute(aFolder,"AttributeName");
465 _PTR(AttributeName) aName(anAttr);
466 aName->SetValue(CLIP_PLANES_FOLDER);
474 //****************************************************************
475 //****************************************************************
476 //****************************************************************
477 VISU_CutPlaneFunction* VISU_CutPlaneFunction::New()
479 return new VISU_CutPlaneFunction();
482 void VISU_CutPlaneFunction::setActive(bool theActive)
484 myIsActive = theActive;
488 double VISU_CutPlaneFunction::EvaluateFunction(double x[3])
491 return vtkPlane::EvaluateFunction(x);
496 double VISU_CutPlaneFunction::EvaluateFunction(double x, double y, double z)
499 return vtkPlane::EvaluateFunction(x,y,z);
504 VISU_CutPlaneFunction::VISU_CutPlaneFunction():
509 VISU_CutPlaneFunction::~VISU_CutPlaneFunction()