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>
60 XAO::Dimension shapeEnumToDimension(const TopAbs_ShapeEnum& shape)
65 dim = XAO::VERTEX; break;
67 dim = XAO::EDGE; break;
69 dim = XAO::FACE; break;
71 dim = XAO::SOLID; break;
73 throw SALOME_Exception("Bad type"); // TODO
78 TopAbs_ShapeEnum getGroupDimension(XAO::Group* group)
80 XAO::Dimension dim = group->getDimension();
81 TopAbs_ShapeEnum rdim;
85 rdim = TopAbs_VERTEX; break;
87 rdim = TopAbs_EDGE; break;
89 rdim = TopAbs_FACE; break;
91 rdim = TopAbs_SOLID; break;
93 rdim = TopAbs_COMPOUND; break;
98 //=============================================================================
102 //=============================================================================
103 XAOPlugin_IOperations::XAOPlugin_IOperations( GEOM_Engine* theEngine )
104 : GEOMImpl_IBaseIEOperations( theEngine )
106 MESSAGE( "XAOPlugin_IOperations::XAOPlugin_IOperations" );
109 //=============================================================================
113 //=============================================================================
114 XAOPlugin_IOperations::~XAOPlugin_IOperations()
116 MESSAGE( "XAOPlugin_IOperations::~XAOPlugin_IOperations" );
119 bool XAOPlugin_IOperations::exportGroups( std::list<Handle(GEOM_Object)> groupList,
121 XAO::BrepGeometry* geometry )
124 std::list<Handle(GEOM_Object)>::iterator groupIterator = groupList.begin();
125 while (groupIterator != groupList.end())
127 Handle(GEOM_Object) currGroup = (*groupIterator++);
128 if (currGroup->GetType() != GEOM_GROUP) {
129 SetErrorCode("Error when export groups: you could perform this operation only with group.");
132 Handle(TColStd_HArray1OfInteger) groupIds = myGroupOperations->GetObjects(currGroup);
134 TopAbs_ShapeEnum shapeGroup = myGroupOperations->GetType(currGroup);
135 XAO::Dimension dim = shapeEnumToDimension(shapeGroup);
136 XAO::Group* group = xaoObject->addGroup(dim, currGroup->GetName().ToCString());
141 for (int i = 1; i <= groupIds->Length(); i++)
143 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
144 int index = geometry->getVertexIndexByReference(ref);
149 for (int i = 1; i <= groupIds->Length(); i++)
151 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
152 int index = geometry->getEdgeIndexByReference(ref);
157 for (int i = 1; i <= groupIds->Length(); i++)
159 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
160 int index = geometry->getFaceIndexByReference(ref);
165 for (int i = 1; i <= groupIds->Length(); i++)
167 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
168 int index = geometry->getSolidIndexByReference(ref);
177 void XAOPlugin_IOperations::exportFields( std::list<Handle(GEOM_Field)> fieldList,
179 XAO::BrepGeometry* geometry )
181 std::list<Handle(GEOM_Field)>::iterator fieldIterator = fieldList.begin();
182 while (fieldIterator != fieldList.end())
184 Handle(GEOM_Field) currField = (*fieldIterator++);
186 int fdim = currField->GetDimension();
187 int ftype = currField->GetDataType();
188 int nbComponents = currField->GetNbComponents();
189 std::string name = currField->GetName().ToCString();
191 XAO::Field* field = xaoObject->addField((XAO::Type)ftype, (XAO::Dimension)fdim, nbComponents, name);
193 Handle(TColStd_HArray1OfExtendedString) components = currField->GetComponents();
194 for (int i = components->Lower(), j = 0; i <= components->Upper(); ++i, ++j)
196 field->setComponentName(j, TCollection_AsciiString(components->Value(i)).ToCString());
199 std::list< Handle(GEOM_FieldStep)> steps = currField->GetSteps();
200 std::list<Handle(GEOM_FieldStep)>::iterator stepIterator = steps.begin();
201 while (stepIterator != steps.end())
203 Handle(GEOM_FieldStep) currStep = (*stepIterator++);
205 XAO::Step* step = field->addNewStep(currStep->GetID());
206 step->setStamp(currStep->GetStamp());
212 XAO::BooleanStep* bs = (XAO::BooleanStep*)step;
213 Handle(TColStd_HArray1OfInteger) bvalues = currStep->GetIntValues();
214 std::vector<bool> bv;
215 bv.reserve(bvalues->Upper());
216 for ( int i = bvalues->Lower(), nb = bvalues->Upper(); i <= nb; ++i )
218 bv.push_back(bvalues->Value(i) != 0);
225 XAO::IntegerStep* is = (XAO::IntegerStep*)step;
226 Handle(TColStd_HArray1OfInteger) ivalues = currStep->GetIntValues();
228 iv.reserve(ivalues->Upper());
229 for ( int i = ivalues->Lower(), nb = ivalues->Upper(); i <= nb; ++i )
231 iv.push_back(ivalues->Value(i));
238 XAO::DoubleStep* ds = (XAO::DoubleStep*)step;
239 Handle(TColStd_HArray1OfReal) dvalues = currStep->GetDoubleValues();
240 std::vector<double> dv;
241 dv.reserve(dvalues->Upper());
242 for ( int i = dvalues->Lower(), nb = dvalues->Upper(); i <= nb; ++i )
244 dv.push_back(dvalues->Value(i));
251 XAO::StringStep* ss = (XAO::StringStep*)step;
252 Handle(TColStd_HArray1OfExtendedString) svalues = currStep->GetStringValues();
253 std::vector<std::string> sv;
254 sv.reserve(svalues->Upper());
255 for ( int i = svalues->Lower(), nb = svalues->Upper(); i <= nb; ++i )
257 sv.push_back(TCollection_AsciiString(svalues->Value(i)).ToCString());
267 void XAOPlugin_IOperations::exportSubshapes( const Handle(GEOM_Object)& shape, XAO::BrepGeometry* geometry )
269 Handle(TColStd_HSequenceOfTransient) subObjects = myShapesOperations->GetExistingSubObjects( shape, GEOMImpl_IShapesOperations::SubShapes );
270 int nbSubObjects = subObjects->Length();
271 // set the names of the sub shapes
272 for (int i = 1; i <= nbSubObjects; i++)
274 Handle(Standard_Transient) transientSubObject = subObjects->Value(i);
275 if (transientSubObject.IsNull())
278 Handle(GEOM_Object) subObject = Handle(GEOM_Object)::DownCast( transientSubObject );
279 if (subObject->GetType() != GEOM_GROUP)
281 int subIndex = myShapesOperations->GetSubShapeIndex( shape, subObject );
282 switch (subObject->GetValue().ShapeType())
285 geometry->changeVertexName(subIndex, subObject->GetName().ToCString());
288 geometry->changeEdgeName(subIndex, subObject->GetName().ToCString());
291 geometry->changeFaceName(subIndex, subObject->GetName().ToCString());
294 geometry->changeSolidName(subIndex, subObject->GetName().ToCString());
301 //=============================================================================
303 * Export a shape to XAO format
304 * \param shape The shape to export
305 * \param groups The list of groups to export
306 * \param fields The list of fields to export
307 * \param fileName The name of the file to exported
308 * \return boolean indicating if export was succeful.
310 //=============================================================================
311 bool XAOPlugin_IOperations::ExportXAO( Handle(GEOM_Object) shape,
312 std::list<Handle(GEOM_Object)> groupList,
313 std::list<Handle(GEOM_Field)> fieldList,
315 const char* fileName,
316 const char* shapeFileName )
320 if (shape.IsNull()) return false;
322 // add a new shape function with parameters
323 Handle(GEOM_Function) lastFunction = shape->GetLastFunction();
324 if (lastFunction.IsNull()) return false;
326 // add a new result object
327 Handle(GEOM_Object) result = GetEngine()->AddObject(GEOM_IMPORT);
329 // add an Export function
330 Handle(GEOM_Function) exportFunction = result->AddFunction(XAOPlugin_Driver::GetID(), EXPORT_SHAPE);
331 if (exportFunction.IsNull()) return false;
332 if (exportFunction->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
334 // create the XAO object
335 XAO::Xao* xaoObject = new XAO::Xao();
336 xaoObject->setAuthor(author);
339 XAO::BrepGeometry* geometry = (XAO::BrepGeometry*)XAO::Geometry::createGeometry(XAO::BREP);
340 TopoDS_Shape topoShape = shape->GetValue();
341 exportFunction->SetValue(topoShape);
342 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)geometry;
343 brep->setTopoDS_Shape(topoShape);
345 geometry->setName(shape->GetName().ToCString());
346 exportSubshapes(shape, geometry);
347 xaoObject->setGeometry(geometry);
349 if (!exportGroups(groupList, xaoObject, geometry)) return false;
350 exportFields(fieldList, xaoObject, geometry);
352 // export the XAO to the file
353 xaoObject->exportXAO(fileName, shapeFileName);
355 // make a Python command
356 GEOM::TPythonDump pd(exportFunction);
357 std::string convFileName = Kernel_Utils::BackSlashToSlash(fileName);
358 pd << "exported = geompy.ExportXAO(" << shape;
362 if (groupList.size() > 0)
364 std::list<Handle(GEOM_Object)>::iterator itGroup = groupList.begin();
366 while (itGroup != groupList.end())
368 pd << ", " << (*itGroup++);
374 if (fieldList.size() > 0)
376 std::list<Handle(GEOM_Field)>::iterator itField = fieldList.begin();
378 while (itField != fieldList.end())
380 pd << ", " << (*itField++);
384 pd << "\"" << author << "\", \"" << convFileName.c_str() << "\", \"" << shapeFileName << "\")";
392 void XAOPlugin_IOperations::importSubShapes( XAO::Geometry* xaoGeometry,
393 Handle(GEOM_Function) function, int shapeType, int dim,
394 Handle(TColStd_HSequenceOfTransient)& subShapeList )
396 Handle(GEOM_Object) subShape;
397 Handle(GEOM_Function) aFunction;
398 Handle(TColStd_HArray1OfInteger) anArray;
400 XAO::GeometricElementList::iterator elementIterator = xaoGeometry->begin((XAO::Dimension)dim);
401 for (; elementIterator != xaoGeometry->end((XAO::Dimension)dim); elementIterator++)
403 XAO::GeometricElement element = elementIterator->second;
404 if (!element.hasName())
407 std::string name = element.getName();
408 std::string ref = element.getReference();
409 int iref = XAO::XaoUtils::stringToInt(ref);
411 anArray = new TColStd_HArray1OfInteger(1, 1);
412 anArray->SetValue(1, iref);
414 subShape = GetEngine()->AddObject(GEOM_SUBSHAPE);
415 Handle(GEOM_Function) aFunction = subShape->AddFunction(GEOM_Object::GetSubShapeID(), 1);
416 if (aFunction.IsNull())
419 subShape->SetName(name.c_str());
420 subShape->SetType(shapeType);
422 GEOM_ISubShape aSSI(aFunction);
423 aSSI.SetMainShape(function);
424 aSSI.SetIndices(anArray);
426 //aFunction->SetValue(aValue);
427 subShapeList->Append(subShape);
429 // Put this subshape in the list of sub-shapes of theMainShape
430 function->AddSubShapeReference(aFunction);
434 //=============================================================================
436 * Import a shape from XAO format
437 * \param fileName The name of the file to import
438 * \param shape The imported shape
439 * \param subShapes The list of imported groups
440 * \param groups The list of imported groups
441 * \param fields The list of imported fields
442 * \return boolean indicating if import was succeful.
444 //=============================================================================
445 bool XAOPlugin_IOperations::ImportXAO( const char* fileName,
446 Handle(GEOM_Object)& shape,
447 Handle(TColStd_HSequenceOfTransient)& subShapes,
448 Handle(TColStd_HSequenceOfTransient)& groups,
449 Handle(TColStd_HSequenceOfTransient)& fields )
453 if (fileName == NULL || groups.IsNull() || fields.IsNull())
457 XAO::Xao* xaoObject = new XAO::Xao();
460 xaoObject->importXAO(fileName);
462 catch (XAO::XAO_Exception& exc)
465 SetErrorCode(exc.what());
469 XAO::Geometry* xaoGeometry = xaoObject->getGeometry();
470 if (xaoGeometry == NULL)
473 SetErrorCode("Cannot import XAO: geometry format not supported.");
478 shape = GetEngine()->AddObject(GEOM_IMPORT);
479 Handle(GEOM_Function) function = shape->AddFunction(XAOPlugin_Driver::GetID(), IMPORT_SHAPE);
480 if (function.IsNull()) return false;
481 if (function->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
483 function->SetString( XAOPlugin_Driver::GetFileNameTag(), fileName );
486 if (xaoGeometry->getFormat() == XAO::BREP)
488 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)xaoGeometry;
489 TopoDS_Shape geomShape = brep->getTopoDS_Shape();
490 function->SetValue(geomShape);
491 shape->SetName(xaoGeometry->getName().c_str());
496 SetErrorCode("Cannot import XAO: geometry format not supported.");
500 // create sub shapes with names
501 importSubShapes(xaoGeometry, function, GEOM_POINT, XAO::VERTEX, subShapes);
502 importSubShapes(xaoGeometry, function, GEOM_EDGE, XAO::EDGE, subShapes);
503 importSubShapes(xaoGeometry, function, GEOM_FACE, XAO::FACE, subShapes);
504 importSubShapes(xaoGeometry, function, GEOM_SOLID, XAO::SOLID, subShapes);
507 int nbGroups = xaoObject->countGroups();
508 for (int i = 0; i < nbGroups; ++i)
510 XAO::Group* xaoGroup = xaoObject->getGroup(i);
512 // build an array with the indexes of the sub shapes
513 int nbElt = xaoGroup->count();
514 Handle(TColStd_HArray1OfInteger) array = new TColStd_HArray1OfInteger(1, nbElt);
516 for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it)
519 std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
520 array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
523 // create the group with the array of sub shapes indexes
524 Handle(GEOM_Object) group = GetEngine()->AddSubShape(shape, array);
525 group->SetType(GEOM_GROUP);
526 group->SetName(xaoGroup->getName().c_str());
528 // Set a sub-shape type
529 TDF_Label freeLabel = group->GetFreeLabel();
530 TDataStd_Integer::Set(freeLabel, (Standard_Integer) getGroupDimension(xaoGroup));
531 groups->Append(group);
535 int nbFields = xaoObject->countFields();
536 for (int i = 0; i < nbFields; ++i)
538 XAO::Field* xaoField = xaoObject->getField(i);
540 Handle(TColStd_HArray1OfExtendedString) components = new TColStd_HArray1OfExtendedString(0, xaoField->countComponents()-1);
541 for (int j = 0; j < xaoField->countComponents(); ++j)
543 components->SetValue(j, (TCollection_ExtendedString)xaoField->getComponentName(j).c_str());
546 Handle(GEOM_Field) field = myFieldOperations->CreateField(shape,
547 xaoField->getName().c_str(),
548 (int)xaoField->getType(),
549 (int)xaoField->getDimension(),
552 switch (xaoField->getType())
556 XAO::BooleanField* bfield = (XAO::BooleanField*)xaoField;
557 for (int j = 0; j < xaoField->countSteps(); ++j)
559 XAO::BooleanStep* bstep = bfield->getStep(j);
560 Handle(GEOM_FieldStep) step = field->AddStep(bstep->getStep(), bstep->getStamp());
562 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, bstep->countValues()-1);
563 std::vector<bool> bvalues = bstep->getValues();
564 for (int k = 0; k < bstep->countValues(); ++k)
566 values->SetValue(k, bvalues[k] ? 1 : 0);
568 step->SetValues(values);
574 XAO::IntegerField* ifield = (XAO::IntegerField*)xaoField;
575 for (int j = 0; j < xaoField->countSteps(); ++j)
577 XAO::IntegerStep* istep = ifield->getStep(j);
578 Handle(GEOM_FieldStep) step = field->AddStep(istep->getStep(), istep->getStamp());
580 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, istep->countValues()-1);
581 std::vector<int> ivalues = istep->getValues();
582 for (int k = 0; k < istep->countValues(); ++k)
584 values->SetValue(k, ivalues[k]);
586 step->SetValues(values);
592 XAO::DoubleField* dfield = (XAO::DoubleField*)xaoField;
593 for (int j = 0; j < xaoField->countSteps(); ++j)
595 XAO::DoubleStep* dstep = dfield->getStep(j);
596 Handle(GEOM_FieldStep) step = field->AddStep(dstep->getStep(), dstep->getStamp());
598 Handle(TColStd_HArray1OfReal) values = new TColStd_HArray1OfReal(0, dstep->countValues()-1);
599 std::vector<double> dvalues = dstep->getValues();
600 for (int k = 0; k < dstep->countValues(); ++k)
602 values->SetValue(k, dvalues[k]);
604 step->SetValues(values);
610 XAO::StringField* sfield = (XAO::StringField*)xaoField;
611 for (int j = 0; j < xaoField->countSteps(); ++j)
613 XAO::StringStep* sstep = sfield->getStep(j);
614 Handle(GEOM_FieldStep) step = field->AddStep(sstep->getStep(), sstep->getStamp());
616 Handle(TColStd_HArray1OfExtendedString) values = new TColStd_HArray1OfExtendedString(0, sstep->countValues()-1);
617 std::vector<std::string> svalues = sstep->getValues();
618 for (int k = 0; k < sstep->countValues(); ++k)
620 values->SetValue(k, TCollection_ExtendedString(svalues[k].c_str()));
622 step->SetValues(values);
628 fields->Append(field);
631 // make a Python command
632 GEOM::TPythonDump pd(function);
633 pd << "(imported, " << shape << ", ";
635 // list of sub shapes
637 int nbSubshapes = subShapes->Length();
640 for (int i = 1; i <= nbSubshapes; i++)
642 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(subShapes->Value(i));
643 pd << obj << ((i < nbSubshapes) ? ", " : "");
651 for (int i = 1; i <= nbGroups; i++)
653 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(groups->Value(i));
654 pd << obj << ((i < nbGroups) ? ", " : "");
663 for (int i = 1; i <= nbFields; i++)
665 Handle(GEOM_Field) obj = Handle(GEOM_Field)::DownCast(fields->Value(i));
666 pd << obj << ((i < nbFields) ? ", " : "");
669 std::string convFileName = Kernel_Utils::BackSlashToSlash( fileName );
671 pd << ") = geompy.ImportXAO(\"" << convFileName.c_str() << "\")";