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