Salome HOME
ddc967c698890b1c62c43db4bc775c3cecbf10b7
[modules/geom.git] / src / GEOM / GEOM_Object.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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
23 #include "GEOM_Object.hxx"
24
25 #include <TDataStd_Integer.hxx>
26 #include <TDataStd_Real.hxx>
27 #include <TDataStd_RealArray.hxx>
28
29 #include "utilities.h"
30
31 // #define FUNCTION_LABEL(theNb) (_label.FindChild(1).FindChild((theNb)))
32 // #define TYPE_LABEL       2 -- Labels used by GEOM_BaseObject
33 // #define FREE_LABEL       3
34 // #define TIC_LABEL        4
35 #define COLOR_LABEL      5
36 #define AUTO_COLOR_LABEL 6
37 #define MARKER_LABEL     7
38
39 #define MARKER_LABEL_TYPE 1
40 #define MARKER_LABEL_SIZE 2
41 #define MARKER_LABEL_ID   3
42
43 //=============================================================================
44 /*!
45  *  GetObject
46  */
47 //=============================================================================
48
49 Handle(GEOM_Object) GEOM_Object::GetObject(const TDF_Label& theLabel)
50 {
51   Handle(GEOM_BaseObject) base = GEOM_BaseObject::GetObject(theLabel);
52   return Handle(GEOM_Object)::DownCast( base );
53 }
54
55 //=============================================================================
56 /*!
57  *  GetReferencedObject
58  */
59 //=============================================================================
60
61 Handle(GEOM_Object) GEOM_Object::GetReferencedObject(const TDF_Label& theLabel)
62 {
63   Handle(GEOM_BaseObject) base = GEOM_BaseObject::GetReferencedObject(theLabel);
64   return Handle(GEOM_Object)::DownCast( base );
65 }
66
67 //=============================================================================
68 /*!
69  *  Constructor: private
70  */
71 //=============================================================================
72
73 GEOM_Object::GEOM_Object(TDF_Label& theEntry)
74   : GEOM_BaseObject(theEntry)
75 {
76 }
77
78 //=============================================================================
79 /*!
80  *  Constructor: public
81  */
82 //=============================================================================
83
84 GEOM_Object::GEOM_Object(TDF_Label& theEntry, int theType)
85   : GEOM_BaseObject( theEntry, theType )
86 {
87 }
88
89 //=============================================================================
90 /*!
91  *  Destructor
92  */
93 //=============================================================================
94 GEOM_Object::~GEOM_Object()
95 {
96   //MESSAGE("GEOM_Object::~GEOM_Object()");
97 }
98
99 //=============================================================================
100 /*!
101  *  GetValue
102  */
103 //=============================================================================
104 TopoDS_Shape GEOM_Object::GetValue()
105 {
106   TopoDS_Shape aShape;
107
108   Handle(GEOM_Function) aFunction = GetLastFunction();
109
110   if (!aFunction.IsNull())
111     aShape = aFunction->GetValue();
112
113   return aShape;
114 }
115
116 //=============================================================================
117 /*!
118  *  SetColor
119  */
120 //=============================================================================
121 void GEOM_Object::SetColor(const GEOM_Object::Color& theColor)
122 {
123   Handle(TDataStd_RealArray) anArray = new TDataStd_RealArray();
124   anArray->Init( 1, 3 );
125   anArray->SetValue( 1, theColor.R );
126   anArray->SetValue( 2, theColor.G );
127   anArray->SetValue( 3, theColor.B );
128
129   Handle(TDataStd_RealArray) anAttr =
130     TDataStd_RealArray::Set(_label.FindChild(COLOR_LABEL), anArray->Lower(), anArray->Upper());
131   anAttr->ChangeArray(anArray->Array());
132 }
133
134 //=============================================================================
135 /*!
136  *  GetColor
137  */
138 //=============================================================================
139 GEOM_Object::Color GEOM_Object::GetColor()
140 {
141   Handle(TDataStd_RealArray) anArray;
142   bool isFound = _label.FindChild(COLOR_LABEL).FindAttribute(TDataStd_RealArray::GetID(), anArray);
143
144   GEOM_Object::Color aColor;
145   aColor.R = isFound ? anArray->Value( 1 ) : -1;
146   aColor.G = isFound ? anArray->Value( 2 ) : -1;
147   aColor.B = isFound ? anArray->Value( 3 ) : -1;
148
149   return aColor;
150 }
151
152 //=============================================================================
153 /*!
154  *  SetAutoColor
155  */
156 //=============================================================================
157 void GEOM_Object::SetAutoColor(bool theAutoColor)
158 {
159   TDataStd_Integer::Set(_label.FindChild(AUTO_COLOR_LABEL), (int)theAutoColor);
160 }
161
162 //=============================================================================
163 /*!
164  *  GetAutoColor
165  */
166 //=============================================================================
167 bool GEOM_Object::GetAutoColor()
168 {
169   Handle(TDataStd_Integer) anAutoColor;
170   if(!_label.FindChild(AUTO_COLOR_LABEL).FindAttribute(TDataStd_Integer::GetID(), anAutoColor)) return false;
171
172   return bool(anAutoColor->Get());
173 }
174
175 //=============================================================================
176 /*!
177  *  SetMarkerStd
178  */
179 //=============================================================================
180 void GEOM_Object::SetMarkerStd(const Aspect_TypeOfMarker theType, double theSize)
181 {
182   TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL);
183   TDataStd_Integer::Set(aMarkerLabel.FindChild(MARKER_LABEL_TYPE), (int)theType);
184   TDataStd_Real::Set(aMarkerLabel.FindChild(MARKER_LABEL_SIZE), theSize);
185 }
186   
187 //=============================================================================
188 /*!
189  *  SetMarkerTexture
190  */
191 //=============================================================================
192 void GEOM_Object::SetMarkerTexture(int theTextureId)
193 {
194   TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL);
195   TDataStd_Integer::Set(aMarkerLabel.FindChild(MARKER_LABEL_TYPE), (int)Aspect_TOM_USERDEFINED);
196   TDataStd_Integer::Set(aMarkerLabel.FindChild(MARKER_LABEL_ID),   theTextureId);
197 }
198
199 //=============================================================================
200 /*!
201  *  GetMarkerType
202  */
203 //=============================================================================
204 Aspect_TypeOfMarker GEOM_Object::GetMarkerType()
205 {
206   Standard_Integer aType = -1;
207   TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL, Standard_False);
208   if(!aMarkerLabel.IsNull()) {
209     TDF_Label aTypeLabel = aMarkerLabel.FindChild(MARKER_LABEL_TYPE, Standard_False);
210     Handle(TDataStd_Integer) aTypeAttr;
211     if (!aTypeLabel.IsNull() && aTypeLabel.FindAttribute(TDataStd_Integer::GetID(), aTypeAttr))
212       aType = aTypeAttr->Get();
213   }
214   return (Aspect_TypeOfMarker)aType;
215 }
216
217 //=============================================================================
218 /*!
219  *  GetMarkerSize
220  */
221 //=============================================================================
222 double GEOM_Object::GetMarkerSize()
223 {
224   Standard_Real aSize = 0.;
225   TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL, Standard_False);
226   if(!aMarkerLabel.IsNull()) {
227     TDF_Label aSizeLabel = aMarkerLabel.FindChild(MARKER_LABEL_SIZE, Standard_False);
228     Handle(TDataStd_Real) aSizeAttr;
229     if (!aSizeLabel.IsNull() && aSizeLabel.FindAttribute(TDataStd_Real::GetID(), aSizeAttr))
230       aSize = aSizeAttr->Get();
231   }
232   return aSize;
233 }
234
235 //=============================================================================
236 /*!
237  *  GetMarkerTexture
238  */
239 //=============================================================================
240 int GEOM_Object::GetMarkerTexture()
241 {
242   Standard_Integer anId = 0;
243   if ( GetMarkerType() == Aspect_TOM_USERDEFINED) {
244     TDF_Label aMarkerLabel = _label.FindChild(MARKER_LABEL, Standard_False);
245     if(!aMarkerLabel.IsNull()) {
246       TDF_Label aTypeLabel = aMarkerLabel.FindChild(MARKER_LABEL_ID, Standard_False);
247       Handle(TDataStd_Integer) anIdAttr;
248       if (!aTypeLabel.IsNull() && aTypeLabel.FindAttribute(TDataStd_Integer::GetID(), anIdAttr))
249         anId = anIdAttr->Get();
250     }
251   }
252   return anId;
253 }
254
255 //=============================================================================
256 /*!
257  *  SetAuxData
258  */
259 //=============================================================================
260 void GEOM_Object::UnsetMarker()
261 {
262   SetMarkerStd((Aspect_TypeOfMarker)-1, 0.);
263 }
264
265 //=============================================================================
266 /*!
267  *  IsSubShape
268  */
269 //=============================================================================
270 bool GEOM_Object::IsMainShape()
271 {
272   Handle(GEOM_Function) aFunction = GetFunction(1);
273   if(aFunction.IsNull() || aFunction->GetDriverGUID() != GetSubShapeID())
274     return true; // mkr : IPAL9921
275   return false;
276 }
277
278 //================================================================================
279 /*!
280  * \brief Returns GetLastFunction() of given objects
281  */
282 //================================================================================
283
284 Handle(TColStd_HSequenceOfTransient)
285 GEOM_Object::GetLastFunctions( const std::list< Handle(GEOM_Object) >& theObjects )
286 {
287   Handle(TColStd_HSequenceOfTransient) funs = new TColStd_HSequenceOfTransient;
288   std::list<Handle(GEOM_Object)>::const_iterator it = theObjects.begin();
289   for (; it != theObjects.end(); it++)
290   {
291     Handle(GEOM_Function) fun = (*it)->GetLastFunction();
292     if ( fun.IsNull())
293       return Handle(TColStd_HSequenceOfTransient)();
294     funs->Append( fun );
295   }
296   return funs;
297 }
298
299 IMPLEMENT_STANDARD_RTTIEXT(GEOM_Object, GEOM_BaseObject )
300