]> SALOME platform Git repositories - modules/geom.git/blob - src/GEOMImpl/GEOMImpl_IInsertOperations.cxx
Salome HOME
Remove obsolete OCC_VERSION_LARGE defines.
[modules/geom.git] / src / GEOMImpl / GEOMImpl_IInsertOperations.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 <Standard_Stream.hxx>
24
25 #include <GEOMImpl_IInsertOperations.hxx>
26
27 #include <GEOMImpl_CopyDriver.hxx>
28 #include <GEOMImpl_ExportDriver.hxx>
29 #include <GEOMImpl_ImportDriver.hxx>
30 #include <GEOMImpl_ICopy.hxx>
31 #include <GEOMImpl_IImportExport.hxx>
32 #include <GEOMImpl_Types.hxx>
33 #include "GEOMImpl_IShapesOperations.hxx"
34 #include "GEOMImpl_IGroupOperations.hxx"
35 #include "GEOMImpl_IFieldOperations.hxx"
36 #include "GEOMImpl_XAODriver.hxx"
37 #include "GEOMImpl_IImportExportXAO.hxx"
38
39 #include <GEOM_Function.hxx>
40 #include <GEOM_PythonDump.hxx>
41 #include "GEOM_ISubShape.hxx"
42
43 #include <XAO_Xao.hxx>
44 #include <XAO_Geometry.hxx>
45 #include <XAO_BrepGeometry.hxx>
46 #include <XAO_Group.hxx>
47 #include <XAO_Field.hxx>
48 #include <XAO_XaoUtils.hxx>
49 #include <XAO_BooleanField.hxx>
50 #include <XAO_IntegerField.hxx>
51 #include <XAO_DoubleField.hxx>
52 #include <XAO_StringField.hxx>
53 #include <XAO_BooleanStep.hxx>
54 #include <XAO_IntegerStep.hxx>
55 #include <XAO_DoubleStep.hxx>
56 #include <XAO_StringStep.hxx>
57
58 #include <Basics_OCCTVersion.hxx>
59
60 #include "utilities.h"
61 #include <OpUtil.hxx>
62 #include <Utils_ExceptHandlers.hxx>
63
64 #include <TFunction_DriverTable.hxx>
65 #include <TFunction_Driver.hxx>
66 #include <TFunction_Logbook.hxx>
67 #include <TDF_ChildIDIterator.hxx>
68 #include <TDF_Tool.hxx>
69 #include <TDataStd_Integer.hxx>
70 #include <TNaming_NamedShape.hxx>
71 #include <TDataStd_Comment.hxx>
72 #include <TopTools_IndexedMapOfShape.hxx>
73 #include <TopExp.hxx>
74
75 #include <TopoDS.hxx>
76 #include <TopoDS_Vertex.hxx>
77 #include <BRep_Builder.hxx>
78 #include <BRep_Tool.hxx>
79 #include <BRepTools.hxx>
80 #include <gp_Pnt.hxx>
81
82 #include <TColStd_HArray1OfByte.hxx>
83 #include <TColStd_HArray1OfReal.hxx>
84
85 #include <Standard_Failure.hxx>
86 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
87
88 /**
89  * This function returns the input format name from the original format name.
90  */
91 static TCollection_AsciiString GetImportFormatName
92         (const TCollection_AsciiString& theFormatName)
93 {
94   return theFormatName.Token("_");
95 }
96
97 //=============================================================================
98 /*!
99  *  constructor
100  */
101 //=============================================================================
102 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
103 : GEOM_IOperations(theEngine, theDocID)
104 {
105   MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
106   myShapesOperations = new GEOMImpl_IShapesOperations(GetEngine(), GetDocID());
107   myGroupOperations = new GEOMImpl_IGroupOperations(GetEngine(), GetDocID());
108   myFieldOperations = new GEOMImpl_IFieldOperations(GetEngine(), GetDocID());
109 }
110
111 //=============================================================================
112 /*!
113  *  destructor
114  */
115 //=============================================================================
116 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
117 {
118   MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
119   delete myShapesOperations;
120   delete myGroupOperations;
121   delete myFieldOperations;
122 }
123
124 //=============================================================================
125 /*!
126  *  MakeCopy
127  */
128 //=============================================================================
129 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
130 {
131   SetErrorCode(KO);
132
133   if (theOriginal.IsNull()) return NULL;
134
135   //Add a new Copy object
136   Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
137
138   //Add a Copy function for creation a copy object
139   Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
140
141   //Check if the function is set correctly
142   if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
143
144   GEOMImpl_ICopy aCI(aFunction);
145
146   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
147   if (aRefFunction.IsNull()) return NULL;
148
149   aCI.SetOriginal(aRefFunction);
150
151   //Compute the Copy value
152   try {
153     OCC_CATCH_SIGNALS;
154     if (!GetSolver()->ComputeFunction(aFunction)) {
155       SetErrorCode("Copy driver failed");
156       return NULL;
157     }
158   }
159   catch (Standard_Failure) {
160     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
161     SetErrorCode(aFail->GetMessageString());
162     return NULL;
163   }
164
165   //Make a Python command
166   GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
167
168   SetErrorCode(OK);
169   return aCopy;
170 }
171
172 //=============================================================================
173 /*!
174  *  Export
175  */
176 //=============================================================================
177 void GEOMImpl_IInsertOperations::Export
178                      (const Handle(GEOM_Object)      theOriginal,
179                       const TCollection_AsciiString& theFileName,
180                       const TCollection_AsciiString& theFormatName)
181 {
182   SetErrorCode(KO);
183
184   if (theOriginal.IsNull()) return;
185
186   Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
187   if (aRefFunction.IsNull()) return;  //There is no function which creates an object to be exported
188
189   //Add a new result object
190   Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
191
192   //Add an Export function
193   Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
194   if (aFunction.IsNull()) return;
195
196   //Check if the function is set correctly
197   if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
198
199   Handle(TCollection_HAsciiString) aHLibName;
200   if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
201     return;
202   }
203   TCollection_AsciiString aLibName = aHLibName->String();
204
205   //Set parameters
206   GEOMImpl_IImportExport aCI (aFunction);
207   aCI.SetOriginal(aRefFunction);
208   aCI.SetFileName(theFileName);
209   aCI.SetFormatName(theFormatName);
210   aCI.SetPluginName(aLibName);
211
212   //Perform the Export
213   try {
214     OCC_CATCH_SIGNALS;
215     if (!GetSolver()->ComputeFunction(aFunction)) {
216       SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
217       return;
218     }
219   }
220   catch (Standard_Failure) {
221     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
222     SetErrorCode(aFail->GetMessageString());
223     return;
224   }
225
226   //Make a Python command
227   GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
228     << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
229
230   SetErrorCode(OK);
231 }
232
233 //=============================================================================
234 /*!
235  *  Import
236  */
237 //=============================================================================
238 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IInsertOperations::Import
239                                  (const TCollection_AsciiString& theFileName,
240                                   const TCollection_AsciiString& theFormatName)
241 {
242   SetErrorCode(KO);
243
244   if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
245
246   //Add a new result object
247   Handle(GEOM_Object) anImported = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
248
249   //Add an Import function
250   Handle(GEOM_Function) aFunction =
251     anImported->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
252
253   if (aFunction.IsNull()) return NULL;
254
255   //Check if the function is set correctly
256   if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return NULL;
257
258   Handle(TCollection_HAsciiString) aHLibName;
259   if (!IsSupported
260           (Standard_True, GetImportFormatName(theFormatName), aHLibName)) {
261     return NULL;
262   }
263   TCollection_AsciiString aLibName = aHLibName->String();
264
265   //Set parameters
266   GEOMImpl_IImportExport aCI (aFunction);
267   aCI.SetFileName(theFileName);
268   aCI.SetFormatName(theFormatName);
269   aCI.SetPluginName(aLibName);
270
271   //Perform the Import
272   Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
273
274   try {
275     OCC_CATCH_SIGNALS;
276     if (!GetSolver()->ComputeFunction(aFunction)) {
277       SetErrorCode("Import driver failed");
278       return NULL;
279     }
280
281     aSeq->Append(anImported);
282
283     // Greate material groups.
284     MakeMaterialGroups(anImported, aSeq);
285   }
286   catch (Standard_Failure) {
287     Handle(Standard_Failure) aFail = Standard_Failure::Caught();
288     SetErrorCode(aFail->GetMessageString());
289     return NULL;
290   }
291
292   //Make a Python command
293   if (theFormatName != "IGES_UNIT") {
294     GEOM::TPythonDump pd (aFunction);
295     if (theFormatName == "BREP")
296       pd << aSeq << " = geompy.ImportBREP(\"" << theFileName.ToCString() << "\")";
297     else if (theFormatName == "IGES")
298       pd << aSeq << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\")";
299     else if (theFormatName == "IGES_SCALE")
300       pd << aSeq << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\", True)";
301     else if (theFormatName == "STEP")
302       pd << aSeq << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\")";
303     else if (theFormatName == "STEP_SCALE")
304       pd << aSeq << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", True)";
305     else {
306       pd << aSeq << " = geompy.ImportFile(\""
307          << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
308     }
309   }
310
311   SetErrorCode(OK);
312
313   // OLD CODE: begin
314   if (theFormatName == "IGES_UNIT") {
315     TopoDS_Shape S = aFunction->GetValue();
316     TopoDS_Vertex V = TopoDS::Vertex(S);
317     gp_Pnt P = BRep_Tool::Pnt(V);
318     double scale = P.X();
319     TCollection_AsciiString aUnitName = "UNIT_M";
320     if (fabs(scale-0.01) < 1.e-6)
321       aUnitName = "UNIT_CM";
322     else if (fabs(scale-0.001) < 1.e-6)
323       aUnitName = "UNIT_MM";
324     //cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
325     SetErrorCode(aUnitName);
326   }
327   // OLD CODE: end
328
329   return aSeq;
330 }
331
332 //=============================================================================
333 /*!
334  *  ReadValue
335  */
336 //=============================================================================
337 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
338                                  (const TCollection_AsciiString& theFileName,
339                                   const TCollection_AsciiString& theFormatName,
340                                   const TCollection_AsciiString& theParameterName)
341 {
342   SetErrorCode(KO);
343
344   TCollection_AsciiString aValue, anError;
345
346   if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
347
348   Handle(TCollection_HAsciiString) aHLibName;
349   if (!IsSupported
350           (Standard_True, GetImportFormatName(theFormatName), aHLibName)) {
351     return aValue;
352   }
353   TCollection_AsciiString aLibName = aHLibName->String();
354
355   aValue = GEOMImpl_ImportDriver::ReadValue(theFileName, aLibName, theParameterName, anError);
356   if (anError.IsEmpty())
357     SetErrorCode(OK);
358   else
359     SetErrorCode(anError.ToCString());
360
361   return aValue;
362 }
363
364 //=============================================================================
365 /*!
366  *  ImportTranslators
367  */
368 //=============================================================================
369 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
370                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
371                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
372 {
373   if (theFormats.IsNull())
374     theFormats = new TColStd_HSequenceOfAsciiString;
375   else
376     theFormats->Clear();
377
378   if (thePatterns.IsNull())
379     thePatterns = new TColStd_HSequenceOfAsciiString;
380   else
381     thePatterns->Clear();
382
383   if (!InitResMgr()) return Standard_False;
384
385   // Read Import formats from directories
386   Handle(Resource_Manager) aResMgr;
387   Handle(TColStd_HSequenceOfAsciiString) aFormatsToAdd;
388   for(int index = 0; index < myResMgrList.size(); index++) {
389     int anOldLen = theFormats->Length();
390     aResMgr = myResMgrList.at(index);
391     if (aResMgr->Find("Import")) {
392       TCollection_AsciiString aFormats (aResMgr->Value("Import"));
393       TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
394       for (int i = 1; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
395         int aLenFormats = theFormats->Length();
396         bool isFound = false;
397         for(int aInd=1;aInd<=aLenFormats;aInd++){
398           if( theFormats->Value(aInd) == aToken ){
399             isFound = true;
400             break;
401           }
402         }
403         if(!isFound)
404           theFormats->Append(aToken);
405       }
406     }
407
408     // Read Patterns for each supported format
409     for (int j = anOldLen+1; j <= theFormats->Length(); j++) {
410       TCollection_AsciiString aKey, aPattern;
411       aKey = theFormats->Value(j) + ".ImportPattern";
412       if (aResMgr->Find(aKey.ToCString()))
413         aPattern = aResMgr->Value(aKey.ToCString());
414       else {
415         aKey = theFormats->Value(j) + ".Pattern";
416         if (aResMgr->Find(aKey.ToCString()))
417           aPattern = aResMgr->Value(aKey.ToCString());
418         else {
419           aPattern = theFormats->Value(j);
420           aPattern += " Files ( *.* )";
421         }
422       }
423       thePatterns->Append(aPattern);
424     }
425   }
426
427   return (!theFormats->IsEmpty());
428 }
429
430 //=============================================================================
431 /*!
432  *  ExportTranslators
433  */
434 //=============================================================================
435 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
436                      (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
437                       Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
438 {
439   if (theFormats.IsNull())
440     theFormats = new TColStd_HSequenceOfAsciiString;
441   else
442     theFormats->Clear();
443
444   if (thePatterns.IsNull())
445     thePatterns = new TColStd_HSequenceOfAsciiString;
446   else
447     thePatterns->Clear();
448
449   if (!InitResMgr()) return Standard_False;
450
451   // Read Export formats list from directories
452   Handle(Resource_Manager) aResMgr;
453   for(int index=0; index < myResMgrList.size(); index++) {
454     int anOldLen = theFormats->Length();
455     aResMgr = myResMgrList.at(index);
456     if (aResMgr->Find("Export")) {
457       TCollection_AsciiString aFormats (aResMgr->Value("Export"));
458       TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
459       for (int i = 1; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
460         int aLenFormats = theFormats->Length();
461         bool isFound = false;
462         for(int aInd=1;aInd<=aLenFormats;aInd++){
463           if( theFormats->Value(aInd) == aToken){
464             isFound = true;
465             break;
466           }
467         }
468         if(!isFound)
469           theFormats->Append(aToken);
470       }
471     }
472
473     // Read Patterns for each supported format
474     for (int j = anOldLen+1; j <= theFormats->Length(); j++) {
475       TCollection_AsciiString aKey, aPattern;
476       aKey = theFormats->Value(j) + ".ExportPattern";
477       if (aResMgr->Find(aKey.ToCString()))
478         aPattern = aResMgr->Value(aKey.ToCString());
479       else {
480         aKey = theFormats->Value(j) + ".Pattern";
481         if (aResMgr->Find(aKey.ToCString()))
482           aPattern = aResMgr->Value(aKey.ToCString());
483         else {
484           aPattern = theFormats->Value(j);
485           aPattern += " Files ( *.* )";
486         }
487       }
488       thePatterns->Append(aPattern);
489     }
490   }
491
492   return (!theFormats->IsEmpty());
493 }
494
495 //=============================================================================
496 /*!
497  *  IsSupported
498  */
499 //=============================================================================
500 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
501                             (const Standard_Boolean isImport,
502                              const TCollection_AsciiString& theFormat,
503                              Handle(TCollection_HAsciiString)& theLibName)
504 {
505   if (!InitResMgr()) return Standard_False;
506
507   // Import/Export mode
508   TCollection_AsciiString aMode;
509   //Standard_CString aMode;
510   if (isImport) aMode = "Import";
511   else aMode = "Export";
512
513   // Read supported formats for the certain mode from user directory
514   Handle(Resource_Manager) aResMgr;
515   for(int index=0; index < myResMgrList.size(); index++) {
516     aResMgr = myResMgrList.at(index);
517     if (aResMgr->Find(aMode.ToCString())) {
518       TCollection_AsciiString aFormats (aResMgr->Value(aMode.ToCString()));
519       if (aFormats.Search(theFormat) > -1) {
520         // Read library name for the supported format
521         TCollection_AsciiString aKey (theFormat);
522         aKey += ".";
523         aKey += aMode;
524         if (aResMgr->Find(aKey.ToCString())) {
525           TCollection_AsciiString aLibName (aResMgr->Value(aKey.ToCString()));
526   #ifndef WIN32
527           if ( aLibName.Length() > 3 && aLibName.SubString(1,3) != "lib" )
528             aLibName.Prepend("lib");
529           aLibName += ".so";
530   #else
531           aLibName += ".dll";
532   #endif
533           theLibName = new TCollection_HAsciiString (aLibName);
534           return Standard_True;
535         }
536       }
537     }
538   }
539   
540   return Standard_False;
541 }
542
543 //=============================================================================
544 /*!
545  *  InitResMgr
546  */
547 //=============================================================================
548 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
549 {
550   bool isResourceFound = false;
551   TCollection_AsciiString aNull;
552   
553   myResMgrList.clear();
554
555   // Initialize the GEOM Resource Manager
556   TCollection_AsciiString aResDir;
557   aResDir = getenv("GEOM_ROOT_DIR");
558 #ifdef WIN32
559   aResDir += "\\share\\salome\\resources\\geom";
560 #else
561   aResDir += "/share/salome/resources/geom";
562 #endif
563   Handle(Resource_Manager) aGeomResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
564   if ( aGeomResMgr->Find("Import") || aGeomResMgr->Find("Export") ) {
565     myResMgrList.push_back( aGeomResMgr );
566     isResourceFound = true;
567   }
568
569   // Initialize the user's Resource Manager
570   TCollection_AsciiString aResDirsStr;
571   aResDirsStr = getenv("GEOM_ENGINE_RESOURCES_DIR");
572   if ( !aResDirsStr.IsEmpty() )
573   {
574     std::string aSep = ":";
575 #ifdef WIN32
576     aSep = ";";
577 #endif
578     aResDir = aResDirsStr.Token(aSep.c_str(), 1);
579     for (int i = 1; !aResDir.IsEmpty(); aResDir = aResDirsStr.Token(aSep.c_str(), ++i)) {
580       Handle(Resource_Manager) anUserResMgr = new Resource_Manager ("ImportExport", aNull, aResDir, Standard_False);
581       if (anUserResMgr->Find("Import") || anUserResMgr->Find("Export")) {
582         myResMgrList.push_back( anUserResMgr );
583         isResourceFound = true;
584       }
585     }
586   }
587   else
588   {
589     aResDir = getenv("HOME");
590 #ifdef WIN32
591     aResDir += "\\.config\\salome";
592 #else
593     aResDir += "/.config/salome";
594 #endif
595     Handle(Resource_Manager) anUserResMgr = new Resource_Manager ("ImportExport", aNull, aResDir, Standard_False);
596     if (anUserResMgr->Find("Import") || anUserResMgr->Find("Export")) {
597       myResMgrList.push_back( anUserResMgr );
598       isResourceFound = true;
599     }
600   }
601   return isResourceFound;
602 }
603
604 //=============================================================================
605 /*!
606  *  RestoreShape
607  */
608 //=============================================================================
609 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
610 {
611   SetErrorCode(KO);
612
613   //Add a new result object
614   Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
615
616   //Add a Copy function
617   Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
618   if (aFunction.IsNull()) return NULL;
619
620   //Check if the function is set correctly
621   if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
622
623   //Read a shape from the stream
624   TopoDS_Shape aShape;
625   BRep_Builder B;
626   BRepTools::Read(aShape, theStream, B);
627   if (aShape.IsNull()) {
628     SetErrorCode("RestoreShape error: BREP reading failed");
629   }
630
631   //Set function value
632   aFunction->SetValue(aShape);
633
634   //Special dump to avoid restored shapes publication.
635   //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
636   //GEOM::TPythonDump(aFunction) << "#";
637
638   GEOM::TPythonDump(aFunction) << result
639     << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
640
641   SetErrorCode(OK);
642
643   return result;
644 }
645
646 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
647 {
648   SetErrorCode(KO);
649
650   if (theTextureFile.IsEmpty()) return 0;
651
652   Handle(TColStd_HArray1OfByte) aTexture;
653
654   FILE* fp = fopen(theTextureFile.ToCString(), "r");
655   if (!fp) return 0;
656
657   std::list<std::string> lines;
658   char buffer[4096];
659   int maxlen = 0;
660   while (!feof(fp)) {
661     if ((fgets(buffer, 4096, fp)) == NULL) break;
662     int aLen = strlen(buffer);
663     if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
664     lines.push_back(buffer);
665     maxlen = std::max(maxlen, (int)strlen(buffer));
666   }
667
668   fclose(fp);
669
670   int lenbytes = maxlen/8;
671   if (maxlen%8) lenbytes++;
672
673   if (lenbytes == 0 || lines.empty())
674     return 0;
675
676   std::list<unsigned char> bytedata;
677   std::list<std::string>::const_iterator it;
678   for (it = lines.begin(); it != lines.end(); ++it) {
679     std::string line = *it;
680     int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
681     for (int i = 0; i < lenline/8; i++) {
682       unsigned char byte = 0;
683       for (int j = 0; j < 8; j++)
684         byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
685       bytedata.push_back(byte);
686     }
687     for (int i = lenline/8; i < lenbytes; i++)
688       bytedata.push_back((unsigned char)0);
689   }
690
691   if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
692     return 0;
693
694   aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
695
696   std::list<unsigned char>::iterator bdit;
697   int i;
698   for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
699     aTexture->SetValue(i, (Standard_Byte)(*bdit));
700
701   int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
702   if (aTextureId > 0) SetErrorCode(OK);
703   return aTextureId;
704 }
705   
706 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight, 
707                                            const Handle(TColStd_HArray1OfByte)& theTexture)
708 {
709   SetErrorCode(KO);
710   int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
711   if (aTextureId > 0) SetErrorCode(OK);
712   return aTextureId;
713 }
714
715 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
716                                                                      int& theWidth, int& theHeight)
717 {
718   SetErrorCode(KO);
719   
720   Handle(TColStd_HArray1OfByte) aTexture;
721
722   theWidth = theHeight = 0;
723   TCollection_AsciiString aFileName;
724
725   if (theTextureId <= 0)
726     return aTexture;
727
728   aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
729
730   if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
731
732   return aTexture;
733 }
734
735 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
736 {
737   SetErrorCode(KO);
738   std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
739   SetErrorCode(OK);
740   return id_list;
741 }
742
743 TopAbs_ShapeEnum getGroupDimension(XAO::Group* group)
744 {
745   XAO::Dimension dim = group->getDimension();
746   TopAbs_ShapeEnum rdim;
747   switch ( dim )
748   {
749   case XAO::VERTEX:
750     rdim = TopAbs_VERTEX; break;
751   case XAO::EDGE:
752     rdim = TopAbs_EDGE; break;
753   case XAO::FACE:
754     rdim = TopAbs_FACE; break;
755   case XAO::SOLID:
756     rdim = TopAbs_SOLID; break;
757   default:
758     rdim = TopAbs_COMPOUND; break;
759   }
760   return rdim;
761 }
762
763 XAO::Dimension shapeEnumToDimension(const TopAbs_ShapeEnum& shape)
764 {
765   XAO::Dimension dim;
766   switch( shape ) {
767   case TopAbs_VERTEX:
768     dim = XAO::VERTEX; break;
769   case TopAbs_EDGE:
770     dim = XAO::EDGE; break;
771   case TopAbs_FACE:
772     dim = XAO::FACE; break;
773   case TopAbs_SOLID:
774     dim = XAO::SOLID; break;
775   default:
776     throw SALOME_Exception("Bad type"); // TODO
777   }
778   return dim;
779 }
780
781 void GEOMImpl_IInsertOperations::exportGroups(std::list<Handle(GEOM_Object)> groupList,
782                                               XAO::Xao* xaoObject,
783                                               XAO::BrepGeometry* geometry)
784 {
785   // add the groups
786   std::list<Handle(GEOM_Object)>::iterator groupIterator = groupList.begin();
787   while (groupIterator != groupList.end())
788   {
789     Handle(GEOM_Object) currGroup = (*groupIterator++);
790     Handle(TColStd_HArray1OfInteger) groupIds = myGroupOperations->GetObjects(currGroup);
791     
792     TopAbs_ShapeEnum shapeGroup = myGroupOperations->GetType(currGroup);
793     XAO::Dimension dim = shapeEnumToDimension(shapeGroup);
794     XAO::Group* group = xaoObject->addGroup(dim, currGroup->GetName().ToCString());
795     
796     switch (shapeGroup)
797     {
798     case TopAbs_VERTEX:
799       for (int i = 1; i <= groupIds->Length(); i++)
800       {
801         std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
802         int index = geometry->getVertexIndexByReference(ref);
803         group->add(index);
804       }
805       break;
806     case TopAbs_EDGE:
807       for (int i = 1; i <= groupIds->Length(); i++)
808       {
809         std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
810         int index = geometry->getEdgeIndexByReference(ref);
811         group->add(index);
812       }
813       break;
814     case TopAbs_FACE:
815       for (int i = 1; i <= groupIds->Length(); i++)
816       {
817         std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
818         int index = geometry->getFaceIndexByReference(ref);
819         group->add(index);
820       }
821       break;
822     case TopAbs_SOLID:
823       for (int i = 1; i <= groupIds->Length(); i++)
824       {
825         std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
826         int index = geometry->getSolidIndexByReference(ref);
827         group->add(index);
828       }
829       break;
830     }
831   }
832 }
833
834 void GEOMImpl_IInsertOperations::exportFields(std::list<Handle(GEOM_Field)> fieldList,
835                                               XAO::Xao* xaoObject,
836                                               XAO::BrepGeometry* geometry)
837 {
838     std::list<Handle(GEOM_Field)>::iterator fieldIterator = fieldList.begin();
839     while (fieldIterator != fieldList.end())
840     {
841         Handle(GEOM_Field) currField = (*fieldIterator++);
842
843         int fdim = currField->GetDimension();
844         int ftype = currField->GetDataType();
845         int nbComponents = currField->GetNbComponents();
846         std::string name = currField->GetName().ToCString();
847
848         XAO::Field* field = xaoObject->addField((XAO::Type)ftype, (XAO::Dimension)fdim, nbComponents, name);
849
850         Handle(TColStd_HArray1OfExtendedString) components = currField->GetComponents();
851         for (int i = components->Lower(), j = 0; i <= components->Upper(); ++i, ++j)
852         {
853             field->setComponentName(j, TCollection_AsciiString(components->Value(i)).ToCString());
854         }
855
856         std::list< Handle(GEOM_FieldStep)> steps = currField->GetSteps();
857         std::list<Handle(GEOM_FieldStep)>::iterator stepIterator = steps.begin();
858         while (stepIterator != steps.end())
859         {
860             Handle(GEOM_FieldStep) currStep = (*stepIterator++);
861
862             XAO::Step* step = field->addNewStep(currStep->GetID());
863             step->setStamp(currStep->GetStamp());
864
865             switch (ftype)
866             {
867                 case 0: // bool
868                 {
869                     XAO::BooleanStep* bs = (XAO::BooleanStep*)step;
870                     Handle(TColStd_HArray1OfInteger) bvalues = currStep->GetIntValues();
871                     std::vector<bool> bv;
872                     bv.reserve(bvalues->Upper());
873                     for ( int i = bvalues->Lower(), nb = bvalues->Upper(); i <= nb; ++i )
874                     {
875                         bv.push_back(bvalues->Value(i) != 0);
876                     }
877                     bs->setValues(bv);
878                     break;
879                 }
880                 case 1: // integer
881                 {
882                     XAO::IntegerStep* is = (XAO::IntegerStep*)step;
883                     Handle(TColStd_HArray1OfInteger) ivalues = currStep->GetIntValues();
884                     std::vector<int> iv;
885                     iv.reserve(ivalues->Upper());
886                     for ( int i = ivalues->Lower(), nb = ivalues->Upper(); i <= nb; ++i )
887                     {
888                         iv.push_back(ivalues->Value(i));
889                     }
890                     is->setValues(iv);
891                     break;
892                 }
893                 case 2: // double
894                 {
895                     XAO::DoubleStep* ds = (XAO::DoubleStep*)step;
896                     Handle(TColStd_HArray1OfReal) dvalues = currStep->GetDoubleValues();
897                     std::vector<double> dv;
898                     dv.reserve(dvalues->Upper());
899                     for ( int i = dvalues->Lower(), nb = dvalues->Upper(); i <= nb; ++i )
900                     {
901                         dv.push_back(dvalues->Value(i));
902                     }
903                     ds->setValues(dv);
904                     break;
905                 }
906                 case 3: // string
907                 {
908                     XAO::StringStep* ss = (XAO::StringStep*)step;
909                     Handle(TColStd_HArray1OfExtendedString) svalues = currStep->GetStringValues();
910                     std::vector<std::string> sv;
911                     sv.reserve(svalues->Upper());
912                     for ( int i = svalues->Lower(), nb = svalues->Upper(); i <= nb; ++i )
913                     {
914                         sv.push_back(TCollection_AsciiString(svalues->Value(i)).ToCString());
915                     }
916                     ss->setValues(sv);
917                     break;
918                 }
919             }
920         }
921     }
922 }
923
924 void GEOMImpl_IInsertOperations::exportSubshapes(const Handle(GEOM_Object)& shape, XAO::BrepGeometry* geometry)
925 {
926   Handle(TColStd_HSequenceOfTransient) subObjects = myShapesOperations->GetExistingSubObjects(shape, false);
927   int nbSubObjects = subObjects->Length();
928   // set the names of the sub shapes
929   for (int i = 1; i <= nbSubObjects; i++)
930   {
931     Handle(Standard_Transient) transientSubObject = subObjects->Value(i);
932     if (transientSubObject.IsNull())
933       continue;
934     
935     Handle(GEOM_Object) subObject = Handle(GEOM_Object)::DownCast(transientSubObject);
936     if (subObject->GetType() != GEOM_GROUP)
937     {
938       int subIndex = myShapesOperations->GetSubShapeIndex(shape, subObject);
939       switch (subObject->GetValue().ShapeType())
940       {
941       case TopAbs_VERTEX:
942         geometry->changeVertexName(subIndex, subObject->GetName().ToCString());
943         break;
944       case TopAbs_EDGE:
945         geometry->changeEdgeName(subIndex, subObject->GetName().ToCString());
946         break;
947       case TopAbs_FACE:
948         geometry->changeFaceName(subIndex, subObject->GetName().ToCString());
949         break;
950       case TopAbs_SOLID:
951         geometry->changeSolidName(subIndex, subObject->GetName().ToCString());
952         break;
953       }
954     }
955   }
956 }
957
958 //=============================================================================
959 /*!
960  *  Export a shape to XAO format
961  *  \param shape The shape to export
962  *  \param groups The list of groups to export
963  *  \param fields The list of fields to export
964  *  \param fileName The name of the file to exported
965  *  \return boolean indicating if export was succeful.
966  */
967 //=============================================================================
968 bool GEOMImpl_IInsertOperations::ExportXAO(Handle(GEOM_Object) shape,
969                                            std::list<Handle(GEOM_Object)> groupList,
970                                            std::list<Handle(GEOM_Field)> fieldList,
971                                            const char* author,
972                                            const char* fileName)
973 {
974   SetErrorCode(KO);
975
976   if (shape.IsNull()) return false;
977   
978   // add a new shape function with parameters
979   Handle(GEOM_Function) lastFunction = shape->GetLastFunction();
980   if (lastFunction.IsNull()) return false;
981   
982   // add a new result object
983   Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
984   
985   // add an Export function
986   Handle(GEOM_Function) exportFunction = result->AddFunction(GEOMImpl_XAODriver::GetID(), IMPORTEXPORT_EXPORTXAO);
987   if (exportFunction.IsNull()) return false;
988   if (exportFunction->GetDriverGUID() != GEOMImpl_XAODriver::GetID()) return false;
989   
990   // create the XAO object
991   XAO::Xao* xaoObject = new XAO::Xao();
992   xaoObject->setAuthor(author);
993   
994   // add the geometry
995   XAO::BrepGeometry* geometry = (XAO::BrepGeometry*)XAO::Geometry::createGeometry(XAO::BREP);
996   TopoDS_Shape topoShape = shape->GetValue();
997   exportFunction->SetValue(topoShape);
998   XAO::BrepGeometry* brep = (XAO::BrepGeometry*)geometry;
999   brep->setTopoDS_Shape(topoShape);
1000   
1001   geometry->setName(shape->GetName().ToCString());
1002   exportSubshapes(shape, geometry);
1003   xaoObject->setGeometry(geometry);
1004   
1005   exportGroups(groupList, xaoObject, geometry);
1006   exportFields(fieldList, xaoObject, geometry);
1007   
1008   // export the XAO to the file
1009   xaoObject->exportXAO(fileName);
1010   
1011   // make a Python command
1012   GEOM::TPythonDump pd(exportFunction);
1013   pd << "exported = geompy.ExportXAO(" << shape;
1014   
1015   // list of groups
1016   pd << ", [";
1017   if (groupList.size() > 0)
1018   {
1019     std::list<Handle(GEOM_Object)>::iterator itGroup = groupList.begin();
1020     pd << (*itGroup++);
1021     while (itGroup != groupList.end())
1022     {
1023       pd << ", " << (*itGroup++);
1024     }
1025   }
1026
1027   // list of fields
1028   pd << "], [";
1029   if (fieldList.size() > 0)
1030   {
1031     std::list<Handle(GEOM_Field)>::iterator itField = fieldList.begin();
1032     pd << (*itField++);
1033     while (itField != fieldList.end())
1034     {
1035       pd << ", " << (*itField++);
1036     }
1037   }
1038   pd << "], ";
1039   pd << "\"" << author << "\", \"" << fileName << "\")";
1040   
1041   SetErrorCode(OK);
1042   delete xaoObject;
1043   
1044   return true;
1045 }
1046
1047 void GEOMImpl_IInsertOperations::importSubShapes(XAO::Geometry* xaoGeometry,
1048                                                  Handle(GEOM_Function) function, int shapeType, int dim,
1049                                                  Handle(TColStd_HSequenceOfTransient)& subShapeList)
1050 {
1051   Handle(GEOM_Object) subShape;
1052   Handle(GEOM_Function) aFunction;
1053   Handle(TColStd_HArray1OfInteger) anArray;
1054   
1055   XAO::GeometricElementList::iterator elementIterator = xaoGeometry->begin((XAO::Dimension)dim);
1056   for (; elementIterator != xaoGeometry->end((XAO::Dimension)dim); elementIterator++)
1057   {
1058     XAO::GeometricElement element = elementIterator->second;
1059     if (!element.hasName())
1060       continue;
1061     
1062     std::string name = element.getName();
1063     std::string ref = element.getReference();
1064     int iref = XAO::XaoUtils::stringToInt(ref);
1065     
1066     anArray = new TColStd_HArray1OfInteger(1, 1);
1067     anArray->SetValue(1, iref);
1068     
1069     subShape = GetEngine()->AddObject(GetDocID(), GEOM_SUBSHAPE);
1070     Handle(GEOM_Function) aFunction = subShape->AddFunction(GEOM_Object::GetSubShapeID(), 1);
1071     if (aFunction.IsNull())
1072       return;
1073     
1074     subShape->SetName(name.c_str());
1075     subShape->SetType(shapeType);
1076     
1077     GEOM_ISubShape aSSI(aFunction);
1078     aSSI.SetMainShape(function);
1079     aSSI.SetIndices(anArray);
1080     
1081     //aFunction->SetValue(aValue);
1082     subShapeList->Append(subShape);
1083     
1084     // Put this subshape in the list of sub-shapes of theMainShape
1085     function->AddSubShapeReference(aFunction);
1086   }
1087 }
1088
1089 //=============================================================================
1090 /*!
1091  *  Import a shape from XAO format
1092  *  \param fileName The name of the file to import
1093  *  \param shape The imported shape
1094  *  \param subShapes The list of imported groups
1095  *  \param groups The list of imported groups
1096  *  \param fields The list of imported fields
1097  *  \return boolean indicating if import was succeful.
1098  */
1099 //=============================================================================
1100 bool GEOMImpl_IInsertOperations::ImportXAO(const char* fileName,
1101                                            Handle(GEOM_Object)& shape,
1102                                            Handle(TColStd_HSequenceOfTransient)& subShapes,
1103                                            Handle(TColStd_HSequenceOfTransient)& groups,
1104                                            Handle(TColStd_HSequenceOfTransient)& fields)
1105 {
1106   SetErrorCode(KO);
1107   
1108   if (fileName == NULL || groups.IsNull() || fields.IsNull())
1109     return false;
1110   
1111   // Read the XAO
1112   XAO::Xao* xaoObject = new XAO::Xao();
1113   try
1114   {
1115     xaoObject->importXAO(fileName);
1116   }
1117   catch (XAO::XAO_Exception& exc)
1118   {
1119     delete xaoObject;
1120     SetErrorCode(exc.what());
1121     return false;
1122   }
1123   
1124   XAO::Geometry* xaoGeometry = xaoObject->getGeometry();
1125   if (xaoGeometry == NULL)
1126   {
1127     delete xaoObject;
1128     SetErrorCode("Cannot import XAO: geometry format not supported.");
1129     return false;
1130   }
1131   
1132   // create the shape
1133   shape = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
1134   Handle(GEOM_Function) function = shape->AddFunction(GEOMImpl_XAODriver::GetID(), IMPORTEXPORT_EXPORTXAO);
1135   if (function.IsNull()) return false;
1136   if (function->GetDriverGUID() != GEOMImpl_XAODriver::GetID()) return false;
1137   
1138   // set the geometry
1139   if (xaoGeometry->getFormat() == XAO::BREP)
1140   {
1141     XAO::BrepGeometry* brep = (XAO::BrepGeometry*)xaoGeometry;
1142     TopoDS_Shape geomShape = brep->getTopoDS_Shape();
1143     function->SetValue(geomShape);
1144     shape->SetName(xaoGeometry->getName().c_str());
1145   }
1146   else
1147   {
1148     delete xaoObject;
1149     SetErrorCode("Cannot import XAO: geometry format not supported.");
1150     return false;
1151   }
1152   
1153   // create sub shapes with names
1154   importSubShapes(xaoGeometry, function, GEOM_POINT, XAO::VERTEX, subShapes);
1155   importSubShapes(xaoGeometry, function, GEOM_EDGE, XAO::EDGE, subShapes);
1156   importSubShapes(xaoGeometry, function, GEOM_FACE, XAO::FACE, subShapes);
1157   importSubShapes(xaoGeometry, function, GEOM_SOLID, XAO::SOLID, subShapes);
1158   
1159   // create groups
1160   int nbGroups = xaoObject->countGroups();
1161   for (int i = 0; i < nbGroups; ++i)
1162   {
1163     XAO::Group* xaoGroup = xaoObject->getGroup(i);
1164     
1165     // build an array with the indexes of the sub shapes
1166     int nbElt = xaoGroup->count();
1167     Handle(TColStd_HArray1OfInteger) array = new TColStd_HArray1OfInteger(1, nbElt);
1168     int j = 0;
1169     for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it)
1170     {
1171       int index = (*it);
1172       std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
1173       array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
1174     }
1175     
1176     // create the group with the array of sub shapes indexes
1177     Handle(GEOM_Object) group = GetEngine()->AddSubShape(shape, array);
1178     group->SetType(GEOM_GROUP);
1179     group->SetName(xaoGroup->getName().c_str());
1180     
1181     // Set a sub-shape type
1182     TDF_Label freeLabel = group->GetFreeLabel();
1183     TDataStd_Integer::Set(freeLabel, (Standard_Integer) getGroupDimension(xaoGroup));
1184     groups->Append(group);
1185     
1186     function = group->GetLastFunction();
1187   }
1188   
1189   // create the fields
1190   int nbFields = xaoObject->countFields();
1191   for (int i = 0; i < nbFields; ++i)
1192   {
1193     XAO::Field* xaoField = xaoObject->getField(i);
1194
1195     Handle(TColStd_HArray1OfExtendedString) components = new TColStd_HArray1OfExtendedString(0, xaoField->countComponents()-1);
1196     for (int j = 0; j < xaoField->countComponents(); ++j)
1197     {
1198         components->SetValue(j, (TCollection_ExtendedString)xaoField->getComponentName(j).c_str());
1199     }
1200
1201     Handle(GEOM_Field) field = myFieldOperations->CreateField(shape,
1202                  xaoField->getName().c_str(),
1203                  (int)xaoField->getType(),
1204                  (int)xaoField->getDimension(),
1205                  components);
1206
1207     switch (xaoField->getType())
1208     {
1209         case XAO::BOOLEAN:
1210         {
1211             XAO::BooleanField* bfield = (XAO::BooleanField*)xaoField;
1212             for (int j = 0; j < xaoField->countSteps(); ++j)
1213             {
1214                 XAO::BooleanStep* bstep = bfield->getStep(j);
1215                 Handle(GEOM_FieldStep) step = field->AddStep(bstep->getStep(), bstep->getStamp());
1216
1217                 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, bstep->countValues()-1);
1218                 std::vector<bool> bvalues = bstep->getValues();
1219                 for (int k = 0; k < bstep->countValues(); ++k)
1220                 {
1221                     values->SetValue(k, bvalues[k] ? 1 : 0);
1222                 }
1223                 step->SetValues(values);
1224             }
1225             break;
1226         }
1227         case XAO::INTEGER:
1228         {
1229             XAO::IntegerField* ifield = (XAO::IntegerField*)xaoField;
1230             for (int j = 0; j < xaoField->countSteps(); ++j)
1231             {
1232                 XAO::IntegerStep* istep = ifield->getStep(j);
1233                 Handle(GEOM_FieldStep) step = field->AddStep(istep->getStep(), istep->getStamp());
1234
1235                 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, istep->countValues()-1);
1236                 std::vector<int> ivalues = istep->getValues();
1237                 for (int k = 0; k < istep->countValues(); ++k)
1238                 {
1239                     values->SetValue(k, ivalues[k]);
1240                 }
1241                 step->SetValues(values);
1242             }
1243             break;
1244         }
1245         case XAO::DOUBLE:
1246         {
1247             XAO::DoubleField* dfield = (XAO::DoubleField*)xaoField;
1248             for (int j = 0; j < xaoField->countSteps(); ++j)
1249             {
1250                 XAO::DoubleStep* dstep = dfield->getStep(j);
1251                 Handle(GEOM_FieldStep) step = field->AddStep(dstep->getStep(), dstep->getStamp());
1252
1253                 Handle(TColStd_HArray1OfReal) values = new TColStd_HArray1OfReal(0, dstep->countValues()-1);
1254                 std::vector<double> dvalues = dstep->getValues();
1255                 for (int k = 0; k < dstep->countValues(); ++k)
1256                 {
1257                     values->SetValue(k, dvalues[k]);
1258                 }
1259                 step->SetValues(values);
1260             }
1261             break;
1262         }
1263         case XAO::STRING:
1264         {
1265             XAO::StringField* sfield = (XAO::StringField*)xaoField;
1266             for (int j = 0; j < xaoField->countSteps(); ++j)
1267             {
1268                 XAO::StringStep* sstep = sfield->getStep(j);
1269                 Handle(GEOM_FieldStep) step = field->AddStep(sstep->getStep(), sstep->getStamp());
1270
1271                 Handle(TColStd_HArray1OfExtendedString) values = new TColStd_HArray1OfExtendedString(0, sstep->countValues()-1);
1272                 std::vector<std::string> svalues = sstep->getValues();
1273                 for (int k = 0; k < sstep->countValues(); ++k)
1274                 {
1275                     values->SetValue(k, TCollection_ExtendedString(svalues[k].c_str()));
1276                 }
1277                 step->SetValues(values);
1278             }
1279             break;
1280         }
1281     }
1282
1283     fields->Append(field);
1284   }
1285   
1286   // make a Python command
1287   GEOM::TPythonDump pd(function);
1288   pd << "(imported, " << shape << ", ";
1289   
1290   // list of sub shapes
1291   pd << "[";
1292   int nbSubshapes = subShapes->Length();
1293   if (nbSubshapes > 0)
1294   {
1295     for (int i = 1; i <= nbSubshapes; i++)
1296     {
1297       Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(subShapes->Value(i));
1298       pd << obj << ((i < nbSubshapes) ? ", " : "");
1299     }
1300   }
1301   pd << "], [";
1302   
1303   // list of groups
1304   if (nbGroups > 0)
1305   {
1306     for (int i = 1; i <= nbGroups; i++)
1307     {
1308       Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(groups->Value(i));
1309       pd << obj << ((i < nbGroups) ? ", " : "");
1310     }
1311   }
1312   
1313   pd << "], [";
1314   
1315   // list of fields
1316   if (nbFields > 0)
1317   {
1318     for (int i = 1; i <= nbFields; i++)
1319     {
1320       Handle(GEOM_Field) obj = Handle(GEOM_Field)::DownCast(fields->Value(i));
1321       pd << obj << ((i < nbFields) ? ", " : "");
1322     }
1323   }
1324   pd << "]";
1325   pd << ") = geompy.ImportXAO(\"" << fileName << "\")";
1326   
1327   delete xaoObject;
1328   SetErrorCode(OK);
1329   
1330   return true;
1331 }
1332
1333 //=============================================================================
1334 /*!
1335  *  This method creates material groups for an imported object.
1336  *  \param theObject the imported object.
1337  */
1338 //=============================================================================
1339 void GEOMImpl_IInsertOperations::MakeMaterialGroups
1340                         (const Handle(GEOM_Object) &theObject,
1341                          const Handle(TColStd_HSequenceOfTransient) &theSeq)
1342 {
1343   TopoDS_Shape aResShape = theObject->GetValue();
1344
1345   if (aResShape.IsNull() == Standard_False) {
1346     // Group shapes by material names.
1347     Handle(GEOM_Function)      aFunction = theObject->GetLastFunction();
1348     DataMapOfStringListOfShape aMapMaterialShapes;
1349
1350     // check all named shapes using iterator
1351     TDF_ChildIDIterator anIt (aFunction->GetNamingEntry(),
1352         TNaming_NamedShape::GetID(), Standard_True);
1353
1354     for (; anIt.More(); anIt.Next()) {
1355       Handle(TNaming_NamedShape) anAttr =
1356           Handle(TNaming_NamedShape)::DownCast(anIt.Value());
1357
1358       if (anAttr.IsNull() == Standard_False) {
1359         TDF_Label                aLabel = anAttr->Label();
1360         Handle(TDataStd_Comment) aComment;
1361
1362         if (aLabel.FindAttribute(TDataStd_Comment::GetID(), aComment)) {
1363           TCollection_ExtendedString aMatName = aComment->Get();
1364           TopoDS_Shape               aShape   = anAttr->Get();
1365
1366           if (aMapMaterialShapes.IsBound(aMatName) == Standard_False) {
1367             NCollection_List<TopoDS_Shape> anEmptyList;
1368
1369             aMapMaterialShapes.Bind(aMatName, anEmptyList);
1370           }
1371
1372           aMapMaterialShapes(aMatName).Append(aShape);
1373         }
1374       }
1375     }
1376
1377     if (aMapMaterialShapes.IsEmpty() == Standard_False) {
1378       // Construct groups.
1379       TopAbs_ShapeEnum aType = aResShape.ShapeType();
1380       Standard_Integer i;
1381       DataMapOfStringListOfShape::Iterator aMapIter;
1382
1383       // Check each shape type.
1384       for(i = aType; i <= TopAbs_VERTEX; i++) {
1385         DataMapOfStringListOfShape::Iterator aMapIter(aMapMaterialShapes);
1386
1387         for (; aMapIter.More(); aMapIter.Next()) {
1388           NCollection_List<TopoDS_Shape> &aShList = aMapIter.ChangeValue();
1389           NCollection_List<TopoDS_Shape>::Iterator aShIter(aShList);
1390           NCollection_List<TopoDS_Shape>  aShListSameType;
1391
1392           while (aShIter.More()) {
1393             const TopoDS_Shape &aShape = aShIter.Value();
1394
1395             if (i == aShape.ShapeType()) {
1396               // Treat this element.
1397               aShListSameType.Append(aShape);
1398               aShList.Remove(aShIter);
1399             } else {
1400               // Go to the next element.
1401               aShIter.Next();
1402             }
1403           }
1404
1405           if (aShListSameType.IsEmpty() == Standard_False) {
1406             // Construct a group.
1407             Handle(GEOM_Object) aGroup =
1408               MakeGroup(theObject, aMapIter.Key(), aShListSameType);
1409
1410             if (aGroup.IsNull() == Standard_False) {
1411               theSeq->Append(aGroup);
1412             }
1413           }
1414         }
1415       }
1416     }
1417   }
1418 }
1419
1420
1421 //=============================================================================
1422 /*!
1423  *  This method creates a group of shapes of certain type.
1424  *  \param theObject the imported object.
1425  *  \param theName the material name.
1426  *  \param theShapes the list of shapes to be added to this group.
1427  *  \return the created group.
1428  */
1429 //=============================================================================
1430 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeGroup
1431                   (const Handle(GEOM_Object)            &theObject,
1432                    const TCollection_ExtendedString     &theName,
1433                    const NCollection_List<TopoDS_Shape> &theShapes)
1434 {
1435   Handle(GEOM_Object)                aGroup;
1436   TopTools_IndexedMapOfShape         anIndices;
1437   Handle(TColStd_HSequenceOfInteger) aSeqIDs = new TColStd_HSequenceOfInteger;
1438   NCollection_List<TopoDS_Shape>::Iterator anIter(theShapes);
1439
1440   TopExp::MapShapes(theObject->GetValue(), anIndices);
1441
1442   // Compose shape IDs.
1443   for (; anIter.More(); anIter.Next()) {
1444     const TopoDS_Shape &aShape = anIter.Value();
1445     const Standard_Integer anIndex = anIndices.FindIndex(aShape);
1446
1447     if (anIndex > 0) {
1448       aSeqIDs->Append(anIndex);
1449     }
1450   }
1451
1452   if (aSeqIDs->IsEmpty() == Standard_False) {
1453     // Create a group.
1454     const TopAbs_ShapeEnum aType  = theShapes.First().ShapeType();
1455
1456     aGroup = myGroupOperations->CreateGroup(theObject, aType);
1457
1458     if (aGroup.IsNull() == Standard_False) {
1459       aGroup->GetLastFunction()->SetDescription("");
1460       myGroupOperations->UnionIDs(aGroup, aSeqIDs);
1461       aGroup->GetLastFunction()->SetDescription("");
1462
1463       // Compose the group name.
1464       TCollection_AsciiString aGroupName(theName);
1465
1466       switch(aType) {
1467         case TopAbs_VERTEX:
1468           aGroupName += "_VERTEX";
1469           break;
1470         case TopAbs_EDGE:
1471           aGroupName += "_EDGE";
1472           break;
1473         case TopAbs_WIRE:
1474           aGroupName += "_WIRE";
1475           break;
1476         case TopAbs_FACE:
1477           aGroupName += "_FACE";
1478           break;
1479         case TopAbs_SHELL:
1480           aGroupName += "_SHELL";
1481           break;
1482         case TopAbs_SOLID:
1483           aGroupName += "_SOLID";
1484           break;
1485         case TopAbs_COMPSOLID:
1486           aGroupName += "_COMPSOLID";
1487           break;
1488         case TopAbs_COMPOUND:
1489           aGroupName += "_COMPOUND";
1490           break;
1491         default:
1492           aGroupName += "_SHAPE";
1493           break;
1494       }
1495
1496       aGroup->SetName(aGroupName.ToCString());
1497     }
1498   }
1499
1500   return aGroup;
1501 }