Salome HOME
7e817402fff0901fe1cf8b3d884da4c9b4f4ce57
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IFieldOperations.cxx
1 // Copyright (C) 2007-2014  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 "GEOMImpl_IFieldOperations.hxx"
24
25 #include "GEOMImpl_Types.hxx"
26
27 #include "GEOM_Field.hxx"
28 #include "GEOM_Function.hxx"
29 #include "GEOM_IField.hxx"
30 #include "GEOM_Object.hxx"
31 #include "GEOM_PythonDump.hxx"
32
33 #include <TColStd_HArray1OfExtendedString.hxx>
34 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
35
36 #include <utilities.h>
37
38
39 //=============================================================================
40 /*!
41  *   constructor:
42  */
43 //=============================================================================
44 GEOMImpl_IFieldOperations::GEOMImpl_IFieldOperations (GEOM_Engine* theEngine, int theDocID)
45 : GEOM_IOperations(theEngine, theDocID)
46 {
47   MESSAGE("GEOMImpl_IFieldOperations::GEOMImpl_IFieldOperations");
48 }
49
50 //=============================================================================
51 /*!
52  *  destructor
53  */
54 //=============================================================================
55 GEOMImpl_IFieldOperations::~GEOMImpl_IFieldOperations()
56 {
57   MESSAGE("GEOMImpl_IFieldOperations::~GEOMImpl_IFieldOperations");
58 }
59
60
61 //=============================================================================
62 /*!
63  *  CreateField
64  */
65 //=============================================================================
66 Handle(GEOM_Field) GEOMImpl_IFieldOperations::
67 CreateField( const Handle(GEOM_Object)&                     theShape,
68              const char*                                    theName,
69              const int                                      theType,
70              const int                                      theDimension,
71              const Handle(TColStd_HArray1OfExtendedString)& theComponentNames)
72 {
73   SetErrorCode(KO);
74
75   // check input data
76   if ( theShape.IsNull() ) {
77     SetErrorCode( "Error: NULL shape" );
78     return NULL;
79   }
80   if ( !theName || !theName[0] ) {
81     SetErrorCode( "Error: empty field name" );
82     return NULL;
83   }
84   if ( theType < 0 || theType > 3) {
85     SetErrorCode( "Error: invalid field type."
86                   "Valid types are: 0 - boolean, 1 - integer, 2 - double, 3 - string");
87     return NULL;
88   }
89   if ( theDimension < -1 || theDimension > 3) {
90     SetErrorCode( "Error: invalid shape dimension."
91                   "Valid types are: 0 - VERTEX, 1 - EDGE, 2 - FACE, 3 - SOLID, -1 - whole shape");
92     return NULL;
93   }
94   if ( theComponentNames.IsNull() || theComponentNames->Length() < 1 ) {
95     SetErrorCode( "Error: no component names provided");
96     return NULL;
97   }
98
99   // make a field
100   Handle(GEOM_Field) aField = Handle(GEOM_Field)::DownCast
101     ( GetEngine()->AddBaseObject( GetDocID(), GEOM_FIELD ));
102
103   // set field data
104   aField->Init( theShape, theName, theType, theDimension, theComponentNames );
105
106   // remember aField as a sub-object of theShape
107   Handle(GEOM_Function) aFieldFun = aField->GetFunction(1);
108   Handle(GEOM_Function) aShapeFun = theShape->GetFunction(1);
109   aShapeFun->AddSubShapeReference(aFieldFun);
110
111   //make a Python command
112   GEOM::TPythonDump(aFieldFun) << aField << " = geompy.CreateField( "
113                                << theShape << ", '"
114                                << theName << "', "
115                                << "GEOM.FDT_" << aField->GetDataTypeString( theType ) << ", "
116                                << theDimension << ", "
117                                << aField->GetComponentsForPython() << ")";
118   SetErrorCode(OK);
119   return aField;
120 }
121
122 //=======================================================================
123 //function : CountFields
124 //purpose  : Returns number of fields on a shape
125 //=======================================================================
126
127 int GEOMImpl_IFieldOperations::CountFields( const Handle(GEOM_Object)& theShape )
128 {
129   SetErrorCode(OK);
130
131   if ( theShape.IsNull() )
132     return 0;
133
134   Handle(GEOM_Function) aShapeFun = theShape->GetFunction(1);
135   if ( aShapeFun.IsNull() || !aShapeFun->HasSubShapeReferences() )
136     return 0;
137
138   const TDataStd_ListOfExtendedString& aListEntries = aShapeFun->GetSubShapeReferences();
139   if (aListEntries.IsEmpty()) return 0;
140
141   int nbFields = 0;
142   char entry[200], *pentry = entry; // the entry can't be too long
143   TDataStd_ListIteratorOfListOfExtendedString anIt (aListEntries);
144   for (; anIt.More(); anIt.Next()) {
145     TCollection_ExtendedString& anEntry = anIt.Value();
146     anEntry.ToUTF8CString( (Standard_PCharacter&) pentry );
147     Handle(GEOM_BaseObject) anObj = GetEngine()->GetObject(GetDocID(), entry, false);
148     nbFields += ( !anObj.IsNull() && anObj->IsKind(STANDARD_TYPE(GEOM_Field)) );
149   }
150
151   return nbFields;
152 }
153
154 //=======================================================================
155 //function : GetFields
156 //purpose  : Returns all fields on a shape
157 //=======================================================================
158
159 Handle(TColStd_HSequenceOfTransient)
160 GEOMImpl_IFieldOperations::GetFields( const Handle(GEOM_Object)& theShape )
161 {
162   SetErrorCode(KO);
163
164   Handle(TColStd_HSequenceOfTransient) fields = new TColStd_HSequenceOfTransient;
165   if ( theShape.IsNull() ) {
166     SetErrorCode( "Error: NULL shape" );
167     return fields;
168   }
169   SetErrorCode(OK);
170
171   Handle(GEOM_Function) aShapeFun = theShape->GetFunction(1);
172   if ( aShapeFun.IsNull() || !aShapeFun->HasSubShapeReferences() )
173     return fields;
174
175   const TDataStd_ListOfExtendedString& aListEntries = aShapeFun->GetSubShapeReferences();
176   if (aListEntries.IsEmpty()) return fields;
177
178   char entry[200], *pentry = entry; // the entry can't be too long
179   TDataStd_ListIteratorOfListOfExtendedString anIt (aListEntries);
180   for (; anIt.More(); anIt.Next()) {
181     TCollection_ExtendedString& anEntry = anIt.Value();
182     anEntry.ToUTF8CString( (Standard_PCharacter&) pentry );
183     Handle(GEOM_BaseObject) anObj = GetEngine()->GetObject(GetDocID(), entry, false);
184     if ( !anObj.IsNull() && anObj->IsKind(STANDARD_TYPE(GEOM_Field)) )
185     {
186         Handle(GEOM_Field) field = Handle(GEOM_Field)::DownCast( anObj );
187         if ( !field.IsNull() )
188           fields->Append( field );
189     }
190   }
191
192   return fields;
193 }
194
195 //=======================================================================
196 //function : GetField
197 //purpose  : Returns a field on a shape by its name
198 //=======================================================================
199
200 Handle(GEOM_Field)
201 GEOMImpl_IFieldOperations::GetField( const Handle(GEOM_Object)& theShape,
202                                      const char*                theName )
203 {
204   SetErrorCode(OK);
205
206   Handle(GEOM_Field) field;
207   if ( theShape.IsNull() ) {
208     //SetErrorCode( "Error: NULL shape" );
209     return field;
210   }
211   Handle(GEOM_Function) aShapeFun = theShape->GetFunction(1);
212   if ( aShapeFun.IsNull() || !aShapeFun->HasSubShapeReferences() ) {
213     //SetErrorCode( "Error: No fields on the shape at all" );
214     return field;
215   }
216   const TDataStd_ListOfExtendedString& aListEntries = aShapeFun->GetSubShapeReferences();
217   if (aListEntries.IsEmpty())  {
218     //SetErrorCode( "Error: No fields on the shape at all" );
219     return field;
220   }
221
222   char entry[200], *pentry = entry; // the entry can't be too long
223   TDataStd_ListIteratorOfListOfExtendedString anIt (aListEntries);
224   for (; anIt.More(); anIt.Next()) {
225     TCollection_ExtendedString& anEntry = anIt.Value();
226     anEntry.ToUTF8CString( (Standard_PCharacter&) pentry );
227     field = Handle(GEOM_Field)::DownCast( GetEngine()->GetObject( GetDocID(), entry, false ));
228     if ( !field.IsNull() && field->GetName() == theName ) {
229       SetErrorCode(OK);
230       break;
231     }
232   }
233
234   return field;
235 }