1 // Copyright (C) 2014 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 void 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 Handle(TColStd_HArray1OfInteger) groupIds = myGroupOperations->GetObjects(currGroup);
127 TopAbs_ShapeEnum shapeGroup = myGroupOperations->GetType(currGroup);
128 XAO::Dimension dim = shapeEnumToDimension(shapeGroup);
129 XAO::Group* group = xaoObject->addGroup(dim, currGroup->GetName().ToCString());
134 for (int i = 1; i <= groupIds->Length(); i++)
136 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
137 int index = geometry->getVertexIndexByReference(ref);
142 for (int i = 1; i <= groupIds->Length(); i++)
144 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
145 int index = geometry->getEdgeIndexByReference(ref);
150 for (int i = 1; i <= groupIds->Length(); i++)
152 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
153 int index = geometry->getFaceIndexByReference(ref);
158 for (int i = 1; i <= groupIds->Length(); i++)
160 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
161 int index = geometry->getSolidIndexByReference(ref);
169 void XAOPlugin_IOperations::exportFields( std::list<Handle(GEOM_Field)> fieldList,
171 XAO::BrepGeometry* geometry )
173 std::list<Handle(GEOM_Field)>::iterator fieldIterator = fieldList.begin();
174 while (fieldIterator != fieldList.end())
176 Handle(GEOM_Field) currField = (*fieldIterator++);
178 int fdim = currField->GetDimension();
179 int ftype = currField->GetDataType();
180 int nbComponents = currField->GetNbComponents();
181 std::string name = currField->GetName().ToCString();
183 XAO::Field* field = xaoObject->addField((XAO::Type)ftype, (XAO::Dimension)fdim, nbComponents, name);
185 Handle(TColStd_HArray1OfExtendedString) components = currField->GetComponents();
186 for (int i = components->Lower(), j = 0; i <= components->Upper(); ++i, ++j)
188 field->setComponentName(j, TCollection_AsciiString(components->Value(i)).ToCString());
191 std::list< Handle(GEOM_FieldStep)> steps = currField->GetSteps();
192 std::list<Handle(GEOM_FieldStep)>::iterator stepIterator = steps.begin();
193 while (stepIterator != steps.end())
195 Handle(GEOM_FieldStep) currStep = (*stepIterator++);
197 XAO::Step* step = field->addNewStep(currStep->GetID());
198 step->setStamp(currStep->GetStamp());
204 XAO::BooleanStep* bs = (XAO::BooleanStep*)step;
205 Handle(TColStd_HArray1OfInteger) bvalues = currStep->GetIntValues();
206 std::vector<bool> bv;
207 bv.reserve(bvalues->Upper());
208 for ( int i = bvalues->Lower(), nb = bvalues->Upper(); i <= nb; ++i )
210 bv.push_back(bvalues->Value(i) != 0);
217 XAO::IntegerStep* is = (XAO::IntegerStep*)step;
218 Handle(TColStd_HArray1OfInteger) ivalues = currStep->GetIntValues();
220 iv.reserve(ivalues->Upper());
221 for ( int i = ivalues->Lower(), nb = ivalues->Upper(); i <= nb; ++i )
223 iv.push_back(ivalues->Value(i));
230 XAO::DoubleStep* ds = (XAO::DoubleStep*)step;
231 Handle(TColStd_HArray1OfReal) dvalues = currStep->GetDoubleValues();
232 std::vector<double> dv;
233 dv.reserve(dvalues->Upper());
234 for ( int i = dvalues->Lower(), nb = dvalues->Upper(); i <= nb; ++i )
236 dv.push_back(dvalues->Value(i));
243 XAO::StringStep* ss = (XAO::StringStep*)step;
244 Handle(TColStd_HArray1OfExtendedString) svalues = currStep->GetStringValues();
245 std::vector<std::string> sv;
246 sv.reserve(svalues->Upper());
247 for ( int i = svalues->Lower(), nb = svalues->Upper(); i <= nb; ++i )
249 sv.push_back(TCollection_AsciiString(svalues->Value(i)).ToCString());
259 void XAOPlugin_IOperations::exportSubshapes( const Handle(GEOM_Object)& shape, XAO::BrepGeometry* geometry )
261 Handle(TColStd_HSequenceOfTransient) subObjects = myShapesOperations->GetExistingSubObjects( shape, false );
262 int nbSubObjects = subObjects->Length();
263 // set the names of the sub shapes
264 for (int i = 1; i <= nbSubObjects; i++)
266 Handle(Standard_Transient) transientSubObject = subObjects->Value(i);
267 if (transientSubObject.IsNull())
270 Handle(GEOM_Object) subObject = Handle(GEOM_Object)::DownCast( transientSubObject );
271 if (subObject->GetType() != GEOM_GROUP)
273 int subIndex = myShapesOperations->GetSubShapeIndex( shape, subObject );
274 switch (subObject->GetValue().ShapeType())
277 geometry->changeVertexName(subIndex, subObject->GetName().ToCString());
280 geometry->changeEdgeName(subIndex, subObject->GetName().ToCString());
283 geometry->changeFaceName(subIndex, subObject->GetName().ToCString());
286 geometry->changeSolidName(subIndex, subObject->GetName().ToCString());
293 //=============================================================================
295 * Export a shape to XAO format
296 * \param shape The shape to export
297 * \param groups The list of groups to export
298 * \param fields The list of fields to export
299 * \param fileName The name of the file to exported
300 * \return boolean indicating if export was succeful.
302 //=============================================================================
303 bool XAOPlugin_IOperations::ExportXAO( Handle(GEOM_Object) shape,
304 std::list<Handle(GEOM_Object)> groupList,
305 std::list<Handle(GEOM_Field)> fieldList,
307 const char* fileName )
311 if (shape.IsNull()) return false;
313 // add a new shape function with parameters
314 Handle(GEOM_Function) lastFunction = shape->GetLastFunction();
315 if (lastFunction.IsNull()) return false;
317 // add a new result object
318 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
320 // add an Export function
321 Handle(GEOM_Function) exportFunction = result->AddFunction(XAOPlugin_Driver::GetID(), EXPORT_SHAPE);
322 if (exportFunction.IsNull()) return false;
323 if (exportFunction->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
325 // create the XAO object
326 XAO::Xao* xaoObject = new XAO::Xao();
327 xaoObject->setAuthor(author);
330 XAO::BrepGeometry* geometry = (XAO::BrepGeometry*)XAO::Geometry::createGeometry(XAO::BREP);
331 TopoDS_Shape topoShape = shape->GetValue();
332 exportFunction->SetValue(topoShape);
333 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)geometry;
334 brep->setTopoDS_Shape(topoShape);
336 geometry->setName(shape->GetName().ToCString());
337 exportSubshapes(shape, geometry);
338 xaoObject->setGeometry(geometry);
340 exportGroups(groupList, xaoObject, geometry);
341 exportFields(fieldList, xaoObject, geometry);
343 // export the XAO to the file
344 xaoObject->exportXAO(fileName);
346 // make a Python command
347 GEOM::TPythonDump pd(exportFunction);
348 pd << "exported = geompy.ExportXAO(" << shape;
352 if (groupList.size() > 0)
354 std::list<Handle(GEOM_Object)>::iterator itGroup = groupList.begin();
356 while (itGroup != groupList.end())
358 pd << ", " << (*itGroup++);
364 if (fieldList.size() > 0)
366 std::list<Handle(GEOM_Field)>::iterator itField = fieldList.begin();
368 while (itField != fieldList.end())
370 pd << ", " << (*itField++);
374 pd << "\"" << author << "\", \"" << fileName << "\")";
382 void XAOPlugin_IOperations::importSubShapes( XAO::Geometry* xaoGeometry,
383 Handle(GEOM_Function) function, int shapeType, int dim,
384 Handle(TColStd_HSequenceOfTransient)& subShapeList )
386 Handle(GEOM_Object) subShape;
387 Handle(GEOM_Function) aFunction;
388 Handle(TColStd_HArray1OfInteger) anArray;
390 XAO::GeometricElementList::iterator elementIterator = xaoGeometry->begin((XAO::Dimension)dim);
391 for (; elementIterator != xaoGeometry->end((XAO::Dimension)dim); elementIterator++)
393 XAO::GeometricElement element = elementIterator->second;
394 if (!element.hasName())
397 std::string name = element.getName();
398 std::string ref = element.getReference();
399 int iref = XAO::XaoUtils::stringToInt(ref);
401 anArray = new TColStd_HArray1OfInteger(1, 1);
402 anArray->SetValue(1, iref);
404 subShape = GetEngine()->AddObject(GetDocID(), GEOM_SUBSHAPE);
405 Handle(GEOM_Function) aFunction = subShape->AddFunction(GEOM_Object::GetSubShapeID(), 1);
406 if (aFunction.IsNull())
409 subShape->SetName(name.c_str());
410 subShape->SetType(shapeType);
412 GEOM_ISubShape aSSI(aFunction);
413 aSSI.SetMainShape(function);
414 aSSI.SetIndices(anArray);
416 //aFunction->SetValue(aValue);
417 subShapeList->Append(subShape);
419 // Put this subshape in the list of sub-shapes of theMainShape
420 function->AddSubShapeReference(aFunction);
424 //=============================================================================
426 * Import a shape from XAO format
427 * \param fileName The name of the file to import
428 * \param shape The imported shape
429 * \param subShapes The list of imported groups
430 * \param groups The list of imported groups
431 * \param fields The list of imported fields
432 * \return boolean indicating if import was succeful.
434 //=============================================================================
435 bool XAOPlugin_IOperations::ImportXAO( const char* fileName,
436 Handle(GEOM_Object)& shape,
437 Handle(TColStd_HSequenceOfTransient)& subShapes,
438 Handle(TColStd_HSequenceOfTransient)& groups,
439 Handle(TColStd_HSequenceOfTransient)& fields )
443 if (fileName == NULL || groups.IsNull() || fields.IsNull())
447 XAO::Xao* xaoObject = new XAO::Xao();
450 xaoObject->importXAO(fileName);
452 catch (XAO::XAO_Exception& exc)
455 SetErrorCode(exc.what());
459 XAO::Geometry* xaoGeometry = xaoObject->getGeometry();
460 if (xaoGeometry == NULL)
463 SetErrorCode("Cannot import XAO: geometry format not supported.");
468 shape = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
469 Handle(GEOM_Function) function = shape->AddFunction(XAOPlugin_Driver::GetID(), IMPORT_SHAPE);
470 if (function.IsNull()) return false;
471 if (function->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
474 if (xaoGeometry->getFormat() == XAO::BREP)
476 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)xaoGeometry;
477 TopoDS_Shape geomShape = brep->getTopoDS_Shape();
478 function->SetValue(geomShape);
479 shape->SetName(xaoGeometry->getName().c_str());
484 SetErrorCode("Cannot import XAO: geometry format not supported.");
488 // create sub shapes with names
489 importSubShapes(xaoGeometry, function, GEOM_POINT, XAO::VERTEX, subShapes);
490 importSubShapes(xaoGeometry, function, GEOM_EDGE, XAO::EDGE, subShapes);
491 importSubShapes(xaoGeometry, function, GEOM_FACE, XAO::FACE, subShapes);
492 importSubShapes(xaoGeometry, function, GEOM_SOLID, XAO::SOLID, subShapes);
495 int nbGroups = xaoObject->countGroups();
496 for (int i = 0; i < nbGroups; ++i)
498 XAO::Group* xaoGroup = xaoObject->getGroup(i);
500 // build an array with the indexes of the sub shapes
501 int nbElt = xaoGroup->count();
502 Handle(TColStd_HArray1OfInteger) array = new TColStd_HArray1OfInteger(1, nbElt);
504 for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it)
507 std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
508 array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
511 // create the group with the array of sub shapes indexes
512 Handle(GEOM_Object) group = GetEngine()->AddSubShape(shape, array);
513 group->SetType(GEOM_GROUP);
514 group->SetName(xaoGroup->getName().c_str());
516 // Set a sub-shape type
517 TDF_Label freeLabel = group->GetFreeLabel();
518 TDataStd_Integer::Set(freeLabel, (Standard_Integer) getGroupDimension(xaoGroup));
519 groups->Append(group);
521 function = group->GetLastFunction();
525 int nbFields = xaoObject->countFields();
526 for (int i = 0; i < nbFields; ++i)
528 XAO::Field* xaoField = xaoObject->getField(i);
530 Handle(TColStd_HArray1OfExtendedString) components = new TColStd_HArray1OfExtendedString(0, xaoField->countComponents()-1);
531 for (int j = 0; j < xaoField->countComponents(); ++j)
533 components->SetValue(j, (TCollection_ExtendedString)xaoField->getComponentName(j).c_str());
536 Handle(GEOM_Field) field = myFieldOperations->CreateField(shape,
537 xaoField->getName().c_str(),
538 (int)xaoField->getType(),
539 (int)xaoField->getDimension(),
542 switch (xaoField->getType())
546 XAO::BooleanField* bfield = (XAO::BooleanField*)xaoField;
547 for (int j = 0; j < xaoField->countSteps(); ++j)
549 XAO::BooleanStep* bstep = bfield->getStep(j);
550 Handle(GEOM_FieldStep) step = field->AddStep(bstep->getStep(), bstep->getStamp());
552 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, bstep->countValues()-1);
553 std::vector<bool> bvalues = bstep->getValues();
554 for (int k = 0; k < bstep->countValues(); ++k)
556 values->SetValue(k, bvalues[k] ? 1 : 0);
558 step->SetValues(values);
564 XAO::IntegerField* ifield = (XAO::IntegerField*)xaoField;
565 for (int j = 0; j < xaoField->countSteps(); ++j)
567 XAO::IntegerStep* istep = ifield->getStep(j);
568 Handle(GEOM_FieldStep) step = field->AddStep(istep->getStep(), istep->getStamp());
570 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, istep->countValues()-1);
571 std::vector<int> ivalues = istep->getValues();
572 for (int k = 0; k < istep->countValues(); ++k)
574 values->SetValue(k, ivalues[k]);
576 step->SetValues(values);
582 XAO::DoubleField* dfield = (XAO::DoubleField*)xaoField;
583 for (int j = 0; j < xaoField->countSteps(); ++j)
585 XAO::DoubleStep* dstep = dfield->getStep(j);
586 Handle(GEOM_FieldStep) step = field->AddStep(dstep->getStep(), dstep->getStamp());
588 Handle(TColStd_HArray1OfReal) values = new TColStd_HArray1OfReal(0, dstep->countValues()-1);
589 std::vector<double> dvalues = dstep->getValues();
590 for (int k = 0; k < dstep->countValues(); ++k)
592 values->SetValue(k, dvalues[k]);
594 step->SetValues(values);
600 XAO::StringField* sfield = (XAO::StringField*)xaoField;
601 for (int j = 0; j < xaoField->countSteps(); ++j)
603 XAO::StringStep* sstep = sfield->getStep(j);
604 Handle(GEOM_FieldStep) step = field->AddStep(sstep->getStep(), sstep->getStamp());
606 Handle(TColStd_HArray1OfExtendedString) values = new TColStd_HArray1OfExtendedString(0, sstep->countValues()-1);
607 std::vector<std::string> svalues = sstep->getValues();
608 for (int k = 0; k < sstep->countValues(); ++k)
610 values->SetValue(k, TCollection_ExtendedString(svalues[k].c_str()));
612 step->SetValues(values);
618 fields->Append(field);
621 // make a Python command
622 GEOM::TPythonDump pd(function);
623 pd << "(imported, " << shape << ", ";
625 // list of sub shapes
627 int nbSubshapes = subShapes->Length();
630 for (int i = 1; i <= nbSubshapes; i++)
632 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(subShapes->Value(i));
633 pd << obj << ((i < nbSubshapes) ? ", " : "");
641 for (int i = 1; i <= nbGroups; i++)
643 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(groups->Value(i));
644 pd << obj << ((i < nbGroups) ? ", " : "");
653 for (int i = 1; i <= nbFields; i++)
655 Handle(GEOM_Field) obj = Handle(GEOM_Field)::DownCast(fields->Value(i));
656 pd << obj << ((i < nbFields) ? ", " : "");
660 pd << ") = geompy.ImportXAO(\"" << fileName << "\")";