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_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 Standard_Integer aLastInd = 4;
95 if (theFormatName.Search("STL") == 1) {
99 return theFormatName.SubString(1, aLastInd);
102 //=============================================================================
106 //=============================================================================
107 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
108 : GEOM_IOperations(theEngine, theDocID)
110 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
111 myShapesOperations = new GEOMImpl_IShapesOperations(GetEngine(), GetDocID());
112 myGroupOperations = new GEOMImpl_IGroupOperations(GetEngine(), GetDocID());
113 myFieldOperations = new GEOMImpl_IFieldOperations(GetEngine(), GetDocID());
116 //=============================================================================
120 //=============================================================================
121 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
123 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
124 delete myShapesOperations;
125 delete myGroupOperations;
126 delete myFieldOperations;
129 //=============================================================================
133 //=============================================================================
134 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
138 if (theOriginal.IsNull()) return NULL;
140 //Add a new Copy object
141 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
143 //Add a Copy function for creation a copy object
144 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
146 //Check if the function is set correctly
147 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
149 GEOMImpl_ICopy aCI(aFunction);
151 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
152 if (aRefFunction.IsNull()) return NULL;
154 aCI.SetOriginal(aRefFunction);
156 //Compute the Copy value
158 #if OCC_VERSION_LARGE > 0x06010000
161 if (!GetSolver()->ComputeFunction(aFunction)) {
162 SetErrorCode("Copy driver failed");
166 catch (Standard_Failure) {
167 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
168 SetErrorCode(aFail->GetMessageString());
172 //Make a Python command
173 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
179 //=============================================================================
183 //=============================================================================
184 void GEOMImpl_IInsertOperations::Export
185 (const Handle(GEOM_Object) theOriginal,
186 const TCollection_AsciiString& theFileName,
187 const TCollection_AsciiString& theFormatName)
191 if (theOriginal.IsNull()) return;
193 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
194 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
196 //Add a new result object
197 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
199 //Add an Export function
200 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
201 if (aFunction.IsNull()) return;
203 //Check if the function is set correctly
204 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
206 Handle(TCollection_HAsciiString) aHLibName;
207 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
210 TCollection_AsciiString aLibName = aHLibName->String();
213 GEOMImpl_IImportExport aCI (aFunction);
214 aCI.SetOriginal(aRefFunction);
215 aCI.SetFileName(theFileName);
216 aCI.SetFormatName(theFormatName);
217 aCI.SetPluginName(aLibName);
221 #if OCC_VERSION_LARGE > 0x06010000
224 if (!GetSolver()->ComputeFunction(aFunction)) {
225 SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
229 catch (Standard_Failure) {
230 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
231 SetErrorCode(aFail->GetMessageString());
235 //Make a Python command
236 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
237 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
242 //=============================================================================
246 //=============================================================================
247 Handle(GEOM_Object) GEOMImpl_IInsertOperations::Import
248 (const TCollection_AsciiString& theFileName,
249 const TCollection_AsciiString& theFormatName)
253 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
255 //Add a new result object
256 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
258 //Add an Import function
259 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
260 if (aFunction.IsNull()) return result;
262 //Check if the function is set correctly
263 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return result;
265 Handle(TCollection_HAsciiString) aHLibName;
267 (Standard_True, GetImportFormatName(theFormatName), aHLibName)) {
270 TCollection_AsciiString aLibName = aHLibName->String();
273 GEOMImpl_IImportExport aCI (aFunction);
274 aCI.SetFileName(theFileName);
275 aCI.SetFormatName(theFormatName);
276 aCI.SetPluginName(aLibName);
280 #if OCC_VERSION_LARGE > 0x06010000
283 if (!GetSolver()->ComputeFunction(aFunction)) {
284 SetErrorCode("Import driver failed");
288 catch (Standard_Failure) {
289 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
290 SetErrorCode(aFail->GetMessageString());
294 //Make a Python command
295 if (theFormatName != "IGES_UNIT") {
296 GEOM::TPythonDump pd (aFunction);
297 if (theFormatName == "BREP")
298 pd << result << " = geompy.ImportBREP(\"" << theFileName.ToCString() << "\")";
299 else if (theFormatName == "IGES")
300 pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\")";
301 else if (theFormatName == "IGES_SCALE")
302 pd << result << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\", True)";
303 else if (theFormatName == "STEP")
304 pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\")";
305 else if (theFormatName == "STEP_SCALE")
306 pd << result << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", True)";
308 pd << result << " = geompy.ImportFile(\""
309 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
316 if (theFormatName == "IGES_UNIT") {
317 TopoDS_Shape S = aFunction->GetValue();
318 TopoDS_Vertex V = TopoDS::Vertex(S);
319 gp_Pnt P = BRep_Tool::Pnt(V);
320 double scale = P.X();
321 TCollection_AsciiString aUnitName = "UNIT_M";
322 if (fabs(scale-0.01) < 1.e-6)
323 aUnitName = "UNIT_CM";
324 else if (fabs(scale-0.001) < 1.e-6)
325 aUnitName = "UNIT_MM";
326 //cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
327 SetErrorCode(aUnitName);
334 //=============================================================================
338 //=============================================================================
339 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
340 (const TCollection_AsciiString& theFileName,
341 const TCollection_AsciiString& theFormatName,
342 const TCollection_AsciiString& theParameterName)
346 TCollection_AsciiString aValue, anError;
348 if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
350 Handle(TCollection_HAsciiString) aHLibName;
352 (Standard_True, GetImportFormatName(theFormatName), aHLibName)) {
355 TCollection_AsciiString aLibName = aHLibName->String();
357 aValue = GEOMImpl_ImportDriver::ReadValue(theFileName, aLibName, theParameterName, anError);
358 if (anError.IsEmpty())
361 SetErrorCode(anError.ToCString());
366 //=============================================================================
370 //=============================================================================
371 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
372 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
373 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
375 if (theFormats.IsNull())
376 theFormats = new TColStd_HSequenceOfAsciiString;
380 if (thePatterns.IsNull())
381 thePatterns = new TColStd_HSequenceOfAsciiString;
383 thePatterns->Clear();
385 if (!InitResMgr()) return Standard_False;
387 // Read Import formats list from install directory
388 if (myResMgr->Find("Import")) {
389 TCollection_AsciiString aFormats (myResMgr->Value("Import"));
390 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
392 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
393 theFormats->Append(aToken);
397 // Read Import formats from user directory
398 if (myResMgrUser->Find("Import")) {
399 TCollection_AsciiString aFormats (myResMgrUser->Value("Import"));
400 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
402 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
403 int aLenFormats = theFormats->Length();
404 bool isFound = false;
405 for(int aInd=1;aInd<=aLenFormats;aInd++){
406 if( theFormats->Value(aInd) == aToken){
412 theFormats->Append(aToken);
416 // Read Patterns for each supported format
417 int j = 1, len = theFormats->Length();
418 for (; j <= len; j++) {
419 TCollection_AsciiString aKey, aPattern;
420 aKey = theFormats->Value(j) + ".ImportPattern";
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 aKey = theFormats->Value(j) + ".Pattern";
427 if (myResMgr->Find(aKey.ToCString()))
428 aPattern = myResMgr->Value(aKey.ToCString());
429 else if(myResMgrUser->Find(aKey.ToCString()))
430 aPattern = myResMgrUser->Value(aKey.ToCString());
432 aPattern = theFormats->Value(j);
433 aPattern += " Files ( *.* )";
436 thePatterns->Append(aPattern);
439 return (!theFormats->IsEmpty());
442 //=============================================================================
446 //=============================================================================
447 Standard_Boolean GEOMImpl_IInsertOperations::ExportTranslators
448 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
449 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
451 if (theFormats.IsNull())
452 theFormats = new TColStd_HSequenceOfAsciiString;
456 if (thePatterns.IsNull())
457 thePatterns = new TColStd_HSequenceOfAsciiString;
459 thePatterns->Clear();
461 if (!InitResMgr()) return Standard_False;
463 // Read Export formats list from install directory
464 if (myResMgr->Find("Export")) {
465 TCollection_AsciiString aFormats (myResMgr->Value("Export"));
466 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
468 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
469 theFormats->Append(aToken);
473 // Read Export formats list from user directory
474 if (myResMgrUser->Find("Export")) {
475 TCollection_AsciiString aFormats (myResMgrUser->Value("Export"));
476 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
478 for (; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
479 int aLenFormats = theFormats->Length();
480 bool isFound = false;
481 for(int aInd=1;aInd<=aLenFormats;aInd++){
482 if( theFormats->Value(aInd) == aToken){
488 theFormats->Append(aToken);
492 // Read Patterns for each supported format
493 int j = 1, len = theFormats->Length();
494 for (; j <= len; j++) {
495 TCollection_AsciiString aKey, aPattern;
496 aKey = theFormats->Value(j) + ".ExportPattern";
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 aKey = theFormats->Value(j) + ".Pattern";
503 if (myResMgr->Find(aKey.ToCString()))
504 aPattern = myResMgr->Value(aKey.ToCString());
505 else if (myResMgrUser->Find(aKey.ToCString()))
506 aPattern = myResMgrUser->Value(aKey.ToCString());
508 aPattern = theFormats->Value(j);
509 aPattern += " Files ( *.* )";
512 thePatterns->Append(aPattern);
515 return (!theFormats->IsEmpty());
518 //=============================================================================
522 //=============================================================================
523 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
524 (const Standard_Boolean isImport,
525 const TCollection_AsciiString& theFormat,
526 Handle(TCollection_HAsciiString)& theLibName)
528 if (!InitResMgr()) return Standard_False;
530 // Import/Export mode
531 TCollection_AsciiString aMode;
532 //Standard_CString aMode;
533 if (isImport) aMode = "Import";
534 else aMode = "Export";
536 // Read supported formats for the certain mode from install directory
537 if (myResMgr->Find(aMode.ToCString())) {
538 TCollection_AsciiString aFormats (myResMgr->Value(aMode.ToCString()));
539 if (aFormats.Search(theFormat) > -1) {
540 // Read library name for the supported format
541 TCollection_AsciiString aKey (theFormat);
544 if (myResMgr->Find(aKey.ToCString())) {
545 TCollection_AsciiString aLibName (myResMgr->Value(aKey.ToCString()));
547 if ( aLibName.Length() > 3 && aLibName.SubString(1,3) != "lib" )
548 aLibName.Prepend("lib");
553 theLibName = new TCollection_HAsciiString (aLibName);
554 return Standard_True;
559 // Read supported formats for the certain mode from user directory
560 if (myResMgrUser->Find(aMode.ToCString())) {
561 TCollection_AsciiString aFormats (myResMgrUser->Value(aMode.ToCString()));
562 if (aFormats.Search(theFormat) > -1) {
563 // Read library name for the supported format
564 TCollection_AsciiString aKey (theFormat);
567 if (myResMgrUser->Find(aKey.ToCString())) {
568 TCollection_AsciiString aLibName (myResMgrUser->Value(aKey.ToCString()));
570 if ( aLibName.Length() > 3 && aLibName.SubString(1,3) != "lib" )
571 aLibName.Prepend("lib");
576 theLibName = new TCollection_HAsciiString (aLibName);
577 return Standard_True;
582 return Standard_False;
585 //=============================================================================
589 //=============================================================================
590 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
592 bool isResourceFound = false;
593 bool isResourceFoundUser = false;
594 TCollection_AsciiString aUserResDir,aResDir;
596 if (myResMgr.IsNull()) {
597 // Initialize the Resource Manager
598 TCollection_AsciiString aNull;
599 aResDir = TCollection_AsciiString(getenv("GEOM_ROOT_DIR"));
601 aResDir += "\\share\\salome\\resources\\geom";
603 aResDir += "/share/salome/resources/geom";
606 myResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
608 isResourceFound = true;
609 if (!myResMgr->Find("Import") && !myResMgr->Find("Export")) {
610 // instead of complains in Resource_Manager
611 isResourceFound = false;
612 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
615 isResourceFound = true;
617 if (myResMgrUser.IsNull()) {
618 char * dir = getenv("GEOM_ENGINE_RESOURCES_DIR");
619 TCollection_AsciiString aNull;
626 aUserResDir = getenv("HOME");
628 aUserResDir += "\\.salome\\resources";
630 aUserResDir += "/.salome/resources";
634 myResMgrUser = new Resource_Manager ("ImportExport", aNull, aUserResDir, Standard_False);
636 isResourceFoundUser = true;
638 if (!myResMgrUser->Find("Import") && !myResMgrUser->Find("Export")) {
639 // instead of complains in Resource_Manager
640 isResourceFoundUser = false;
644 isResourceFoundUser = true;
646 if(!isResourceFound && !isResourceFoundUser){
647 INFOS("No valid file \"ImportExport\" found in " << aResDir.ToCString());
648 INFOS("No valid file \"ImportExport\" found in " << aUserResDir.ToCString() );
651 return ( myResMgr->Find("Import") || myResMgr->Find("Export") ||
652 myResMgrUser->Find("Import") || myResMgrUser->Find("Export"));
655 //=============================================================================
659 //=============================================================================
660 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
664 //Add a new result object
665 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
667 //Add a Copy function
668 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
669 if (aFunction.IsNull()) return NULL;
671 //Check if the function is set correctly
672 if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
674 //Read a shape from the stream
677 BRepTools::Read(aShape, theStream, B);
678 if (aShape.IsNull()) {
679 SetErrorCode("RestoreShape error: BREP reading failed");
683 aFunction->SetValue(aShape);
685 //Special dump to avoid restored shapes publication.
686 //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
687 //GEOM::TPythonDump(aFunction) << "#";
689 GEOM::TPythonDump(aFunction) << result
690 << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
697 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
701 if (theTextureFile.IsEmpty()) return 0;
703 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
704 Handle(TColStd_HArray1OfByte) aTexture;
706 Handle(TDataStd_HArray1OfByte) aTexture;
709 FILE* fp = fopen(theTextureFile.ToCString(), "r");
712 std::list<std::string> lines;
716 if ((fgets(buffer, 4096, fp)) == NULL) break;
717 int aLen = strlen(buffer);
718 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
719 lines.push_back(buffer);
720 maxlen = std::max(maxlen, (int)strlen(buffer));
725 int lenbytes = maxlen/8;
726 if (maxlen%8) lenbytes++;
728 if (lenbytes == 0 || lines.empty())
731 std::list<unsigned char> bytedata;
732 std::list<std::string>::const_iterator it;
733 for (it = lines.begin(); it != lines.end(); ++it) {
734 std::string line = *it;
735 int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
736 for (int i = 0; i < lenline/8; i++) {
737 unsigned char byte = 0;
738 for (int j = 0; j < 8; j++)
739 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
740 bytedata.push_back(byte);
742 for (int i = lenline/8; i < lenbytes; i++)
743 bytedata.push_back((unsigned char)0);
746 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
749 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
750 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
752 aTexture = new TDataStd_HArray1OfByte (1, lines.size()*lenbytes);
755 std::list<unsigned char>::iterator bdit;
757 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
758 aTexture->SetValue(i, (Standard_Byte)(*bdit));
760 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
761 if (aTextureId > 0) SetErrorCode(OK);
765 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
766 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
767 const Handle(TColStd_HArray1OfByte)& theTexture)
769 const Handle(TDataStd_HArray1OfByte)& theTexture)
773 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
774 if (aTextureId > 0) SetErrorCode(OK);
778 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
779 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
781 Handle(TDataStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
783 int& theWidth, int& theHeight)
787 #if OCC_VERSION_LARGE > 0x06040000 // Porting to OCCT6.5.1
788 Handle(TColStd_HArray1OfByte) aTexture;
790 Handle(TDataStd_HArray1OfByte) aTexture;
793 theWidth = theHeight = 0;
794 TCollection_AsciiString aFileName;
796 if (theTextureId <= 0)
799 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
801 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
806 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
809 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
814 TopAbs_ShapeEnum getGroupDimension(XAO::Group* group)
816 XAO::Dimension dim = group->getDimension();
817 TopAbs_ShapeEnum rdim;
821 rdim = TopAbs_VERTEX; break;
823 rdim = TopAbs_EDGE; break;
825 rdim = TopAbs_FACE; break;
827 rdim = TopAbs_SOLID; break;
829 rdim = TopAbs_COMPOUND; break;
834 XAO::Dimension shapeEnumToDimension(const TopAbs_ShapeEnum& shape)
839 dim = XAO::VERTEX; break;
841 dim = XAO::EDGE; break;
843 dim = XAO::FACE; break;
845 dim = XAO::SOLID; break;
847 throw SALOME_Exception("Bad type"); // TODO
852 void GEOMImpl_IInsertOperations::exportGroups(std::list<Handle(GEOM_Object)> groupList,
854 XAO::BrepGeometry* geometry)
857 std::list<Handle(GEOM_Object)>::iterator groupIterator = groupList.begin();
858 while (groupIterator != groupList.end())
860 Handle(GEOM_Object) currGroup = (*groupIterator++);
861 Handle(TColStd_HArray1OfInteger) groupIds = myGroupOperations->GetObjects(currGroup);
863 TopAbs_ShapeEnum shapeGroup = myGroupOperations->GetType(currGroup);
864 XAO::Dimension dim = shapeEnumToDimension(shapeGroup);
865 XAO::Group* group = xaoObject->addGroup(dim, currGroup->GetName().ToCString());
870 for (int i = 1; i <= groupIds->Length(); i++)
872 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
873 int index = geometry->getVertexIndexByReference(ref);
878 for (int i = 1; i <= groupIds->Length(); i++)
880 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
881 int index = geometry->getEdgeIndexByReference(ref);
886 for (int i = 1; i <= groupIds->Length(); i++)
888 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
889 int index = geometry->getFaceIndexByReference(ref);
894 for (int i = 1; i <= groupIds->Length(); i++)
896 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
897 int index = geometry->getSolidIndexByReference(ref);
905 void GEOMImpl_IInsertOperations::exportFields(std::list<Handle(GEOM_Field)> fieldList,
907 XAO::BrepGeometry* geometry)
909 std::list<Handle(GEOM_Field)>::iterator fieldIterator = fieldList.begin();
910 while (fieldIterator != fieldList.end())
912 Handle(GEOM_Field) currField = (*fieldIterator++);
914 int fdim = currField->GetDimension();
915 int ftype = currField->GetDataType();
916 int nbComponents = currField->GetNbComponents();
917 std::string name = currField->GetName().ToCString();
919 XAO::Field* field = xaoObject->addField((XAO::Type)ftype, (XAO::Dimension)fdim, nbComponents, name);
921 Handle(TColStd_HArray1OfExtendedString) components = currField->GetComponents();
922 std::cout << "nb comp " << nbComponents << std::endl;
923 std::cout << components->Lower() << " - " << components->Upper() << std::endl;
924 for (int i = components->Lower(), j = 0; i <= components->Upper(); ++i, ++j)
926 field->setComponentName(j, TCollection_AsciiString(components->Value(i)).ToCString());
929 std::list< Handle(GEOM_FieldStep)> steps = currField->GetSteps();
930 std::list<Handle(GEOM_FieldStep)>::iterator stepIterator = steps.begin();
931 while (stepIterator != steps.end())
933 Handle(GEOM_FieldStep) currStep = (*stepIterator++);
935 XAO::Step* step = field->addNewStep(currStep->GetID());
936 step->setStamp(currStep->GetStamp());
942 XAO::BooleanStep* bs = (XAO::BooleanStep*)step;
943 Handle(TColStd_HArray1OfInteger) bvalues = currStep->GetIntValues();
944 std::vector<bool> bv;
945 bv.reserve(bvalues->Upper());
946 for ( int i = bvalues->Lower(), nb = bvalues->Upper(); i <= nb; ++i )
948 bv.push_back(bvalues->Value(i) != 0);
955 XAO::IntegerStep* is = (XAO::IntegerStep*)step;
956 Handle(TColStd_HArray1OfInteger) ivalues = currStep->GetIntValues();
958 iv.reserve(ivalues->Upper());
959 for ( int i = ivalues->Lower(), nb = ivalues->Upper(); i <= nb; ++i )
961 iv.push_back(ivalues->Value(i));
968 XAO::DoubleStep* ds = (XAO::DoubleStep*)step;
969 Handle(TColStd_HArray1OfReal) dvalues = currStep->GetDoubleValues();
970 std::vector<double> dv;
971 dv.reserve(dvalues->Upper());
972 for ( int i = dvalues->Lower(), nb = dvalues->Upper(); i <= nb; ++i )
974 dv.push_back(dvalues->Value(i));
981 XAO::StringStep* ss = (XAO::StringStep*)step;
982 Handle(TColStd_HArray1OfExtendedString) svalues = currStep->GetStringValues();
983 std::vector<std::string> sv;
984 sv.reserve(svalues->Upper());
985 for ( int i = svalues->Lower(), nb = svalues->Upper(); i <= nb; ++i )
987 sv.push_back(TCollection_AsciiString(svalues->Value(i)).ToCString());
997 void GEOMImpl_IInsertOperations::exportSubshapes(const Handle(GEOM_Object)& shape, XAO::BrepGeometry* geometry)
999 Handle(TColStd_HSequenceOfTransient) subObjects = myShapesOperations->GetExistingSubObjects(shape, false);
1000 int nbSubObjects = subObjects->Length();
1001 // set the names of the sub shapes
1002 for (int i = 1; i <= nbSubObjects; i++)
1004 Handle(Standard_Transient) transientSubObject = subObjects->Value(i);
1005 if (transientSubObject.IsNull())
1008 Handle(GEOM_Object) subObject = Handle(GEOM_Object)::DownCast(transientSubObject);
1009 if (subObject->GetType() != GEOM_GROUP)
1011 int subIndex = myShapesOperations->GetSubShapeIndex(shape, subObject);
1012 switch (subObject->GetValue().ShapeType())
1015 geometry->changeVertexName(subIndex, subObject->GetName().ToCString());
1018 geometry->changeEdgeName(subIndex, subObject->GetName().ToCString());
1021 geometry->changeFaceName(subIndex, subObject->GetName().ToCString());
1024 geometry->changeSolidName(subIndex, subObject->GetName().ToCString());
1031 //=============================================================================
1033 * Export a shape to XAO format
1034 * \param shape The shape to export
1035 * \param groups The list of groups to export
1036 * \param fields The list of fields to export
1037 * \param fileName The name of the file to exported
1038 * \return boolean indicating if export was succeful.
1040 //=============================================================================
1041 bool GEOMImpl_IInsertOperations::ExportXAO(Handle(GEOM_Object) shape,
1042 std::list<Handle(GEOM_Object)> groupList,
1043 std::list<Handle(GEOM_Field)> fieldList,
1045 const char* fileName)
1049 if (shape.IsNull()) return false;
1051 // add a new shape function with parameters
1052 Handle(GEOM_Function) lastFunction = shape->GetLastFunction();
1053 if (lastFunction.IsNull()) return false;
1055 // add a new result object
1056 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
1058 // add an Export function
1059 Handle(GEOM_Function) exportFunction = result->AddFunction(GEOMImpl_XAODriver::GetID(), IMPORTEXPORT_EXPORTXAO);
1060 if (exportFunction.IsNull()) return false;
1061 if (exportFunction->GetDriverGUID() != GEOMImpl_XAODriver::GetID()) return false;
1063 // create the XAO object
1064 XAO::Xao* xaoObject = new XAO::Xao();
1065 xaoObject->setAuthor(author);
1068 XAO::BrepGeometry* geometry = (XAO::BrepGeometry*)XAO::Geometry::createGeometry(XAO::BREP);
1069 TopoDS_Shape topoShape = shape->GetValue();
1070 exportFunction->SetValue(topoShape);
1071 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)geometry;
1072 brep->setTopoDS_Shape(topoShape);
1074 geometry->setName(shape->GetName().ToCString());
1075 exportSubshapes(shape, geometry);
1076 xaoObject->setGeometry(geometry);
1078 exportGroups(groupList, xaoObject, geometry);
1079 exportFields(fieldList, xaoObject, geometry);
1081 // export the XAO to the file
1082 xaoObject->exportXAO(fileName);
1084 // make a Python command
1085 GEOM::TPythonDump pd(exportFunction);
1086 pd << "exported = geompy.ExportXAO(" << shape;
1090 if (groupList.size() > 0)
1092 std::list<Handle(GEOM_Object)>::iterator itGroup = groupList.begin();
1094 while (itGroup != groupList.end())
1096 pd << ", " << (*itGroup++);
1102 if (fieldList.size() > 0)
1104 std::list<Handle(GEOM_Field)>::iterator itField = fieldList.begin();
1106 while (itField != fieldList.end())
1108 pd << ", " << (*itField++);
1112 pd << author << ", \"" << fileName << "\")";
1120 void GEOMImpl_IInsertOperations::importSubShapes(XAO::Geometry* xaoGeometry,
1121 Handle(GEOM_Function) function, int shapeType, int dim,
1122 Handle(TColStd_HSequenceOfTransient)& subShapeList)
1124 Handle(GEOM_Object) subShape;
1125 Handle(GEOM_Function) aFunction;
1126 Handle(TColStd_HArray1OfInteger) anArray;
1128 XAO::GeometricElementList::iterator elementIterator = xaoGeometry->begin((XAO::Dimension)dim);
1129 for (; elementIterator != xaoGeometry->end((XAO::Dimension)dim); elementIterator++)
1131 XAO::GeometricElement element = elementIterator->second;
1132 if (!element.hasName())
1135 std::string name = element.getName();
1136 std::string ref = element.getReference();
1137 int iref = XAO::XaoUtils::stringToInt(ref);
1139 anArray = new TColStd_HArray1OfInteger(1, 1);
1140 anArray->SetValue(1, iref);
1142 subShape = GetEngine()->AddObject(GetDocID(), GEOM_SUBSHAPE);
1143 Handle(GEOM_Function) aFunction = subShape->AddFunction(GEOM_Object::GetSubShapeID(), 1);
1144 if (aFunction.IsNull())
1147 subShape->SetName(name.c_str());
1148 subShape->SetType(shapeType);
1150 GEOM_ISubShape aSSI(aFunction);
1151 aSSI.SetMainShape(function);
1152 aSSI.SetIndices(anArray);
1154 //aFunction->SetValue(aValue);
1155 subShapeList->Append(subShape);
1157 // Put this subshape in the list of sub-shapes of theMainShape
1158 function->AddSubShapeReference(aFunction);
1162 //=============================================================================
1164 * Import a shape from XAO format
1165 * \param fileName The name of the file to import
1166 * \param shape The imported shape
1167 * \param subShapes The list of imported groups
1168 * \param groups The list of imported groups
1169 * \param fields The list of imported fields
1170 * \return boolean indicating if import was succeful.
1172 //=============================================================================
1173 bool GEOMImpl_IInsertOperations::ImportXAO(const char* fileName,
1174 Handle(GEOM_Object)& shape,
1175 Handle(TColStd_HSequenceOfTransient)& subShapes,
1176 Handle(TColStd_HSequenceOfTransient)& groups,
1177 Handle(TColStd_HSequenceOfTransient)& fields)
1181 if (fileName == NULL || groups.IsNull() || fields.IsNull())
1185 XAO::Xao* xaoObject = new XAO::Xao();
1188 xaoObject->importXAO(fileName);
1190 catch (XAO::XAO_Exception& exc)
1193 SetErrorCode(exc.what());
1197 XAO::Geometry* xaoGeometry = xaoObject->getGeometry();
1198 if (xaoGeometry == NULL)
1201 SetErrorCode("Cannot import XAO: geometry format not supported.");
1206 shape = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
1207 Handle(GEOM_Function) function = shape->AddFunction(GEOMImpl_XAODriver::GetID(), IMPORTEXPORT_EXPORTXAO);
1208 if (function.IsNull()) return false;
1209 if (function->GetDriverGUID() != GEOMImpl_XAODriver::GetID()) return false;
1212 if (xaoGeometry->getFormat() == XAO::BREP)
1214 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)xaoGeometry;
1215 TopoDS_Shape geomShape = brep->getTopoDS_Shape();
1216 function->SetValue(geomShape);
1217 shape->SetName(xaoGeometry->getName().c_str());
1222 SetErrorCode("Cannot import XAO: geometry format not supported.");
1226 // create sub shapes with names
1227 importSubShapes(xaoGeometry, function, GEOM_POINT, XAO::VERTEX, subShapes);
1228 importSubShapes(xaoGeometry, function, GEOM_EDGE, XAO::EDGE, subShapes);
1229 importSubShapes(xaoGeometry, function, GEOM_FACE, XAO::FACE, subShapes);
1230 importSubShapes(xaoGeometry, function, GEOM_SOLID, XAO::SOLID, subShapes);
1233 int nbGroups = xaoObject->countGroups();
1234 for (int i = 0; i < nbGroups; ++i)
1236 XAO::Group* xaoGroup = xaoObject->getGroup(i);
1238 // build an array with the indexes of the sub shapes
1239 int nbElt = xaoGroup->count();
1240 Handle(TColStd_HArray1OfInteger) array = new TColStd_HArray1OfInteger(1, nbElt);
1242 for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it)
1245 std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
1246 array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
1249 // create the group with the array of sub shapes indexes
1250 Handle(GEOM_Object) group = GetEngine()->AddSubShape(shape, array);
1251 group->SetType(GEOM_GROUP);
1252 group->SetName(xaoGroup->getName().c_str());
1254 // Set a sub-shape type
1255 TDF_Label freeLabel = group->GetFreeLabel();
1256 TDataStd_Integer::Set(freeLabel, (Standard_Integer) getGroupDimension(xaoGroup));
1257 groups->Append(group);
1259 function = group->GetLastFunction();
1262 // create the fields
1263 int nbFields = xaoObject->countFields();
1264 for (int i = 0; i < nbFields; ++i)
1266 XAO::Field* xaoField = xaoObject->getField(i);
1268 Handle(TColStd_HArray1OfExtendedString) components = new TColStd_HArray1OfExtendedString(0, xaoField->countComponents());
1269 for (int j = 0; j < xaoField->countComponents(); ++j)
1271 components->SetValue(j, (TCollection_ExtendedString)xaoField->getComponentName(j).c_str());
1274 Handle(GEOM_Field) field = myFieldOperations->CreateField(shape,
1275 xaoField->getName().c_str(),
1276 (int)xaoField->getType(),
1277 (int)xaoField->getDimension(),
1280 switch (xaoField->getType())
1284 XAO::BooleanField* bfield = (XAO::BooleanField*)xaoField;
1285 for (int j = 0; j < xaoField->countSteps(); ++j)
1287 XAO::BooleanStep* bstep = bfield->getStep(j);
1288 Handle(GEOM_FieldStep) step = field->AddStep(bstep->getStep(), bstep->getStamp());
1290 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, bstep->countValues());
1291 std::vector<bool> bvalues = bstep->getValues();
1292 for (int k = 0; k < bstep->countValues(); ++k)
1294 values->SetValue(0, bvalues[k] ? 1 : 0);
1296 step->SetValues(values);
1302 XAO::IntegerField* ifield = (XAO::IntegerField*)xaoField;
1303 for (int j = 0; j < xaoField->countSteps(); ++j)
1305 XAO::IntegerStep* istep = ifield->getStep(j);
1306 Handle(GEOM_FieldStep) step = field->AddStep(istep->getStep(), istep->getStamp());
1308 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, istep->countValues());
1309 std::vector<int> ivalues = istep->getValues();
1310 for (int k = 0; k < istep->countValues(); ++k)
1312 values->SetValue(0, ivalues[k]);
1314 step->SetValues(values);
1320 XAO::DoubleField* dfield = (XAO::DoubleField*)xaoField;
1321 for (int j = 0; j < xaoField->countSteps(); ++j)
1323 XAO::DoubleStep* dstep = dfield->getStep(j);
1324 Handle(GEOM_FieldStep) step = field->AddStep(dstep->getStep(), dstep->getStamp());
1326 Handle(TColStd_HArray1OfReal) values = new TColStd_HArray1OfReal(0, dstep->countValues());
1327 std::vector<double> dvalues = dstep->getValues();
1328 for (int k = 0; k < dstep->countValues(); ++k)
1330 values->SetValue(0, dvalues[k]);
1332 step->SetValues(values);
1338 XAO::StringField* sfield = (XAO::StringField*)xaoField;
1339 for (int j = 0; j < xaoField->countSteps(); ++j)
1341 XAO::StringStep* sstep = sfield->getStep(j);
1342 Handle(GEOM_FieldStep) step = field->AddStep(sstep->getStep(), sstep->getStamp());
1344 Handle(TColStd_HArray1OfExtendedString) values = new TColStd_HArray1OfExtendedString(0, sstep->countValues());
1345 std::vector<std::string> svalues = sstep->getValues();
1346 for (int k = 0; k < sstep->countValues(); ++k)
1348 values->SetValue(0, TCollection_ExtendedString(svalues[k].c_str()));
1350 step->SetValues(values);
1356 fields->Append(field);
1359 // make a Python command
1360 GEOM::TPythonDump pd(function);
1361 pd << "(imported, " << shape << ", ";
1363 // list of sub shapes
1365 int nbSubshapes = subShapes->Length();
1366 std::cout << "Nb SubShapes = " << nbSubshapes << std::endl;
1367 if (nbSubshapes > 0)
1369 for (int i = 1; i <= nbSubshapes; i++)
1371 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(subShapes->Value(i));
1372 pd << obj << ((i < nbSubshapes) ? ", " : "");
1380 for (int i = 1; i <= nbGroups; i++)
1382 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(groups->Value(i));
1383 pd << obj << ((i < nbGroups) ? ", " : "");
1391 pd << ") = geompy.ImportXAO(\"" << fileName << "\")";