Salome HOME
Update copyright: 2016
[modules/geom.git] / src / GEOM / GEOM_BaseDriver.cxx
1 // Copyright (C) 2007-2016  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 // File      : GEOM_BaseDriver.cxx
24 // Created   : Thu Jun  6 15:44:27 2013
25 // Author    : Edward AGAPOV (eap)
26
27 #include "GEOM_BaseDriver.hxx"
28
29 #include "GEOM_Function.hxx"
30 #include "GEOM_Object.hxx"
31 #include "GEOM_Engine.hxx"
32
33 #include <TColStd_HArray1OfInteger.hxx>
34 #include <TDataStd_Name.hxx>
35 #include <TDocStd_Owner.hxx>
36
37 IMPLEMENT_STANDARD_HANDLE (GEOM_BaseDriver,TFunction_Driver);
38 IMPLEMENT_STANDARD_RTTIEXT(GEOM_BaseDriver,TFunction_Driver);
39
40 //================================================================================
41 /*!
42  * Returns document id
43  */
44 //================================================================================
45 int GEOM_BaseDriver::GetDocID() const
46 {
47   int docId = 0;
48   if (!Label().IsNull()) {
49     Handle(TDocStd_Document) aDoc = TDocStd_Owner::GetDocument(Label().Data());
50     docId = GEOM_Engine::GetEngine()->GetDocID(aDoc);
51   }
52   return docId;
53 }
54
55 //================================================================================
56 /*!
57  * \brief Returns a name of creation operation and names and values of creation parameters
58  */
59 //================================================================================
60
61 // bool GEOM_BaseDriver::GetCreationInformation(std::string&             theOperationName,
62 //                                              std::vector<GEOM_Param>& theParams) = 0;
63
64 //================================================================================
65 /*!
66  * \brief Adds GEOM_Param to params and sets its name
67  *
68  * This method is safer than resizing the params vector and accessing to its items
69  */
70 //================================================================================
71
72 GEOM_Param& GEOM_BaseDriver::AddParam(std::vector<GEOM_Param>& params,
73                                       const char*              name)
74 {
75   GEOM_Param p;
76   p.Set( name );
77   params.push_back( p );
78   return params.back();
79 }
80
81 //================================================================================
82 /*!
83  * \brief Appends a GEOM_Function value
84  */
85 //================================================================================
86
87 GEOM_Param & GEOM_Param::operator<<( const Handle(Standard_Transient)& fun )
88 {
89   return *this << Handle(GEOM_Function)::DownCast( fun );
90 }
91
92 //================================================================================
93 /*!
94  * \brief Appends a GEOM_Function value
95  */
96 //================================================================================
97
98 GEOM_Param & GEOM_Param::operator<<( const Handle(GEOM_Function)& fun )
99 {
100   if ( !fun.IsNull() )
101   {
102     TDF_Label label = fun->GetOwnerEntry();
103     Handle(GEOM_Object) obj = GEOM_Object::GetObject( label );
104     if ( !obj.IsNull() )
105     {
106       TopoDS_Shape s = obj->GetValue();
107       (*this) << ( s.IsNull() ? TopAbs_SHAPE : s.ShapeType() );
108
109       TCollection_AsciiString entry = obj->GetAuxData();
110       TCollection_ExtendedString name;
111       {
112         Handle(TDataStd_Name) aNameAttr;
113         if( obj->GetEntry().FindAttribute(TDataStd_Name::GetID(), aNameAttr))
114           name = aNameAttr->Get();
115       }
116       if ( name.Length() > 0 && entry.Length() > 0 )
117         (*this) << "('" <<  name << "'," << entry << ")";
118       else if ( name.Length() > 0 )
119         (*this) << "('" << name << "')";
120       else if ( entry.Length() > 0 )
121         (*this) << "(" << entry << ")";
122     }
123   }
124   return *this;
125 }
126
127 //================================================================================
128 /*!
129  * \brief Appends several GEOM_Function's to the value
130  */
131 //================================================================================
132
133 GEOM_Param & GEOM_Param::operator<<( const Handle(TColStd_HSequenceOfTransient)& funs )
134 {
135   if ( !funs.IsNull() )
136   {
137     if ( funs->Length() > 1 )
138       (*this) << funs->Length() << " objects: ";
139     for ( int i = 1; i <= funs->Length(); ++i )
140       (*this) << funs->Value( i ) << " ";
141   }
142   return *this;
143 }
144
145 template <class HSEQ> void appendSeq( GEOM_Param& param,
146                                       const HSEQ& seq,
147                                       int         iLow,
148                                       int         iUp)
149 {
150   int len = 1 + iUp - iLow;
151   if ( len > 1 )
152     param << len << " items: ";
153   for ( ; iLow <= iUp; ++iLow )
154     param << seq->Value( iLow ) << " ";
155 }
156
157 //================================================================================
158 /*!
159  * \brief Appends several int's to the value
160  */
161 //================================================================================
162
163 GEOM_Param & GEOM_Param::operator<<( const Handle(TColStd_HArray1OfInteger)& vals )
164 {
165   if ( !vals.IsNull() )
166     appendSeq( *this, vals, vals->Lower(), vals->Upper() );
167   return *this;
168 }
169
170 //================================================================================
171 /*!
172  * \brief Appends TopAbs_ShapeEnum to the value
173  */
174 //================================================================================
175
176 GEOM_Param & GEOM_Param::operator<<( TopAbs_ShapeEnum type )
177 {
178   const char* str[] = {
179     "Compound","Compsolid","Solid","Shell","Face","Wire","Edge","Vertex","Shape"
180   };
181   if ( 0 <= type && type <= TopAbs_SHAPE )
182     (*this) << str[type];
183   else
184     (*this) << "TopAbs_ShapeEnum(" << type << ")";
185   return *this;
186 }
187
188 //================================================================================
189 /*!
190  * \brief Appends TopAbs_State to the value
191  */
192 //================================================================================
193
194 GEOM_Param & GEOM_Param::operator<<( TopAbs_State state )
195 {
196   const char* str[] = {"IN","OUT","ON","UNKNOWN"};
197   if ( 0 <= state && state <= TopAbs_UNKNOWN )
198     (*this) << str[state];
199   else
200     (*this) << "TopAbs_State(" << state << ")";
201   return *this;
202 }