1 // Copyright (C) 2014-2019 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());
142 for (int i = 1; i <= groupIds->Length(); i++)
144 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
145 int index = geometry->getVertexIndexByReference(ref);
150 for (int i = 1; i <= groupIds->Length(); i++)
152 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
153 int index = geometry->getEdgeIndexByReference(ref);
158 for (int i = 1; i <= groupIds->Length(); i++)
160 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
161 int index = geometry->getFaceIndexByReference(ref);
166 for (int i = 1; i <= groupIds->Length(); i++)
168 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
169 int index = geometry->getSolidIndexByReference(ref);
180 void XAOPlugin_IOperations::exportFields( std::list<Handle(GEOM_Field)> fieldList,
182 XAO::BrepGeometry* geometry )
184 std::list<Handle(GEOM_Field)>::iterator fieldIterator = fieldList.begin();
185 while (fieldIterator != fieldList.end())
187 Handle(GEOM_Field) currField = (*fieldIterator++);
189 int fdim = currField->GetDimension();
190 int ftype = currField->GetDataType();
191 int nbComponents = currField->GetNbComponents();
192 std::string name = currField->GetName().ToCString();
194 XAO::Field* field = xaoObject->addField((XAO::Type)ftype, (XAO::Dimension)fdim, nbComponents, name);
196 Handle(TColStd_HArray1OfExtendedString) components = currField->GetComponents();
197 for (int i = components->Lower(), j = 0; i <= components->Upper(); ++i, ++j)
199 field->setComponentName(j, TCollection_AsciiString(components->Value(i)).ToCString());
202 std::list< Handle(GEOM_FieldStep)> steps = currField->GetSteps();
203 std::list<Handle(GEOM_FieldStep)>::iterator stepIterator = steps.begin();
204 while (stepIterator != steps.end())
206 Handle(GEOM_FieldStep) currStep = (*stepIterator++);
208 XAO::Step* step = field->addNewStep(currStep->GetID());
209 step->setStamp(currStep->GetStamp());
215 XAO::BooleanStep* bs = (XAO::BooleanStep*)step;
216 Handle(TColStd_HArray1OfInteger) bvalues = currStep->GetIntValues();
217 std::vector<bool> bv;
218 bv.reserve(bvalues->Upper());
219 for ( int i = bvalues->Lower(), nb = bvalues->Upper(); i <= nb; ++i )
221 bv.push_back(bvalues->Value(i) != 0);
228 XAO::IntegerStep* is = (XAO::IntegerStep*)step;
229 Handle(TColStd_HArray1OfInteger) ivalues = currStep->GetIntValues();
231 iv.reserve(ivalues->Upper());
232 for ( int i = ivalues->Lower(), nb = ivalues->Upper(); i <= nb; ++i )
234 iv.push_back(ivalues->Value(i));
241 XAO::DoubleStep* ds = (XAO::DoubleStep*)step;
242 Handle(TColStd_HArray1OfReal) dvalues = currStep->GetDoubleValues();
243 std::vector<double> dv;
244 dv.reserve(dvalues->Upper());
245 for ( int i = dvalues->Lower(), nb = dvalues->Upper(); i <= nb; ++i )
247 dv.push_back(dvalues->Value(i));
254 XAO::StringStep* ss = (XAO::StringStep*)step;
255 Handle(TColStd_HArray1OfExtendedString) svalues = currStep->GetStringValues();
256 std::vector<std::string> sv;
257 sv.reserve(svalues->Upper());
258 for ( int i = svalues->Lower(), nb = svalues->Upper(); i <= nb; ++i )
260 sv.push_back(TCollection_AsciiString(svalues->Value(i)).ToCString());
270 void XAOPlugin_IOperations::exportSubshapes( const Handle(GEOM_Object)& shape, XAO::BrepGeometry* geometry )
272 Handle(TColStd_HSequenceOfTransient) subObjects = myShapesOperations->GetExistingSubObjects( shape, GEOMImpl_IShapesOperations::SubShapes );
273 int nbSubObjects = subObjects->Length();
274 if (!nbSubObjects) return;
276 TopoDS_Shape aMainShape = shape->GetValue();
277 if (aMainShape.IsNull()) return;
278 TopTools_IndexedMapOfShape anIndices;
279 TopExp::MapShapes(aMainShape, anIndices);
281 // set the names of the sub shapes
282 for (int i = 1; i <= nbSubObjects; i++)
284 Handle(Standard_Transient) transientSubObject = subObjects->Value(i);
285 if (transientSubObject.IsNull())
288 Handle(GEOM_Object) subObject = Handle(GEOM_Object)::DownCast( transientSubObject );
289 if (subObject->GetType() != GEOM_GROUP)
291 TopoDS_Shape aSubShape = subObject->GetValue();
292 if (aSubShape.IsNull()) continue;
294 // Do not call GEOMImpl_IShapesOperations::GetSubShapeIndex() here
295 // for time optimization reason (it invokes TopExp::MapShapes())
296 //int subIndex = myShapesOperations->GetSubShapeIndex( shape, subObject );
297 int subIndex = anIndices.FindIndex(aSubShape);
298 if (!subIndex) continue;
300 switch (aSubShape.ShapeType())
303 geometry->changeVertexName(subIndex, subObject->GetName().ToCString());
306 geometry->changeEdgeName(subIndex, subObject->GetName().ToCString());
309 geometry->changeFaceName(subIndex, subObject->GetName().ToCString());
312 geometry->changeSolidName(subIndex, subObject->GetName().ToCString());
321 //=============================================================================
323 * Export a shape to XAO format
324 * \param shape The shape to export
325 * \param groups The list of groups to export
326 * \param fields The list of fields to export
327 * \param fileName The name of the file to exported
328 * \return boolean indicating if export was succeful.
330 //=============================================================================
331 bool XAOPlugin_IOperations::ExportXAO( Handle(GEOM_Object) shape,
332 std::list<Handle(GEOM_Object)> groupList,
333 std::list<Handle(GEOM_Field)> fieldList,
335 const char* fileName,
336 const char* shapeFileName )
340 if (shape.IsNull()) return false;
342 // add a new shape function with parameters
343 Handle(GEOM_Function) lastFunction = shape->GetLastFunction();
344 if (lastFunction.IsNull()) return false;
346 // add a new result object
347 Handle(GEOM_Object) result = GetEngine()->AddObject(GEOM_IMPORT);
349 // add an Export function
350 Handle(GEOM_Function) exportFunction = result->AddFunction(XAOPlugin_Driver::GetID(), EXPORT_SHAPE);
351 if (exportFunction.IsNull()) return false;
352 if (exportFunction->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
354 // create the XAO object
355 XAO::Xao* xaoObject = new XAO::Xao();
356 xaoObject->setAuthor(author);
359 XAO::BrepGeometry* geometry = (XAO::BrepGeometry*)XAO::Geometry::createGeometry(XAO::BREP);
360 TopoDS_Shape topoShape = shape->GetValue();
361 exportFunction->SetValue(topoShape);
362 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)geometry;
363 brep->setTopoDS_Shape(topoShape);
365 geometry->setName(shape->GetName().ToCString());
366 exportSubshapes(shape, geometry);
367 xaoObject->setGeometry(geometry);
369 if (!exportGroups(groupList, xaoObject, geometry)) return false;
370 exportFields(fieldList, xaoObject, geometry);
372 // export the XAO to the file
373 xaoObject->exportXAO(fileName, shapeFileName);
375 // make a Python command
376 GEOM::TPythonDump pd(exportFunction);
377 std::string convFileName = Kernel_Utils::BackSlashToSlash(fileName);
378 pd << "exported = geompy.ExportXAO(" << shape;
382 if (groupList.size() > 0)
384 std::list<Handle(GEOM_Object)>::iterator itGroup = groupList.begin();
386 while (itGroup != groupList.end())
388 pd << ", " << (*itGroup++);
394 if (fieldList.size() > 0)
396 std::list<Handle(GEOM_Field)>::iterator itField = fieldList.begin();
398 while (itField != fieldList.end())
400 pd << ", " << (*itField++);
404 pd << "\"" << author << "\", \"" << convFileName.c_str() << "\", \"" << shapeFileName << "\")";
412 void XAOPlugin_IOperations::importSubShapes( XAO::Geometry* xaoGeometry,
413 Handle(GEOM_Function) function, int shapeType, int dim,
414 Handle(TColStd_HSequenceOfTransient)& subShapeList )
416 Handle(GEOM_Object) subShape;
417 Handle(GEOM_Function) aFunction;
418 Handle(TColStd_HArray1OfInteger) anArray;
420 XAO::GeometricElementList::iterator elementIterator = xaoGeometry->begin((XAO::Dimension)dim);
421 for (; elementIterator != xaoGeometry->end((XAO::Dimension)dim); elementIterator++)
423 XAO::GeometricElement element = elementIterator->second;
424 if (!element.hasName())
427 std::string name = element.getName();
428 std::string ref = element.getReference();
429 int iref = XAO::XaoUtils::stringToInt(ref);
431 anArray = new TColStd_HArray1OfInteger(1, 1);
432 anArray->SetValue(1, iref);
434 subShape = GetEngine()->AddObject(GEOM_SUBSHAPE);
435 Handle(GEOM_Function) aFunction = subShape->AddFunction(GEOM_Object::GetSubShapeID(), 1);
436 if (aFunction.IsNull())
439 subShape->SetName(name.c_str());
440 subShape->SetType(shapeType);
442 GEOM_ISubShape aSSI(aFunction);
443 aSSI.SetMainShape(function);
444 aSSI.SetIndices(anArray);
446 //aFunction->SetValue(aValue);
447 subShapeList->Append(subShape);
449 // Put this subshape in the list of sub-shapes of theMainShape
450 function->AddSubShapeReference(aFunction);
454 //=============================================================================
456 * Import a shape from XAO format
457 * \param fileName The name of the file to import
458 * \param shape The imported shape
459 * \param subShapes The list of imported groups
460 * \param groups The list of imported groups
461 * \param fields The list of imported fields
462 * \return boolean indicating if import was succeful.
464 //=============================================================================
465 bool XAOPlugin_IOperations::ImportXAO( const char* fileName,
466 Handle(GEOM_Object)& shape,
467 Handle(TColStd_HSequenceOfTransient)& subShapes,
468 Handle(TColStd_HSequenceOfTransient)& groups,
469 Handle(TColStd_HSequenceOfTransient)& fields )
473 if (fileName == NULL || groups.IsNull() || fields.IsNull())
477 XAO::Xao* xaoObject = new XAO::Xao();
480 xaoObject->importXAO(fileName);
482 catch (XAO::XAO_Exception& exc)
485 SetErrorCode(exc.what());
489 XAO::Geometry* xaoGeometry = xaoObject->getGeometry();
490 if (xaoGeometry == NULL)
493 SetErrorCode("Cannot import XAO: geometry format not supported.");
498 shape = GetEngine()->AddObject(GEOM_IMPORT);
499 Handle(GEOM_Function) function = shape->AddFunction(XAOPlugin_Driver::GetID(), IMPORT_SHAPE);
500 if (function.IsNull()) return false;
501 if (function->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
503 function->SetString( XAOPlugin_Driver::GetFileNameTag(), fileName );
506 if (xaoGeometry->getFormat() == XAO::BREP)
508 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)xaoGeometry;
509 TopoDS_Shape geomShape = brep->getTopoDS_Shape();
510 function->SetValue(geomShape);
511 shape->SetName(xaoGeometry->getName().c_str());
516 SetErrorCode("Cannot import XAO: geometry format not supported.");
520 // create sub shapes with names
521 importSubShapes(xaoGeometry, function, GEOM_POINT, XAO::VERTEX, subShapes);
522 importSubShapes(xaoGeometry, function, GEOM_EDGE, XAO::EDGE, subShapes);
523 importSubShapes(xaoGeometry, function, GEOM_FACE, XAO::FACE, subShapes);
524 importSubShapes(xaoGeometry, function, GEOM_SOLID, XAO::SOLID, subShapes);
527 int nbGroups = xaoObject->countGroups();
528 for (int i = 0; i < nbGroups; ++i)
530 XAO::Group* xaoGroup = xaoObject->getGroup(i);
532 // build an array with the indexes of the sub shapes
533 int nbElt = xaoGroup->count();
534 Handle(TColStd_HArray1OfInteger) array = new TColStd_HArray1OfInteger(1, nbElt);
536 for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it)
539 std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
540 array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
543 // create the group with the array of sub shapes indexes
544 Handle(GEOM_Object) group = GetEngine()->AddSubShape(shape, array);
545 group->SetType(GEOM_GROUP);
546 group->SetName(xaoGroup->getName().c_str());
548 // Set a sub-shape type
549 TDF_Label freeLabel = group->GetFreeLabel();
550 TDataStd_Integer::Set(freeLabel, (Standard_Integer) getGroupDimension(xaoGroup));
551 groups->Append(group);
555 int nbFields = xaoObject->countFields();
556 for (int i = 0; i < nbFields; ++i)
558 XAO::Field* xaoField = xaoObject->getField(i);
560 Handle(TColStd_HArray1OfExtendedString) components = new TColStd_HArray1OfExtendedString(0, xaoField->countComponents()-1);
561 for (int j = 0; j < xaoField->countComponents(); ++j)
563 components->SetValue(j, (TCollection_ExtendedString)xaoField->getComponentName(j).c_str());
566 Handle(GEOM_Field) field = myFieldOperations->CreateField(shape,
567 xaoField->getName().c_str(),
568 (int)xaoField->getType(),
569 (int)xaoField->getDimension(),
572 switch (xaoField->getType())
576 XAO::BooleanField* bfield = (XAO::BooleanField*)xaoField;
577 for (int j = 0; j < xaoField->countSteps(); ++j)
579 XAO::BooleanStep* bstep = bfield->getStep(j);
580 Handle(GEOM_FieldStep) step = field->AddStep(bstep->getStep(), bstep->getStamp());
582 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, bstep->countValues()-1);
583 std::vector<bool> bvalues = bstep->getValues();
584 for (int k = 0; k < bstep->countValues(); ++k)
586 values->SetValue(k, bvalues[k] ? 1 : 0);
588 step->SetValues(values);
594 XAO::IntegerField* ifield = (XAO::IntegerField*)xaoField;
595 for (int j = 0; j < xaoField->countSteps(); ++j)
597 XAO::IntegerStep* istep = ifield->getStep(j);
598 Handle(GEOM_FieldStep) step = field->AddStep(istep->getStep(), istep->getStamp());
600 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, istep->countValues()-1);
601 std::vector<int> ivalues = istep->getValues();
602 for (int k = 0; k < istep->countValues(); ++k)
604 values->SetValue(k, ivalues[k]);
606 step->SetValues(values);
612 XAO::DoubleField* dfield = (XAO::DoubleField*)xaoField;
613 for (int j = 0; j < xaoField->countSteps(); ++j)
615 XAO::DoubleStep* dstep = dfield->getStep(j);
616 Handle(GEOM_FieldStep) step = field->AddStep(dstep->getStep(), dstep->getStamp());
618 Handle(TColStd_HArray1OfReal) values = new TColStd_HArray1OfReal(0, dstep->countValues()-1);
619 std::vector<double> dvalues = dstep->getValues();
620 for (int k = 0; k < dstep->countValues(); ++k)
622 values->SetValue(k, dvalues[k]);
624 step->SetValues(values);
630 XAO::StringField* sfield = (XAO::StringField*)xaoField;
631 for (int j = 0; j < xaoField->countSteps(); ++j)
633 XAO::StringStep* sstep = sfield->getStep(j);
634 Handle(GEOM_FieldStep) step = field->AddStep(sstep->getStep(), sstep->getStamp());
636 Handle(TColStd_HArray1OfExtendedString) values = new TColStd_HArray1OfExtendedString(0, sstep->countValues()-1);
637 std::vector<std::string> svalues = sstep->getValues();
638 for (int k = 0; k < sstep->countValues(); ++k)
640 values->SetValue(k, TCollection_ExtendedString(svalues[k].c_str()));
642 step->SetValues(values);
648 fields->Append(field);
651 // make a Python command
652 GEOM::TPythonDump pd(function);
653 pd << "(imported, " << shape << ", ";
655 // list of sub shapes
657 int nbSubshapes = subShapes->Length();
660 for (int i = 1; i <= nbSubshapes; i++)
662 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(subShapes->Value(i));
663 pd << obj << ((i < nbSubshapes) ? ", " : "");
671 for (int i = 1; i <= nbGroups; i++)
673 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(groups->Value(i));
674 pd << obj << ((i < nbGroups) ? ", " : "");
683 for (int i = 1; i <= nbFields; i++)
685 Handle(GEOM_Field) obj = Handle(GEOM_Field)::DownCast(fields->Value(i));
686 pd << obj << ((i < nbFields) ? ", " : "");
689 std::string convFileName = Kernel_Utils::BackSlashToSlash( fileName );
691 pd << ") = geompy.ImportXAO(\"" << convFileName.c_str() << "\")";