0024947: Redesign OCCT legacy type system -- automatic
[occt.git] / src / TObj / TObj_Partition.cxx
1 // Created on: 2004-11-23
2 // Created by: Pavel TELKOV
3 // Copyright (c) 2004-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 // The original implementation Copyright: (C) RINA S.p.A
17
18 #include <TObj_Partition.hxx>
19
20 #include <TObj_Model.hxx>
21 #include <TObj_TNameContainer.hxx>
22
23 #include <TDataStd_Name.hxx>
24
25
26 IMPLEMENT_TOBJOCAF_PERSISTENCE(TObj_Partition)
27
28 //=======================================================================
29 //function : TObj_Partition
30 //purpose  :
31 //=======================================================================
32
33 TObj_Partition::TObj_Partition (const TDF_Label& theLabel)
34      : TObj_Object( theLabel )
35 {
36 }
37
38 //=======================================================================
39 //function : Create
40 //purpose  :
41 //=======================================================================
42
43 Handle(TObj_Partition) TObj_Partition::Create
44                            (const TDF_Label& theLabel)
45 {
46   Handle(TObj_Partition) aPartition =
47     new TObj_Partition(theLabel);
48   aPartition->SetLastIndex(0);
49   return aPartition;
50 }
51
52 //=======================================================================
53 //function : NewLabel
54 //purpose  :
55 //=======================================================================
56
57 TDF_Label TObj_Partition::NewLabel() const
58 {
59   TDF_Label aLabel;
60   TDF_TagSource aTag;
61   aLabel = aTag.NewChild(GetChildLabel());
62   return aLabel;
63 }
64
65 //=======================================================================
66 //function : SetNamePrefix
67 //purpose  :
68 //=======================================================================
69
70 void TObj_Partition::SetNamePrefix
71                         (const Handle(TCollection_HExtendedString)& thePrefix)
72 { myPrefix = thePrefix; }
73
74 //=======================================================================
75 //function : NewName
76 //purpose  :
77 //=======================================================================
78
79 Handle(TCollection_HExtendedString) TObj_Partition::GetNewName
80 ( const Standard_Boolean theIsToChangeCount )
81 {
82   if ( myPrefix.IsNull() ) return 0;
83
84   Standard_Integer aRank = GetLastIndex()+1;
85   Standard_Integer saveRank = aRank;
86   Handle(TCollection_HExtendedString) aName;
87   do
88   {
89     aName = new TCollection_HExtendedString(myPrefix->String()+aRank++);
90   } while( GetModel()->IsRegisteredName( aName, GetDictionary() ) );
91
92   // the last index is increased taking into account only names that are
93   // actually set; the name requested by the current operation can be
94   // dropped later and this will not cause index to be increased
95   if ( theIsToChangeCount && --aRank > saveRank )
96     SetLastIndex ( aRank );
97   return aName;
98 }
99
100 //=======================================================================
101 //function : GetPartition
102 //purpose  :
103 //=======================================================================
104
105 Handle(TObj_Partition) TObj_Partition::GetPartition
106                         (const Handle(TObj_Object)& theObject)
107 {
108   Handle(TObj_Partition) aPartition;
109   if(!theObject.IsNull())
110   {
111     TDF_Label aLabel = theObject->GetLabel().Father();
112
113     // find partition which contains the object
114     while(aPartition.IsNull() && !aLabel.IsNull())
115     {
116       Handle(TObj_Object) anObject;
117       if(TObj_Object::GetObj(aLabel,anObject,Standard_True))
118         aPartition = Handle(TObj_Partition)::DownCast(anObject);
119
120       if(aPartition.IsNull())
121         aLabel = aLabel.Father();
122     }
123   }
124   return aPartition;
125 }
126
127 //=======================================================================
128 //function : GetLastIndex
129 //purpose  :
130 //=======================================================================
131
132 Standard_Integer TObj_Partition::GetLastIndex() const
133 {
134   return getInteger(DataTag_LastIndex);
135 }
136
137 //=======================================================================
138 //function : SetLastIndex
139 //purpose  :
140 //=======================================================================
141
142 void TObj_Partition::SetLastIndex(const Standard_Integer theIndex)
143 {
144   setInteger(theIndex,DataTag_LastIndex);
145 }
146
147 //=======================================================================
148 //function : copyData
149 //purpose  : protected
150 //=======================================================================
151
152 Standard_Boolean TObj_Partition::copyData
153                 (const Handle(TObj_Object)& theTargetObject)
154 {
155   Standard_Boolean IsDone;
156   Handle(TObj_Partition) aTargetPartition =
157     Handle(TObj_Partition)::DownCast(theTargetObject);
158   IsDone = aTargetPartition.IsNull() ? Standard_False : Standard_True;
159   if(IsDone) 
160   {
161     IsDone = TObj_Object::copyData(theTargetObject);
162     if ( IsDone ) 
163     {
164       aTargetPartition->myPrefix = myPrefix;
165     }
166   }
167   return IsDone;
168 }
169
170 //=======================================================================
171 //function : SetName
172 //purpose  : do not register a name in the dictionary
173 //=======================================================================
174
175 Standard_Boolean TObj_Partition::SetName(const Handle(TCollection_HExtendedString)& theName) const
176 {
177   Handle(TCollection_HExtendedString) anOldName = GetName();
178   if( !anOldName.IsNull() && theName->String().IsEqual(anOldName->String()) )
179     return Standard_True;
180
181   TDataStd_Name::Set(GetLabel(),theName->String());
182   return Standard_True;
183 }
184
185 //=======================================================================
186 //function : AfterRetrieval
187 //purpose  : do not register a name in the dictionary
188 //=======================================================================
189
190 void TObj_Partition::AfterRetrieval()
191 {
192 }