1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : SALOMEDSImpl_UseCaseBuilder.cxx
21 // Author : Sergey RUIN
25 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
26 #include "SALOMEDSImpl_SObject.hxx"
27 #include "SALOMEDSImpl_SComponent.hxx"
28 #include "SALOMEDSImpl_Study.hxx"
29 #include "SALOMEDSImpl_Attributes.hxx"
31 #include <TDF_Label.hxx>
32 #include <TDF_Tool.hxx>
33 #include <TDF_Data.hxx>
34 #include <TDF_AttributeList.hxx>
35 #include <TDF_ListIteratorOfAttributeList.hxx>
36 #include <TCollection_AsciiString.hxx>
37 #include <TDF_ChildIterator.hxx>
41 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_UseCaseBuilder, MMgt_TShared )
42 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_UseCaseBuilder, MMgt_TShared )
45 #define USE_CASE_LABEL_TAG 2
46 #define USE_CASE_GUID "AA43BB12-D9CD-11d6-945D-0050DA506788"
49 //============================================================================
50 /*! Function : constructor
53 //============================================================================
54 SALOMEDSImpl_UseCaseBuilder::SALOMEDSImpl_UseCaseBuilder(const Handle(TDocStd_Document)& theDocument)
57 if(_doc.IsNull()) return;
59 TDF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
60 if(!aLabel.FindAttribute(Standard_GUID(USE_CASE_GUID), _root)) {
61 _root = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, Standard_GUID(USE_CASE_GUID));
64 Handle(SALOMEDSImpl_AttributeReference) aRef;
65 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
66 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
69 Handle(SALOMEDSImpl_AttributeName) aNameAttr;
70 if(!aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aNameAttr)) {
71 aNameAttr = SALOMEDSImpl_AttributeName::Set(aLabel, "Use cases");
75 //============================================================================
76 /*! Function : destructor
79 //============================================================================
80 SALOMEDSImpl_UseCaseBuilder::~SALOMEDSImpl_UseCaseBuilder()
85 //============================================================================
89 //============================================================================
90 bool SALOMEDSImpl_UseCaseBuilder::Append(const Handle(SALOMEDSImpl_SObject)& theObject)
92 if(_root.IsNull() || theObject.IsNull()) return false;
94 TDF_Label aLabel = theObject->GetLabel();
95 if(aLabel.IsNull()) return false;
97 Handle(SALOMEDSImpl_AttributeTreeNode) aNode, aCurrentNode;
98 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
101 Handle(SALOMEDSImpl_AttributeReference) aRef;
102 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
103 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
106 TDF_Label aCurrent = aRef->Get();
107 if(aCurrent.IsNull() || !aCurrent.FindAttribute(_root->ID(), aCurrentNode))
108 aCurrentNode = _root;
110 aCurrentNode->Append(aNode);
115 //============================================================================
116 /*! Function : Remove
119 //============================================================================
120 bool SALOMEDSImpl_UseCaseBuilder::Remove(const Handle(SALOMEDSImpl_SObject)& theObject)
122 if(_root.IsNull() || theObject.IsNull()) return false;
124 TDF_Label aLabel = theObject->GetLabel();
125 if(aLabel.IsNull()) return false;
127 Handle(SALOMEDSImpl_AttributeTreeNode) aNode;
128 if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
132 TDF_AttributeList aList;
135 Handle(SALOMEDSImpl_AttributeReference) aRef;
136 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
137 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
139 TDF_Label aCurrent = aRef->Get();
141 SALOMEDSImpl_ChildNodeIterator aChildItr(aNode, Standard_True);
142 for(; aChildItr.More(); aChildItr.Next())
143 aList.Append(aChildItr.Value());
145 TDF_ListIteratorOfAttributeList anIterator(aList);
146 for(; anIterator.More(); anIterator.Next()) {
147 if(anIterator.Value()->Label() == aCurrent) { //The current node is removed
148 aRef->Set(_root->Label()); //Reset the current node to the root
150 anIterator.Value()->Label().ForgetAttribute(_root->ID());
157 //============================================================================
158 /*! Function : AppendTo
161 //============================================================================
162 bool SALOMEDSImpl_UseCaseBuilder::AppendTo(const Handle(SALOMEDSImpl_SObject)& theFather,
163 const Handle(SALOMEDSImpl_SObject)& theObject)
165 if(_root.IsNull() || theFather.IsNull() || theObject.IsNull()) return false;
167 TDF_Label aFatherLabel = theFather->GetLabel(), aLabel = theObject->GetLabel();
168 if(aFatherLabel == aLabel) return false;
170 Handle(SALOMEDSImpl_AttributeTreeNode) aFather, aNode;
172 if(aFatherLabel.IsNull()) return false;
173 if(!aFatherLabel.FindAttribute(_root->ID(), aFather)) return false;
175 if(aLabel.IsNull()) return false;
176 if(!aLabel.FindAttribute(_root->ID(), aNode)) {
177 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
182 return aFather->Append(aNode);
185 //============================================================================
186 /*! Function : InsertBefore
189 //============================================================================
190 bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const Handle(SALOMEDSImpl_SObject)& theFirst,
191 const Handle(SALOMEDSImpl_SObject)& theNext)
193 if(_root.IsNull() || theFirst.IsNull() || theNext.IsNull()) return false;
195 TDF_Label aFirstLabel = theFirst->GetLabel(), aLabel= theNext->GetLabel();
196 if(aFirstLabel == aLabel) return false;
198 Handle(SALOMEDSImpl_AttributeTreeNode) aFirstNode, aNode;
200 if(aFirstLabel.IsNull()) return false;
201 if(aFirstLabel.FindAttribute(_root->ID(), aFirstNode)) {
202 aFirstNode->Remove();
203 aFirstLabel.ForgetAttribute(aFirstNode->ID());
206 aFirstNode = SALOMEDSImpl_AttributeTreeNode::Set(aFirstLabel, _root->ID());
208 if(aLabel.IsNull()) return false;
209 if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
211 aFirstNode->Remove();
213 return aNode->InsertBefore(aFirstNode);
217 //============================================================================
218 /*! Function : SetCurrentObject
221 //============================================================================
222 bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const Handle(SALOMEDSImpl_SObject)& theObject)
224 if(_root.IsNull() || theObject.IsNull()) return false;
226 TDF_Label aLabel = theObject->GetLabel();
227 Handle(SALOMEDSImpl_AttributeTreeNode) aNode;
228 if(aLabel.IsNull()) return false;
229 if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
232 Handle(SALOMEDSImpl_AttributeReference) aRef;
233 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
234 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), aNode->Label());
237 aRef->Set(aNode->Label());
242 //============================================================================
243 /*! Function : SetRootCurrent
246 //============================================================================
247 bool SALOMEDSImpl_UseCaseBuilder::SetRootCurrent()
249 if(_root.IsNull()) return false;
251 Handle(SALOMEDSImpl_AttributeReference) aRef;
252 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef))
253 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
255 aRef->Set(_root->Label());
259 //============================================================================
260 /*! Function : HasChildren
263 //============================================================================
264 bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const Handle(SALOMEDSImpl_SObject)& theObject)
266 if(_root.IsNull()) return false;
269 if (theObject.IsNull()) aLabel = _root->Label();
271 aLabel = theObject->GetLabel();
272 if(aLabel.IsNull()) return false;
274 Handle(SALOMEDSImpl_AttributeTreeNode) aNode;
275 if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
277 return !(aNode->GetFirst().IsNull());
280 //============================================================================
281 /*! Function : SetName
284 //============================================================================
285 bool SALOMEDSImpl_UseCaseBuilder::SetName(const TCollection_AsciiString& theName) {
286 if(_root.IsNull()) return false;
288 Handle(SALOMEDSImpl_AttributeName) aNameAttrib;
290 if (!_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aNameAttrib))
291 aNameAttrib = SALOMEDSImpl_AttributeName::Set(_root->Label(), theName);
293 aNameAttrib->SetValue(theName);
299 //============================================================================
300 /*! Function : GetCurrentObject
303 //============================================================================
304 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
306 if(_root.IsNull()) return NULL;
308 Handle(SALOMEDSImpl_AttributeReference) aRef;
309 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
310 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
312 TDF_Label aCurrent = aRef->Get();
313 if(aCurrent.IsNull()) return NULL;
315 return SALOMEDSImpl_Study::SObject(aCurrent);
318 //============================================================================
319 /*! Function : GetName
322 //============================================================================
323 TCollection_AsciiString SALOMEDSImpl_UseCaseBuilder::GetName()
325 TCollection_AsciiString aString;
326 if(_root.IsNull()) return aString;
328 Handle(SALOMEDSImpl_AttributeName) aName;
329 if (!_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aName)) return aString;
330 aString = TCollection_AsciiString(aName->Value());
334 //============================================================================
335 /*! Function : IsUseCase
338 //============================================================================
339 bool SALOMEDSImpl_UseCaseBuilder::IsUseCase(const Handle(SALOMEDSImpl_SObject)& theObject)
341 if(theObject.IsNull()) return false;
342 TDF_Label aFather, aLabel = theObject->GetLabel();
343 aFather = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
344 if(aLabel.Father() == aFather) return true;
348 //============================================================================
349 /*! Function : NewUseCase
352 //============================================================================
353 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::AddUseCase(const TCollection_AsciiString& theName)
355 Standard_GUID aBasicGUID(USE_CASE_GUID);
357 //Create a use cases structure if it not exists
359 Handle(SALOMEDSImpl_AttributeTreeNode) aFatherNode, aNode;
360 Handle(SALOMEDSImpl_AttributeInteger) anInteger;
361 Handle(SALOMEDSImpl_AttributeReference) aRef;
363 TDF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
365 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
366 aRef = SALOMEDSImpl_AttributeReference::Set(aLabel, aLabel);
369 if(!aRef->Get().FindAttribute(aBasicGUID, aFatherNode)) {
370 aFatherNode = SALOMEDSImpl_AttributeTreeNode::Set(aRef->Get(), aBasicGUID);
373 if(!_root->FindAttribute(SALOMEDSImpl_AttributeInteger::GetID(), anInteger)) {
374 anInteger = SALOMEDSImpl_AttributeInteger::Set(aLabel, 0);
377 //Create a new use case
378 anInteger->SetValue(anInteger->Value()+1);
379 TDF_Label aChild = aLabel.FindChild(anInteger->Value());
380 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aChild, aBasicGUID);
382 aFatherNode->Append(aNode);
383 SALOMEDSImpl_AttributeName::Set(aChild, theName);
385 return SALOMEDSImpl_Study::SObject(aChild);
388 //============================================================================
389 /*! Function : GetUseCaseIterator
390 * Purpose : Creates a new UseCase iterator, if anObject is null all use cases are iterated
392 //============================================================================
393 Handle(SALOMEDSImpl_UseCaseIterator)
394 SALOMEDSImpl_UseCaseBuilder::GetUseCaseIterator(const Handle(SALOMEDSImpl_SObject)& theObject)
398 if(!theObject.IsNull()) {
399 aLabel = theObject->GetLabel(); //Iterate only sub tree in the use case
402 aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
405 return new SALOMEDSImpl_UseCaseIterator(aLabel, USE_CASE_GUID, false);
409 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::GetSObject(const TCollection_AsciiString& theEntry)
412 TDF_Tool::Label(_doc->GetData(), theEntry, aLabel);
413 return SALOMEDSImpl_Study::SObject(aLabel);