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