1 // Copyright (C) 2014-2021 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "XAOPlugin_IOperations.hxx"
22 #include "XAOPlugin_Driver.hxx"
23 #include "XAOPlugin_IImportExport.hxx"
26 #include <Basics_DirUtils.hxx>
27 #include <utilities.h>
28 #include <Utils_SALOME_Exception.hxx>
31 #include <GEOM_PythonDump.hxx>
32 #include <GEOMImpl_Types.hxx>
33 #include <GEOMImpl_IGroupOperations.hxx>
34 #include <GEOMImpl_IShapesOperations.hxx>
35 #include <GEOMImpl_IFieldOperations.hxx>
36 #include <GEOM_ISubShape.hxx>
37 #include <GEOM_Object.hxx>
38 #include <GEOM_Field.hxx>
40 #include <XAO_Geometry.hxx>
41 #include <XAO_BrepGeometry.hxx>
42 #include <XAO_Xao.hxx>
43 #include <XAO_Group.hxx>
44 #include <XAO_Field.hxx>
45 #include <XAO_BooleanField.hxx>
46 #include <XAO_IntegerField.hxx>
47 #include <XAO_DoubleField.hxx>
48 #include <XAO_StringField.hxx>
49 #include <XAO_DoubleStep.hxx>
50 #include <XAO_StringStep.hxx>
52 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
55 #include <TColStd_HArray1OfByte.hxx>
56 #include <TColStd_HArray1OfReal.hxx>
57 #include <TDataStd_Integer.hxx>
61 XAO::Dimension shapeEnumToDimension(const TopAbs_ShapeEnum& shape)
66 dim = XAO::VERTEX; break;
68 dim = XAO::EDGE; break;
70 dim = XAO::FACE; break;
72 dim = XAO::SOLID; break;
74 throw SALOME_Exception("Bad type"); // TODO
79 TopAbs_ShapeEnum getGroupDimension(XAO::Group* group)
81 XAO::Dimension dim = group->getDimension();
82 TopAbs_ShapeEnum rdim;
86 rdim = TopAbs_VERTEX; break;
88 rdim = TopAbs_EDGE; break;
90 rdim = TopAbs_FACE; break;
92 rdim = TopAbs_SOLID; break;
94 rdim = TopAbs_COMPOUND; break;
99 //=============================================================================
103 //=============================================================================
104 XAOPlugin_IOperations::XAOPlugin_IOperations( GEOM_Engine* theEngine )
105 : GEOMImpl_IBaseIEOperations( theEngine )
107 MESSAGE( "XAOPlugin_IOperations::XAOPlugin_IOperations" );
110 //=============================================================================
114 //=============================================================================
115 XAOPlugin_IOperations::~XAOPlugin_IOperations()
117 MESSAGE( "XAOPlugin_IOperations::~XAOPlugin_IOperations" );
120 bool XAOPlugin_IOperations::exportGroups( std::list<Handle(GEOM_Object)> groupList,
122 XAO::BrepGeometry* geometry )
125 std::list<Handle(GEOM_Object)>::iterator groupIterator = groupList.begin();
126 while (groupIterator != groupList.end())
128 Handle(GEOM_Object) currGroup = (*groupIterator++);
129 if (currGroup->GetType() != GEOM_GROUP) {
130 SetErrorCode("Error when export groups: you could perform this operation only with group.");
133 Handle(TColStd_HArray1OfInteger) groupIds = myGroupOperations->GetObjects(currGroup);
135 TopAbs_ShapeEnum shapeGroup = myGroupOperations->GetType(currGroup);
136 XAO::Dimension dim = shapeEnumToDimension(shapeGroup);
137 XAO::Group* group = xaoObject->addGroup(dim, currGroup->GetName().ToCString());
139 // Group can be empty
140 if (groupIds.IsNull()) continue;
145 for (int i = 1; i <= groupIds->Length(); i++)
147 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
148 int index = geometry->getVertexIndexByReference(ref);
153 for (int i = 1; i <= groupIds->Length(); i++)
155 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
156 int index = geometry->getEdgeIndexByReference(ref);
161 for (int i = 1; i <= groupIds->Length(); i++)
163 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
164 int index = geometry->getFaceIndexByReference(ref);
169 for (int i = 1; i <= groupIds->Length(); i++)
171 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
172 int index = geometry->getSolidIndexByReference(ref);
183 void XAOPlugin_IOperations::exportFields( std::list<Handle(GEOM_Field)> fieldList,
185 XAO::BrepGeometry* /*geometry*/ )
187 std::list<Handle(GEOM_Field)>::iterator fieldIterator = fieldList.begin();
188 while (fieldIterator != fieldList.end())
190 Handle(GEOM_Field) currField = (*fieldIterator++);
192 int fdim = currField->GetDimension();
193 int ftype = currField->GetDataType();
194 int nbComponents = currField->GetNbComponents();
195 std::string name = currField->GetName().ToCString();
197 XAO::Field* field = xaoObject->addField((XAO::Type)ftype, (XAO::Dimension)fdim, nbComponents, name);
199 Handle(TColStd_HArray1OfExtendedString) components = currField->GetComponents();
200 for (int i = components->Lower(), j = 0; i <= components->Upper(); ++i, ++j)
202 field->setComponentName(j, TCollection_AsciiString(components->Value(i)).ToCString());
205 std::list< Handle(GEOM_FieldStep)> steps = currField->GetSteps();
206 std::list<Handle(GEOM_FieldStep)>::iterator stepIterator = steps.begin();
207 while (stepIterator != steps.end())
209 Handle(GEOM_FieldStep) currStep = (*stepIterator++);
211 XAO::Step* step = field->addNewStep(currStep->GetID());
212 step->setStamp(currStep->GetStamp());
218 XAO::BooleanStep* bs = (XAO::BooleanStep*)step;
219 Handle(TColStd_HArray1OfInteger) bvalues = currStep->GetIntValues();
220 std::vector<bool> bv;
221 bv.reserve(bvalues->Upper());
222 for ( int i = bvalues->Lower(), nb = bvalues->Upper(); i <= nb; ++i )
224 bv.push_back(bvalues->Value(i) != 0);
231 XAO::IntegerStep* is = (XAO::IntegerStep*)step;
232 Handle(TColStd_HArray1OfInteger) ivalues = currStep->GetIntValues();
234 iv.reserve(ivalues->Upper());
235 for ( int i = ivalues->Lower(), nb = ivalues->Upper(); i <= nb; ++i )
237 iv.push_back(ivalues->Value(i));
244 XAO::DoubleStep* ds = (XAO::DoubleStep*)step;
245 Handle(TColStd_HArray1OfReal) dvalues = currStep->GetDoubleValues();
246 std::vector<double> dv;
247 dv.reserve(dvalues->Upper());
248 for ( int i = dvalues->Lower(), nb = dvalues->Upper(); i <= nb; ++i )
250 dv.push_back(dvalues->Value(i));
257 XAO::StringStep* ss = (XAO::StringStep*)step;
258 Handle(TColStd_HArray1OfExtendedString) svalues = currStep->GetStringValues();
259 std::vector<std::string> sv;
260 sv.reserve(svalues->Upper());
261 for ( int i = svalues->Lower(), nb = svalues->Upper(); i <= nb; ++i )
263 sv.push_back(TCollection_AsciiString(svalues->Value(i)).ToCString());
273 void XAOPlugin_IOperations::exportSubshapes( const Handle(GEOM_Object)& shape, XAO::BrepGeometry* geometry )
275 Handle(TColStd_HSequenceOfTransient) subObjects = myShapesOperations->GetExistingSubObjects( shape, GEOMImpl_IShapesOperations::SubShapes );
276 int nbSubObjects = subObjects->Length();
277 if (!nbSubObjects) return;
279 TopoDS_Shape aMainShape = shape->GetValue();
280 if (aMainShape.IsNull()) return;
281 TopTools_IndexedMapOfShape anIndices;
282 TopExp::MapShapes(aMainShape, anIndices);
284 // set the names of the sub shapes
285 for (int i = 1; i <= nbSubObjects; i++)
287 Handle(Standard_Transient) transientSubObject = subObjects->Value(i);
288 if (transientSubObject.IsNull())
291 Handle(GEOM_Object) subObject = Handle(GEOM_Object)::DownCast( transientSubObject );
292 if (subObject->GetType() != GEOM_GROUP)
294 TopoDS_Shape aSubShape = subObject->GetValue();
295 if (aSubShape.IsNull()) continue;
297 // Do not call GEOMImpl_IShapesOperations::GetSubShapeIndex() here
298 // for time optimization reason (it invokes TopExp::MapShapes())
299 //int subIndex = myShapesOperations->GetSubShapeIndex( shape, subObject );
300 int subIndex = anIndices.FindIndex(aSubShape);
301 if (!subIndex) continue;
303 switch (aSubShape.ShapeType())
306 geometry->changeVertexName(subIndex, subObject->GetName().ToCString());
309 geometry->changeEdgeName(subIndex, subObject->GetName().ToCString());
312 geometry->changeFaceName(subIndex, subObject->GetName().ToCString());
315 geometry->changeSolidName(subIndex, subObject->GetName().ToCString());
324 //=============================================================================
326 * Export a shape to XAO format
327 * \param shape The shape to export
328 * \param groups The list of groups to export
329 * \param fields The list of fields to export
330 * \param fileName The name of the file to exported
331 * \return boolean indicating if export was succeful.
333 //=============================================================================
334 bool XAOPlugin_IOperations::ExportXAO( Handle(GEOM_Object) shape,
335 std::list<Handle(GEOM_Object)> groupList,
336 std::list<Handle(GEOM_Field)> fieldList,
338 const char* fileName,
339 const char* shapeFileName )
343 if (shape.IsNull()) return false;
345 // add a new shape function with parameters
346 Handle(GEOM_Function) lastFunction = shape->GetLastFunction();
347 if (lastFunction.IsNull()) return false;
349 // add a new result object
350 Handle(GEOM_Object) result = GetEngine()->AddObject(GEOM_IMPORT);
352 // add an Export function
353 Handle(GEOM_Function) exportFunction = result->AddFunction(XAOPlugin_Driver::GetID(), EXPORT_SHAPE);
354 if (exportFunction.IsNull()) return false;
355 if (exportFunction->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
357 // create the XAO object
358 XAO::Xao* xaoObject = new XAO::Xao();
359 xaoObject->setAuthor(author);
362 XAO::BrepGeometry* geometry = (XAO::BrepGeometry*)XAO::Geometry::createGeometry(XAO::BREP);
363 TopoDS_Shape topoShape = shape->GetValue();
364 exportFunction->SetValue(topoShape);
365 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)geometry;
366 brep->setTopoDS_Shape(topoShape);
368 geometry->setName(shape->GetName().ToCString());
369 exportSubshapes(shape, geometry);
370 xaoObject->setGeometry(geometry);
372 if (!exportGroups(groupList, xaoObject, geometry)) return false;
373 exportFields(fieldList, xaoObject, geometry);
375 // export the XAO to the file
376 xaoObject->exportXAO(fileName, shapeFileName);
378 // make a Python command
379 GEOM::TPythonDump pd(exportFunction);
380 std::string convFileName = Kernel_Utils::BackSlashToSlash(fileName);
381 pd << "exported = geompy.ExportXAO(" << shape;
385 if (groupList.size() > 0)
387 std::list<Handle(GEOM_Object)>::iterator itGroup = groupList.begin();
389 while (itGroup != groupList.end())
391 pd << ", " << (*itGroup++);
397 if (fieldList.size() > 0)
399 std::list<Handle(GEOM_Field)>::iterator itField = fieldList.begin();
401 while (itField != fieldList.end())
403 pd << ", " << (*itField++);
407 pd << "\"" << author << "\", \"" << convFileName.c_str() << "\", \"" << shapeFileName << "\")";
415 void XAOPlugin_IOperations::importSubShapes( XAO::Geometry* xaoGeometry,
416 Handle(GEOM_Function) function, int shapeType, int dim,
417 Handle(TColStd_HSequenceOfTransient)& subShapeList )
419 Handle(GEOM_Object) subShape;
420 Handle(GEOM_Function) aFunction;
421 Handle(TColStd_HArray1OfInteger) anArray;
423 XAO::GeometricElementList::iterator elementIterator = xaoGeometry->begin((XAO::Dimension)dim);
424 for (; elementIterator != xaoGeometry->end((XAO::Dimension)dim); elementIterator++)
426 XAO::GeometricElement element = elementIterator->second;
427 if (!element.hasName())
430 std::string name = element.getName();
431 std::string ref = element.getReference();
432 int iref = XAO::XaoUtils::stringToInt(ref);
434 anArray = new TColStd_HArray1OfInteger(1, 1);
435 anArray->SetValue(1, iref);
437 subShape = GetEngine()->AddObject(GEOM_SUBSHAPE);
438 Handle(GEOM_Function) aFunction = subShape->AddFunction(GEOM_Object::GetSubShapeID(), 1);
439 if (aFunction.IsNull())
442 subShape->SetName(name.c_str());
443 subShape->SetType(shapeType);
445 GEOM_ISubShape aSSI(aFunction);
446 aSSI.SetMainShape(function);
447 aSSI.SetIndices(anArray);
449 //aFunction->SetValue(aValue);
450 subShapeList->Append(subShape);
452 // Put this subshape in the list of sub-shapes of theMainShape
453 function->AddSubShapeReference(aFunction);
457 //=============================================================================
459 * Import a shape from XAO format
460 * \param fileName The name of the file to import
461 * \param shape The imported shape
462 * \param subShapes The list of imported groups
463 * \param groups The list of imported groups
464 * \param fields The list of imported fields
465 * \return boolean indicating if import was succeful.
467 //=============================================================================
468 bool XAOPlugin_IOperations::ImportXAO( const char* fileName,
469 Handle(GEOM_Object)& shape,
470 Handle(TColStd_HSequenceOfTransient)& subShapes,
471 Handle(TColStd_HSequenceOfTransient)& groups,
472 Handle(TColStd_HSequenceOfTransient)& fields )
476 if (fileName == NULL || groups.IsNull() || fields.IsNull())
480 XAO::Xao* xaoObject = new XAO::Xao();
483 xaoObject->importXAO(fileName);
485 catch (XAO::XAO_Exception& exc)
488 SetErrorCode(exc.what());
492 XAO::Geometry* xaoGeometry = xaoObject->getGeometry();
493 if (xaoGeometry == NULL)
496 SetErrorCode("Cannot import XAO: geometry format not supported.");
501 shape = GetEngine()->AddObject(GEOM_IMPORT);
502 Handle(GEOM_Function) function = shape->AddFunction(XAOPlugin_Driver::GetID(), IMPORT_SHAPE);
503 if (function.IsNull()) return false;
504 if (function->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
506 function->SetString( XAOPlugin_Driver::GetFileNameTag(), fileName );
509 if (xaoGeometry->getFormat() == XAO::BREP)
511 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)xaoGeometry;
512 TopoDS_Shape geomShape = brep->getTopoDS_Shape();
513 function->SetValue(geomShape);
514 shape->SetName(xaoGeometry->getName().c_str());
519 SetErrorCode("Cannot import XAO: geometry format not supported.");
523 // create sub shapes with names
524 importSubShapes(xaoGeometry, function, GEOM_POINT, XAO::VERTEX, subShapes);
525 importSubShapes(xaoGeometry, function, GEOM_EDGE, XAO::EDGE, subShapes);
526 importSubShapes(xaoGeometry, function, GEOM_FACE, XAO::FACE, subShapes);
527 importSubShapes(xaoGeometry, function, GEOM_SOLID, XAO::SOLID, subShapes);
530 int nbGroups = xaoObject->countGroups();
531 for (int i = 0; i < nbGroups; ++i)
533 XAO::Group* xaoGroup = xaoObject->getGroup(i);
535 // build an array with the indexes of the sub shapes
536 int nbElt = xaoGroup->count();
537 Handle(TColStd_HArray1OfInteger) array;
539 array = new TColStd_HArray1OfInteger(1, nbElt);
541 for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it) {
543 std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
544 array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
547 else { // empty group
548 array = new TColStd_HArray1OfInteger(1, 1);
549 array->SetValue(1, -1);
552 // create the group with the array of sub shapes indexes
553 Handle(GEOM_Object) group = GetEngine()->AddSubShape(shape, array);
554 group->SetType(GEOM_GROUP);
555 group->SetName(xaoGroup->getName().c_str());
557 // Set a sub-shape type
558 TDF_Label freeLabel = group->GetFreeLabel();
559 TDataStd_Integer::Set(freeLabel, (Standard_Integer) getGroupDimension(xaoGroup));
560 groups->Append(group);
564 int nbFields = xaoObject->countFields();
565 for (int i = 0; i < nbFields; ++i)
567 XAO::Field* xaoField = xaoObject->getField(i);
569 Handle(TColStd_HArray1OfExtendedString) components = new TColStd_HArray1OfExtendedString(0, xaoField->countComponents()-1);
570 for (int j = 0; j < xaoField->countComponents(); ++j)
572 components->SetValue(j, (TCollection_ExtendedString)xaoField->getComponentName(j).c_str());
575 Handle(GEOM_Field) field = myFieldOperations->CreateField(shape,
576 xaoField->getName().c_str(),
577 (int)xaoField->getType(),
578 (int)xaoField->getDimension(),
581 switch (xaoField->getType())
585 XAO::BooleanField* bfield = (XAO::BooleanField*)xaoField;
586 for (int j = 0; j < xaoField->countSteps(); ++j)
588 XAO::BooleanStep* bstep = bfield->getStep(j);
589 Handle(GEOM_FieldStep) step = field->AddStep(bstep->getStep(), bstep->getStamp());
591 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, bstep->countValues()-1);
592 std::vector<bool> bvalues = bstep->getValues();
593 for (int k = 0; k < bstep->countValues(); ++k)
595 values->SetValue(k, bvalues[k] ? 1 : 0);
597 step->SetValues(values);
603 XAO::IntegerField* ifield = (XAO::IntegerField*)xaoField;
604 for (int j = 0; j < xaoField->countSteps(); ++j)
606 XAO::IntegerStep* istep = ifield->getStep(j);
607 Handle(GEOM_FieldStep) step = field->AddStep(istep->getStep(), istep->getStamp());
609 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, istep->countValues()-1);
610 std::vector<int> ivalues = istep->getValues();
611 for (int k = 0; k < istep->countValues(); ++k)
613 values->SetValue(k, ivalues[k]);
615 step->SetValues(values);
621 XAO::DoubleField* dfield = (XAO::DoubleField*)xaoField;
622 for (int j = 0; j < xaoField->countSteps(); ++j)
624 XAO::DoubleStep* dstep = dfield->getStep(j);
625 Handle(GEOM_FieldStep) step = field->AddStep(dstep->getStep(), dstep->getStamp());
627 Handle(TColStd_HArray1OfReal) values = new TColStd_HArray1OfReal(0, dstep->countValues()-1);
628 std::vector<double> dvalues = dstep->getValues();
629 for (int k = 0; k < dstep->countValues(); ++k)
631 values->SetValue(k, dvalues[k]);
633 step->SetValues(values);
639 XAO::StringField* sfield = (XAO::StringField*)xaoField;
640 for (int j = 0; j < xaoField->countSteps(); ++j)
642 XAO::StringStep* sstep = sfield->getStep(j);
643 Handle(GEOM_FieldStep) step = field->AddStep(sstep->getStep(), sstep->getStamp());
645 Handle(TColStd_HArray1OfExtendedString) values = new TColStd_HArray1OfExtendedString(0, sstep->countValues()-1);
646 std::vector<std::string> svalues = sstep->getValues();
647 for (int k = 0; k < sstep->countValues(); ++k)
649 values->SetValue(k, TCollection_ExtendedString(svalues[k].c_str()));
651 step->SetValues(values);
657 fields->Append(field);
660 // make a Python command
661 GEOM::TPythonDump pd(function);
662 pd << "(imported, " << shape << ", ";
664 // list of sub shapes
666 int nbSubshapes = subShapes->Length();
669 for (int i = 1; i <= nbSubshapes; i++)
671 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(subShapes->Value(i));
672 pd << obj << ((i < nbSubshapes) ? ", " : "");
680 for (int i = 1; i <= nbGroups; i++)
682 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(groups->Value(i));
683 pd << obj << ((i < nbGroups) ? ", " : "");
692 for (int i = 1; i <= nbFields; i++)
694 Handle(GEOM_Field) obj = Handle(GEOM_Field)::DownCast(fields->Value(i));
695 pd << obj << ((i < nbFields) ? ", " : "");
698 std::string convFileName = Kernel_Utils::BackSlashToSlash( fileName );
700 pd << ") = geompy.ImportXAO(\"" << convFileName.c_str() << "\")";