1 // Copyright (C) 2014-2015 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 <utilities.h>
27 #include <Utils_SALOME_Exception.hxx>
30 #include "GEOM_PythonDump.hxx"
31 #include "GEOMImpl_Types.hxx"
32 #include "GEOMImpl_IGroupOperations.hxx"
33 #include "GEOMImpl_IShapesOperations.hxx"
34 #include "GEOMImpl_IFieldOperations.hxx"
35 #include "GEOM_ISubShape.hxx"
37 #include <XAO_Geometry.hxx>
38 #include <XAO_BrepGeometry.hxx>
39 #include <XAO_Xao.hxx>
40 #include <XAO_Group.hxx>
41 #include <XAO_Field.hxx>
42 #include <XAO_BooleanField.hxx>
43 #include <XAO_IntegerField.hxx>
44 #include <XAO_DoubleField.hxx>
45 #include <XAO_StringField.hxx>
46 #include <XAO_DoubleStep.hxx>
47 #include <XAO_StringStep.hxx>
49 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
52 #include <TColStd_HArray1OfByte.hxx>
53 #include <TColStd_HArray1OfReal.hxx>
54 #include <TDataStd_Integer.hxx>
57 XAO::Dimension shapeEnumToDimension(const TopAbs_ShapeEnum& shape)
62 dim = XAO::VERTEX; break;
64 dim = XAO::EDGE; break;
66 dim = XAO::FACE; break;
68 dim = XAO::SOLID; break;
70 throw SALOME_Exception("Bad type"); // TODO
75 TopAbs_ShapeEnum getGroupDimension(XAO::Group* group)
77 XAO::Dimension dim = group->getDimension();
78 TopAbs_ShapeEnum rdim;
82 rdim = TopAbs_VERTEX; break;
84 rdim = TopAbs_EDGE; break;
86 rdim = TopAbs_FACE; break;
88 rdim = TopAbs_SOLID; break;
90 rdim = TopAbs_COMPOUND; break;
95 //=============================================================================
99 //=============================================================================
100 XAOPlugin_IOperations::XAOPlugin_IOperations( GEOM_Engine* theEngine, int theDocID )
101 : GEOMImpl_IBaseIEOperations( theEngine, theDocID )
103 MESSAGE( "XAOPlugin_IOperations::XAOPlugin_IOperations" );
106 //=============================================================================
110 //=============================================================================
111 XAOPlugin_IOperations::~XAOPlugin_IOperations()
113 MESSAGE( "XAOPlugin_IOperations::~XAOPlugin_IOperations" );
116 bool XAOPlugin_IOperations::exportGroups( std::list<Handle(GEOM_Object)> groupList,
118 XAO::BrepGeometry* geometry )
121 std::list<Handle(GEOM_Object)>::iterator groupIterator = groupList.begin();
122 while (groupIterator != groupList.end())
124 Handle(GEOM_Object) currGroup = (*groupIterator++);
125 if (currGroup->GetType() != GEOM_GROUP) {
126 SetErrorCode("Error when export groups: you could perform this operation only with group.");
129 Handle(TColStd_HArray1OfInteger) groupIds = myGroupOperations->GetObjects(currGroup);
131 TopAbs_ShapeEnum shapeGroup = myGroupOperations->GetType(currGroup);
132 XAO::Dimension dim = shapeEnumToDimension(shapeGroup);
133 XAO::Group* group = xaoObject->addGroup(dim, currGroup->GetName().ToCString());
138 for (int i = 1; i <= groupIds->Length(); i++)
140 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
141 int index = geometry->getVertexIndexByReference(ref);
146 for (int i = 1; i <= groupIds->Length(); i++)
148 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
149 int index = geometry->getEdgeIndexByReference(ref);
154 for (int i = 1; i <= groupIds->Length(); i++)
156 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
157 int index = geometry->getFaceIndexByReference(ref);
162 for (int i = 1; i <= groupIds->Length(); i++)
164 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
165 int index = geometry->getSolidIndexByReference(ref);
174 void XAOPlugin_IOperations::exportFields( std::list<Handle(GEOM_Field)> fieldList,
176 XAO::BrepGeometry* geometry )
178 std::list<Handle(GEOM_Field)>::iterator fieldIterator = fieldList.begin();
179 while (fieldIterator != fieldList.end())
181 Handle(GEOM_Field) currField = (*fieldIterator++);
183 int fdim = currField->GetDimension();
184 int ftype = currField->GetDataType();
185 int nbComponents = currField->GetNbComponents();
186 std::string name = currField->GetName().ToCString();
188 XAO::Field* field = xaoObject->addField((XAO::Type)ftype, (XAO::Dimension)fdim, nbComponents, name);
190 Handle(TColStd_HArray1OfExtendedString) components = currField->GetComponents();
191 for (int i = components->Lower(), j = 0; i <= components->Upper(); ++i, ++j)
193 field->setComponentName(j, TCollection_AsciiString(components->Value(i)).ToCString());
196 std::list< Handle(GEOM_FieldStep)> steps = currField->GetSteps();
197 std::list<Handle(GEOM_FieldStep)>::iterator stepIterator = steps.begin();
198 while (stepIterator != steps.end())
200 Handle(GEOM_FieldStep) currStep = (*stepIterator++);
202 XAO::Step* step = field->addNewStep(currStep->GetID());
203 step->setStamp(currStep->GetStamp());
209 XAO::BooleanStep* bs = (XAO::BooleanStep*)step;
210 Handle(TColStd_HArray1OfInteger) bvalues = currStep->GetIntValues();
211 std::vector<bool> bv;
212 bv.reserve(bvalues->Upper());
213 for ( int i = bvalues->Lower(), nb = bvalues->Upper(); i <= nb; ++i )
215 bv.push_back(bvalues->Value(i) != 0);
222 XAO::IntegerStep* is = (XAO::IntegerStep*)step;
223 Handle(TColStd_HArray1OfInteger) ivalues = currStep->GetIntValues();
225 iv.reserve(ivalues->Upper());
226 for ( int i = ivalues->Lower(), nb = ivalues->Upper(); i <= nb; ++i )
228 iv.push_back(ivalues->Value(i));
235 XAO::DoubleStep* ds = (XAO::DoubleStep*)step;
236 Handle(TColStd_HArray1OfReal) dvalues = currStep->GetDoubleValues();
237 std::vector<double> dv;
238 dv.reserve(dvalues->Upper());
239 for ( int i = dvalues->Lower(), nb = dvalues->Upper(); i <= nb; ++i )
241 dv.push_back(dvalues->Value(i));
248 XAO::StringStep* ss = (XAO::StringStep*)step;
249 Handle(TColStd_HArray1OfExtendedString) svalues = currStep->GetStringValues();
250 std::vector<std::string> sv;
251 sv.reserve(svalues->Upper());
252 for ( int i = svalues->Lower(), nb = svalues->Upper(); i <= nb; ++i )
254 sv.push_back(TCollection_AsciiString(svalues->Value(i)).ToCString());
264 void XAOPlugin_IOperations::exportSubshapes( const Handle(GEOM_Object)& shape, XAO::BrepGeometry* geometry )
266 Handle(TColStd_HSequenceOfTransient) subObjects = myShapesOperations->GetExistingSubObjects( shape, GEOMImpl_IShapesOperations::SubShapes );
267 int nbSubObjects = subObjects->Length();
268 // set the names of the sub shapes
269 for (int i = 1; i <= nbSubObjects; i++)
271 Handle(Standard_Transient) transientSubObject = subObjects->Value(i);
272 if (transientSubObject.IsNull())
275 Handle(GEOM_Object) subObject = Handle(GEOM_Object)::DownCast( transientSubObject );
276 if (subObject->GetType() != GEOM_GROUP)
278 int subIndex = myShapesOperations->GetSubShapeIndex( shape, subObject );
279 switch (subObject->GetValue().ShapeType())
282 geometry->changeVertexName(subIndex, subObject->GetName().ToCString());
285 geometry->changeEdgeName(subIndex, subObject->GetName().ToCString());
288 geometry->changeFaceName(subIndex, subObject->GetName().ToCString());
291 geometry->changeSolidName(subIndex, subObject->GetName().ToCString());
298 //=============================================================================
300 * Export a shape to XAO format
301 * \param shape The shape to export
302 * \param groups The list of groups to export
303 * \param fields The list of fields to export
304 * \param fileName The name of the file to exported
305 * \return boolean indicating if export was succeful.
307 //=============================================================================
308 bool XAOPlugin_IOperations::ExportXAO( Handle(GEOM_Object) shape,
309 std::list<Handle(GEOM_Object)> groupList,
310 std::list<Handle(GEOM_Field)> fieldList,
312 const char* fileName )
316 if (shape.IsNull()) return false;
318 // add a new shape function with parameters
319 Handle(GEOM_Function) lastFunction = shape->GetLastFunction();
320 if (lastFunction.IsNull()) return false;
322 // add a new result object
323 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
325 // add an Export function
326 Handle(GEOM_Function) exportFunction = result->AddFunction(XAOPlugin_Driver::GetID(), EXPORT_SHAPE);
327 if (exportFunction.IsNull()) return false;
328 if (exportFunction->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
330 // create the XAO object
331 XAO::Xao* xaoObject = new XAO::Xao();
332 xaoObject->setAuthor(author);
335 XAO::BrepGeometry* geometry = (XAO::BrepGeometry*)XAO::Geometry::createGeometry(XAO::BREP);
336 TopoDS_Shape topoShape = shape->GetValue();
337 exportFunction->SetValue(topoShape);
338 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)geometry;
339 brep->setTopoDS_Shape(topoShape);
341 geometry->setName(shape->GetName().ToCString());
342 exportSubshapes(shape, geometry);
343 xaoObject->setGeometry(geometry);
345 if (!exportGroups(groupList, xaoObject, geometry)) return false;
346 exportFields(fieldList, xaoObject, geometry);
348 // export the XAO to the file
349 xaoObject->exportXAO(fileName);
351 // make a Python command
352 GEOM::TPythonDump pd(exportFunction);
353 pd << "exported = geompy.ExportXAO(" << shape;
357 if (groupList.size() > 0)
359 std::list<Handle(GEOM_Object)>::iterator itGroup = groupList.begin();
361 while (itGroup != groupList.end())
363 pd << ", " << (*itGroup++);
369 if (fieldList.size() > 0)
371 std::list<Handle(GEOM_Field)>::iterator itField = fieldList.begin();
373 while (itField != fieldList.end())
375 pd << ", " << (*itField++);
379 pd << "\"" << author << "\", \"" << fileName << "\")";
387 void XAOPlugin_IOperations::importSubShapes( XAO::Geometry* xaoGeometry,
388 Handle(GEOM_Function) function, int shapeType, int dim,
389 Handle(TColStd_HSequenceOfTransient)& subShapeList )
391 Handle(GEOM_Object) subShape;
392 Handle(GEOM_Function) aFunction;
393 Handle(TColStd_HArray1OfInteger) anArray;
395 XAO::GeometricElementList::iterator elementIterator = xaoGeometry->begin((XAO::Dimension)dim);
396 for (; elementIterator != xaoGeometry->end((XAO::Dimension)dim); elementIterator++)
398 XAO::GeometricElement element = elementIterator->second;
399 if (!element.hasName())
402 std::string name = element.getName();
403 std::string ref = element.getReference();
404 int iref = XAO::XaoUtils::stringToInt(ref);
406 anArray = new TColStd_HArray1OfInteger(1, 1);
407 anArray->SetValue(1, iref);
409 subShape = GetEngine()->AddObject(GetDocID(), GEOM_SUBSHAPE);
410 Handle(GEOM_Function) aFunction = subShape->AddFunction(GEOM_Object::GetSubShapeID(), 1);
411 if (aFunction.IsNull())
414 subShape->SetName(name.c_str());
415 subShape->SetType(shapeType);
417 GEOM_ISubShape aSSI(aFunction);
418 aSSI.SetMainShape(function);
419 aSSI.SetIndices(anArray);
421 //aFunction->SetValue(aValue);
422 subShapeList->Append(subShape);
424 // Put this subshape in the list of sub-shapes of theMainShape
425 function->AddSubShapeReference(aFunction);
429 //=============================================================================
431 * Import a shape from XAO format
432 * \param fileName The name of the file to import
433 * \param shape The imported shape
434 * \param subShapes The list of imported groups
435 * \param groups The list of imported groups
436 * \param fields The list of imported fields
437 * \return boolean indicating if import was succeful.
439 //=============================================================================
440 bool XAOPlugin_IOperations::ImportXAO( const char* fileName,
441 Handle(GEOM_Object)& shape,
442 Handle(TColStd_HSequenceOfTransient)& subShapes,
443 Handle(TColStd_HSequenceOfTransient)& groups,
444 Handle(TColStd_HSequenceOfTransient)& fields )
448 if (fileName == NULL || groups.IsNull() || fields.IsNull())
452 XAO::Xao* xaoObject = new XAO::Xao();
455 xaoObject->importXAO(fileName);
457 catch (XAO::XAO_Exception& exc)
460 SetErrorCode(exc.what());
464 XAO::Geometry* xaoGeometry = xaoObject->getGeometry();
465 if (xaoGeometry == NULL)
468 SetErrorCode("Cannot import XAO: geometry format not supported.");
473 shape = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
474 Handle(GEOM_Function) function = shape->AddFunction(XAOPlugin_Driver::GetID(), IMPORT_SHAPE);
475 if (function.IsNull()) return false;
476 if (function->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
478 function->SetString( XAOPlugin_Driver::GetFileNameTag(), fileName );
481 if (xaoGeometry->getFormat() == XAO::BREP)
483 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)xaoGeometry;
484 TopoDS_Shape geomShape = brep->getTopoDS_Shape();
485 function->SetValue(geomShape);
486 shape->SetName(xaoGeometry->getName().c_str());
491 SetErrorCode("Cannot import XAO: geometry format not supported.");
495 // create sub shapes with names
496 importSubShapes(xaoGeometry, function, GEOM_POINT, XAO::VERTEX, subShapes);
497 importSubShapes(xaoGeometry, function, GEOM_EDGE, XAO::EDGE, subShapes);
498 importSubShapes(xaoGeometry, function, GEOM_FACE, XAO::FACE, subShapes);
499 importSubShapes(xaoGeometry, function, GEOM_SOLID, XAO::SOLID, subShapes);
502 int nbGroups = xaoObject->countGroups();
503 for (int i = 0; i < nbGroups; ++i)
505 XAO::Group* xaoGroup = xaoObject->getGroup(i);
507 // build an array with the indexes of the sub shapes
508 int nbElt = xaoGroup->count();
509 Handle(TColStd_HArray1OfInteger) array = new TColStd_HArray1OfInteger(1, nbElt);
511 for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it)
514 std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
515 array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
518 // create the group with the array of sub shapes indexes
519 Handle(GEOM_Object) group = GetEngine()->AddSubShape(shape, array);
520 group->SetType(GEOM_GROUP);
521 group->SetName(xaoGroup->getName().c_str());
523 // Set a sub-shape type
524 TDF_Label freeLabel = group->GetFreeLabel();
525 TDataStd_Integer::Set(freeLabel, (Standard_Integer) getGroupDimension(xaoGroup));
526 groups->Append(group);
528 function = group->GetLastFunction();
532 int nbFields = xaoObject->countFields();
533 for (int i = 0; i < nbFields; ++i)
535 XAO::Field* xaoField = xaoObject->getField(i);
537 Handle(TColStd_HArray1OfExtendedString) components = new TColStd_HArray1OfExtendedString(0, xaoField->countComponents()-1);
538 for (int j = 0; j < xaoField->countComponents(); ++j)
540 components->SetValue(j, (TCollection_ExtendedString)xaoField->getComponentName(j).c_str());
543 Handle(GEOM_Field) field = myFieldOperations->CreateField(shape,
544 xaoField->getName().c_str(),
545 (int)xaoField->getType(),
546 (int)xaoField->getDimension(),
549 switch (xaoField->getType())
553 XAO::BooleanField* bfield = (XAO::BooleanField*)xaoField;
554 for (int j = 0; j < xaoField->countSteps(); ++j)
556 XAO::BooleanStep* bstep = bfield->getStep(j);
557 Handle(GEOM_FieldStep) step = field->AddStep(bstep->getStep(), bstep->getStamp());
559 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, bstep->countValues()-1);
560 std::vector<bool> bvalues = bstep->getValues();
561 for (int k = 0; k < bstep->countValues(); ++k)
563 values->SetValue(k, bvalues[k] ? 1 : 0);
565 step->SetValues(values);
571 XAO::IntegerField* ifield = (XAO::IntegerField*)xaoField;
572 for (int j = 0; j < xaoField->countSteps(); ++j)
574 XAO::IntegerStep* istep = ifield->getStep(j);
575 Handle(GEOM_FieldStep) step = field->AddStep(istep->getStep(), istep->getStamp());
577 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, istep->countValues()-1);
578 std::vector<int> ivalues = istep->getValues();
579 for (int k = 0; k < istep->countValues(); ++k)
581 values->SetValue(k, ivalues[k]);
583 step->SetValues(values);
589 XAO::DoubleField* dfield = (XAO::DoubleField*)xaoField;
590 for (int j = 0; j < xaoField->countSteps(); ++j)
592 XAO::DoubleStep* dstep = dfield->getStep(j);
593 Handle(GEOM_FieldStep) step = field->AddStep(dstep->getStep(), dstep->getStamp());
595 Handle(TColStd_HArray1OfReal) values = new TColStd_HArray1OfReal(0, dstep->countValues()-1);
596 std::vector<double> dvalues = dstep->getValues();
597 for (int k = 0; k < dstep->countValues(); ++k)
599 values->SetValue(k, dvalues[k]);
601 step->SetValues(values);
607 XAO::StringField* sfield = (XAO::StringField*)xaoField;
608 for (int j = 0; j < xaoField->countSteps(); ++j)
610 XAO::StringStep* sstep = sfield->getStep(j);
611 Handle(GEOM_FieldStep) step = field->AddStep(sstep->getStep(), sstep->getStamp());
613 Handle(TColStd_HArray1OfExtendedString) values = new TColStd_HArray1OfExtendedString(0, sstep->countValues()-1);
614 std::vector<std::string> svalues = sstep->getValues();
615 for (int k = 0; k < sstep->countValues(); ++k)
617 values->SetValue(k, TCollection_ExtendedString(svalues[k].c_str()));
619 step->SetValues(values);
625 fields->Append(field);
628 // make a Python command
629 GEOM::TPythonDump pd(function);
630 pd << "(imported, " << shape << ", ";
632 // list of sub shapes
634 int nbSubshapes = subShapes->Length();
637 for (int i = 1; i <= nbSubshapes; i++)
639 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(subShapes->Value(i));
640 pd << obj << ((i < nbSubshapes) ? ", " : "");
648 for (int i = 1; i <= nbGroups; i++)
650 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(groups->Value(i));
651 pd << obj << ((i < nbGroups) ? ", " : "");
660 for (int i = 1; i <= nbFields; i++)
662 Handle(GEOM_Field) obj = Handle(GEOM_Field)::DownCast(fields->Value(i));
663 pd << obj << ((i < nbFields) ? ", " : "");
667 pd << ") = geompy.ImportXAO(\"" << fileName << "\")";