1 // Copyright (C) 2014-2016 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>
36 #include <GEOM_Object.hxx>
37 #include <GEOM_Field.hxx>
39 #include <XAO_Geometry.hxx>
40 #include <XAO_BrepGeometry.hxx>
41 #include <XAO_Xao.hxx>
42 #include <XAO_Group.hxx>
43 #include <XAO_Field.hxx>
44 #include <XAO_BooleanField.hxx>
45 #include <XAO_IntegerField.hxx>
46 #include <XAO_DoubleField.hxx>
47 #include <XAO_StringField.hxx>
48 #include <XAO_DoubleStep.hxx>
49 #include <XAO_StringStep.hxx>
51 #include <Standard_ErrorHandler.hxx> // CAREFUL ! position of this file is critic : see Lucien PIGNOLONI / OCC
54 #include <TColStd_HArray1OfByte.hxx>
55 #include <TColStd_HArray1OfReal.hxx>
56 #include <TDataStd_Integer.hxx>
59 XAO::Dimension shapeEnumToDimension(const TopAbs_ShapeEnum& shape)
64 dim = XAO::VERTEX; break;
66 dim = XAO::EDGE; break;
68 dim = XAO::FACE; break;
70 dim = XAO::SOLID; break;
72 throw SALOME_Exception("Bad type"); // TODO
77 TopAbs_ShapeEnum getGroupDimension(XAO::Group* group)
79 XAO::Dimension dim = group->getDimension();
80 TopAbs_ShapeEnum rdim;
84 rdim = TopAbs_VERTEX; break;
86 rdim = TopAbs_EDGE; break;
88 rdim = TopAbs_FACE; break;
90 rdim = TopAbs_SOLID; break;
92 rdim = TopAbs_COMPOUND; break;
97 //=============================================================================
101 //=============================================================================
102 XAOPlugin_IOperations::XAOPlugin_IOperations( GEOM_Engine* theEngine, int theDocID )
103 : GEOMImpl_IBaseIEOperations( theEngine, theDocID )
105 MESSAGE( "XAOPlugin_IOperations::XAOPlugin_IOperations" );
108 //=============================================================================
112 //=============================================================================
113 XAOPlugin_IOperations::~XAOPlugin_IOperations()
115 MESSAGE( "XAOPlugin_IOperations::~XAOPlugin_IOperations" );
118 bool XAOPlugin_IOperations::exportGroups( std::list<Handle(GEOM_Object)> groupList,
120 XAO::BrepGeometry* geometry )
123 std::list<Handle(GEOM_Object)>::iterator groupIterator = groupList.begin();
124 while (groupIterator != groupList.end())
126 Handle(GEOM_Object) currGroup = (*groupIterator++);
127 if (currGroup->GetType() != GEOM_GROUP) {
128 SetErrorCode("Error when export groups: you could perform this operation only with group.");
131 Handle(TColStd_HArray1OfInteger) groupIds = myGroupOperations->GetObjects(currGroup);
133 TopAbs_ShapeEnum shapeGroup = myGroupOperations->GetType(currGroup);
134 XAO::Dimension dim = shapeEnumToDimension(shapeGroup);
135 XAO::Group* group = xaoObject->addGroup(dim, currGroup->GetName().ToCString());
140 for (int i = 1; i <= groupIds->Length(); i++)
142 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
143 int index = geometry->getVertexIndexByReference(ref);
148 for (int i = 1; i <= groupIds->Length(); i++)
150 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
151 int index = geometry->getEdgeIndexByReference(ref);
156 for (int i = 1; i <= groupIds->Length(); i++)
158 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
159 int index = geometry->getFaceIndexByReference(ref);
164 for (int i = 1; i <= groupIds->Length(); i++)
166 std::string ref = XAO::XaoUtils::intToString(groupIds->Value(i));
167 int index = geometry->getSolidIndexByReference(ref);
176 void XAOPlugin_IOperations::exportFields( std::list<Handle(GEOM_Field)> fieldList,
178 XAO::BrepGeometry* geometry )
180 std::list<Handle(GEOM_Field)>::iterator fieldIterator = fieldList.begin();
181 while (fieldIterator != fieldList.end())
183 Handle(GEOM_Field) currField = (*fieldIterator++);
185 int fdim = currField->GetDimension();
186 int ftype = currField->GetDataType();
187 int nbComponents = currField->GetNbComponents();
188 std::string name = currField->GetName().ToCString();
190 XAO::Field* field = xaoObject->addField((XAO::Type)ftype, (XAO::Dimension)fdim, nbComponents, name);
192 Handle(TColStd_HArray1OfExtendedString) components = currField->GetComponents();
193 for (int i = components->Lower(), j = 0; i <= components->Upper(); ++i, ++j)
195 field->setComponentName(j, TCollection_AsciiString(components->Value(i)).ToCString());
198 std::list< Handle(GEOM_FieldStep)> steps = currField->GetSteps();
199 std::list<Handle(GEOM_FieldStep)>::iterator stepIterator = steps.begin();
200 while (stepIterator != steps.end())
202 Handle(GEOM_FieldStep) currStep = (*stepIterator++);
204 XAO::Step* step = field->addNewStep(currStep->GetID());
205 step->setStamp(currStep->GetStamp());
211 XAO::BooleanStep* bs = (XAO::BooleanStep*)step;
212 Handle(TColStd_HArray1OfInteger) bvalues = currStep->GetIntValues();
213 std::vector<bool> bv;
214 bv.reserve(bvalues->Upper());
215 for ( int i = bvalues->Lower(), nb = bvalues->Upper(); i <= nb; ++i )
217 bv.push_back(bvalues->Value(i) != 0);
224 XAO::IntegerStep* is = (XAO::IntegerStep*)step;
225 Handle(TColStd_HArray1OfInteger) ivalues = currStep->GetIntValues();
227 iv.reserve(ivalues->Upper());
228 for ( int i = ivalues->Lower(), nb = ivalues->Upper(); i <= nb; ++i )
230 iv.push_back(ivalues->Value(i));
237 XAO::DoubleStep* ds = (XAO::DoubleStep*)step;
238 Handle(TColStd_HArray1OfReal) dvalues = currStep->GetDoubleValues();
239 std::vector<double> dv;
240 dv.reserve(dvalues->Upper());
241 for ( int i = dvalues->Lower(), nb = dvalues->Upper(); i <= nb; ++i )
243 dv.push_back(dvalues->Value(i));
250 XAO::StringStep* ss = (XAO::StringStep*)step;
251 Handle(TColStd_HArray1OfExtendedString) svalues = currStep->GetStringValues();
252 std::vector<std::string> sv;
253 sv.reserve(svalues->Upper());
254 for ( int i = svalues->Lower(), nb = svalues->Upper(); i <= nb; ++i )
256 sv.push_back(TCollection_AsciiString(svalues->Value(i)).ToCString());
266 void XAOPlugin_IOperations::exportSubshapes( const Handle(GEOM_Object)& shape, XAO::BrepGeometry* geometry )
268 Handle(TColStd_HSequenceOfTransient) subObjects = myShapesOperations->GetExistingSubObjects( shape, GEOMImpl_IShapesOperations::SubShapes );
269 int nbSubObjects = subObjects->Length();
270 // set the names of the sub shapes
271 for (int i = 1; i <= nbSubObjects; i++)
273 Handle(Standard_Transient) transientSubObject = subObjects->Value(i);
274 if (transientSubObject.IsNull())
277 Handle(GEOM_Object) subObject = Handle(GEOM_Object)::DownCast( transientSubObject );
278 if (subObject->GetType() != GEOM_GROUP)
280 int subIndex = myShapesOperations->GetSubShapeIndex( shape, subObject );
281 switch (subObject->GetValue().ShapeType())
284 geometry->changeVertexName(subIndex, subObject->GetName().ToCString());
287 geometry->changeEdgeName(subIndex, subObject->GetName().ToCString());
290 geometry->changeFaceName(subIndex, subObject->GetName().ToCString());
293 geometry->changeSolidName(subIndex, subObject->GetName().ToCString());
300 //=============================================================================
302 * Export a shape to XAO format
303 * \param shape The shape to export
304 * \param groups The list of groups to export
305 * \param fields The list of fields to export
306 * \param fileName The name of the file to exported
307 * \return boolean indicating if export was succeful.
309 //=============================================================================
310 bool XAOPlugin_IOperations::ExportXAO( Handle(GEOM_Object) shape,
311 std::list<Handle(GEOM_Object)> groupList,
312 std::list<Handle(GEOM_Field)> fieldList,
314 const char* fileName,
315 const char* shapeFileName )
319 if (shape.IsNull()) return false;
321 // add a new shape function with parameters
322 Handle(GEOM_Function) lastFunction = shape->GetLastFunction();
323 if (lastFunction.IsNull()) return false;
325 // add a new result object
326 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
328 // add an Export function
329 Handle(GEOM_Function) exportFunction = result->AddFunction(XAOPlugin_Driver::GetID(), EXPORT_SHAPE);
330 if (exportFunction.IsNull()) return false;
331 if (exportFunction->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
333 // create the XAO object
334 XAO::Xao* xaoObject = new XAO::Xao();
335 xaoObject->setAuthor(author);
338 XAO::BrepGeometry* geometry = (XAO::BrepGeometry*)XAO::Geometry::createGeometry(XAO::BREP);
339 TopoDS_Shape topoShape = shape->GetValue();
340 exportFunction->SetValue(topoShape);
341 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)geometry;
342 brep->setTopoDS_Shape(topoShape);
344 geometry->setName(shape->GetName().ToCString());
345 exportSubshapes(shape, geometry);
346 xaoObject->setGeometry(geometry);
348 if (!exportGroups(groupList, xaoObject, geometry)) return false;
349 exportFields(fieldList, xaoObject, geometry);
351 // export the XAO to the file
352 xaoObject->exportXAO(fileName, shapeFileName);
354 // make a Python command
355 GEOM::TPythonDump pd(exportFunction);
356 pd << "exported = geompy.ExportXAO(" << shape;
360 if (groupList.size() > 0)
362 std::list<Handle(GEOM_Object)>::iterator itGroup = groupList.begin();
364 while (itGroup != groupList.end())
366 pd << ", " << (*itGroup++);
372 if (fieldList.size() > 0)
374 std::list<Handle(GEOM_Field)>::iterator itField = fieldList.begin();
376 while (itField != fieldList.end())
378 pd << ", " << (*itField++);
382 pd << "\"" << author << "\", \"" << fileName << "\", \"" << shapeFileName << "\")";
390 void XAOPlugin_IOperations::importSubShapes( XAO::Geometry* xaoGeometry,
391 Handle(GEOM_Function) function, int shapeType, int dim,
392 Handle(TColStd_HSequenceOfTransient)& subShapeList )
394 Handle(GEOM_Object) subShape;
395 Handle(GEOM_Function) aFunction;
396 Handle(TColStd_HArray1OfInteger) anArray;
398 XAO::GeometricElementList::iterator elementIterator = xaoGeometry->begin((XAO::Dimension)dim);
399 for (; elementIterator != xaoGeometry->end((XAO::Dimension)dim); elementIterator++)
401 XAO::GeometricElement element = elementIterator->second;
402 if (!element.hasName())
405 std::string name = element.getName();
406 std::string ref = element.getReference();
407 int iref = XAO::XaoUtils::stringToInt(ref);
409 anArray = new TColStd_HArray1OfInteger(1, 1);
410 anArray->SetValue(1, iref);
412 subShape = GetEngine()->AddObject(GetDocID(), GEOM_SUBSHAPE);
413 Handle(GEOM_Function) aFunction = subShape->AddFunction(GEOM_Object::GetSubShapeID(), 1);
414 if (aFunction.IsNull())
417 subShape->SetName(name.c_str());
418 subShape->SetType(shapeType);
420 GEOM_ISubShape aSSI(aFunction);
421 aSSI.SetMainShape(function);
422 aSSI.SetIndices(anArray);
424 //aFunction->SetValue(aValue);
425 subShapeList->Append(subShape);
427 // Put this subshape in the list of sub-shapes of theMainShape
428 function->AddSubShapeReference(aFunction);
432 //=============================================================================
434 * Import a shape from XAO format
435 * \param fileName The name of the file to import
436 * \param shape The imported shape
437 * \param subShapes The list of imported groups
438 * \param groups The list of imported groups
439 * \param fields The list of imported fields
440 * \return boolean indicating if import was succeful.
442 //=============================================================================
443 bool XAOPlugin_IOperations::ImportXAO( const char* fileName,
444 Handle(GEOM_Object)& shape,
445 Handle(TColStd_HSequenceOfTransient)& subShapes,
446 Handle(TColStd_HSequenceOfTransient)& groups,
447 Handle(TColStd_HSequenceOfTransient)& fields )
451 if (fileName == NULL || groups.IsNull() || fields.IsNull())
455 XAO::Xao* xaoObject = new XAO::Xao();
458 xaoObject->importXAO(fileName);
460 catch (XAO::XAO_Exception& exc)
463 SetErrorCode(exc.what());
467 XAO::Geometry* xaoGeometry = xaoObject->getGeometry();
468 if (xaoGeometry == NULL)
471 SetErrorCode("Cannot import XAO: geometry format not supported.");
476 shape = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
477 Handle(GEOM_Function) function = shape->AddFunction(XAOPlugin_Driver::GetID(), IMPORT_SHAPE);
478 if (function.IsNull()) return false;
479 if (function->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
481 function->SetString( XAOPlugin_Driver::GetFileNameTag(), fileName );
484 if (xaoGeometry->getFormat() == XAO::BREP)
486 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)xaoGeometry;
487 TopoDS_Shape geomShape = brep->getTopoDS_Shape();
488 function->SetValue(geomShape);
489 shape->SetName(xaoGeometry->getName().c_str());
494 SetErrorCode("Cannot import XAO: geometry format not supported.");
498 // create sub shapes with names
499 importSubShapes(xaoGeometry, function, GEOM_POINT, XAO::VERTEX, subShapes);
500 importSubShapes(xaoGeometry, function, GEOM_EDGE, XAO::EDGE, subShapes);
501 importSubShapes(xaoGeometry, function, GEOM_FACE, XAO::FACE, subShapes);
502 importSubShapes(xaoGeometry, function, GEOM_SOLID, XAO::SOLID, subShapes);
505 int nbGroups = xaoObject->countGroups();
506 for (int i = 0; i < nbGroups; ++i)
508 XAO::Group* xaoGroup = xaoObject->getGroup(i);
510 // build an array with the indexes of the sub shapes
511 int nbElt = xaoGroup->count();
512 Handle(TColStd_HArray1OfInteger) array = new TColStd_HArray1OfInteger(1, nbElt);
514 for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it)
517 std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
518 array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
521 // create the group with the array of sub shapes indexes
522 Handle(GEOM_Object) group = GetEngine()->AddSubShape(shape, array);
523 group->SetType(GEOM_GROUP);
524 group->SetName(xaoGroup->getName().c_str());
526 // Set a sub-shape type
527 TDF_Label freeLabel = group->GetFreeLabel();
528 TDataStd_Integer::Set(freeLabel, (Standard_Integer) getGroupDimension(xaoGroup));
529 groups->Append(group);
531 function = group->GetLastFunction();
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) ? ", " : "");
670 pd << ") = geompy.ImportXAO(\"" << fileName << "\")";