1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "GEOMImpl_IFieldOperations.hxx"
25 #include "GEOMImpl_Types.hxx"
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"
33 #include <TColStd_HArray1OfExtendedString.hxx>
34 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
36 #include <utilities.h>
39 //=============================================================================
43 //=============================================================================
44 GEOMImpl_IFieldOperations::GEOMImpl_IFieldOperations (GEOM_Engine* theEngine, int theDocID)
45 : GEOM_IOperations(theEngine, theDocID)
47 MESSAGE("GEOMImpl_IFieldOperations::GEOMImpl_IFieldOperations");
50 //=============================================================================
54 //=============================================================================
55 GEOMImpl_IFieldOperations::~GEOMImpl_IFieldOperations()
57 MESSAGE("GEOMImpl_IFieldOperations::~GEOMImpl_IFieldOperations");
61 //=============================================================================
65 //=============================================================================
66 Handle(GEOM_Field) GEOMImpl_IFieldOperations::
67 CreateField( const Handle(GEOM_Object)& theShape,
70 const int theDimension,
71 const Handle(TColStd_HArray1OfExtendedString)& theComponentNames)
76 if ( theShape.IsNull() ) {
77 SetErrorCode( "Error: NULL shape" );
80 if ( !theName || !theName[0] ) {
81 SetErrorCode( "Error: empty field name" );
84 if ( theType < 0 || theType > 3) {
85 SetErrorCode( "Error: invalid field type."
86 "Valid types are: 0 - boolean, 1 - integer, 2 - double, 3 - string");
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");
94 if ( theComponentNames.IsNull() || theComponentNames->Length() < 1 ) {
95 SetErrorCode( "Error: no component names provided");
100 Handle(GEOM_Field) aField = Handle(GEOM_Field)::DownCast
101 ( GetEngine()->AddBaseObject( GetDocID(), GEOM_FIELD ));
104 aField->Init( theShape, theName, theType, theDimension, theComponentNames );
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);
111 //make a Python command
112 GEOM::TPythonDump(aFieldFun) << aField << " = geompy.CreateField( "
115 << "GEOM.FDT_" << aField->GetDataTypeString( theType ) << ", "
116 << theDimension << ", "
117 << aField->GetComponentsForPython() << ")";
122 //=======================================================================
123 //function : CountFields
124 //purpose : Returns number of fields on a shape
125 //=======================================================================
127 int GEOMImpl_IFieldOperations::CountFields( const Handle(GEOM_Object)& theShape )
131 if ( theShape.IsNull() )
134 Handle(GEOM_Function) aShapeFun = theShape->GetFunction(1);
135 if ( aShapeFun.IsNull() || !aShapeFun->HasSubShapeReferences() )
138 const TDataStd_ListOfExtendedString& aListEntries = aShapeFun->GetSubShapeReferences();
139 if (aListEntries.IsEmpty()) return 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)) );
154 //=======================================================================
155 //function : GetFields
156 //purpose : Returns all fields on a shape
157 //=======================================================================
159 Handle(TColStd_HSequenceOfTransient)
160 GEOMImpl_IFieldOperations::GetFields( const Handle(GEOM_Object)& theShape )
164 Handle(TColStd_HSequenceOfTransient) fields = new TColStd_HSequenceOfTransient;
165 if ( theShape.IsNull() ) {
166 SetErrorCode( "Error: NULL shape" );
171 Handle(GEOM_Function) aShapeFun = theShape->GetFunction(1);
172 if ( aShapeFun.IsNull() || !aShapeFun->HasSubShapeReferences() )
175 const TDataStd_ListOfExtendedString& aListEntries = aShapeFun->GetSubShapeReferences();
176 if (aListEntries.IsEmpty()) return fields;
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)) )
186 Handle(GEOM_Field) field = Handle(GEOM_Field)::DownCast( anObj );
187 if ( !field.IsNull() )
188 fields->Append( field );
195 //=======================================================================
196 //function : GetField
197 //purpose : Returns a field on a shape by its name
198 //=======================================================================
201 GEOMImpl_IFieldOperations::GetField( const Handle(GEOM_Object)& theShape,
202 const char* theName )
206 Handle(GEOM_Field) field;
207 if ( theShape.IsNull() ) {
208 //SetErrorCode( "Error: NULL shape" );
211 Handle(GEOM_Function) aShapeFun = theShape->GetFunction(1);
212 if ( aShapeFun.IsNull() || !aShapeFun->HasSubShapeReferences() ) {
213 //SetErrorCode( "Error: No fields on the shape at all" );
216 const TDataStd_ListOfExtendedString& aListEntries = aShapeFun->GetSubShapeReferences();
217 if (aListEntries.IsEmpty()) {
218 //SetErrorCode( "Error: No fields on the shape at all" );
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 ) {