1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_Translation.cpp
4 // Created: 8 June 2015
5 // Author: Dmitry Bobylev
7 #include <FeaturesPlugin_Translation.h>
9 #include <ModelAPI_AttributeDouble.h>
10 #include <ModelAPI_AttributeSelectionList.h>
11 #include <ModelAPI_BodyBuilder.h>
12 #include <ModelAPI_ResultBody.h>
13 #include <ModelAPI_ResultPart.h>
14 #include <ModelAPI_Session.h>
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Lin.h>
19 //=================================================================================================
20 FeaturesPlugin_Translation::FeaturesPlugin_Translation()
24 //=================================================================================================
25 void FeaturesPlugin_Translation::initAttributes()
27 AttributeSelectionListPtr aSelection =
28 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
29 FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
31 data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(),
32 ModelAPI_AttributeSelection::typeId());
33 data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(),
34 ModelAPI_AttributeDouble::typeId());
37 //=================================================================================================
38 void FeaturesPlugin_Translation::execute()
41 ListOfShape anObjects;
42 std::list<ResultPtr> aContextes;
43 AttributeSelectionListPtr anObjectsSelList =
44 selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
45 if (anObjectsSelList->size() == 0) {
48 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
49 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
50 anObjectsSelList->value(anObjectsIndex);
51 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
52 if(!anObject.get()) { // may be for not-activated parts
56 anObjects.push_back(anObject);
57 aContextes.push_back(anObjectAttr->context());
61 std::shared_ptr<GeomAPI_Ax1> anAxis;
62 std::shared_ptr<GeomAPI_Edge> anEdge;
63 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
64 selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID());
65 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
66 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
67 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
68 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
69 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
72 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(),
73 anEdge->line()->direction()));
77 double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
79 // Moving each object.
81 std::list<ResultPtr>::iterator aContext = aContextes.begin();
82 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
83 anObjectsIt++, aContext++) {
84 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
85 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
89 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
90 aTrsf->setTranslation(anAxis, aDistance);
91 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
92 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
93 aResultPart->setTrsf(*aContext, aTrsf);
94 setResult(aResultPart, aResultIndex);
96 GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, anAxis, aDistance);
98 // Checking that the algorithm worked properly.
99 if(!aTranslationAlgo.isDone()) {
100 static const std::string aFeatureError = "Error: Translation algorithm failed.";
101 setError(aFeatureError);
104 if(aTranslationAlgo.shape()->isNull()) {
105 static const std::string aShapeError = "Error: Resulting shape is Null.";
106 setError(aShapeError);
109 if(!aTranslationAlgo.isValid()) {
110 std::string aFeatureError = "Error: Resulting shape is not valid.";
111 setError(aFeatureError);
115 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
116 loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
117 setResult(aResultBody, aResultIndex);
122 // Remove the rest results if there were produced in the previous pass.
123 removeResults(aResultIndex);
126 void FeaturesPlugin_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo,
127 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
128 std::shared_ptr<GeomAPI_Shape> theBaseShape)
131 theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape());
133 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTranslationAlgo.mapOfSubShapes();
135 int aTranslatedTag = 1;
136 std::string aTranslatedName = "Translated";
138 switch(theBaseShape->shapeType()) {
139 case GeomAPI_Shape::COMPOUND:
140 case GeomAPI_Shape::COMPSOLID:
141 case GeomAPI_Shape::SOLID:
142 case GeomAPI_Shape::SHELL:
143 theResultBody->loadAndOrientModifiedShapes(&theTranslationAlgo,
144 theBaseShape, GeomAPI_Shape::FACE,
145 aTranslatedTag, aTranslatedName + "_Face", *aSubShapes.get());
146 case GeomAPI_Shape::FACE:
147 case GeomAPI_Shape::WIRE:
148 theResultBody->loadAndOrientModifiedShapes(&theTranslationAlgo,
149 theBaseShape, GeomAPI_Shape::EDGE,
150 ++aTranslatedTag, aTranslatedName + "_Edge", *aSubShapes.get());
151 case GeomAPI_Shape::EDGE:
152 theResultBody->loadAndOrientModifiedShapes(&theTranslationAlgo,
153 theBaseShape, GeomAPI_Shape::VERTEX,
154 ++aTranslatedTag, aTranslatedName + "_Vertex", *aSubShapes.get());