Salome HOME
da7ed8f3ec87b38db0300b32de7842ebd7fcf4cc
[modules/geom.git] / src / GEOMDS / GEOMDS_Commands.cxx
1 using namespace std;
2 //  File      : GeomDS_Commands.cxx
3 //  Created   :
4 //  Author    : Yves FRICAUD/Lucien PIGNOLONI
5 //  Project   : SALOME
6 //  Module    : GEOM
7 //  Copyright : OPEN CASCADE
8 //  $Header$
9
10 #include "utilities.h"
11 #include "GEOMDS_Commands.ixx"
12
13 #include <TNaming_Builder.hxx>
14 #include <TNaming_NamedShape.hxx>
15 #include <TDataStd_Name.hxx>
16 #include <TDataStd_Integer.hxx>
17 #include <TDF_Reference.hxx>
18 #include <TNaming_Tool.hxx>
19 #include <TDF_ChildIterator.hxx>
20
21
22 //=======================================================================
23 //function : GEOMDS_Commands
24 //purpose  : 
25 //=======================================================================
26 GEOMDS_Commands::GEOMDS_Commands(const TDF_Label& Main)
27   : myLab(Main)
28 {
29 }
30
31
32 //=======================================================================
33 // function : Generated()
34 // purpose  :
35 //=======================================================================
36 TDF_Label GEOMDS_Commands::Generated(const TopoDS_Shape& S,
37                                      const TCollection_ExtendedString& Name)
38 {
39   TDF_Label NewLab = myLab.NewChild();
40   TNaming_Builder B(NewLab);
41   B.Generated(S);
42   TDataStd_Name::Set(NewLab,Name);
43   return NewLab;
44 }
45
46
47
48 //=======================================================================
49 // function : Generated()
50 // purpose  : 
51 //=======================================================================
52 TDF_Label GEOMDS_Commands::Generated(const TopoDS_Shape& S1,
53                                      const TopoDS_Shape& S2,
54                                      const TCollection_ExtendedString& Name)
55 {
56   TDF_Label NewLab = myLab.NewChild();
57   TNaming_Builder B(NewLab);
58   B.Generated(S1,S2);
59   TDataStd_Name::Set(NewLab,Name);
60   return NewLab;
61 }
62
63
64
65 //=======================================================================
66 // function : AddShape()
67 // purpose  :
68 //=======================================================================
69 TDF_Label GEOMDS_Commands::AddShape(const TopoDS_Shape& S,
70                                     const TCollection_ExtendedString& Name)
71 {
72   TDF_Label NewLab = myLab.NewChild();
73   TNaming_Builder B(NewLab);
74   B.Select(S,S);
75   TDataStd_Name::Set(NewLab,Name);
76   return NewLab;
77 }
78
79
80 //=======================================================================
81 // function : AddIndependentShape()
82 // purpose  : SAME than AddShape() : will be renamed later
83 //=======================================================================
84 TDF_Label GEOMDS_Commands::AddIndependentShape(const TopoDS_Shape& S, 
85                                                const TCollection_AsciiString& nameIOR)
86 {
87   TDF_Label NewLab = myLab.NewChild();
88   TNaming_Builder B(NewLab);
89   B.Select(S,S);
90   TDataStd_Name::Set(NewLab, nameIOR);
91   return NewLab;
92 }
93
94
95 //=======================================================================
96 // function : AddDependentShape()
97 // purpose  :
98 //=======================================================================
99 TDF_Label GEOMDS_Commands::AddDependentShape(const TopoDS_Shape& S,
100                                              const TCollection_AsciiString& nameIOR,
101                                              const TDF_Label& mainLab)
102 {
103   TDF_Label NewLab = myLab.NewChild();
104   TNaming_Builder B(NewLab);
105   B.Select(S,S);
106   TDataStd_Name::Set(NewLab, nameIOR);
107   /* NewLab has a reference attribute to mainLab (the main shape in fact) */
108   TDF_Reference::Set(NewLab, mainLab) ;
109   return NewLab;
110 }
111
112
113
114 //=======================================================================
115 // function : AddConstructiveElement()
116 // purpose  : 
117 //=======================================================================
118 TDF_Label GEOMDS_Commands::AddConstructiveElement(const TopoDS_Shape& S,
119                                                   const TCollection_ExtendedString& nameIOR,
120                                                   const GEOMDS_ConstructiveType& aType) 
121 {
122   TDF_Label NewLab = myLab.NewChild();
123   TNaming_Builder B(NewLab);
124   B.Select(S,S);
125   TDataStd_Name::Set(NewLab, nameIOR);
126   /* Add the Attribute Constructive Element coded with a TDataStd_Integer from an enum */
127   TDataStd_Integer::Set(NewLab, Standard_Integer(aType));
128   return NewLab;
129 }
130
131
132 //=======================================================================
133 // function : AddIORNameAttribute()       
134 // purpose  : Add attribute TDataStd_Name to a label
135 //          : this attribute represents the name/IOR of object
136 //          : Return false if attribute exist before
137 //=======================================================================
138 Standard_Boolean GEOMDS_Commands::AddIORNameAttribute(const TDF_Label& aLabel,
139                                                       const TCollection_ExtendedString& nameIOR)
140 {
141   if( this->HasIOR(aLabel) )
142     return false ;
143   TDataStd_Name::Set(aLabel, nameIOR);
144   return true ; 
145 }
146
147
148
149 //=======================================================================
150 // function : IsConstructiveElement() 1/2
151 // purpose  : Return true if 'aLabel' is a constructive element
152 //=======================================================================
153 Standard_Boolean GEOMDS_Commands::IsConstructiveElement(const TDF_Label& aLabel)
154 {
155   Handle(TDataStd_Integer) anAttType ;
156   if( aLabel.FindAttribute(TDataStd_Integer::GetID(), anAttType ) )
157     return true ;
158   return false;
159 }
160
161
162 //=======================================================================
163 // function : IsConstructiveElement() 2/2
164 // purpose  : Return true if 'aLabel' is a constructive element and return the
165 //          : topology ' returnTopo' and type 'returnType'
166 //=======================================================================
167 Standard_Boolean GEOMDS_Commands::IsConstructiveElement(const TDF_Label& aLabel,
168                                                         TopoDS_Shape& returnTopo,
169                                                         GEOMDS_ConstructiveType& returnType)
170 {
171   Handle(TDataStd_Integer) anAttType ;
172   Handle(TNaming_NamedShape) anAttTopo ;
173
174   if( aLabel.FindAttribute(TDataStd_Integer::GetID(), anAttType) && aLabel.FindAttribute(TNaming_NamedShape::GetID(), anAttTopo)) {
175
176     returnTopo = TNaming_Tool::GetShape(anAttTopo) ;
177     returnType = GEOMDS_ConstructiveType( anAttType->Get() ) ;
178     return true ;
179   }
180   return false;
181 }
182
183
184 //=======================================================================
185 // function : GetShape()
186 // purpose  : return true and 'returnTopo' if a topology is found on 'aLabel'
187 //=======================================================================
188 Standard_Boolean GEOMDS_Commands::GetShape(const TDF_Label& aLabel,
189                                            TopoDS_Shape& returnTopo)
190 {
191   Handle(TNaming_NamedShape) anAttTopo ;
192   if( aLabel.FindAttribute(TNaming_NamedShape::GetID(), anAttTopo)) {
193     returnTopo = TNaming_Tool::GetShape(anAttTopo) ;
194     return true ;
195   }
196   return false;
197 }
198
199
200 //=======================================================================
201 // function : IsDependentShape()
202 // purpose  : return true if the shape in the label is dependant (a sub shape)
203 //=======================================================================
204 Standard_Boolean GEOMDS_Commands::IsDependentShape(const TDF_Label& aLabel)
205 {
206   Handle(TDF_Reference) anAttRef ;
207   if( aLabel.FindAttribute(TDF_Reference::GetID(), anAttRef))
208     return true ;
209   return false;
210 }
211
212
213
214 //=======================================================================
215 // function : GetMainShapeLabel()
216 // purpose  : return true if an attribute Reference is found for 'aLabel'
217 //          : so 'returnMainLabel' is defined. 'aLabel' is supposed to be
218 //          : a dependent object, otherwise return false.
219 //=======================================================================
220 Standard_Boolean GEOMDS_Commands::GetMainShapeLabel(const TDF_Label& aLabel,
221                                                     TDF_Label& returnMainLabel)
222 {
223   Handle(TDF_Reference) anAttRef ;
224   if( aLabel.FindAttribute(TDF_Reference::GetID(), anAttRef)) {
225     returnMainLabel = anAttRef->Get() ;
226     return true ;
227   }
228   return false;
229 }
230
231
232 //=======================================================================
233 // function : ClearAllIOR()
234 // purpose  : Clear all IOR from aLabel usually the main label.
235 //          : Useful before reconstruction after a load of a document.
236 //          : IOR is the attribute often called 'name' or 'nameIOR'
237 //=======================================================================
238 Standard_Boolean GEOMDS_Commands::ClearAllIOR(const TDF_Label& aLabel)
239 {
240   TDF_ChildIterator it;
241   Handle(TDataStd_Name) anAttName ;
242   bool notTested = false ;
243   for( it.Initialize(aLabel, Standard_False); it.More(); it.Next() ) {
244     TDF_Label L = it.Value() ;
245     if( L.FindAttribute(TDataStd_Name::GetID(), anAttName) ) {
246       notTested = L.ForgetAttribute(TDataStd_Name::GetID()) ;
247     }
248     if(notTested)
249       MESSAGE("in GEOMDS_Commands::ClearAllIOR : IOR CLEARED" )
250   }
251   return true ;
252 }
253
254
255 //=======================================================================
256 // function : HasIOR()
257 // purpose  : Return true is 'aLabel' has an attribute IOR (nameIOR)
258 //=======================================================================
259 Standard_Boolean GEOMDS_Commands::HasIOR(const TDF_Label& aLabel)
260 {
261   Handle(TDataStd_Name) anAttName ;
262   if( !aLabel.FindAttribute(TDataStd_Name::GetID(), anAttName) )
263     return false ;
264   return true ;
265 }
266
267 //=======================================================================
268 // function : ReturnNameIOR()
269 // purpose  : Return true is 'aLabel' has an attribute IOR (nameIOR)
270 //          : and define 'returnNameIOR'
271 //=======================================================================
272 Standard_Boolean GEOMDS_Commands::ReturnNameIOR(const TDF_Label& aLabel,
273                                                 TCollection_ExtendedString& returnNameIOR)
274 {
275   Handle(TDataStd_Name) anAttName ;
276   if( !aLabel.FindAttribute(TDataStd_Name::GetID(), anAttName) )
277     return false ;
278   else {
279     returnNameIOR = anAttName->Get() ;
280     return true ;
281   }
282 }