1 // File : SALOMEDSImpl_UseCaseBuilder.cxx
2 // Author : Sergey RUIN
6 #include "SALOMEDSImpl_UseCaseBuilder.hxx"
7 #include "SALOMEDSImpl_SObject.hxx"
8 #include "SALOMEDSImpl_SComponent.hxx"
9 #include "SALOMEDSImpl_Study.hxx"
10 #include "SALOMEDSImpl_Attributes.hxx"
12 #include <TDF_Label.hxx>
13 #include <TDF_Tool.hxx>
14 #include <TDF_Data.hxx>
15 #include <TDF_AttributeList.hxx>
16 #include <TDF_ListIteratorOfAttributeList.hxx>
17 #include <TCollection_AsciiString.hxx>
18 #include <TDF_ChildIterator.hxx>
22 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_UseCaseBuilder, MMgt_TShared )
23 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_UseCaseBuilder, MMgt_TShared )
26 #define USE_CASE_LABEL_TAG 2
27 #define USE_CASE_GUID "AA43BB12-D9CD-11d6-945D-0050DA506788"
30 //============================================================================
31 /*! Function : constructor
34 //============================================================================
35 SALOMEDSImpl_UseCaseBuilder::SALOMEDSImpl_UseCaseBuilder(const Handle(TDocStd_Document)& theDocument)
38 if(_doc.IsNull()) return;
40 TDF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
41 if(!aLabel.FindAttribute(Standard_GUID(USE_CASE_GUID), _root)) {
42 _root = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, Standard_GUID(USE_CASE_GUID));
45 Handle(SALOMEDSImpl_AttributeReference) aRef;
46 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
47 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
50 Handle(SALOMEDSImpl_AttributeName) aNameAttr;
51 if(!aLabel.FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aNameAttr)) {
52 aNameAttr = SALOMEDSImpl_AttributeName::Set(aLabel, "Use cases");
56 //============================================================================
57 /*! Function : destructor
60 //============================================================================
61 SALOMEDSImpl_UseCaseBuilder::~SALOMEDSImpl_UseCaseBuilder()
66 //============================================================================
70 //============================================================================
71 bool SALOMEDSImpl_UseCaseBuilder::Append(const Handle(SALOMEDSImpl_SObject)& theObject)
73 if(_root.IsNull() || theObject.IsNull()) return false;
75 TDF_Label aLabel = theObject->GetLabel();
76 if(aLabel.IsNull()) return false;
78 Handle(SALOMEDSImpl_AttributeTreeNode) aNode, aCurrentNode;
79 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
82 Handle(SALOMEDSImpl_AttributeReference) aRef;
83 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
84 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
87 TDF_Label aCurrent = aRef->Get();
88 if(aCurrent.IsNull() || !aCurrent.FindAttribute(_root->ID(), aCurrentNode))
91 aCurrentNode->Append(aNode);
96 //============================================================================
100 //============================================================================
101 bool SALOMEDSImpl_UseCaseBuilder::Remove(const Handle(SALOMEDSImpl_SObject)& theObject)
103 if(_root.IsNull() || theObject.IsNull()) return false;
105 TDF_Label aLabel = theObject->GetLabel();
106 if(aLabel.IsNull()) return false;
108 Handle(SALOMEDSImpl_AttributeTreeNode) aNode;
109 if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
113 TDF_AttributeList aList;
116 Handle(SALOMEDSImpl_AttributeReference) aRef;
117 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
118 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
120 TDF_Label aCurrent = aRef->Get();
122 SALOMEDSImpl_ChildNodeIterator aChildItr(aNode, Standard_True);
123 for(; aChildItr.More(); aChildItr.Next())
124 aList.Append(aChildItr.Value());
126 TDF_ListIteratorOfAttributeList anIterator(aList);
127 for(; anIterator.More(); anIterator.Next()) {
128 if(anIterator.Value()->Label() == aCurrent) { //The current node is removed
129 aRef->Set(_root->Label()); //Reset the current node to the root
131 anIterator.Value()->Label().ForgetAttribute(_root->ID());
138 //============================================================================
139 /*! Function : AppendTo
142 //============================================================================
143 bool SALOMEDSImpl_UseCaseBuilder::AppendTo(const Handle(SALOMEDSImpl_SObject)& theFather,
144 const Handle(SALOMEDSImpl_SObject)& theObject)
146 if(_root.IsNull() || theFather.IsNull() || theObject.IsNull()) return false;
148 TDF_Label aFatherLabel = theFather->GetLabel(), aLabel = theObject->GetLabel();
149 Handle(SALOMEDSImpl_AttributeTreeNode) aFather, aNode;
151 if(aFatherLabel.IsNull()) return false;
152 if(!aFatherLabel.FindAttribute(_root->ID(), aFather)) return false;
154 if(aLabel.IsNull()) return false;
155 if(!aLabel.FindAttribute(_root->ID(), aNode)) {
156 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aLabel, _root->ID());
161 return aFather->Append(aNode);
164 //============================================================================
165 /*! Function : InsertBefore
168 //============================================================================
169 bool SALOMEDSImpl_UseCaseBuilder::InsertBefore(const Handle(SALOMEDSImpl_SObject)& theFirst,
170 const Handle(SALOMEDSImpl_SObject)& theNext)
172 if(_root.IsNull() || theFirst.IsNull() || theNext.IsNull()) return false;
174 TDF_Label aFirstLabel = theFirst->GetLabel(), aLabel= theNext->GetLabel();
175 Handle(SALOMEDSImpl_AttributeTreeNode) aFirstNode, aNode;
177 if(aFirstLabel.IsNull()) return false;
178 if(aFirstLabel.FindAttribute(_root->ID(), aFirstNode)) {
179 aFirstNode->Remove();
180 aFirstLabel.ForgetAttribute(aFirstNode->ID());
183 aFirstNode = SALOMEDSImpl_AttributeTreeNode::Set(aFirstLabel, _root->ID());
185 if(aLabel.IsNull()) return false;
186 if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
188 aFirstNode->Remove();
190 return aNode->InsertBefore(aFirstNode);
194 //============================================================================
195 /*! Function : SetCurrentObject
198 //============================================================================
199 bool SALOMEDSImpl_UseCaseBuilder::SetCurrentObject(const Handle(SALOMEDSImpl_SObject)& theObject)
201 if(_root.IsNull() || theObject.IsNull()) return false;
203 TDF_Label aLabel = theObject->GetLabel();
204 Handle(SALOMEDSImpl_AttributeTreeNode) aNode;
205 if(aLabel.IsNull()) return false;
206 if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
209 Handle(SALOMEDSImpl_AttributeReference) aRef;
210 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
211 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), aNode->Label());
214 aRef->Set(aNode->Label());
219 //============================================================================
220 /*! Function : SetRootCurrent
223 //============================================================================
224 bool SALOMEDSImpl_UseCaseBuilder::SetRootCurrent()
226 if(_root.IsNull()) return false;
228 Handle(SALOMEDSImpl_AttributeReference) aRef;
229 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef))
230 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
232 aRef->Set(_root->Label());
236 //============================================================================
237 /*! Function : HasChildren
240 //============================================================================
241 bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const Handle(SALOMEDSImpl_SObject)& theObject)
243 if(_root.IsNull()) return false;
246 if (theObject.IsNull()) aLabel = _root->Label();
248 aLabel = theObject->GetLabel();
249 if(aLabel.IsNull()) return false;
251 Handle(SALOMEDSImpl_AttributeTreeNode) aNode;
252 if(!aLabel.FindAttribute(_root->ID(), aNode)) return false;
254 return !(aNode->GetFirst().IsNull());
257 //============================================================================
258 /*! Function : SetName
261 //============================================================================
262 bool SALOMEDSImpl_UseCaseBuilder::SetName(const TCollection_AsciiString& theName) {
263 if(_root.IsNull()) return false;
265 Handle(SALOMEDSImpl_AttributeName) aNameAttrib;
267 if (!_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aNameAttrib))
268 aNameAttrib = SALOMEDSImpl_AttributeName::Set(_root->Label(), theName);
270 aNameAttrib->SetValue(theName);
276 //============================================================================
277 /*! Function : GetCurrentObject
280 //============================================================================
281 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::GetCurrentObject()
283 if(_root.IsNull()) return NULL;
285 Handle(SALOMEDSImpl_AttributeReference) aRef;
286 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
287 aRef = SALOMEDSImpl_AttributeReference::Set(_root->Label(), _root->Label());
289 TDF_Label aCurrent = aRef->Get();
290 if(aCurrent.IsNull()) return NULL;
292 return SALOMEDSImpl_Study::SObject(aCurrent);
295 //============================================================================
296 /*! Function : GetName
299 //============================================================================
300 TCollection_AsciiString SALOMEDSImpl_UseCaseBuilder::GetName()
302 TCollection_AsciiString aString;
303 if(_root.IsNull()) return aString;
305 Handle(SALOMEDSImpl_AttributeName) aName;
306 if (!_root->FindAttribute(SALOMEDSImpl_AttributeName::GetID(), aName)) return aString;
307 aString = TCollection_AsciiString(aName->Value());
311 //============================================================================
312 /*! Function : IsUseCase
315 //============================================================================
316 bool SALOMEDSImpl_UseCaseBuilder::IsUseCase(const Handle(SALOMEDSImpl_SObject)& theObject)
318 if(theObject.IsNull()) return false;
319 TDF_Label aFather, aLabel = theObject->GetLabel();
320 aFather = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
321 if(aLabel.Father() == aFather) return true;
325 //============================================================================
326 /*! Function : NewUseCase
329 //============================================================================
330 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::AddUseCase(const TCollection_AsciiString& theName)
332 Standard_GUID aBasicGUID(USE_CASE_GUID);
334 //Create a use cases structure if it not exists
336 Handle(SALOMEDSImpl_AttributeTreeNode) aFatherNode, aNode;
337 Handle(SALOMEDSImpl_AttributeInteger) anInteger;
338 Handle(SALOMEDSImpl_AttributeReference) aRef;
340 TDF_Label aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG);
342 if(!_root->FindAttribute(SALOMEDSImpl_AttributeReference::GetID(), aRef)) {
343 aRef = SALOMEDSImpl_AttributeReference::Set(aLabel, aLabel);
346 if(!aRef->Get().FindAttribute(aBasicGUID, aFatherNode)) {
347 aFatherNode = SALOMEDSImpl_AttributeTreeNode::Set(aRef->Get(), aBasicGUID);
350 if(!_root->FindAttribute(SALOMEDSImpl_AttributeInteger::GetID(), anInteger)) {
351 anInteger = SALOMEDSImpl_AttributeInteger::Set(aLabel, 0);
354 //Create a new use case
355 anInteger->SetValue(anInteger->Value()+1);
356 TDF_Label aChild = aLabel.FindChild(anInteger->Value());
357 aNode = SALOMEDSImpl_AttributeTreeNode::Set(aChild, aBasicGUID);
359 aFatherNode->Append(aNode);
360 SALOMEDSImpl_AttributeName::Set(aChild, theName);
362 return SALOMEDSImpl_Study::SObject(aChild);
365 //============================================================================
366 /*! Function : GetUseCaseIterator
367 * Purpose : Creates a new UseCase iterator, if anObject is null all use cases are iterated
369 //============================================================================
370 Handle(SALOMEDSImpl_UseCaseIterator)
371 SALOMEDSImpl_UseCaseBuilder::GetUseCaseIterator(const Handle(SALOMEDSImpl_SObject)& theObject)
375 if(!theObject.IsNull()) {
376 aLabel = theObject->GetLabel(); //Iterate only sub tree in the use case
379 aLabel = _doc->Main().Root().FindChild(USE_CASE_LABEL_TAG); //Iterate all use cases
382 return new SALOMEDSImpl_UseCaseIterator(aLabel, USE_CASE_GUID, false);
386 Handle(SALOMEDSImpl_SObject) SALOMEDSImpl_UseCaseBuilder::GetSObject(const TCollection_AsciiString& theEntry)
389 TDF_Tool::Label(_doc->GetData(), theEntry, aLabel);
390 return SALOMEDSImpl_Study::SObject(aLabel);