1 // Copyright (C) 2007-2013 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.
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_XAODriver.hxx"
36 #include "GEOMImpl_IImportExportXAO.hxx"
38 #include <GEOM_Function.hxx>
39 #include <GEOM_PythonDump.hxx>
40 #include "GEOM_ISubShape.hxx"
42 #include <XAO_Xao.hxx>
43 #include <XAO_Geometry.hxx>
44 #include <XAO_BrepGeometry.hxx>
45 #include <XAO_Group.hxx>
46 #include <XAO_Field.hxx>
47 #include <XAO_XaoUtils.hxx>
49 #include <Basics_OCCTVersion.hxx>
51 #include "utilities.h"
53 #include <Utils_ExceptHandlers.hxx>
55 #include <TFunction_DriverTable.hxx>
56 #include <TFunction_Driver.hxx>
57 #include <TFunction_Logbook.hxx>
58 #include <TDF_Tool.hxx>
59 #include <TDataStd_Integer.hxx>
62 #include <TopoDS_Vertex.hxx>
63 #include <BRep_Builder.hxx>
64 #include <BRep_Tool.hxx>
65 #include <BRepTools.hxx>
68 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
69 #include <TColStd_HArray1OfByte.hxx>
71 #include <TDataStd_HArray1OfByte.hxx>
74 #include <Standard_Failure.hxx>
75 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
78 * This function returns the input format name from the original format name.
80 static TCollection_AsciiString GetImportFormatName
81 (const TCollection_AsciiString& theFormatName)
83 Standard_Integer aLastInd = 4;
85 if (theFormatName.Search("STL") == 1) {
89 return theFormatName.SubString(1, aLastInd);
92 //=============================================================================
96 //=============================================================================
97 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
98 : GEOM_IOperations(theEngine, theDocID)
100 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
101 myShapesOperations = new GEOMImpl_IShapesOperations(GetEngine(), GetDocID());
102 myGroupOperations = new GEOMImpl_IGroupOperations(GetEngine(), GetDocID());
105 //=============================================================================
109 //=============================================================================
110 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
112 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
113 delete myShapesOperations;
114 delete myGroupOperations;
117 //=============================================================================
121 //=============================================================================
122 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
126 if (theOriginal.IsNull()) return NULL;
128 //Add a new Copy object
129 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
131 //Add a Copy function for creation a copy object
132 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
134 //Check if the function is set correctly
135 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
137 GEOMImpl_ICopy aCI(aFunction);
139 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
140 if (aRefFunction.IsNull()) return NULL;
142 aCI.SetOriginal(aRefFunction);
144 //Compute the Copy value
146 #if OCC_VERSION_LARGE > 0x06010000
149 if (!GetSolver()->ComputeFunction(aFunction)) {
150 SetErrorCode("Copy driver failed");
154 catch (Standard_Failure) {
155 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
156 SetErrorCode(aFail->GetMessageString());
160 //Make a Python command
161 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
167 //=============================================================================
171 //=============================================================================
172 void GEOMImpl_IInsertOperations::Export
173 (const Handle(GEOM_Object) theOriginal,
174 const TCollection_AsciiString& theFileName,
175 const TCollection_AsciiString& theFormatName)
179 if (theOriginal.IsNull()) return;
181 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
182 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
184 //Add a new result object
185 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
187 //Add an Export function
188 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
189 if (aFunction.IsNull()) return;
191 //Check if the function is set correctly
192 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
194 Handle(TCollection_HAsciiString) aHLibName;
195 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
198 TCollection_AsciiString aLibName = aHLibName->String();
201 GEOMImpl_IImportExport aCI (aFunction);
202 aCI.SetOriginal(aRefFunction);
203 aCI.SetFileName(theFileName);
204 aCI.SetFormatName(theFormatName);
205 aCI.SetPluginName(aLibName);
209 #if OCC_VERSION_LARGE > 0x06010000
212 if (!GetSolver()->ComputeFunction(aFunction)) {
213 SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
217 catch (Standard_Failure) {
218 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
219 SetErrorCode(aFail->GetMessageString());
223 //Make a Python command
224 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
225 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
230 //=============================================================================
234 //=============================================================================
235 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
236 (const TCollection_AsciiString& theFileName,
237 const TCollection_AsciiString& theFormatName)
241 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
243 //Add a new result object
244 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
246 //Add an Import function
247 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
248 if (aFunction.IsNull()) return result;
250 //Check if the function is set correctly
251 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
253 Handle(TCollection_HAsciiString) aHLibName;
255 (Standard_True, GetImportFormatName(theFormatName), aHLibName)) {
258 TCollection_AsciiString aLibName = aHLibName->String();
261 GEOMImpl_IImportExport aCI (aFunction);
262 aCI.SetFileName(theFileName);
263 aCI.SetFormatName(theFormatName);
264 aCI.SetPluginName(aLibName);
268 #if OCC_VERSION_LARGE > 0x06010000
271 if (!GetSolver()->ComputeFunction(aFunction)) {
272 SetErrorCode("Import driver failed");
276 catch (Standard_Failure) {
277 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
278 SetErrorCode(aFail->GetMessageString());
282 //Make a Python command
283 if (theFormatName != "IGES_UNIT") {
284 GEOM::TPythonDump pd (aFunction);
285 if (theFormatName == "BREP")
286 pd << result << " = geompy.ImportBREP(\"" << theFileName.ToCString() << "\")";
287 else if (theFormatName == "IGES")
288 pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\")";
289 else if (theFormatName == "IGES_SCALE")
290 pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\", True)";
291 else if (theFormatName == "STEP")
292 pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\")";
293 else if (theFormatName == "STEP_SCALE")
294 pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", True)";
296 pd << result << " = geompy.ImportFile(\""
297 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
304 if (theFormatName == "IGES_UNIT") {
305 TopoDS_Shape S = aFunction->GetValue();
306 TopoDS_Vertex V = TopoDS::Vertex(S);
307 gp_Pnt P = BRep_Tool::Pnt(V);
308 double scale = P.X();
309 TCollection_AsciiString aUnitName = "UNIT_M";
310 if (fabs(scale-0.01) < 1.e-6)
311 aUnitName = "UNIT_CM";
312 else if (fabs(scale-0.001) < 1.e-6)
313 aUnitName = "UNIT_MM";
314 //cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
315 SetErrorCode(aUnitName);
322 //=============================================================================
326 //=============================================================================
327 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
328 (const TCollection_AsciiString& theFileName,
329 const TCollection_AsciiString& theFormatName,
330 const TCollection_AsciiString& theParameterName)
334 TCollection_AsciiString aValue, anError;
336 if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
338 Handle(TCollection_HAsciiString) aHLibName;
340 (Standard_True, GetImportFormatName(theFormatName), aHLibName)) {
343 TCollection_AsciiString aLibName = aHLibName->String();
345 aValue = GEOMImpl_ImportDriver::ReadValue(theFileName, aLibName, theParameterName, anError);
346 if (anError.IsEmpty())
349 SetErrorCode(anError.ToCString());
354 //=============================================================================
358 //=============================================================================
359 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
360 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
361 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
363 if (theFormats.IsNull())
364 theFormats = new TColStd_HSequenceOfAsciiString;
368 if (thePatterns.IsNull())
369 thePatterns = new TColStd_HSequenceOfAsciiString;
371 thePatterns->Clear();
373 if (!InitResMgr()) return Standard_False;
375 // Read Import formats list from install directory
376 if (myResMgr->Find("Import")) {
377 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
378 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
380 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
381 theFormats->Append(aToken);
385 // Read Import formats from user directory
386 if (myResMgrUser->Find("Import")) {
387 TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
388 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
390 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
391 int aLenFormats = theFormats->Length();
392 bool isFound = false;
393 for(int aInd=1;aInd<=aLenFormats;aInd++){
394 if( theFormats->Value(aInd) == aToken){
400 theFormats->Append(aToken);
404 // Read Patterns for each supported format
405 int j = 1, len = theFormats->Length();
406 for (; j <= len; j++) {
407 TCollection_AsciiString aKey, aPattern;
408 aKey = theFormats->Value(j) + ".ImportPattern";
409 if (myResMgr->Find(aKey.ToCString()))
410 aPattern = myResMgr->Value(aKey.ToCString());
411 else if(myResMgrUser->Find(aKey.ToCString()))
412 aPattern = myResMgrUser->Value(aKey.ToCString());
414 aKey = theFormats->Value(j) + ".Pattern";
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 aPattern = theFormats->Value(j);
421 aPattern += " Files ( *.* )";
424 thePatterns->Append(aPattern);
427 return (!theFormats->IsEmpty());
430 //=============================================================================
434 //=============================================================================
435 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
436 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
437 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
439 if (theFormats.IsNull())
440 theFormats = new TColStd_HSequenceOfAsciiString;
444 if (thePatterns.IsNull())
445 thePatterns = new TColStd_HSequenceOfAsciiString;
447 thePatterns->Clear();
449 if (!InitResMgr()) return Standard_False;
451 // Read Export formats list from install directory
452 if (myResMgr->Find("Export")) {
453 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
454 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
456 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
457 theFormats->Append(aToken);
461 // Read Export formats list from user directory
462 if (myResMgrUser->Find("Export")) {
463 TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
464 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
466 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
467 int aLenFormats = theFormats->Length();
468 bool isFound = false;
469 for(int aInd=1;aInd<=aLenFormats;aInd++){
470 if( theFormats->Value(aInd) == aToken){
476 theFormats->Append(aToken);
480 // Read Patterns for each supported format
481 int j = 1, len = theFormats->Length();
482 for (; j <= len; j++) {
483 TCollection_AsciiString aKey, aPattern;
484 aKey = theFormats->Value(j) + ".ExportPattern";
485 if (myResMgr->Find(aKey.ToCString()))
486 aPattern = myResMgr->Value(aKey.ToCString());
487 else if (myResMgrUser->Find(aKey.ToCString()))
488 aPattern = myResMgrUser->Value(aKey.ToCString());
490 aKey = theFormats->Value(j) + ".Pattern";
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 aPattern = theFormats->Value(j);
497 aPattern += " Files ( *.* )";
500 thePatterns->Append(aPattern);
503 return (!theFormats->IsEmpty());
506 //=============================================================================
510 //=============================================================================
511 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
512 (const Standard_Boolean isImport,
513 const TCollection_AsciiString& theFormat,
514 Handle(TCollection_HAsciiString)& theLibName)
516 if (!InitResMgr()) return Standard_False;
518 // Import/Export mode
519 TCollection_AsciiString aMode;
520 //Standard_CString aMode;
521 if (isImport) aMode = "Import";
522 else aMode = "Export";
524 // Read supported formats for the certain mode from install directory
525 if (myResMgr->Find(aMode.ToCString())) {
526 TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
527 if (aFormats.Search(theFormat) > -1) {
528 // Read library name for the supported format
529 TCollection_AsciiString aKey (theFormat);
532 if (myResMgr->Find(aKey.ToCString())) {
533 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
535 if ( aLibName.Length() > 3 && aLibName.SubString(1,3) != "lib" )
536 aLibName.Prepend("lib");
541 theLibName = new TCollection_HAsciiString (aLibName);
542 return Standard_True;
547 // Read supported formats for the certain mode from user directory
548 if (myResMgrUser->Find(aMode.ToCString())) {
549 TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
550 if (aFormats.Search(theFormat) > -1) {
551 // Read library name for the supported format
552 TCollection_AsciiString aKey (theFormat);
555 if (myResMgrUser->Find(aKey.ToCString())) {
556 TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
558 if ( aLibName.Length() > 3 && aLibName.SubString(1,3) != "lib" )
559 aLibName.Prepend("lib");
564 theLibName = new TCollection_HAsciiString (aLibName);
565 return Standard_True;
570 return Standard_False;
573 //=============================================================================
577 //=============================================================================
578 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
580 bool isResourceFound = false;
581 bool isResourceFoundUser = false;
582 TCollection_AsciiString aUserResDir,aResDir;
584 if (myResMgr.IsNull()) {
585 // Initialize the Resource Manager
586 TCollection_AsciiString aNull;
587 aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
589 aResDir += "\\share\\salome\\resources\\geom";
591 aResDir += "/share/salome/resources/geom";
594 myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
596 isResourceFound = true;
597 if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
598 // instead of complains in Resource_Manager
599 isResourceFound = false;
600 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
603 isResourceFound = true;
605 if (myResMgrUser.IsNull()) {
606 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
607 TCollection_AsciiString aNull;
614 aUserResDir = getenv("HOME");
616 aUserResDir += "\\.salome\\resources";
618 aUserResDir += "/.salome/resources";
622 myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
624 isResourceFoundUser = true;
626 if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
627 // instead of complains in Resource_Manager
628 isResourceFoundUser = false;
632 isResourceFoundUser = true;
634 if(!isResourceFound && !isResourceFoundUser){
635 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
636 INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
639 return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
640 myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
643 //=============================================================================
647 //=============================================================================
648 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
652 //Add a new result object
653 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
655 //Add a Copy function
656 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
657 if (aFunction.IsNull()) return NULL;
659 //Check if the function is set correctly
660 if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
662 //Read a shape from the stream
665 BRepTools::Read(aShape, theStream, B);
666 if (aShape.IsNull()) {
667 SetErrorCode("RestoreShape error: BREP reading failed");
671 aFunction->SetValue(aShape);
673 //Special dump to avoid restored shapes publication.
674 //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
675 //GEOM::TPythonDump(aFunction) << "#";
677 GEOM::TPythonDump(aFunction) << result
678 << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
685 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
689 if (theTextureFile.IsEmpty()) return 0;
691 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
692 Handle(TColStd_HArray1OfByte) aTexture;
694 Handle(TDataStd_HArray1OfByte) aTexture;
697 FILE* fp = fopen(theTextureFile.ToCString(), "r");
700 std::list<std::string> lines;
704 if ((fgets(buffer, 4096, fp)) == NULL) break;
705 int aLen = strlen(buffer);
706 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
707 lines.push_back(buffer);
708 maxlen = std::max(maxlen, (int)strlen(buffer));
713 int lenbytes = maxlen/8;
714 if (maxlen%8) lenbytes++;
716 if (lenbytes == 0 || lines.empty())
719 std::list<unsigned char> bytedata;
720 std::list<std::string>::const_iterator it;
721 for (it = lines.begin(); it != lines.end(); ++it) {
722 std::string line = *it;
723 int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
724 for (int i = 0; i < lenline/8; i++) {
725 unsigned char byte = 0;
726 for (int j = 0; j < 8; j++)
727 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
728 bytedata.push_back(byte);
730 for (int i = lenline/8; i < lenbytes; i++)
731 bytedata.push_back((unsigned char)0);
734 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
737 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
738 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
740 aTexture = new TDataStd_HArray1OfByte (1, lines.size()*lenbytes);
743 std::list<unsigned char>::iterator bdit;
745 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
746 aTexture->SetValue(i, (Standard_Byte)(*bdit));
748 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
749 if (aTextureId > 0) SetErrorCode(OK);
753 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
754 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
755 const Handle(TColStd_HArray1OfByte)& theTexture)
757 const Handle(TDataStd_HArray1OfByte)& theTexture)
761 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
762 if (aTextureId > 0) SetErrorCode(OK);
766 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
767 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
769 Handle(TDataStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
771 int& theWidth, int& theHeight)
775 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
776 Handle(TColStd_HArray1OfByte) aTexture;
778 Handle(TDataStd_HArray1OfByte) aTexture;
781 theWidth = theHeight = 0;
782 TCollection_AsciiString aFileName;
784 if (theTextureId <= 0)
787 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
789 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
794 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
797 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
802 TopAbs_ShapeEnum getGroupDimension(XAO::Group* group)
804 XAO::Dimension dim = group->getDimension();
805 TopAbs_ShapeEnum rdim;
809 rdim = TopAbs_VERTEX; break;
811 rdim = TopAbs_EDGE; break;
813 rdim = TopAbs_FACE; break;
815 rdim = TopAbs_SOLID; break;
817 rdim = TopAbs_COMPOUND; break;
822 XAO::Dimension shapeEnumToDimension(const TopAbs_ShapeEnum& shape)
827 dim = XAO::VERTEX; break;
829 dim = XAO::EDGE; break;
831 dim = XAO::FACE; break;
833 dim = XAO::SOLID; break;
835 throw SALOME_Exception("Bad type"); // TODO
840 void GEOMImpl_IInsertOperations::exportGroups(std::list<Handle(GEOM_Object)> groupList, XAO::Xao* xaoObject, XAO::BrepGeometry* geometry)
843 std::list<Handle(GEOM_Object)>::iterator groupIterator = groupList.begin();
844 while (groupIterator != groupList.end())
846 Handle(GEOM_Object) currGroup = (*groupIterator++);
847 Handle(TColStd_HArray1OfInteger) groupIds = myGroupOperations->GetObjects(currGroup);
849 TopAbs_ShapeEnum shapeGroup = myGroupOperations->GetType(currGroup);
850 XAO::Dimension dim = shapeEnumToDimension(shapeGroup);
851 XAO::Group* group = xaoObject->addGroup(dim, currGroup->GetName().ToCString());
856 for (int i = 1; i <= groupIds->Length(); i++)
858 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
859 int index = geometry->getVertexIndexByReference(ref);
864 for (int i = 1; i <= groupIds->Length(); i++)
866 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
867 int index = geometry->getEdgeIndexByReference(ref);
872 for (int i = 1; i <= groupIds->Length(); i++)
874 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
875 int index = geometry->getFaceIndexByReference(ref);
880 for (int i = 1; i <= groupIds->Length(); i++)
882 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
883 int index = geometry->getSolidIndexByReference(ref);
891 void GEOMImpl_IInsertOperations::exportFields(std::list<Handle(GEOM_Object)> fieldList, XAO::Xao* xaoObject, XAO::BrepGeometry* geometry)
896 void GEOMImpl_IInsertOperations::exportSubshapes(const Handle(GEOM_Object)& shape, XAO::BrepGeometry* geometry)
898 Handle(TColStd_HSequenceOfTransient) subObjects = myShapesOperations->GetExistingSubObjects(shape, false);
899 int nbSubObjects = subObjects->Length();
900 // set the names of the sub shapes
901 for (int i = 1; i <= nbSubObjects; i++)
903 Handle(Standard_Transient) transientSubObject = subObjects->Value(i);
904 if (transientSubObject.IsNull())
907 Handle(GEOM_Object) subObject = Handle(GEOM_Object)::DownCast(transientSubObject);
908 if (subObject->GetType() != GEOM_GROUP)
910 int subIndex = myShapesOperations->GetSubShapeIndex(shape, subObject);
911 switch (subObject->GetValue().ShapeType())
914 geometry->changeVertexName(subIndex, subObject->GetName().ToCString());
917 geometry->changeEdgeName(subIndex, subObject->GetName().ToCString());
920 geometry->changeFaceName(subIndex, subObject->GetName().ToCString());
923 geometry->changeSolidName(subIndex, subObject->GetName().ToCString());
930 //=============================================================================
932 * Export a shape to XAO format
933 * \param shape The shape to export
934 * \param groups The list of groups to export
935 * \param fields The list of fields to export
936 * \param fileName The name of the file to exported
937 * \return boolean indicating if export was succeful.
939 //=============================================================================
940 bool GEOMImpl_IInsertOperations::ExportXAO(Handle(GEOM_Object) shape,
941 std::list<Handle(GEOM_Object)> groupList,
942 std::list<Handle(GEOM_Object)> fieldList,
944 const char* fileName)
948 if (shape.IsNull()) return false;
950 // add a new shape function with parameters
951 Handle(GEOM_Function) lastFunction = shape->GetLastFunction();
952 if (lastFunction.IsNull()) return false;
954 // add a new result object
955 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
957 // add an Export function
958 Handle(GEOM_Function) exportFunction = result->AddFunction(GEOMImpl_XAODriver::GetID(), IMPORTEXPORT_EXPORTXAO);
959 if (exportFunction.IsNull()) return false;
960 if (exportFunction->GetDriverGUID() != GEOMImpl_XAODriver::GetID()) return false;
962 // create the XAO object
963 XAO::Xao* xaoObject = new XAO::Xao();
964 xaoObject->setAuthor(author);
967 XAO::BrepGeometry* geometry = (XAO::BrepGeometry*)XAO::Geometry::createGeometry(XAO::BREP);
968 TopoDS_Shape topoShape = shape->GetValue();
969 exportFunction->SetValue(topoShape);
970 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)geometry;
971 brep->setTopoDS_Shape(topoShape);
973 geometry->setName(shape->GetName().ToCString());
974 exportSubshapes(shape, geometry);
975 xaoObject->setGeometry(geometry);
977 exportGroups(groupList, xaoObject, geometry);
978 exportFields(fieldList, xaoObject, geometry);
980 // export the XAO to the file
981 xaoObject->exportXAO(fileName);
983 // make a Python command
984 GEOM::TPythonDump pd(exportFunction);
985 pd << "exported = geompy.ExportXAO(" << shape;
989 if (groupList.size() > 0)
991 std::list<Handle(GEOM_Object)>::iterator itGroup = groupList.begin();
993 while (itGroup != groupList.end())
995 pd << ", " << (*itGroup++);
1001 if (fieldList.size() > 0)
1003 std::list<Handle(GEOM_Object)>::iterator itField = fieldList.begin();
1005 while (itField != fieldList.end())
1007 pd << ", " << (*itField++);
1011 pd << author << ", \"" << fileName << "\")";
1019 void GEOMImpl_IInsertOperations::importSubShapes(XAO::Geometry* xaoGeometry,
1020 Handle(GEOM_Function) function, int shapeType, int dim,
1021 Handle(TColStd_HSequenceOfTransient)& subShapeList)
1023 Handle(GEOM_Object) subShape;
1024 Handle(GEOM_Function) aFunction;
1025 Handle(TColStd_HArray1OfInteger) anArray;
1027 XAO::GeometricElementList::iterator elementIterator = xaoGeometry->begin((XAO::Dimension)dim);
1028 for (; elementIterator != xaoGeometry->end((XAO::Dimension)dim); elementIterator++)
1030 XAO::GeometricElement element = elementIterator->second;
1031 if (!element.hasName())
1034 std::string name = element.getName();
1035 std::string ref = element.getReference();
1036 int iref = XAO::XaoUtils::stringToInt(ref);
1038 anArray = new TColStd_HArray1OfInteger(1, 1);
1039 anArray->SetValue(1, iref);
1041 subShape = GetEngine()->AddObject(GetDocID(), GEOM_SUBSHAPE);
1042 Handle(GEOM_Function) aFunction = subShape->AddFunction(GEOM_Object::GetSubShapeID(), 1);
1043 if (aFunction.IsNull())
1046 subShape->SetName(name.c_str());
1047 subShape->SetType(shapeType);
1049 GEOM_ISubShape aSSI(aFunction);
1050 aSSI.SetMainShape(function);
1051 aSSI.SetIndices(anArray);
1053 //aFunction->SetValue(aValue);
1054 subShapeList->Append(subShape);
1056 // Put this subshape in the list of sub-shapes of theMainShape
1057 function->AddSubShapeReference(aFunction);
1061 //=============================================================================
1063 * Import a shape from XAO format
1064 * \param fileName The name of the file to import
1065 * \param shape The imported shape
1066 * \param subShapes The list of imported groups
1067 * \param groups The list of imported groups
1068 * \param fields The list of imported fields
1069 * \return boolean indicating if import was succeful.
1071 //=============================================================================
1072 bool GEOMImpl_IInsertOperations::ImportXAO(const char* fileName,
1073 Handle(GEOM_Object)& shape,
1074 Handle(TColStd_HSequenceOfTransient)& subShapes,
1075 Handle(TColStd_HSequenceOfTransient)& groups,
1076 Handle(TColStd_HSequenceOfTransient)& fields)
1080 if (fileName == NULL || groups.IsNull() || fields.IsNull())
1084 XAO::Xao* xaoObject = new XAO::Xao();
1087 xaoObject->importXAO(fileName);
1089 catch (XAO::XAO_Exception& exc)
1092 SetErrorCode(exc.what());
1096 XAO::Geometry* xaoGeometry = xaoObject->getGeometry();
1097 if (xaoGeometry == NULL)
1100 SetErrorCode("Cannot import XAO: geometry format not supported.");
1105 shape = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
1106 Handle(GEOM_Function) function = shape->AddFunction(GEOMImpl_XAODriver::GetID(), IMPORTEXPORT_EXPORTXAO);
1107 if (function.IsNull()) return false;
1108 if (function->GetDriverGUID() != GEOMImpl_XAODriver::GetID()) return false;
1111 if (xaoGeometry->getFormat() == XAO::BREP)
1113 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)xaoGeometry;
1114 TopoDS_Shape geomShape = brep->getTopoDS_Shape();
1115 function->SetValue(geomShape);
1116 shape->SetName(xaoGeometry->getName().c_str());
1121 SetErrorCode("Cannot import XAO: geometry format not supported.");
1125 // create sub shapes with names
1126 importSubShapes(xaoGeometry, function, GEOM_POINT, XAO::VERTEX, subShapes);
1127 importSubShapes(xaoGeometry, function, GEOM_EDGE, XAO::EDGE, subShapes);
1128 importSubShapes(xaoGeometry, function, GEOM_FACE, XAO::FACE, subShapes);
1129 importSubShapes(xaoGeometry, function, GEOM_SOLID, XAO::SOLID, subShapes);
1132 int nbGroups = xaoObject->countGroups();
1133 for (int i = 0; i < nbGroups; ++i)
1135 XAO::Group* xaoGroup = xaoObject->getGroup(i);
1137 // build an array with the indexes of the sub shapes
1138 int nbElt = xaoGroup->count();
1139 Handle(TColStd_HArray1OfInteger) array = new TColStd_HArray1OfInteger(1, nbElt);
1141 for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it)
1144 std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
1145 array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
1148 // create the group with the array of sub shapes indexes
1149 Handle(GEOM_Object) group = GetEngine()->AddSubShape(shape, array);
1150 group->SetType(GEOM_GROUP);
1151 group->SetName(xaoGroup->getName().c_str());
1153 // Set a sub-shape type
1154 TDF_Label freeLabel = group->GetFreeLabel();
1155 TDataStd_Integer::Set(freeLabel, (Standard_Integer) getGroupDimension(xaoGroup));
1156 groups->Append(group);
1158 function = group->GetLastFunction();
1161 // TODO: create the fields
1163 // make a Python command
1164 GEOM::TPythonDump pd(function);
1165 pd << "(imported, " << shape << ", ";
1167 // list of sub shapes
1169 int nbSubshapes = subShapes->Length();
1170 std::cout << "Nb SubShapes = " << nbSubshapes << std::endl;
1171 if (nbSubshapes > 0)
1173 for (int i = 1; i <= nbSubshapes; i++)
1175 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(subShapes->Value(i));
1176 pd << obj << ((i < nbSubshapes) ? ", " : "");
1184 for (int i = 1; i <= nbGroups; i++)
1186 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(groups->Value(i));
1187 pd << obj << ((i < nbGroups) ? ", " : "");
1195 pd << ") = geompy.ImportXAO(\"" << fileName << "\")";