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