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(), ModelAPI_AttributeSelection::typeId());
32 data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(), ModelAPI_AttributeDouble::typeId());
35 //=================================================================================================
36 void FeaturesPlugin_Translation::execute()
39 ListOfShape anObjects;
40 std::list<ResultPtr> aContextes;
41 AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID());
42 if (anObjectsSelList->size() == 0) {
45 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
46 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr = anObjectsSelList->value(anObjectsIndex);
47 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
48 if(!anObject.get()) { // may be for not-activated parts
52 anObjects.push_back(anObject);
53 aContextes.push_back(anObjectAttr->context());
57 std::shared_ptr<GeomAPI_Ax1> anAxis;
58 std::shared_ptr<GeomAPI_Edge> anEdge;
59 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef = selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID());
60 if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) {
61 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->value()));
62 } else if (anObjRef && !anObjRef->value() && anObjRef->context() &&
63 anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) {
64 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anObjRef->context()->shape()));
67 anAxis = std::shared_ptr<GeomAPI_Ax1>(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction()));
71 double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value();
73 // Moving each object.
75 std::list<ResultPtr>::iterator aContext = aContextes.begin();
76 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
77 anObjectsIt++, aContext++) {
78 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
79 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
83 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
84 aTrsf->setTranslation(anAxis, aDistance);
85 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
86 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
87 aResultPart->setTrsf(*aContext, aTrsf);
88 setResult(aResultPart, aResultIndex);
90 GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, anAxis, aDistance);
92 // Checking that the algorithm worked properly.
93 if(!aTranslationAlgo.isDone()) {
94 static const std::string aFeatureError = "Error: Translation algorithm failed.";
95 setError(aFeatureError);
98 if(aTranslationAlgo.shape()->isNull()) {
99 static const std::string aShapeError = "Error: Resulting shape is Null.";
100 setError(aShapeError);
103 if(!aTranslationAlgo.isValid()) {
104 std::string aFeatureError = "Error: Resulting shape is not valid.";
105 setError(aFeatureError);
109 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
110 loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape);
111 setResult(aResultBody, aResultIndex);
116 // Remove the rest results if there were produced in the previous pass.
117 removeResults(aResultIndex);
120 void FeaturesPlugin_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo,
121 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
122 std::shared_ptr<GeomAPI_Shape> theBaseShape)
125 theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape());
127 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theTranslationAlgo.mapOfSubShapes();
129 int aTranslatedTag = 1;
130 std::string aTranslatedName = "Translated";
132 switch(theBaseShape->shapeType()) {
133 case GeomAPI_Shape::COMPOUND:
134 case GeomAPI_Shape::COMPSOLID:
135 case GeomAPI_Shape::SOLID:
136 case GeomAPI_Shape::SHELL:
137 theResultBody->loadAndOrientModifiedShapes(&theTranslationAlgo,
138 theBaseShape, GeomAPI_Shape::FACE,
139 aTranslatedTag, aTranslatedName + "_Face", *aSubShapes.get());
140 case GeomAPI_Shape::FACE:
141 case GeomAPI_Shape::WIRE:
142 theResultBody->loadAndOrientModifiedShapes(&theTranslationAlgo,
143 theBaseShape, GeomAPI_Shape::EDGE,
144 ++aTranslatedTag, aTranslatedName + "_Edge", *aSubShapes.get());
145 case GeomAPI_Shape::EDGE:
146 theResultBody->loadAndOrientModifiedShapes(&theTranslationAlgo,
147 theBaseShape, GeomAPI_Shape::VERTEX,
148 ++aTranslatedTag, aTranslatedName + "_Vertex", *aSubShapes.get());