1 // Copyright (C) 2014-201x CEA/DEN, EDF R&D
3 // File: FeaturesPlugin_Scale.cpp
4 // Created: 13 Jan 2017
5 // Author: Clarisse Genrault (CEA)
7 #include <FeaturesPlugin_Scale.h>
9 #include <GeomAlgoAPI_PointBuilder.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeSelectionList.h>
13 #include <ModelAPI_AttributeString.h>
14 #include <ModelAPI_ResultBody.h>
15 #include <ModelAPI_ResultPart.h>
19 //=================================================================================================
20 FeaturesPlugin_Scale::FeaturesPlugin_Scale()
24 //=================================================================================================
25 void FeaturesPlugin_Scale::initAttributes()
27 AttributeSelectionListPtr aSelection =
28 std::dynamic_pointer_cast<ModelAPI_AttributeSelectionList>(data()->addAttribute(
29 FeaturesPlugin_Scale::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId()));
31 data()->addAttribute(FeaturesPlugin_Scale::CENTER_POINT_ID(),
32 ModelAPI_AttributeSelection::typeId());
34 data()->addAttribute(FeaturesPlugin_Scale::SCALE_FACTOR_ID(),
35 ModelAPI_AttributeDouble::typeId());
38 //=================================================================================================
39 void FeaturesPlugin_Scale::execute()
42 ListOfShape anObjects;
43 std::list<ResultPtr> aContextes;
44 AttributeSelectionListPtr anObjectsSelList =
45 selectionList(FeaturesPlugin_Scale::OBJECTS_LIST_ID());
46 if (anObjectsSelList->size() == 0) {
49 for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) {
50 std::shared_ptr<ModelAPI_AttributeSelection> anObjectAttr =
51 anObjectsSelList->value(anObjectsIndex);
52 std::shared_ptr<GeomAPI_Shape> anObject = anObjectAttr->value();
53 if(!anObject.get()) { // may be for not-activated parts
57 anObjects.push_back(anObject);
58 aContextes.push_back(anObjectAttr->context());
61 // Getting the center point
62 std::shared_ptr<GeomAPI_Pnt> aCenterPoint;
63 std::shared_ptr<ModelAPI_AttributeSelection> anObjRef =
64 selection(FeaturesPlugin_Scale::CENTER_POINT_ID());
65 if (anObjRef.get() != NULL) {
66 GeomShapePtr aShape = anObjRef->value();
68 aShape = anObjRef->context()->shape();
71 aCenterPoint = GeomAlgoAPI_PointBuilder::point(aShape);
75 // Getting scale factor
76 double aScaleFactor = real(FeaturesPlugin_Scale::SCALE_FACTOR_ID())->value();
78 // Moving each object.
80 std::list<ResultPtr>::iterator aContext = aContextes.begin();
81 for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end();
82 anObjectsIt++, aContext++) {
83 std::shared_ptr<GeomAPI_Shape> aBaseShape = *anObjectsIt;
84 bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group();
88 std::shared_ptr<GeomAPI_Trsf> aTrsf(new GeomAPI_Trsf());
89 aTrsf->setScale(aCenterPoint, aScaleFactor);
90 ResultPartPtr anOrigin = std::dynamic_pointer_cast<ModelAPI_ResultPart>(*aContext);
91 ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex);
92 aResultPart->setTrsf(*aContext, aTrsf);
93 setResult(aResultPart, aResultIndex);
95 GeomAlgoAPI_Scale aScaleAlgo(aBaseShape, aCenterPoint, aScaleFactor);
97 if (!aScaleAlgo.check()) {
98 setError(aScaleAlgo.getError());
104 // Checking that the algorithm worked properly.
105 if(!aScaleAlgo.isDone()) {
106 static const std::string aFeatureError = "Error: Symmetry algorithm failed.";
107 setError(aFeatureError);
110 if(aScaleAlgo.shape()->isNull()) {
111 static const std::string aShapeError = "Error: Resulting shape is Null.";
112 setError(aShapeError);
115 if(!aScaleAlgo.isValid()) {
116 std::string aFeatureError = "Error: Resulting shape is not valid.";
117 setError(aFeatureError);
121 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
122 loadNamingDS(aScaleAlgo, aResultBody, aBaseShape);
123 setResult(aResultBody, aResultIndex);
128 // Remove the rest results if there were produced in the previous pass.
129 removeResults(aResultIndex);
132 //=================================================================================================
133 void FeaturesPlugin_Scale::loadNamingDS(GeomAlgoAPI_Scale& theScaleAlgo,
134 std::shared_ptr<ModelAPI_ResultBody> theResultBody,
135 std::shared_ptr<GeomAPI_Shape> theBaseShape)
137 // Store and name the result.
138 theResultBody->storeModified(theBaseShape, theScaleAlgo.shape());
141 std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = theScaleAlgo.mapOfSubShapes();
142 int aReflectedTag = 1;
143 std::string aReflectedName = "Scaled";
144 theResultBody->loadAndOrientModifiedShapes(&theScaleAlgo,
145 theBaseShape, GeomAPI_Shape::FACE,
146 aReflectedTag, aReflectedName, *aSubShapes.get());