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_ChildIDIterator.hxx>
68 #include <TDF_Tool.hxx>
69 #include <TDataStd_Integer.hxx>
70 #include <TNaming_NamedShape.hxx>
71 #include <TDataStd_Comment.hxx>
72 #include <TopTools_IndexedMapOfShape.hxx>
76 #include <TopoDS_Vertex.hxx>
77 #include <BRep_Builder.hxx>
78 #include <BRep_Tool.hxx>
79 #include <BRepTools.hxx>
82 #include <TColStd_HArray1OfByte.hxx>
83 #include <TColStd_HArray1OfReal.hxx>
85 #include <Standard_Failure.hxx>
86 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
89 * This function returns the input format name from the original format name.
91 static TCollection_AsciiString GetImportFormatName
92 (const TCollection_AsciiString& theFormatName)
94 return theFormatName.Token("_");
97 //=============================================================================
101 //=============================================================================
102 GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations(GEOM_Engine* theEngine, int theDocID)
103 : GEOM_IOperations(theEngine, theDocID)
105 MESSAGE("GEOMImpl_IInsertOperations::GEOMImpl_IInsertOperations");
106 myShapesOperations = new GEOMImpl_IShapesOperations(GetEngine(), GetDocID());
107 myGroupOperations = new GEOMImpl_IGroupOperations(GetEngine(), GetDocID());
108 myFieldOperations = new GEOMImpl_IFieldOperations(GetEngine(), GetDocID());
111 //=============================================================================
115 //=============================================================================
116 GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations()
118 MESSAGE("GEOMImpl_IInsertOperations::~GEOMImpl_IInsertOperations");
119 delete myShapesOperations;
120 delete myGroupOperations;
121 delete myFieldOperations;
124 //=============================================================================
128 //=============================================================================
129 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeCopy (Handle(GEOM_Object) theOriginal)
133 if (theOriginal.IsNull()) return NULL;
135 //Add a new Copy object
136 Handle(GEOM_Object) aCopy = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
138 //Add a Copy function for creation a copy object
139 Handle(GEOM_Function) aFunction = aCopy->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITH_REF);
141 //Check if the function is set correctly
142 if(aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
144 GEOMImpl_ICopy aCI(aFunction);
146 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
147 if (aRefFunction.IsNull()) return NULL;
149 aCI.SetOriginal(aRefFunction);
151 //Compute the Copy value
154 if (!GetSolver()->ComputeFunction(aFunction)) {
155 SetErrorCode("Copy driver failed");
159 catch (Standard_Failure) {
160 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
161 SetErrorCode(aFail->GetMessageString());
165 //Make a Python command
166 GEOM::TPythonDump(aFunction) << aCopy << " = geompy.MakeCopy(" << theOriginal << ")";
172 //=============================================================================
176 //=============================================================================
177 void GEOMImpl_IInsertOperations::Export
178 (const Handle(GEOM_Object) theOriginal,
179 const TCollection_AsciiString& theFileName,
180 const TCollection_AsciiString& theFormatName)
184 if (theOriginal.IsNull()) return;
186 Handle(GEOM_Function) aRefFunction = theOriginal->GetLastFunction();
187 if (aRefFunction.IsNull()) return; //There is no function which creates an object to be exported
189 //Add a new result object
190 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
192 //Add an Export function
193 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_ExportDriver::GetID(), EXPORT_SHAPE);
194 if (aFunction.IsNull()) return;
196 //Check if the function is set correctly
197 if (aFunction->GetDriverGUID() != GEOMImpl_ExportDriver::GetID()) return;
199 Handle(TCollection_HAsciiString) aHLibName;
200 if (!IsSupported(Standard_False, theFormatName, aHLibName)) {
203 TCollection_AsciiString aLibName = aHLibName->String();
206 GEOMImpl_IImportExport aCI (aFunction);
207 aCI.SetOriginal(aRefFunction);
208 aCI.SetFileName(theFileName);
209 aCI.SetFormatName(theFormatName);
210 aCI.SetPluginName(aLibName);
215 if (!GetSolver()->ComputeFunction(aFunction)) {
216 SetErrorCode("Not enough space on disk, or you haven't permissions to write this directory");
220 catch (Standard_Failure) {
221 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
222 SetErrorCode(aFail->GetMessageString());
226 //Make a Python command
227 GEOM::TPythonDump(aFunction) << "geompy.Export(" << theOriginal << ", \""
228 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
233 //=============================================================================
237 //=============================================================================
238 Handle(TColStd_HSequenceOfTransient) GEOMImpl_IInsertOperations::Import
239 (const TCollection_AsciiString& theFileName,
240 const TCollection_AsciiString& theFormatName)
244 if (theFileName.IsEmpty() || theFormatName.IsEmpty()) return NULL;
246 //Add a new result object
247 Handle(GEOM_Object) anImported = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
249 //Add an Import function
250 Handle(GEOM_Function) aFunction =
251 anImported->AddFunction(GEOMImpl_ImportDriver::GetID(), IMPORT_SHAPE);
253 if (aFunction.IsNull()) return NULL;
255 //Check if the function is set correctly
256 if (aFunction->GetDriverGUID() != GEOMImpl_ImportDriver::GetID()) return NULL;
258 Handle(TCollection_HAsciiString) aHLibName;
260 (Standard_True, GetImportFormatName(theFormatName), aHLibName)) {
263 TCollection_AsciiString aLibName = aHLibName->String();
266 GEOMImpl_IImportExport aCI (aFunction);
267 aCI.SetFileName(theFileName);
268 aCI.SetFormatName(theFormatName);
269 aCI.SetPluginName(aLibName);
272 Handle(TColStd_HSequenceOfTransient) aSeq = new TColStd_HSequenceOfTransient;
276 if (!GetSolver()->ComputeFunction(aFunction)) {
277 SetErrorCode("Import driver failed");
281 aSeq->Append(anImported);
283 // Greate material groups.
284 MakeMaterialGroups(anImported, aSeq);
286 catch (Standard_Failure) {
287 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
288 SetErrorCode(aFail->GetMessageString());
292 //Make a Python command
293 if (theFormatName != "IGES_UNIT") {
294 GEOM::TPythonDump pd (aFunction);
295 if (theFormatName == "BREP")
296 pd << aSeq << " = geompy.ImportBREP(\"" << theFileName.ToCString() << "\")";
297 else if (theFormatName == "IGES")
298 pd << aSeq << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\")";
299 else if (theFormatName == "IGES_SCALE")
300 pd << aSeq << " = geompy.ImportIGES(\"" << theFileName.ToCString() << "\", True)";
301 else if (theFormatName == "STEP")
302 pd << aSeq << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\")";
303 else if (theFormatName == "STEP_SCALE")
304 pd << aSeq << " = geompy.ImportSTEP(\"" << theFileName.ToCString() << "\", True)";
306 pd << aSeq << " = geompy.ImportFile(\""
307 << theFileName.ToCString() << "\", \"" << theFormatName.ToCString() << "\")";
314 if (theFormatName == "IGES_UNIT") {
315 TopoDS_Shape S = aFunction->GetValue();
316 TopoDS_Vertex V = TopoDS::Vertex(S);
317 gp_Pnt P = BRep_Tool::Pnt(V);
318 double scale = P.X();
319 TCollection_AsciiString aUnitName = "UNIT_M";
320 if (fabs(scale-0.01) < 1.e-6)
321 aUnitName = "UNIT_CM";
322 else if (fabs(scale-0.001) < 1.e-6)
323 aUnitName = "UNIT_MM";
324 //cout<<"IIO: aUnitName = "<<aUnitName.ToCString()<<endl;
325 SetErrorCode(aUnitName);
332 //=============================================================================
336 //=============================================================================
337 TCollection_AsciiString GEOMImpl_IInsertOperations::ReadValue
338 (const TCollection_AsciiString& theFileName,
339 const TCollection_AsciiString& theFormatName,
340 const TCollection_AsciiString& theParameterName)
344 TCollection_AsciiString aValue, anError;
346 if (theFileName.IsEmpty() || theFormatName.IsEmpty() || theParameterName.IsEmpty()) return aValue;
348 Handle(TCollection_HAsciiString) aHLibName;
350 (Standard_True, GetImportFormatName(theFormatName), aHLibName)) {
353 TCollection_AsciiString aLibName = aHLibName->String();
355 aValue = GEOMImpl_ImportDriver::ReadValue(theFileName, aLibName, theParameterName, anError);
356 if (anError.IsEmpty())
359 SetErrorCode(anError.ToCString());
364 //=============================================================================
368 //=============================================================================
369 Standard_Boolean GEOMImpl_IInsertOperations::ImportTranslators
370 (Handle(TColStd_HSequenceOfAsciiString)& theFormats,
371 Handle(TColStd_HSequenceOfAsciiString)& thePatterns)
373 if (theFormats.IsNull())
374 theFormats = new TColStd_HSequenceOfAsciiString;
378 if (thePatterns.IsNull())
379 thePatterns = new TColStd_HSequenceOfAsciiString;
381 thePatterns->Clear();
383 if (!InitResMgr()) return Standard_False;
385 // Read Import formats from directories
386 Handle(Resource_Manager) aResMgr;
387 Handle(TColStd_HSequenceOfAsciiString) aFormatsToAdd;
388 for(int index = 0; index < myResMgrList.size(); index++) {
389 int anOldLen = theFormats->Length();
390 aResMgr = myResMgrList.at(index);
391 if (aResMgr->Find("Import")) {
392 TCollection_AsciiString aFormats (aResMgr->Value("Import"));
393 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
394 for (int i = 1; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
395 int aLenFormats = theFormats->Length();
396 bool isFound = false;
397 for(int aInd=1;aInd<=aLenFormats;aInd++){
398 if( theFormats->Value(aInd) == aToken ){
404 theFormats->Append(aToken);
408 // Read Patterns for each supported format
409 for (int j = anOldLen+1; j <= theFormats->Length(); j++) {
410 TCollection_AsciiString aKey, aPattern;
411 aKey = theFormats->Value(j) + ".ImportPattern";
412 if (aResMgr->Find(aKey.ToCString()))
413 aPattern = aResMgr->Value(aKey.ToCString());
415 aKey = theFormats->Value(j) + ".Pattern";
416 if (aResMgr->Find(aKey.ToCString()))
417 aPattern = aResMgr->Value(aKey.ToCString());
419 aPattern = theFormats->Value(j);
420 aPattern += " Files ( *.* )";
423 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 directories
452 Handle(Resource_Manager) aResMgr;
453 for(int index=0; index < myResMgrList.size(); index++) {
454 int anOldLen = theFormats->Length();
455 aResMgr = myResMgrList.at(index);
456 if (aResMgr->Find("Export")) {
457 TCollection_AsciiString aFormats (aResMgr->Value("Export"));
458 TCollection_AsciiString aToken = aFormats.Token("| \t", 1);
459 for (int i = 1; !aToken.IsEmpty(); aToken = aFormats.Token("| \t", ++i)) {
460 int aLenFormats = theFormats->Length();
461 bool isFound = false;
462 for(int aInd=1;aInd<=aLenFormats;aInd++){
463 if( theFormats->Value(aInd) == aToken){
469 theFormats->Append(aToken);
473 // Read Patterns for each supported format
474 for (int j = anOldLen+1; j <= theFormats->Length(); j++) {
475 TCollection_AsciiString aKey, aPattern;
476 aKey = theFormats->Value(j) + ".ExportPattern";
477 if (aResMgr->Find(aKey.ToCString()))
478 aPattern = aResMgr->Value(aKey.ToCString());
480 aKey = theFormats->Value(j) + ".Pattern";
481 if (aResMgr->Find(aKey.ToCString()))
482 aPattern = aResMgr->Value(aKey.ToCString());
484 aPattern = theFormats->Value(j);
485 aPattern += " Files ( *.* )";
488 thePatterns->Append(aPattern);
492 return (!theFormats->IsEmpty());
495 //=============================================================================
499 //=============================================================================
500 Standard_Boolean GEOMImpl_IInsertOperations::IsSupported
501 (const Standard_Boolean isImport,
502 const TCollection_AsciiString& theFormat,
503 Handle(TCollection_HAsciiString)& theLibName)
505 if (!InitResMgr()) return Standard_False;
507 // Import/Export mode
508 TCollection_AsciiString aMode;
509 //Standard_CString aMode;
510 if (isImport) aMode = "Import";
511 else aMode = "Export";
513 // Read supported formats for the certain mode from user directory
514 Handle(Resource_Manager) aResMgr;
515 for(int index=0; index < myResMgrList.size(); index++) {
516 aResMgr = myResMgrList.at(index);
517 if (aResMgr->Find(aMode.ToCString())) {
518 TCollection_AsciiString aFormats (aResMgr->Value(aMode.ToCString()));
519 if (aFormats.Search(theFormat) > -1) {
520 // Read library name for the supported format
521 TCollection_AsciiString aKey (theFormat);
524 if (aResMgr->Find(aKey.ToCString())) {
525 TCollection_AsciiString aLibName (aResMgr->Value(aKey.ToCString()));
527 if ( aLibName.Length() > 3 && aLibName.SubString(1,3) != "lib" )
528 aLibName.Prepend("lib");
533 theLibName = new TCollection_HAsciiString (aLibName);
534 return Standard_True;
540 return Standard_False;
543 //=============================================================================
547 //=============================================================================
548 Standard_Boolean GEOMImpl_IInsertOperations::InitResMgr()
550 bool isResourceFound = false;
551 TCollection_AsciiString aNull;
553 myResMgrList.clear();
555 // Initialize the GEOM Resource Manager
556 TCollection_AsciiString aResDir;
557 aResDir = getenv("GEOM_ROOT_DIR");
559 aResDir += "\\share\\salome\\resources\\geom";
561 aResDir += "/share/salome/resources/geom";
563 Handle(Resource_Manager) aGeomResMgr = new Resource_Manager ("ImportExport", aResDir, aNull, Standard_False);
564 if ( aGeomResMgr->Find("Import") || aGeomResMgr->Find("Export") ) {
565 myResMgrList.push_back( aGeomResMgr );
566 isResourceFound = true;
569 // Initialize the user's Resource Manager
570 TCollection_AsciiString aResDirsStr;
571 aResDirsStr = getenv("GEOM_ENGINE_RESOURCES_DIR");
572 if ( !aResDirsStr.IsEmpty() )
574 std::string aSep = ":";
578 aResDir = aResDirsStr.Token(aSep.c_str(), 1);
579 for (int i = 1; !aResDir.IsEmpty(); aResDir = aResDirsStr.Token(aSep.c_str(), ++i)) {
580 Handle(Resource_Manager) anUserResMgr = new Resource_Manager ("ImportExport", aNull, aResDir, Standard_False);
581 if (anUserResMgr->Find("Import") || anUserResMgr->Find("Export")) {
582 myResMgrList.push_back( anUserResMgr );
583 isResourceFound = true;
589 aResDir = getenv("HOME");
591 aResDir += "\\.config\\salome";
593 aResDir += "/.config/salome";
595 Handle(Resource_Manager) anUserResMgr = new Resource_Manager ("ImportExport", aNull, aResDir, Standard_False);
596 if (anUserResMgr->Find("Import") || anUserResMgr->Find("Export")) {
597 myResMgrList.push_back( anUserResMgr );
598 isResourceFound = true;
601 return isResourceFound;
604 //=============================================================================
608 //=============================================================================
609 Handle(GEOM_Object) GEOMImpl_IInsertOperations::RestoreShape (std::istringstream& theStream)
613 //Add a new result object
614 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_COPY);
616 //Add a Copy function
617 Handle(GEOM_Function) aFunction = result->AddFunction(GEOMImpl_CopyDriver::GetID(), COPY_WITHOUT_REF);
618 if (aFunction.IsNull()) return NULL;
620 //Check if the function is set correctly
621 if (aFunction->GetDriverGUID() != GEOMImpl_CopyDriver::GetID()) return NULL;
623 //Read a shape from the stream
626 BRepTools::Read(aShape, theStream, B);
627 if (aShape.IsNull()) {
628 SetErrorCode("RestoreShape error: BREP reading failed");
632 aFunction->SetValue(aShape);
634 //Special dump to avoid restored shapes publication.
635 //See correcponding code in GEOM_Engine.cxx (method ProcessFunction)
636 //GEOM::TPythonDump(aFunction) << "#";
638 GEOM::TPythonDump(aFunction) << result
639 << " = geompy.RestoreShape(\"\") # the shape string has not been dump for performance reason";
646 int GEOMImpl_IInsertOperations::LoadTexture(const TCollection_AsciiString& theTextureFile)
650 if (theTextureFile.IsEmpty()) return 0;
652 Handle(TColStd_HArray1OfByte) aTexture;
654 FILE* fp = fopen(theTextureFile.ToCString(), "r");
657 std::list<std::string> lines;
661 if ((fgets(buffer, 4096, fp)) == NULL) break;
662 int aLen = strlen(buffer);
663 if (buffer[aLen-1] == '\n') buffer[aLen-1] = '\0';
664 lines.push_back(buffer);
665 maxlen = std::max(maxlen, (int)strlen(buffer));
670 int lenbytes = maxlen/8;
671 if (maxlen%8) lenbytes++;
673 if (lenbytes == 0 || lines.empty())
676 std::list<unsigned char> bytedata;
677 std::list<std::string>::const_iterator it;
678 for (it = lines.begin(); it != lines.end(); ++it) {
679 std::string line = *it;
680 int lenline = (line.size()/8 + (line.size()%8 ? 1 : 0)) * 8;
681 for (int i = 0; i < lenline/8; i++) {
682 unsigned char byte = 0;
683 for (int j = 0; j < 8; j++)
684 byte = (byte << 1) + ( i*8+j < line.size() && line[i*8+j] != '0' ? 1 : 0 );
685 bytedata.push_back(byte);
687 for (int i = lenline/8; i < lenbytes; i++)
688 bytedata.push_back((unsigned char)0);
691 if (bytedata.empty() || bytedata.size() != lines.size()*lenbytes)
694 aTexture = new TColStd_HArray1OfByte (1, lines.size()*lenbytes);
696 std::list<unsigned char>::iterator bdit;
698 for (i = 1, bdit = bytedata.begin(); bdit != bytedata.end(); ++bdit, ++i)
699 aTexture->SetValue(i, (Standard_Byte)(*bdit));
701 int aTextureId = GetEngine()->addTexture(GetDocID(), lenbytes*8, lines.size(), aTexture, theTextureFile);
702 if (aTextureId > 0) SetErrorCode(OK);
706 int GEOMImpl_IInsertOperations::AddTexture(int theWidth, int theHeight,
707 const Handle(TColStd_HArray1OfByte)& theTexture)
710 int aTextureId = GetEngine()->addTexture(GetDocID(), theWidth, theHeight, theTexture);
711 if (aTextureId > 0) SetErrorCode(OK);
715 Handle(TColStd_HArray1OfByte) GEOMImpl_IInsertOperations::GetTexture(int theTextureId,
716 int& theWidth, int& theHeight)
720 Handle(TColStd_HArray1OfByte) aTexture;
722 theWidth = theHeight = 0;
723 TCollection_AsciiString aFileName;
725 if (theTextureId <= 0)
728 aTexture = GetEngine()->getTexture(GetDocID(), theTextureId, theWidth, theHeight, aFileName);
730 if (theWidth > 0 && theHeight > 0 && aTexture->Length() > 0) SetErrorCode(OK);
735 std::list<int> GEOMImpl_IInsertOperations::GetAllTextures()
738 std::list<int> id_list = GetEngine()->getAllTextures(GetDocID());
743 TopAbs_ShapeEnum getGroupDimension(XAO::Group* group)
745 XAO::Dimension dim = group->getDimension();
746 TopAbs_ShapeEnum rdim;
750 rdim = TopAbs_VERTEX; break;
752 rdim = TopAbs_EDGE; break;
754 rdim = TopAbs_FACE; break;
756 rdim = TopAbs_SOLID; break;
758 rdim = TopAbs_COMPOUND; break;
763 XAO::Dimension shapeEnumToDimension(const TopAbs_ShapeEnum& shape)
768 dim = XAO::VERTEX; break;
770 dim = XAO::EDGE; break;
772 dim = XAO::FACE; break;
774 dim = XAO::SOLID; break;
776 throw SALOME_Exception("Bad type"); // TODO
781 void GEOMImpl_IInsertOperations::exportGroups(std::list<Handle(GEOM_Object)> groupList,
783 XAO::BrepGeometry* geometry)
786 std::list<Handle(GEOM_Object)>::iterator groupIterator = groupList.begin();
787 while (groupIterator != groupList.end())
789 Handle(GEOM_Object) currGroup = (*groupIterator++);
790 Handle(TColStd_HArray1OfInteger) groupIds = myGroupOperations->GetObjects(currGroup);
792 TopAbs_ShapeEnum shapeGroup = myGroupOperations->GetType(currGroup);
793 XAO::Dimension dim = shapeEnumToDimension(shapeGroup);
794 XAO::Group* group = xaoObject->addGroup(dim, currGroup->GetName().ToCString());
799 for (int i = 1; i <= groupIds->Length(); i++)
801 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
802 int index = geometry->getVertexIndexByReference(ref);
807 for (int i = 1; i <= groupIds->Length(); i++)
809 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
810 int index = geometry->getEdgeIndexByReference(ref);
815 for (int i = 1; i <= groupIds->Length(); i++)
817 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
818 int index = geometry->getFaceIndexByReference(ref);
823 for (int i = 1; i <= groupIds->Length(); i++)
825 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
826 int index = geometry->getSolidIndexByReference(ref);
834 void GEOMImpl_IInsertOperations::exportFields(std::list<Handle(GEOM_Field)> fieldList,
836 XAO::BrepGeometry* geometry)
838 std::list<Handle(GEOM_Field)>::iterator fieldIterator = fieldList.begin();
839 while (fieldIterator != fieldList.end())
841 Handle(GEOM_Field) currField = (*fieldIterator++);
843 int fdim = currField->GetDimension();
844 int ftype = currField->GetDataType();
845 int nbComponents = currField->GetNbComponents();
846 std::string name = currField->GetName().ToCString();
848 XAO::Field* field = xaoObject->addField((XAO::Type)ftype, (XAO::Dimension)fdim, nbComponents, name);
850 Handle(TColStd_HArray1OfExtendedString) components = currField->GetComponents();
851 for (int i = components->Lower(), j = 0; i <= components->Upper(); ++i, ++j)
853 field->setComponentName(j, TCollection_AsciiString(components->Value(i)).ToCString());
856 std::list< Handle(GEOM_FieldStep)> steps = currField->GetSteps();
857 std::list<Handle(GEOM_FieldStep)>::iterator stepIterator = steps.begin();
858 while (stepIterator != steps.end())
860 Handle(GEOM_FieldStep) currStep = (*stepIterator++);
862 XAO::Step* step = field->addNewStep(currStep->GetID());
863 step->setStamp(currStep->GetStamp());
869 XAO::BooleanStep* bs = (XAO::BooleanStep*)step;
870 Handle(TColStd_HArray1OfInteger) bvalues = currStep->GetIntValues();
871 std::vector<bool> bv;
872 bv.reserve(bvalues->Upper());
873 for ( int i = bvalues->Lower(), nb = bvalues->Upper(); i <= nb; ++i )
875 bv.push_back(bvalues->Value(i) != 0);
882 XAO::IntegerStep* is = (XAO::IntegerStep*)step;
883 Handle(TColStd_HArray1OfInteger) ivalues = currStep->GetIntValues();
885 iv.reserve(ivalues->Upper());
886 for ( int i = ivalues->Lower(), nb = ivalues->Upper(); i <= nb; ++i )
888 iv.push_back(ivalues->Value(i));
895 XAO::DoubleStep* ds = (XAO::DoubleStep*)step;
896 Handle(TColStd_HArray1OfReal) dvalues = currStep->GetDoubleValues();
897 std::vector<double> dv;
898 dv.reserve(dvalues->Upper());
899 for ( int i = dvalues->Lower(), nb = dvalues->Upper(); i <= nb; ++i )
901 dv.push_back(dvalues->Value(i));
908 XAO::StringStep* ss = (XAO::StringStep*)step;
909 Handle(TColStd_HArray1OfExtendedString) svalues = currStep->GetStringValues();
910 std::vector<std::string> sv;
911 sv.reserve(svalues->Upper());
912 for ( int i = svalues->Lower(), nb = svalues->Upper(); i <= nb; ++i )
914 sv.push_back(TCollection_AsciiString(svalues->Value(i)).ToCString());
924 void GEOMImpl_IInsertOperations::exportSubshapes(const Handle(GEOM_Object)& shape, XAO::BrepGeometry* geometry)
926 Handle(TColStd_HSequenceOfTransient) subObjects = myShapesOperations->GetExistingSubObjects(shape, false);
927 int nbSubObjects = subObjects->Length();
928 // set the names of the sub shapes
929 for (int i = 1; i <= nbSubObjects; i++)
931 Handle(Standard_Transient) transientSubObject = subObjects->Value(i);
932 if (transientSubObject.IsNull())
935 Handle(GEOM_Object) subObject = Handle(GEOM_Object)::DownCast(transientSubObject);
936 if (subObject->GetType() != GEOM_GROUP)
938 int subIndex = myShapesOperations->GetSubShapeIndex(shape, subObject);
939 switch (subObject->GetValue().ShapeType())
942 geometry->changeVertexName(subIndex, subObject->GetName().ToCString());
945 geometry->changeEdgeName(subIndex, subObject->GetName().ToCString());
948 geometry->changeFaceName(subIndex, subObject->GetName().ToCString());
951 geometry->changeSolidName(subIndex, subObject->GetName().ToCString());
958 //=============================================================================
960 * Export a shape to XAO format
961 * \param shape The shape to export
962 * \param groups The list of groups to export
963 * \param fields The list of fields to export
964 * \param fileName The name of the file to exported
965 * \return boolean indicating if export was succeful.
967 //=============================================================================
968 bool GEOMImpl_IInsertOperations::ExportXAO(Handle(GEOM_Object) shape,
969 std::list<Handle(GEOM_Object)> groupList,
970 std::list<Handle(GEOM_Field)> fieldList,
972 const char* fileName)
976 if (shape.IsNull()) return false;
978 // add a new shape function with parameters
979 Handle(GEOM_Function) lastFunction = shape->GetLastFunction();
980 if (lastFunction.IsNull()) return false;
982 // add a new result object
983 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
985 // add an Export function
986 Handle(GEOM_Function) exportFunction = result->AddFunction(GEOMImpl_XAODriver::GetID(), IMPORTEXPORT_EXPORTXAO);
987 if (exportFunction.IsNull()) return false;
988 if (exportFunction->GetDriverGUID() != GEOMImpl_XAODriver::GetID()) return false;
990 // create the XAO object
991 XAO::Xao* xaoObject = new XAO::Xao();
992 xaoObject->setAuthor(author);
995 XAO::BrepGeometry* geometry = (XAO::BrepGeometry*)XAO::Geometry::createGeometry(XAO::BREP);
996 TopoDS_Shape topoShape = shape->GetValue();
997 exportFunction->SetValue(topoShape);
998 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)geometry;
999 brep->setTopoDS_Shape(topoShape);
1001 geometry->setName(shape->GetName().ToCString());
1002 exportSubshapes(shape, geometry);
1003 xaoObject->setGeometry(geometry);
1005 exportGroups(groupList, xaoObject, geometry);
1006 exportFields(fieldList, xaoObject, geometry);
1008 // export the XAO to the file
1009 xaoObject->exportXAO(fileName);
1011 // make a Python command
1012 GEOM::TPythonDump pd(exportFunction);
1013 pd << "exported = geompy.ExportXAO(" << shape;
1017 if (groupList.size() > 0)
1019 std::list<Handle(GEOM_Object)>::iterator itGroup = groupList.begin();
1021 while (itGroup != groupList.end())
1023 pd << ", " << (*itGroup++);
1029 if (fieldList.size() > 0)
1031 std::list<Handle(GEOM_Field)>::iterator itField = fieldList.begin();
1033 while (itField != fieldList.end())
1035 pd << ", " << (*itField++);
1039 pd << "\"" << author << "\", \"" << fileName << "\")";
1047 void GEOMImpl_IInsertOperations::importSubShapes(XAO::Geometry* xaoGeometry,
1048 Handle(GEOM_Function) function, int shapeType, int dim,
1049 Handle(TColStd_HSequenceOfTransient)& subShapeList)
1051 Handle(GEOM_Object) subShape;
1052 Handle(GEOM_Function) aFunction;
1053 Handle(TColStd_HArray1OfInteger) anArray;
1055 XAO::GeometricElementList::iterator elementIterator = xaoGeometry->begin((XAO::Dimension)dim);
1056 for (; elementIterator != xaoGeometry->end((XAO::Dimension)dim); elementIterator++)
1058 XAO::GeometricElement element = elementIterator->second;
1059 if (!element.hasName())
1062 std::string name = element.getName();
1063 std::string ref = element.getReference();
1064 int iref = XAO::XaoUtils::stringToInt(ref);
1066 anArray = new TColStd_HArray1OfInteger(1, 1);
1067 anArray->SetValue(1, iref);
1069 subShape = GetEngine()->AddObject(GetDocID(), GEOM_SUBSHAPE);
1070 Handle(GEOM_Function) aFunction = subShape->AddFunction(GEOM_Object::GetSubShapeID(), 1);
1071 if (aFunction.IsNull())
1074 subShape->SetName(name.c_str());
1075 subShape->SetType(shapeType);
1077 GEOM_ISubShape aSSI(aFunction);
1078 aSSI.SetMainShape(function);
1079 aSSI.SetIndices(anArray);
1081 //aFunction->SetValue(aValue);
1082 subShapeList->Append(subShape);
1084 // Put this subshape in the list of sub-shapes of theMainShape
1085 function->AddSubShapeReference(aFunction);
1089 //=============================================================================
1091 * Import a shape from XAO format
1092 * \param fileName The name of the file to import
1093 * \param shape The imported shape
1094 * \param subShapes The list of imported groups
1095 * \param groups The list of imported groups
1096 * \param fields The list of imported fields
1097 * \return boolean indicating if import was succeful.
1099 //=============================================================================
1100 bool GEOMImpl_IInsertOperations::ImportXAO(const char* fileName,
1101 Handle(GEOM_Object)& shape,
1102 Handle(TColStd_HSequenceOfTransient)& subShapes,
1103 Handle(TColStd_HSequenceOfTransient)& groups,
1104 Handle(TColStd_HSequenceOfTransient)& fields)
1108 if (fileName == NULL || groups.IsNull() || fields.IsNull())
1112 XAO::Xao* xaoObject = new XAO::Xao();
1115 xaoObject->importXAO(fileName);
1117 catch (XAO::XAO_Exception& exc)
1120 SetErrorCode(exc.what());
1124 XAO::Geometry* xaoGeometry = xaoObject->getGeometry();
1125 if (xaoGeometry == NULL)
1128 SetErrorCode("Cannot import XAO: geometry format not supported.");
1133 shape = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
1134 Handle(GEOM_Function) function = shape->AddFunction(GEOMImpl_XAODriver::GetID(), IMPORTEXPORT_EXPORTXAO);
1135 if (function.IsNull()) return false;
1136 if (function->GetDriverGUID() != GEOMImpl_XAODriver::GetID()) return false;
1139 if (xaoGeometry->getFormat() == XAO::BREP)
1141 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)xaoGeometry;
1142 TopoDS_Shape geomShape = brep->getTopoDS_Shape();
1143 function->SetValue(geomShape);
1144 shape->SetName(xaoGeometry->getName().c_str());
1149 SetErrorCode("Cannot import XAO: geometry format not supported.");
1153 // create sub shapes with names
1154 importSubShapes(xaoGeometry, function, GEOM_POINT, XAO::VERTEX, subShapes);
1155 importSubShapes(xaoGeometry, function, GEOM_EDGE, XAO::EDGE, subShapes);
1156 importSubShapes(xaoGeometry, function, GEOM_FACE, XAO::FACE, subShapes);
1157 importSubShapes(xaoGeometry, function, GEOM_SOLID, XAO::SOLID, subShapes);
1160 int nbGroups = xaoObject->countGroups();
1161 for (int i = 0; i < nbGroups; ++i)
1163 XAO::Group* xaoGroup = xaoObject->getGroup(i);
1165 // build an array with the indexes of the sub shapes
1166 int nbElt = xaoGroup->count();
1167 Handle(TColStd_HArray1OfInteger) array = new TColStd_HArray1OfInteger(1, nbElt);
1169 for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it)
1172 std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
1173 array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
1176 // create the group with the array of sub shapes indexes
1177 Handle(GEOM_Object) group = GetEngine()->AddSubShape(shape, array);
1178 group->SetType(GEOM_GROUP);
1179 group->SetName(xaoGroup->getName().c_str());
1181 // Set a sub-shape type
1182 TDF_Label freeLabel = group->GetFreeLabel();
1183 TDataStd_Integer::Set(freeLabel, (Standard_Integer) getGroupDimension(xaoGroup));
1184 groups->Append(group);
1186 function = group->GetLastFunction();
1189 // create the fields
1190 int nbFields = xaoObject->countFields();
1191 for (int i = 0; i < nbFields; ++i)
1193 XAO::Field* xaoField = xaoObject->getField(i);
1195 Handle(TColStd_HArray1OfExtendedString) components = new TColStd_HArray1OfExtendedString(0, xaoField->countComponents()-1);
1196 for (int j = 0; j < xaoField->countComponents(); ++j)
1198 components->SetValue(j, (TCollection_ExtendedString)xaoField->getComponentName(j).c_str());
1201 Handle(GEOM_Field) field = myFieldOperations->CreateField(shape,
1202 xaoField->getName().c_str(),
1203 (int)xaoField->getType(),
1204 (int)xaoField->getDimension(),
1207 switch (xaoField->getType())
1211 XAO::BooleanField* bfield = (XAO::BooleanField*)xaoField;
1212 for (int j = 0; j < xaoField->countSteps(); ++j)
1214 XAO::BooleanStep* bstep = bfield->getStep(j);
1215 Handle(GEOM_FieldStep) step = field->AddStep(bstep->getStep(), bstep->getStamp());
1217 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, bstep->countValues()-1);
1218 std::vector<bool> bvalues = bstep->getValues();
1219 for (int k = 0; k < bstep->countValues(); ++k)
1221 values->SetValue(k, bvalues[k] ? 1 : 0);
1223 step->SetValues(values);
1229 XAO::IntegerField* ifield = (XAO::IntegerField*)xaoField;
1230 for (int j = 0; j < xaoField->countSteps(); ++j)
1232 XAO::IntegerStep* istep = ifield->getStep(j);
1233 Handle(GEOM_FieldStep) step = field->AddStep(istep->getStep(), istep->getStamp());
1235 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, istep->countValues()-1);
1236 std::vector<int> ivalues = istep->getValues();
1237 for (int k = 0; k < istep->countValues(); ++k)
1239 values->SetValue(k, ivalues[k]);
1241 step->SetValues(values);
1247 XAO::DoubleField* dfield = (XAO::DoubleField*)xaoField;
1248 for (int j = 0; j < xaoField->countSteps(); ++j)
1250 XAO::DoubleStep* dstep = dfield->getStep(j);
1251 Handle(GEOM_FieldStep) step = field->AddStep(dstep->getStep(), dstep->getStamp());
1253 Handle(TColStd_HArray1OfReal) values = new TColStd_HArray1OfReal(0, dstep->countValues()-1);
1254 std::vector<double> dvalues = dstep->getValues();
1255 for (int k = 0; k < dstep->countValues(); ++k)
1257 values->SetValue(k, dvalues[k]);
1259 step->SetValues(values);
1265 XAO::StringField* sfield = (XAO::StringField*)xaoField;
1266 for (int j = 0; j < xaoField->countSteps(); ++j)
1268 XAO::StringStep* sstep = sfield->getStep(j);
1269 Handle(GEOM_FieldStep) step = field->AddStep(sstep->getStep(), sstep->getStamp());
1271 Handle(TColStd_HArray1OfExtendedString) values = new TColStd_HArray1OfExtendedString(0, sstep->countValues()-1);
1272 std::vector<std::string> svalues = sstep->getValues();
1273 for (int k = 0; k < sstep->countValues(); ++k)
1275 values->SetValue(k, TCollection_ExtendedString(svalues[k].c_str()));
1277 step->SetValues(values);
1283 fields->Append(field);
1286 // make a Python command
1287 GEOM::TPythonDump pd(function);
1288 pd << "(imported, " << shape << ", ";
1290 // list of sub shapes
1292 int nbSubshapes = subShapes->Length();
1293 if (nbSubshapes > 0)
1295 for (int i = 1; i <= nbSubshapes; i++)
1297 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(subShapes->Value(i));
1298 pd << obj << ((i < nbSubshapes) ? ", " : "");
1306 for (int i = 1; i <= nbGroups; i++)
1308 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(groups->Value(i));
1309 pd << obj << ((i < nbGroups) ? ", " : "");
1318 for (int i = 1; i <= nbFields; i++)
1320 Handle(GEOM_Field) obj = Handle(GEOM_Field)::DownCast(fields->Value(i));
1321 pd << obj << ((i < nbFields) ? ", " : "");
1325 pd << ") = geompy.ImportXAO(\"" << fileName << "\")";
1333 //=============================================================================
1335 * This method creates material groups for an imported object.
1336 * \param theObject the imported object.
1338 //=============================================================================
1339 void GEOMImpl_IInsertOperations::MakeMaterialGroups
1340 (const Handle(GEOM_Object) &theObject,
1341 const Handle(TColStd_HSequenceOfTransient) &theSeq)
1343 TopoDS_Shape aResShape = theObject->GetValue();
1345 if (aResShape.IsNull() == Standard_False) {
1346 // Group shapes by material names.
1347 Handle(GEOM_Function) aFunction = theObject->GetLastFunction();
1348 DataMapOfStringListOfShape aMapMaterialShapes;
1350 // check all named shapes using iterator
1351 TDF_ChildIDIterator anIt (aFunction->GetNamingEntry(),
1352 TNaming_NamedShape::GetID(), Standard_True);
1354 for (; anIt.More(); anIt.Next()) {
1355 Handle(TNaming_NamedShape) anAttr =
1356 Handle(TNaming_NamedShape)::DownCast(anIt.Value());
1358 if (anAttr.IsNull() == Standard_False) {
1359 TDF_Label aLabel = anAttr->Label();
1360 Handle(TDataStd_Comment) aComment;
1362 if (aLabel.FindAttribute(TDataStd_Comment::GetID(), aComment)) {
1363 TCollection_ExtendedString aMatName = aComment->Get();
1364 TopoDS_Shape aShape = anAttr->Get();
1366 if (aMapMaterialShapes.IsBound(aMatName) == Standard_False) {
1367 NCollection_List<TopoDS_Shape> anEmptyList;
1369 aMapMaterialShapes.Bind(aMatName, anEmptyList);
1372 aMapMaterialShapes(aMatName).Append(aShape);
1377 if (aMapMaterialShapes.IsEmpty() == Standard_False) {
1378 // Construct groups.
1379 TopAbs_ShapeEnum aType = aResShape.ShapeType();
1381 DataMapOfStringListOfShape::Iterator aMapIter;
1383 // Check each shape type.
1384 for(i = aType; i <= TopAbs_VERTEX; i++) {
1385 DataMapOfStringListOfShape::Iterator aMapIter(aMapMaterialShapes);
1387 for (; aMapIter.More(); aMapIter.Next()) {
1388 NCollection_List<TopoDS_Shape> &aShList = aMapIter.ChangeValue();
1389 NCollection_List<TopoDS_Shape>::Iterator aShIter(aShList);
1390 NCollection_List<TopoDS_Shape> aShListSameType;
1392 while (aShIter.More()) {
1393 const TopoDS_Shape &aShape = aShIter.Value();
1395 if (i == aShape.ShapeType()) {
1396 // Treat this element.
1397 aShListSameType.Append(aShape);
1398 aShList.Remove(aShIter);
1400 // Go to the next element.
1405 if (aShListSameType.IsEmpty() == Standard_False) {
1406 // Construct a group.
1407 Handle(GEOM_Object) aGroup =
1408 MakeGroup(theObject, aMapIter.Key(), aShListSameType);
1410 if (aGroup.IsNull() == Standard_False) {
1411 theSeq->Append(aGroup);
1421 //=============================================================================
1423 * This method creates a group of shapes of certain type.
1424 * \param theObject the imported object.
1425 * \param theName the material name.
1426 * \param theShapes the list of shapes to be added to this group.
1427 * \return the created group.
1429 //=============================================================================
1430 Handle(GEOM_Object) GEOMImpl_IInsertOperations::MakeGroup
1431 (const Handle(GEOM_Object) &theObject,
1432 const TCollection_ExtendedString &theName,
1433 const NCollection_List<TopoDS_Shape> &theShapes)
1435 Handle(GEOM_Object) aGroup;
1436 TopTools_IndexedMapOfShape anIndices;
1437 Handle(TColStd_HSequenceOfInteger) aSeqIDs = new TColStd_HSequenceOfInteger;
1438 NCollection_List<TopoDS_Shape>::Iterator anIter(theShapes);
1440 TopExp::MapShapes(theObject->GetValue(), anIndices);
1442 // Compose shape IDs.
1443 for (; anIter.More(); anIter.Next()) {
1444 const TopoDS_Shape &aShape = anIter.Value();
1445 const Standard_Integer anIndex = anIndices.FindIndex(aShape);
1448 aSeqIDs->Append(anIndex);
1452 if (aSeqIDs->IsEmpty() == Standard_False) {
1454 const TopAbs_ShapeEnum aType = theShapes.First().ShapeType();
1456 aGroup = myGroupOperations->CreateGroup(theObject, aType);
1458 if (aGroup.IsNull() == Standard_False) {
1459 aGroup->GetLastFunction()->SetDescription("");
1460 myGroupOperations->UnionIDs(aGroup, aSeqIDs);
1461 aGroup->GetLastFunction()->SetDescription("");
1463 // Compose the group name.
1464 TCollection_AsciiString aGroupName(theName);
1468 aGroupName += "_VERTEX";
1471 aGroupName += "_EDGE";
1474 aGroupName += "_WIRE";
1477 aGroupName += "_FACE";
1480 aGroupName += "_SHELL";
1483 aGroupName += "_SOLID";
1485 case TopAbs_COMPSOLID:
1486 aGroupName += "_COMPSOLID";
1488 case TopAbs_COMPOUND:
1489 aGroupName += "_COMPOUND";
1492 aGroupName += "_SHAPE";
1496 aGroup->SetName(aGroupName.ToCString());