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 <Standard_Stream.hxx>
25 #include <GEOMImpl_IInsertOperations.hxx>
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"
39 #include <GEOM_Function.hxx>
40 #include <GEOM_PythonDump.hxx>
41 #include "GEOM_ISubShape.hxx"
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>
58 #include <Basics_OCCTVersion.hxx>
60 #include "utilities.h"
62 #include <Utils_ExceptHandlers.hxx>
64 #include <TFunction_DriverTable.hxx>
65 #include <TFunction_Driver.hxx>
66 #include <TFunction_Logbook.hxx>
67 #include <TDF_Tool.hxx>
68 #include <TDataStd_Integer.hxx>
71 #include <TopoDS_Vertex.hxx>
72 #include <BRep_Builder.hxx>
73 #include <BRep_Tool.hxx>
74 #include <BRepTools.hxx>
77 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
78 #include <TColStd_HArray1OfByte.hxx>
79 #include <TColStd_HArray1OfReal.hxx>
81 #include <TDataStd_HArray1OfByte.hxx>
84 #include <Standard_Failure.hxx>
85 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
88 * This function returns the input format name from the original format name.
90 static TCollection_AsciiString GetImportFormatName
91 (const TCollection_AsciiString& theFormatName)
93 return theFormatName.Token("_");
96 //=============================================================================
100 //=============================================================================
101 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
102 : GEOM_IOperations(theEngine, theDocID)
104 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
105 myShapesOperations = new GEOMImpl_IShapesOperations(GetEngine(), GetDocID());
106 myGroupOperations = new GEOMImpl_IGroupOperations(GetEngine(), GetDocID());
107 myFieldOperations = new GEOMImpl_IFieldOperations(GetEngine(), GetDocID());
110 //=============================================================================
114 //=============================================================================
115 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
117 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
118 delete myShapesOperations;
119 delete myGroupOperations;
120 delete myFieldOperations;
123 //=============================================================================
127 //=============================================================================
128 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
132 if (theOriginal.IsNull()) return NULL;
134 //Add a new Copy object
135 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
137 //Add a Copy function for creation a copy object
138 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
140 //Check if the function is set correctly
141 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
143 GEOMImpl_ICopy aCI(aFunction);
145 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
146 if (aRefFunction.IsNull()) return NULL;
148 aCI.SetOriginal(aRefFunction);
150 //Compute the Copy value
152 #if OCC_VERSION_LARGE > 0x06010000
155 if (!GetSolver()->ComputeFunction(aFunction)) {
156 SetErrorCode("Copy driver failed");
160 catch (Standard_Failure) {
161 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
162 SetErrorCode(aFail->GetMessageString());
166 //Make a Python command
167 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
173 //=============================================================================
177 //=============================================================================
178 void GEOMImpl_IInsertOperations::Export
179 (const Handle(GEOM_Object) theOriginal,
180 const TCollection_AsciiString& theFileName,
181 const TCollection_AsciiString& theFormatName)
185 if (theOriginal.IsNull()) return;
187 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
188 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
190 //Add a new result object
191 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
193 //Add an Export function
194 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
195 if (aFunction.IsNull()) return;
197 //Check if the function is set correctly
198 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
200 Handle(TCollection_HAsciiString) aHLibName;
201 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
204 TCollection_AsciiString aLibName = aHLibName->String();
207 GEOMImpl_IImportExport aCI (aFunction);
208 aCI.SetOriginal(aRefFunction);
209 aCI.SetFileName(theFileName);
210 aCI.SetFormatName(theFormatName);
211 aCI.SetPluginName(aLibName);
215 #if OCC_VERSION_LARGE > 0x06010000
218 if (!GetSolver()->ComputeFunction(aFunction)) {
219 SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
223 catch (Standard_Failure) {
224 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
225 SetErrorCode(aFail->GetMessageString());
229 //Make a Python command
230 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
231 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
236 //=============================================================================
240 //=============================================================================
241 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
242 (const TCollection_AsciiString& theFileName,
243 const TCollection_AsciiString& theFormatName)
247 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
249 //Add a new result object
250 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
252 //Add an Import function
253 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
254 if (aFunction.IsNull()) return result;
256 //Check if the function is set correctly
257 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
259 Handle(TCollection_HAsciiString) aHLibName;
261 (Standard_True, GetImportFormatName(theFormatName), aHLibName)) {
264 TCollection_AsciiString aLibName = aHLibName->String();
267 GEOMImpl_IImportExport aCI (aFunction);
268 aCI.SetFileName(theFileName);
269 aCI.SetFormatName(theFormatName);
270 aCI.SetPluginName(aLibName);
274 #if OCC_VERSION_LARGE > 0x06010000
277 if (!GetSolver()->ComputeFunction(aFunction)) {
278 SetErrorCode("Import driver failed");
282 catch (Standard_Failure) {
283 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
284 SetErrorCode(aFail->GetMessageString());
288 //Make a Python command
289 if (theFormatName != "IGES_UNIT") {
290 GEOM::TPythonDump pd (aFunction);
291 if (theFormatName == "BREP")
292 pd << result << " = geompy.ImportBREP(\"" << theFileName.ToCString() << "\")";
293 else if (theFormatName == "IGES")
294 pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\")";
295 else if (theFormatName == "IGES_SCALE")
296 pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\", True)";
297 else if (theFormatName == "STEP")
298 pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\")";
299 else if (theFormatName == "STEP_SCALE")
300 pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", True)";
302 pd << result << " = geompy.ImportFile(\""
303 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
310 if (theFormatName == "IGES_UNIT") {
311 TopoDS_Shape S = aFunction->GetValue();
312 TopoDS_Vertex V = TopoDS::Vertex(S);
313 gp_Pnt P = BRep_Tool::Pnt(V);
314 double scale = P.X();
315 TCollection_AsciiString aUnitName = "UNIT_M";
316 if (fabs(scale-0.01) < 1.e-6)
317 aUnitName = "UNIT_CM";
318 else if (fabs(scale-0.001) < 1.e-6)
319 aUnitName = "UNIT_MM";
320 //cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
321 SetErrorCode(aUnitName);
328 //=============================================================================
332 //=============================================================================
333 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
334 (const TCollection_AsciiString& theFileName,
335 const TCollection_AsciiString& theFormatName,
336 const TCollection_AsciiString& theParameterName)
340 TCollection_AsciiString aValue, anError;
342 if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
344 Handle(TCollection_HAsciiString) aHLibName;
346 (Standard_True, GetImportFormatName(theFormatName), aHLibName)) {
349 TCollection_AsciiString aLibName = aHLibName->String();
351 aValue = GEOMImpl_ImportDriver::ReadValue(theFileName, aLibName, theParameterName, anError);
352 if (anError.IsEmpty())
355 SetErrorCode(anError.ToCString());
360 //=============================================================================
364 //=============================================================================
365 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
366 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
367 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
369 if (theFormats.IsNull())
370 theFormats = new TColStd_HSequenceOfAsciiString;
374 if (thePatterns.IsNull())
375 thePatterns = new TColStd_HSequenceOfAsciiString;
377 thePatterns->Clear();
379 if (!InitResMgr()) return Standard_False;
381 // Read Import formats list from install directory
382 if (myResMgr->Find("Import")) {
383 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
384 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
386 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
387 theFormats->Append(aToken);
391 // Read Import formats from user directory
392 if (myResMgrUser->Find("Import")) {
393 TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
394 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
396 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
397 int aLenFormats = theFormats->Length();
398 bool isFound = false;
399 for(int aInd=1;aInd<=aLenFormats;aInd++){
400 if( theFormats->Value(aInd) == aToken){
406 theFormats->Append(aToken);
410 // Read Patterns for each supported format
411 int j = 1, len = theFormats->Length();
412 for (; j <= len; j++) {
413 TCollection_AsciiString aKey, aPattern;
414 aKey = theFormats->Value(j) + ".ImportPattern";
415 if (myResMgr->Find(aKey.ToCString()))
416 aPattern = myResMgr->Value(aKey.ToCString());
417 else if(myResMgrUser->Find(aKey.ToCString()))
418 aPattern = myResMgrUser->Value(aKey.ToCString());
420 aKey = theFormats->Value(j) + ".Pattern";
421 if (myResMgr->Find(aKey.ToCString()))
422 aPattern = myResMgr->Value(aKey.ToCString());
423 else if(myResMgrUser->Find(aKey.ToCString()))
424 aPattern = myResMgrUser->Value(aKey.ToCString());
426 aPattern = theFormats->Value(j);
427 aPattern += " Files ( *.* )";
430 thePatterns->Append(aPattern);
433 return (!theFormats->IsEmpty());
436 //=============================================================================
440 //=============================================================================
441 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
442 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
443 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
445 if (theFormats.IsNull())
446 theFormats = new TColStd_HSequenceOfAsciiString;
450 if (thePatterns.IsNull())
451 thePatterns = new TColStd_HSequenceOfAsciiString;
453 thePatterns->Clear();
455 if (!InitResMgr()) return Standard_False;
457 // Read Export formats list from install directory
458 if (myResMgr->Find("Export")) {
459 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
460 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
462 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
463 theFormats->Append(aToken);
467 // Read Export formats list from user directory
468 if (myResMgrUser->Find("Export")) {
469 TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
470 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
472 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
473 int aLenFormats = theFormats->Length();
474 bool isFound = false;
475 for(int aInd=1;aInd<=aLenFormats;aInd++){
476 if( theFormats->Value(aInd) == aToken){
482 theFormats->Append(aToken);
486 // Read Patterns for each supported format
487 int j = 1, len = theFormats->Length();
488 for (; j <= len; j++) {
489 TCollection_AsciiString aKey, aPattern;
490 aKey = theFormats->Value(j) + ".ExportPattern";
491 if (myResMgr->Find(aKey.ToCString()))
492 aPattern = myResMgr->Value(aKey.ToCString());
493 else if (myResMgrUser->Find(aKey.ToCString()))
494 aPattern = myResMgrUser->Value(aKey.ToCString());
496 aKey = theFormats->Value(j) + ".Pattern";
497 if (myResMgr->Find(aKey.ToCString()))
498 aPattern = myResMgr->Value(aKey.ToCString());
499 else if (myResMgrUser->Find(aKey.ToCString()))
500 aPattern = myResMgrUser->Value(aKey.ToCString());
502 aPattern = theFormats->Value(j);
503 aPattern += " Files ( *.* )";
506 thePatterns->Append(aPattern);
509 return (!theFormats->IsEmpty());
512 //=============================================================================
516 //=============================================================================
517 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
518 (const Standard_Boolean isImport,
519 const TCollection_AsciiString& theFormat,
520 Handle(TCollection_HAsciiString)& theLibName)
522 if (!InitResMgr()) return Standard_False;
524 // Import/Export mode
525 TCollection_AsciiString aMode;
526 //Standard_CString aMode;
527 if (isImport) aMode = "Import";
528 else aMode = "Export";
530 // Read supported formats for the certain mode from install directory
531 if (myResMgr->Find(aMode.ToCString())) {
532 TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
533 if (aFormats.Search(theFormat) > -1) {
534 // Read library name for the supported format
535 TCollection_AsciiString aKey (theFormat);
538 if (myResMgr->Find(aKey.ToCString())) {
539 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
541 if ( aLibName.Length() > 3 && aLibName.SubString(1,3) != "lib" )
542 aLibName.Prepend("lib");
547 theLibName = new TCollection_HAsciiString (aLibName);
548 return Standard_True;
553 // Read supported formats for the certain mode from user directory
554 if (myResMgrUser->Find(aMode.ToCString())) {
555 TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
556 if (aFormats.Search(theFormat) > -1) {
557 // Read library name for the supported format
558 TCollection_AsciiString aKey (theFormat);
561 if (myResMgrUser->Find(aKey.ToCString())) {
562 TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
564 if ( aLibName.Length() > 3 && aLibName.SubString(1,3) != "lib" )
565 aLibName.Prepend("lib");
570 theLibName = new TCollection_HAsciiString (aLibName);
571 return Standard_True;
576 return Standard_False;
579 //=============================================================================
583 //=============================================================================
584 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
586 bool isResourceFound = false;
587 bool isResourceFoundUser = false;
588 TCollection_AsciiString aUserResDir,aResDir;
590 if (myResMgr.IsNull()) {
591 // Initialize the Resource Manager
592 TCollection_AsciiString aNull;
593 aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
595 aResDir += "\\share\\salome\\resources\\geom";
597 aResDir += "/share/salome/resources/geom";
600 myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
602 isResourceFound = true;
603 if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
604 // instead of complains in Resource_Manager
605 isResourceFound = false;
606 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
609 isResourceFound = true;
611 if (myResMgrUser.IsNull()) {
612 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
613 TCollection_AsciiString aNull;
620 aUserResDir = getenv("HOME");
622 aUserResDir += "\\.salome\\resources";
624 aUserResDir += "/.salome/resources";
628 myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
630 isResourceFoundUser = true;
632 if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
633 // instead of complains in Resource_Manager
634 isResourceFoundUser = false;
638 isResourceFoundUser = true;
640 if(!isResourceFound && !isResourceFoundUser){
641 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
642 INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
645 return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
646 myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
649 //=============================================================================
653 //=============================================================================
654 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
658 //Add a new result object
659 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
661 //Add a Copy function
662 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
663 if (aFunction.IsNull()) return NULL;
665 //Check if the function is set correctly
666 if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
668 //Read a shape from the stream
671 BRepTools::Read(aShape, theStream, B);
672 if (aShape.IsNull()) {
673 SetErrorCode("RestoreShape error: BREP reading failed");
677 aFunction->SetValue(aShape);
679 //Special dump to avoid restored shapes publication.
680 //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
681 //GEOM::TPythonDump(aFunction) << "#";
683 GEOM::TPythonDump(aFunction) << result
684 << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
691 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
695 if (theTextureFile.IsEmpty()) return 0;
697 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
698 Handle(TColStd_HArray1OfByte) aTexture;
700 Handle(TDataStd_HArray1OfByte) aTexture;
703 FILE* fp = fopen(theTextureFile.ToCString(), "r");
706 std::list<std::string> lines;
710 if ((fgets(buffer, 4096, fp)) == NULL) break;
711 int aLen = strlen(buffer);
712 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
713 lines.push_back(buffer);
714 maxlen = std::max(maxlen, (int)strlen(buffer));
719 int lenbytes = maxlen/8;
720 if (maxlen%8) lenbytes++;
722 if (lenbytes == 0 || lines.empty())
725 std::list<unsigned char> bytedata;
726 std::list<std::string>::const_iterator it;
727 for (it = lines.begin(); it != lines.end(); ++it) {
728 std::string line = *it;
729 int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
730 for (int i = 0; i < lenline/8; i++) {
731 unsigned char byte = 0;
732 for (int j = 0; j < 8; j++)
733 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
734 bytedata.push_back(byte);
736 for (int i = lenline/8; i < lenbytes; i++)
737 bytedata.push_back((unsigned char)0);
740 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
743 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
744 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
746 aTexture = new TDataStd_HArray1OfByte (1, lines.size()*lenbytes);
749 std::list<unsigned char>::iterator bdit;
751 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
752 aTexture->SetValue(i, (Standard_Byte)(*bdit));
754 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
755 if (aTextureId > 0) SetErrorCode(OK);
759 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
760 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
761 const Handle(TColStd_HArray1OfByte)& theTexture)
763 const Handle(TDataStd_HArray1OfByte)& theTexture)
767 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
768 if (aTextureId > 0) SetErrorCode(OK);
772 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
773 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
775 Handle(TDataStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
777 int& theWidth, int& theHeight)
781 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
782 Handle(TColStd_HArray1OfByte) aTexture;
784 Handle(TDataStd_HArray1OfByte) aTexture;
787 theWidth = theHeight = 0;
788 TCollection_AsciiString aFileName;
790 if (theTextureId <= 0)
793 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
795 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
800 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
803 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
808 TopAbs_ShapeEnum getGroupDimension(XAO::Group* group)
810 XAO::Dimension dim = group->getDimension();
811 TopAbs_ShapeEnum rdim;
815 rdim = TopAbs_VERTEX; break;
817 rdim = TopAbs_EDGE; break;
819 rdim = TopAbs_FACE; break;
821 rdim = TopAbs_SOLID; break;
823 rdim = TopAbs_COMPOUND; break;
828 XAO::Dimension shapeEnumToDimension(const TopAbs_ShapeEnum& shape)
833 dim = XAO::VERTEX; break;
835 dim = XAO::EDGE; break;
837 dim = XAO::FACE; break;
839 dim = XAO::SOLID; break;
841 throw SALOME_Exception("Bad type"); // TODO
846 void GEOMImpl_IInsertOperations::exportGroups(std::list<Handle(GEOM_Object)> groupList,
848 XAO::BrepGeometry* geometry)
851 std::list<Handle(GEOM_Object)>::iterator groupIterator = groupList.begin();
852 while (groupIterator != groupList.end())
854 Handle(GEOM_Object) currGroup = (*groupIterator++);
855 Handle(TColStd_HArray1OfInteger) groupIds = myGroupOperations->GetObjects(currGroup);
857 TopAbs_ShapeEnum shapeGroup = myGroupOperations->GetType(currGroup);
858 XAO::Dimension dim = shapeEnumToDimension(shapeGroup);
859 XAO::Group* group = xaoObject->addGroup(dim, currGroup->GetName().ToCString());
864 for (int i = 1; i <= groupIds->Length(); i++)
866 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
867 int index = geometry->getVertexIndexByReference(ref);
872 for (int i = 1; i <= groupIds->Length(); i++)
874 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
875 int index = geometry->getEdgeIndexByReference(ref);
880 for (int i = 1; i <= groupIds->Length(); i++)
882 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
883 int index = geometry->getFaceIndexByReference(ref);
888 for (int i = 1; i <= groupIds->Length(); i++)
890 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
891 int index = geometry->getSolidIndexByReference(ref);
899 void GEOMImpl_IInsertOperations::exportFields(std::list<Handle(GEOM_Field)> fieldList,
901 XAO::BrepGeometry* geometry)
903 std::list<Handle(GEOM_Field)>::iterator fieldIterator = fieldList.begin();
904 while (fieldIterator != fieldList.end())
906 Handle(GEOM_Field) currField = (*fieldIterator++);
908 int fdim = currField->GetDimension();
909 int ftype = currField->GetDataType();
910 int nbComponents = currField->GetNbComponents();
911 std::string name = currField->GetName().ToCString();
913 XAO::Field* field = xaoObject->addField((XAO::Type)ftype, (XAO::Dimension)fdim, nbComponents, name);
915 Handle(TColStd_HArray1OfExtendedString) components = currField->GetComponents();
916 for (int i = components->Lower(), j = 0; i <= components->Upper(); ++i, ++j)
918 field->setComponentName(j, TCollection_AsciiString(components->Value(i)).ToCString());
921 std::list< Handle(GEOM_FieldStep)> steps = currField->GetSteps();
922 std::list<Handle(GEOM_FieldStep)>::iterator stepIterator = steps.begin();
923 while (stepIterator != steps.end())
925 Handle(GEOM_FieldStep) currStep = (*stepIterator++);
927 XAO::Step* step = field->addNewStep(currStep->GetID());
928 step->setStamp(currStep->GetStamp());
934 XAO::BooleanStep* bs = (XAO::BooleanStep*)step;
935 Handle(TColStd_HArray1OfInteger) bvalues = currStep->GetIntValues();
936 std::vector<bool> bv;
937 bv.reserve(bvalues->Upper());
938 for ( int i = bvalues->Lower(), nb = bvalues->Upper(); i <= nb; ++i )
940 bv.push_back(bvalues->Value(i) != 0);
947 XAO::IntegerStep* is = (XAO::IntegerStep*)step;
948 Handle(TColStd_HArray1OfInteger) ivalues = currStep->GetIntValues();
950 iv.reserve(ivalues->Upper());
951 for ( int i = ivalues->Lower(), nb = ivalues->Upper(); i <= nb; ++i )
953 iv.push_back(ivalues->Value(i));
960 XAO::DoubleStep* ds = (XAO::DoubleStep*)step;
961 Handle(TColStd_HArray1OfReal) dvalues = currStep->GetDoubleValues();
962 std::vector<double> dv;
963 dv.reserve(dvalues->Upper());
964 for ( int i = dvalues->Lower(), nb = dvalues->Upper(); i <= nb; ++i )
966 dv.push_back(dvalues->Value(i));
973 XAO::StringStep* ss = (XAO::StringStep*)step;
974 Handle(TColStd_HArray1OfExtendedString) svalues = currStep->GetStringValues();
975 std::vector<std::string> sv;
976 sv.reserve(svalues->Upper());
977 for ( int i = svalues->Lower(), nb = svalues->Upper(); i <= nb; ++i )
979 sv.push_back(TCollection_AsciiString(svalues->Value(i)).ToCString());
989 void GEOMImpl_IInsertOperations::exportSubshapes(const Handle(GEOM_Object)& shape, XAO::BrepGeometry* geometry)
991 Handle(TColStd_HSequenceOfTransient) subObjects = myShapesOperations->GetExistingSubObjects(shape, false);
992 int nbSubObjects = subObjects->Length();
993 // set the names of the sub shapes
994 for (int i = 1; i <= nbSubObjects; i++)
996 Handle(Standard_Transient) transientSubObject = subObjects->Value(i);
997 if (transientSubObject.IsNull())
1000 Handle(GEOM_Object) subObject = Handle(GEOM_Object)::DownCast(transientSubObject);
1001 if (subObject->GetType() != GEOM_GROUP)
1003 int subIndex = myShapesOperations->GetSubShapeIndex(shape, subObject);
1004 switch (subObject->GetValue().ShapeType())
1007 geometry->changeVertexName(subIndex, subObject->GetName().ToCString());
1010 geometry->changeEdgeName(subIndex, subObject->GetName().ToCString());
1013 geometry->changeFaceName(subIndex, subObject->GetName().ToCString());
1016 geometry->changeSolidName(subIndex, subObject->GetName().ToCString());
1023 //=============================================================================
1025 * Export a shape to XAO format
1026 * \param shape The shape to export
1027 * \param groups The list of groups to export
1028 * \param fields The list of fields to export
1029 * \param fileName The name of the file to exported
1030 * \return boolean indicating if export was succeful.
1032 //=============================================================================
1033 bool GEOMImpl_IInsertOperations::ExportXAO(Handle(GEOM_Object) shape,
1034 std::list<Handle(GEOM_Object)> groupList,
1035 std::list<Handle(GEOM_Field)> fieldList,
1037 const char* fileName)
1041 if (shape.IsNull()) return false;
1043 // add a new shape function with parameters
1044 Handle(GEOM_Function) lastFunction = shape->GetLastFunction();
1045 if (lastFunction.IsNull()) return false;
1047 // add a new result object
1048 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
1050 // add an Export function
1051 Handle(GEOM_Function) exportFunction = result->AddFunction(GEOMImpl_XAODriver::GetID(), IMPORTEXPORT_EXPORTXAO);
1052 if (exportFunction.IsNull()) return false;
1053 if (exportFunction->GetDriverGUID() != GEOMImpl_XAODriver::GetID()) return false;
1055 // create the XAO object
1056 XAO::Xao* xaoObject = new XAO::Xao();
1057 xaoObject->setAuthor(author);
1060 XAO::BrepGeometry* geometry = (XAO::BrepGeometry*)XAO::Geometry::createGeometry(XAO::BREP);
1061 TopoDS_Shape topoShape = shape->GetValue();
1062 exportFunction->SetValue(topoShape);
1063 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)geometry;
1064 brep->setTopoDS_Shape(topoShape);
1066 geometry->setName(shape->GetName().ToCString());
1067 exportSubshapes(shape, geometry);
1068 xaoObject->setGeometry(geometry);
1070 exportGroups(groupList, xaoObject, geometry);
1071 exportFields(fieldList, xaoObject, geometry);
1073 // export the XAO to the file
1074 xaoObject->exportXAO(fileName);
1076 // make a Python command
1077 GEOM::TPythonDump pd(exportFunction);
1078 pd << "exported = geompy.ExportXAO(" << shape;
1082 if (groupList.size() > 0)
1084 std::list<Handle(GEOM_Object)>::iterator itGroup = groupList.begin();
1086 while (itGroup != groupList.end())
1088 pd << ", " << (*itGroup++);
1094 if (fieldList.size() > 0)
1096 std::list<Handle(GEOM_Field)>::iterator itField = fieldList.begin();
1098 while (itField != fieldList.end())
1100 pd << ", " << (*itField++);
1104 pd << "\"" << author << "\", \"" << fileName << "\")";
1112 void GEOMImpl_IInsertOperations::importSubShapes(XAO::Geometry* xaoGeometry,
1113 Handle(GEOM_Function) function, int shapeType, int dim,
1114 Handle(TColStd_HSequenceOfTransient)& subShapeList)
1116 Handle(GEOM_Object) subShape;
1117 Handle(GEOM_Function) aFunction;
1118 Handle(TColStd_HArray1OfInteger) anArray;
1120 XAO::GeometricElementList::iterator elementIterator = xaoGeometry->begin((XAO::Dimension)dim);
1121 for (; elementIterator != xaoGeometry->end((XAO::Dimension)dim); elementIterator++)
1123 XAO::GeometricElement element = elementIterator->second;
1124 if (!element.hasName())
1127 std::string name = element.getName();
1128 std::string ref = element.getReference();
1129 int iref = XAO::XaoUtils::stringToInt(ref);
1131 anArray = new TColStd_HArray1OfInteger(1, 1);
1132 anArray->SetValue(1, iref);
1134 subShape = GetEngine()->AddObject(GetDocID(), GEOM_SUBSHAPE);
1135 Handle(GEOM_Function) aFunction = subShape->AddFunction(GEOM_Object::GetSubShapeID(), 1);
1136 if (aFunction.IsNull())
1139 subShape->SetName(name.c_str());
1140 subShape->SetType(shapeType);
1142 GEOM_ISubShape aSSI(aFunction);
1143 aSSI.SetMainShape(function);
1144 aSSI.SetIndices(anArray);
1146 //aFunction->SetValue(aValue);
1147 subShapeList->Append(subShape);
1149 // Put this subshape in the list of sub-shapes of theMainShape
1150 function->AddSubShapeReference(aFunction);
1154 //=============================================================================
1156 * Import a shape from XAO format
1157 * \param fileName The name of the file to import
1158 * \param shape The imported shape
1159 * \param subShapes The list of imported groups
1160 * \param groups The list of imported groups
1161 * \param fields The list of imported fields
1162 * \return boolean indicating if import was succeful.
1164 //=============================================================================
1165 bool GEOMImpl_IInsertOperations::ImportXAO(const char* fileName,
1166 Handle(GEOM_Object)& shape,
1167 Handle(TColStd_HSequenceOfTransient)& subShapes,
1168 Handle(TColStd_HSequenceOfTransient)& groups,
1169 Handle(TColStd_HSequenceOfTransient)& fields)
1173 if (fileName == NULL || groups.IsNull() || fields.IsNull())
1177 XAO::Xao* xaoObject = new XAO::Xao();
1180 xaoObject->importXAO(fileName);
1182 catch (XAO::XAO_Exception& exc)
1185 SetErrorCode(exc.what());
1189 XAO::Geometry* xaoGeometry = xaoObject->getGeometry();
1190 if (xaoGeometry == NULL)
1193 SetErrorCode("Cannot import XAO: geometry format not supported.");
1198 shape = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
1199 Handle(GEOM_Function) function = shape->AddFunction(GEOMImpl_XAODriver::GetID(), IMPORTEXPORT_EXPORTXAO);
1200 if (function.IsNull()) return false;
1201 if (function->GetDriverGUID() != GEOMImpl_XAODriver::GetID()) return false;
1204 if (xaoGeometry->getFormat() == XAO::BREP)
1206 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)xaoGeometry;
1207 TopoDS_Shape geomShape = brep->getTopoDS_Shape();
1208 function->SetValue(geomShape);
1209 shape->SetName(xaoGeometry->getName().c_str());
1214 SetErrorCode("Cannot import XAO: geometry format not supported.");
1218 // create sub shapes with names
1219 importSubShapes(xaoGeometry, function, GEOM_POINT, XAO::VERTEX, subShapes);
1220 importSubShapes(xaoGeometry, function, GEOM_EDGE, XAO::EDGE, subShapes);
1221 importSubShapes(xaoGeometry, function, GEOM_FACE, XAO::FACE, subShapes);
1222 importSubShapes(xaoGeometry, function, GEOM_SOLID, XAO::SOLID, subShapes);
1225 int nbGroups = xaoObject->countGroups();
1226 for (int i = 0; i < nbGroups; ++i)
1228 XAO::Group* xaoGroup = xaoObject->getGroup(i);
1230 // build an array with the indexes of the sub shapes
1231 int nbElt = xaoGroup->count();
1232 Handle(TColStd_HArray1OfInteger) array = new TColStd_HArray1OfInteger(1, nbElt);
1234 for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it)
1237 std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
1238 array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
1241 // create the group with the array of sub shapes indexes
1242 Handle(GEOM_Object) group = GetEngine()->AddSubShape(shape, array);
1243 group->SetType(GEOM_GROUP);
1244 group->SetName(xaoGroup->getName().c_str());
1246 // Set a sub-shape type
1247 TDF_Label freeLabel = group->GetFreeLabel();
1248 TDataStd_Integer::Set(freeLabel, (Standard_Integer) getGroupDimension(xaoGroup));
1249 groups->Append(group);
1251 function = group->GetLastFunction();
1254 // create the fields
1255 int nbFields = xaoObject->countFields();
1256 for (int i = 0; i < nbFields; ++i)
1258 XAO::Field* xaoField = xaoObject->getField(i);
1260 Handle(TColStd_HArray1OfExtendedString) components = new TColStd_HArray1OfExtendedString(0, xaoField->countComponents()-1);
1261 for (int j = 0; j < xaoField->countComponents(); ++j)
1263 components->SetValue(j, (TCollection_ExtendedString)xaoField->getComponentName(j).c_str());
1266 Handle(GEOM_Field) field = myFieldOperations->CreateField(shape,
1267 xaoField->getName().c_str(),
1268 (int)xaoField->getType(),
1269 (int)xaoField->getDimension(),
1272 switch (xaoField->getType())
1276 XAO::BooleanField* bfield = (XAO::BooleanField*)xaoField;
1277 for (int j = 0; j < xaoField->countSteps(); ++j)
1279 XAO::BooleanStep* bstep = bfield->getStep(j);
1280 Handle(GEOM_FieldStep) step = field->AddStep(bstep->getStep(), bstep->getStamp());
1282 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, bstep->countValues()-1);
1283 std::vector<bool> bvalues = bstep->getValues();
1284 for (int k = 0; k < bstep->countValues(); ++k)
1286 values->SetValue(k, bvalues[k] ? 1 : 0);
1288 step->SetValues(values);
1294 XAO::IntegerField* ifield = (XAO::IntegerField*)xaoField;
1295 for (int j = 0; j < xaoField->countSteps(); ++j)
1297 XAO::IntegerStep* istep = ifield->getStep(j);
1298 Handle(GEOM_FieldStep) step = field->AddStep(istep->getStep(), istep->getStamp());
1300 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, istep->countValues()-1);
1301 std::vector<int> ivalues = istep->getValues();
1302 for (int k = 0; k < istep->countValues(); ++k)
1304 values->SetValue(k, ivalues[k]);
1306 step->SetValues(values);
1312 XAO::DoubleField* dfield = (XAO::DoubleField*)xaoField;
1313 for (int j = 0; j < xaoField->countSteps(); ++j)
1315 XAO::DoubleStep* dstep = dfield->getStep(j);
1316 Handle(GEOM_FieldStep) step = field->AddStep(dstep->getStep(), dstep->getStamp());
1318 Handle(TColStd_HArray1OfReal) values = new TColStd_HArray1OfReal(0, dstep->countValues()-1);
1319 std::vector<double> dvalues = dstep->getValues();
1320 for (int k = 0; k < dstep->countValues(); ++k)
1322 values->SetValue(k, dvalues[k]);
1324 step->SetValues(values);
1330 XAO::StringField* sfield = (XAO::StringField*)xaoField;
1331 for (int j = 0; j < xaoField->countSteps(); ++j)
1333 XAO::StringStep* sstep = sfield->getStep(j);
1334 Handle(GEOM_FieldStep) step = field->AddStep(sstep->getStep(), sstep->getStamp());
1336 Handle(TColStd_HArray1OfExtendedString) values = new TColStd_HArray1OfExtendedString(0, sstep->countValues()-1);
1337 std::vector<std::string> svalues = sstep->getValues();
1338 for (int k = 0; k < sstep->countValues(); ++k)
1340 values->SetValue(k, TCollection_ExtendedString(svalues[k].c_str()));
1342 step->SetValues(values);
1348 fields->Append(field);
1351 // make a Python command
1352 GEOM::TPythonDump pd(function);
1353 pd << "(imported, " << shape << ", ";
1355 // list of sub shapes
1357 int nbSubshapes = subShapes->Length();
1358 if (nbSubshapes > 0)
1360 for (int i = 1; i <= nbSubshapes; i++)
1362 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(subShapes->Value(i));
1363 pd << obj << ((i < nbSubshapes) ? ", " : "");
1371 for (int i = 1; i <= nbGroups; i++)
1373 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(groups->Value(i));
1374 pd << obj << ((i < nbGroups) ? ", " : "");
1383 for (int i = 1; i <= nbFields; i++)
1385 Handle(GEOM_Field) obj = Handle(GEOM_Field)::DownCast(fields->Value(i));
1386 pd << obj << ((i < nbFields) ? ", " : "");
1390 pd << ") = geompy.ImportXAO(\"" << fileName << "\")";