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 )
318 if (shape.IsNull()) return false;
320 // add a new shape function with parameters
321 Handle(GEOM_Function) lastFunction = shape->GetLastFunction();
322 if (lastFunction.IsNull()) return false;
324 // add a new result object
325 Handle(GEOM_Object) result = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
327 // add an Export function
328 Handle(GEOM_Function) exportFunction = result->AddFunction(XAOPlugin_Driver::GetID(), EXPORT_SHAPE);
329 if (exportFunction.IsNull()) return false;
330 if (exportFunction->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
332 // create the XAO object
333 XAO::Xao* xaoObject = new XAO::Xao();
334 xaoObject->setAuthor(author);
337 XAO::BrepGeometry* geometry = (XAO::BrepGeometry*)XAO::Geometry::createGeometry(XAO::BREP);
338 TopoDS_Shape topoShape = shape->GetValue();
339 exportFunction->SetValue(topoShape);
340 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)geometry;
341 brep->setTopoDS_Shape(topoShape);
343 geometry->setName(shape->GetName().ToCString());
344 exportSubshapes(shape, geometry);
345 xaoObject->setGeometry(geometry);
347 if (!exportGroups(groupList, xaoObject, geometry)) return false;
348 exportFields(fieldList, xaoObject, geometry);
350 // export the XAO to the file
351 xaoObject->exportXAO(fileName);
353 // make a Python command
354 GEOM::TPythonDump pd(exportFunction);
355 pd << "exported = geompy.ExportXAO(" << shape;
359 if (groupList.size() > 0)
361 std::list<Handle(GEOM_Object)>::iterator itGroup = groupList.begin();
363 while (itGroup != groupList.end())
365 pd << ", " << (*itGroup++);
371 if (fieldList.size() > 0)
373 std::list<Handle(GEOM_Field)>::iterator itField = fieldList.begin();
375 while (itField != fieldList.end())
377 pd << ", " << (*itField++);
381 pd << "\"" << author << "\", \"" << fileName << "\")";
389 void XAOPlugin_IOperations::importSubShapes( XAO::Geometry* xaoGeometry,
390 Handle(GEOM_Function) function, int shapeType, int dim,
391 Handle(TColStd_HSequenceOfTransient)& subShapeList )
393 Handle(GEOM_Object) subShape;
394 Handle(GEOM_Function) aFunction;
395 Handle(TColStd_HArray1OfInteger) anArray;
397 XAO::GeometricElementList::iterator elementIterator = xaoGeometry->begin((XAO::Dimension)dim);
398 for (; elementIterator != xaoGeometry->end((XAO::Dimension)dim); elementIterator++)
400 XAO::GeometricElement element = elementIterator->second;
401 if (!element.hasName())
404 std::string name = element.getName();
405 std::string ref = element.getReference();
406 int iref = XAO::XaoUtils::stringToInt(ref);
408 anArray = new TColStd_HArray1OfInteger(1, 1);
409 anArray->SetValue(1, iref);
411 subShape = GetEngine()->AddObject(GetDocID(), GEOM_SUBSHAPE);
412 Handle(GEOM_Function) aFunction = subShape->AddFunction(GEOM_Object::GetSubShapeID(), 1);
413 if (aFunction.IsNull())
416 subShape->SetName(name.c_str());
417 subShape->SetType(shapeType);
419 GEOM_ISubShape aSSI(aFunction);
420 aSSI.SetMainShape(function);
421 aSSI.SetIndices(anArray);
423 //aFunction->SetValue(aValue);
424 subShapeList->Append(subShape);
426 // Put this subshape in the list of sub-shapes of theMainShape
427 function->AddSubShapeReference(aFunction);
431 //=============================================================================
433 * Import a shape from XAO format
434 * \param fileName The name of the file to import
435 * \param shape The imported shape
436 * \param subShapes The list of imported groups
437 * \param groups The list of imported groups
438 * \param fields The list of imported fields
439 * \return boolean indicating if import was succeful.
441 //=============================================================================
442 bool XAOPlugin_IOperations::ImportXAO( const char* fileName,
443 Handle(GEOM_Object)& shape,
444 Handle(TColStd_HSequenceOfTransient)& subShapes,
445 Handle(TColStd_HSequenceOfTransient)& groups,
446 Handle(TColStd_HSequenceOfTransient)& fields )
450 if (fileName == NULL || groups.IsNull() || fields.IsNull())
454 XAO::Xao* xaoObject = new XAO::Xao();
457 xaoObject->importXAO(fileName);
459 catch (XAO::XAO_Exception& exc)
462 SetErrorCode(exc.what());
466 XAO::Geometry* xaoGeometry = xaoObject->getGeometry();
467 if (xaoGeometry == NULL)
470 SetErrorCode("Cannot import XAO: geometry format not supported.");
475 shape = GetEngine()->AddObject(GetDocID(), GEOM_IMPORT);
476 Handle(GEOM_Function) function = shape->AddFunction(XAOPlugin_Driver::GetID(), IMPORT_SHAPE);
477 if (function.IsNull()) return false;
478 if (function->GetDriverGUID() != XAOPlugin_Driver::GetID()) return false;
480 function->SetString( XAOPlugin_Driver::GetFileNameTag(), fileName );
483 if (xaoGeometry->getFormat() == XAO::BREP)
485 XAO::BrepGeometry* brep = (XAO::BrepGeometry*)xaoGeometry;
486 TopoDS_Shape geomShape = brep->getTopoDS_Shape();
487 function->SetValue(geomShape);
488 shape->SetName(xaoGeometry->getName().c_str());
493 SetErrorCode("Cannot import XAO: geometry format not supported.");
497 // create sub shapes with names
498 importSubShapes(xaoGeometry, function, GEOM_POINT, XAO::VERTEX, subShapes);
499 importSubShapes(xaoGeometry, function, GEOM_EDGE, XAO::EDGE, subShapes);
500 importSubShapes(xaoGeometry, function, GEOM_FACE, XAO::FACE, subShapes);
501 importSubShapes(xaoGeometry, function, GEOM_SOLID, XAO::SOLID, subShapes);
504 int nbGroups = xaoObject->countGroups();
505 for (int i = 0; i < nbGroups; ++i)
507 XAO::Group* xaoGroup = xaoObject->getGroup(i);
509 // build an array with the indexes of the sub shapes
510 int nbElt = xaoGroup->count();
511 Handle(TColStd_HArray1OfInteger) array = new TColStd_HArray1OfInteger(1, nbElt);
513 for (std::set<int>::iterator it = xaoGroup->begin(); it != xaoGroup->end(); ++it)
516 std::string ref = xaoGeometry->getElementReference(xaoGroup->getDimension(), index);
517 array->SetValue(++j, XAO::XaoUtils::stringToInt(ref));
520 // create the group with the array of sub shapes indexes
521 Handle(GEOM_Object) group = GetEngine()->AddSubShape(shape, array);
522 group->SetType(GEOM_GROUP);
523 group->SetName(xaoGroup->getName().c_str());
525 // Set a sub-shape type
526 TDF_Label freeLabel = group->GetFreeLabel();
527 TDataStd_Integer::Set(freeLabel, (Standard_Integer) getGroupDimension(xaoGroup));
528 groups->Append(group);
530 function = group->GetLastFunction();
534 int nbFields = xaoObject->countFields();
535 for (int i = 0; i < nbFields; ++i)
537 XAO::Field* xaoField = xaoObject->getField(i);
539 Handle(TColStd_HArray1OfExtendedString) components = new TColStd_HArray1OfExtendedString(0, xaoField->countComponents()-1);
540 for (int j = 0; j < xaoField->countComponents(); ++j)
542 components->SetValue(j, (TCollection_ExtendedString)xaoField->getComponentName(j).c_str());
545 Handle(GEOM_Field) field = myFieldOperations->CreateField(shape,
546 xaoField->getName().c_str(),
547 (int)xaoField->getType(),
548 (int)xaoField->getDimension(),
551 switch (xaoField->getType())
555 XAO::BooleanField* bfield = (XAO::BooleanField*)xaoField;
556 for (int j = 0; j < xaoField->countSteps(); ++j)
558 XAO::BooleanStep* bstep = bfield->getStep(j);
559 Handle(GEOM_FieldStep) step = field->AddStep(bstep->getStep(), bstep->getStamp());
561 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, bstep->countValues()-1);
562 std::vector<bool> bvalues = bstep->getValues();
563 for (int k = 0; k < bstep->countValues(); ++k)
565 values->SetValue(k, bvalues[k] ? 1 : 0);
567 step->SetValues(values);
573 XAO::IntegerField* ifield = (XAO::IntegerField*)xaoField;
574 for (int j = 0; j < xaoField->countSteps(); ++j)
576 XAO::IntegerStep* istep = ifield->getStep(j);
577 Handle(GEOM_FieldStep) step = field->AddStep(istep->getStep(), istep->getStamp());
579 Handle(TColStd_HArray1OfInteger) values = new TColStd_HArray1OfInteger(0, istep->countValues()-1);
580 std::vector<int> ivalues = istep->getValues();
581 for (int k = 0; k < istep->countValues(); ++k)
583 values->SetValue(k, ivalues[k]);
585 step->SetValues(values);
591 XAO::DoubleField* dfield = (XAO::DoubleField*)xaoField;
592 for (int j = 0; j < xaoField->countSteps(); ++j)
594 XAO::DoubleStep* dstep = dfield->getStep(j);
595 Handle(GEOM_FieldStep) step = field->AddStep(dstep->getStep(), dstep->getStamp());
597 Handle(TColStd_HArray1OfReal) values = new TColStd_HArray1OfReal(0, dstep->countValues()-1);
598 std::vector<double> dvalues = dstep->getValues();
599 for (int k = 0; k < dstep->countValues(); ++k)
601 values->SetValue(k, dvalues[k]);
603 step->SetValues(values);
609 XAO::StringField* sfield = (XAO::StringField*)xaoField;
610 for (int j = 0; j < xaoField->countSteps(); ++j)
612 XAO::StringStep* sstep = sfield->getStep(j);
613 Handle(GEOM_FieldStep) step = field->AddStep(sstep->getStep(), sstep->getStamp());
615 Handle(TColStd_HArray1OfExtendedString) values = new TColStd_HArray1OfExtendedString(0, sstep->countValues()-1);
616 std::vector<std::string> svalues = sstep->getValues();
617 for (int k = 0; k < sstep->countValues(); ++k)
619 values->SetValue(k, TCollection_ExtendedString(svalues[k].c_str()));
621 step->SetValues(values);
627 fields->Append(field);
630 // make a Python command
631 GEOM::TPythonDump pd(function);
632 pd << "(imported, " << shape << ", ";
634 // list of sub shapes
636 int nbSubshapes = subShapes->Length();
639 for (int i = 1; i <= nbSubshapes; i++)
641 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(subShapes->Value(i));
642 pd << obj << ((i < nbSubshapes) ? ", " : "");
650 for (int i = 1; i <= nbGroups; i++)
652 Handle(GEOM_Object) obj = Handle(GEOM_Object)::DownCast(groups->Value(i));
653 pd << obj << ((i < nbGroups) ? ", " : "");
662 for (int i = 1; i <= nbFields; i++)
664 Handle(GEOM_Field) obj = Handle(GEOM_Field)::DownCast(fields->Value(i));
665 pd << obj << ((i < nbFields) ? ", " : "");
669 pd << ") = geompy.ImportXAO(\"" << fileName << "\")";