1 // Copyright (C) 2007-2016 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_Function.hxx"
28 #include "GEOM_IField.hxx"
29 #include "GEOM_Object.hxx"
30 #include "GEOM_PythonDump.hxx"
32 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
34 #include <utilities.h>
37 //=============================================================================
41 //=============================================================================
42 GEOMImpl_IFieldOperations::GEOMImpl_IFieldOperations (GEOM_Engine* theEngine, int theDocID)
43 : GEOM_IOperations(theEngine, theDocID)
45 MESSAGE("GEOMImpl_IFieldOperations::GEOMImpl_IFieldOperations");
48 //=============================================================================
52 //=============================================================================
53 GEOMImpl_IFieldOperations::~GEOMImpl_IFieldOperations()
55 MESSAGE("GEOMImpl_IFieldOperations::~GEOMImpl_IFieldOperations");
59 //=============================================================================
63 //=============================================================================
64 Handle(GEOM_Field) GEOMImpl_IFieldOperations::
65 CreateField( const Handle(GEOM_Object)& theShape,
68 const int theDimension,
69 const Handle(TColStd_HArray1OfExtendedString)& theComponentNames)
74 if ( theShape.IsNull() ) {
75 SetErrorCode( "Error: NULL shape" );
78 if ( !theName || !theName[0] ) {
79 SetErrorCode( "Error: empty field name" );
82 if ( theType < 0 || theType > 3) {
83 SetErrorCode( "Error: invalid field type."
84 "Valid types are: 0 - boolean, 1 - integer, 2 - double, 3 - string");
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");
92 if ( theComponentNames.IsNull() || theComponentNames->Length() < 1 ) {
93 SetErrorCode( "Error: no component names provided");
98 Handle(GEOM_Field) aField = Handle(GEOM_Field)::DownCast
99 ( GetEngine()->AddBaseObject( GetDocID(), GEOM_FIELD ));
102 aField->Init( theShape, theName, theType, theDimension, theComponentNames );
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);
109 //make a Python command
110 GEOM::TPythonDump(aFieldFun) << aField << " = geompy.CreateField( "
113 << "GEOM.FDT_" << aField->GetDataTypeString( theType ) << ", "
114 << theDimension << ", "
115 << aField->GetComponentsForPython() << ")";
120 //=======================================================================
121 //function : CountFields
122 //purpose : Returns number of fields on a shape
123 //=======================================================================
125 int GEOMImpl_IFieldOperations::CountFields( const Handle(GEOM_Object)& theShape )
129 if ( theShape.IsNull() )
132 Handle(GEOM_Function) aShapeFun = theShape->GetFunction(1);
133 if ( aShapeFun.IsNull() || !aShapeFun->HasSubShapeReferences() )
136 const TDataStd_ListOfExtendedString& aListEntries = aShapeFun->GetSubShapeReferences();
137 if (aListEntries.IsEmpty()) return 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(GetDocID(), entry, false);
146 nbFields += ( !anObj.IsNull() && anObj->IsKind(STANDARD_TYPE(GEOM_Field)) );
152 //=======================================================================
153 //function : GetFields
154 //purpose : Returns all fields on a shape
155 //=======================================================================
157 Handle(TColStd_HSequenceOfTransient)
158 GEOMImpl_IFieldOperations::GetFields( const Handle(GEOM_Object)& theShape )
162 Handle(TColStd_HSequenceOfTransient) fields = new TColStd_HSequenceOfTransient;
163 if ( theShape.IsNull() ) {
164 SetErrorCode( "Error: NULL shape" );
169 Handle(GEOM_Function) aShapeFun = theShape->GetFunction(1);
170 if ( aShapeFun.IsNull() || !aShapeFun->HasSubShapeReferences() )
173 const TDataStd_ListOfExtendedString& aListEntries = aShapeFun->GetSubShapeReferences();
174 if (aListEntries.IsEmpty()) return fields;
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(GetDocID(), entry, false);
182 if ( !anObj.IsNull() && anObj->IsKind(STANDARD_TYPE(GEOM_Field)) )
184 Handle(GEOM_Field) field = Handle(GEOM_Field)::DownCast( anObj );
185 if ( !field.IsNull() )
186 fields->Append( field );
193 //=======================================================================
194 //function : GetField
195 //purpose : Returns a field on a shape by its name
196 //=======================================================================
199 GEOMImpl_IFieldOperations::GetField( const Handle(GEOM_Object)& theShape,
200 const char* theName )
204 Handle(GEOM_Field) field;
205 if ( theShape.IsNull() ) {
206 //SetErrorCode( "Error: NULL shape" );
209 Handle(GEOM_Function) aShapeFun = theShape->GetFunction(1);
210 if ( aShapeFun.IsNull() || !aShapeFun->HasSubShapeReferences() ) {
211 //SetErrorCode( "Error: No fields on the shape at all" );
214 const TDataStd_ListOfExtendedString& aListEntries = aShapeFun->GetSubShapeReferences();
215 if (aListEntries.IsEmpty()) {
216 //SetErrorCode( "Error: No fields on the shape at all" );
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( GetDocID(), entry, false ));
226 if ( !field.IsNull() && field->GetName() == theName ) {