1 // Copyright (C) 2014-2016 CEA/DEN, EDF R&D
3 // File: PrimitivesPlugin_Box.cpp
4 // Created: 10 Mar 2016
5 // Author: Clarisse Genrault (CEA)
7 #include <PrimitivesPlugin_Box.h>
9 #include <ModelAPI_Data.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeSelection.h>
13 #include <ModelAPI_AttributeString.h>
15 #include <GeomAlgoAPI_PointBuilder.h>
19 //=================================================================================================
20 PrimitivesPlugin_Box::PrimitivesPlugin_Box() // Nothing to do during instantiation
24 //=================================================================================================
25 void PrimitivesPlugin_Box::initAttributes()
27 data()->addAttribute(PrimitivesPlugin_Box::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
29 data()->addAttribute(PrimitivesPlugin_Box::DX_ID(), ModelAPI_AttributeDouble::typeId());
30 data()->addAttribute(PrimitivesPlugin_Box::DY_ID(), ModelAPI_AttributeDouble::typeId());
31 data()->addAttribute(PrimitivesPlugin_Box::DZ_ID(), ModelAPI_AttributeDouble::typeId());
33 data()->addAttribute(PrimitivesPlugin_Box::POINT_FIRST_ID(), ModelAPI_AttributeSelection::typeId());
34 data()->addAttribute(PrimitivesPlugin_Box::POINT_SECOND_ID(), ModelAPI_AttributeSelection::typeId());
37 //=================================================================================================
38 void PrimitivesPlugin_Box::execute()
40 AttributeStringPtr aMethodTypeAttr = string(PrimitivesPlugin_Box::CREATION_METHOD());
41 std::string aMethodType = aMethodTypeAttr->value();
43 if (aMethodType == CREATION_METHOD_BY_DIMENSIONS())
44 createBoxByDimensions();
46 if (aMethodType == CREATION_METHOD_BY_TWO_POINTS())
47 createBoxByTwoPoints();
50 //=================================================================================================
51 void PrimitivesPlugin_Box::createBoxByDimensions()
53 double aDx = real(PrimitivesPlugin_Box::DX_ID())->value();
54 double aDy = real(PrimitivesPlugin_Box::DY_ID())->value();
55 double aDz = real(PrimitivesPlugin_Box::DZ_ID())->value();
57 std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo(new GeomAlgoAPI_Box(aDx,aDy,aDz));
59 // These checks should be made to the GUI for the feature but the corresponding validator does not exist yet.
60 if (!aBoxAlgo->check()) {
61 // The error is not displayed in a popup window. It must be in the status bar.
62 setError(aBoxAlgo->getError(), false);
69 // Check if the creation of the box
70 if(!aBoxAlgo->isDone()) {
71 setError(aBoxAlgo->getError());
74 if(!aBoxAlgo->checkValid("Box builder with dimensions")) {
75 setError(aBoxAlgo->getError());
80 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
81 loadNamingDS(aBoxAlgo, aResultBox);
82 setResult(aResultBox, aResultIndex);
85 //=================================================================================================
86 void PrimitivesPlugin_Box::createBoxByTwoPoints()
88 AttributeSelectionPtr aRef1 = data()->selection(PrimitivesPlugin_Box::POINT_FIRST_ID());
89 AttributeSelectionPtr aRef2 = data()->selection(PrimitivesPlugin_Box::POINT_SECOND_ID());
91 std::shared_ptr<GeomAlgoAPI_BoxPoints> aBoxAlgo;
93 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
94 GeomShapePtr aShape1 = aRef1->value();
95 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
96 aShape1 = aRef1->context()->shape();
97 GeomShapePtr aShape2 = aRef2->value();
99 aShape2 = aRef2->context()->shape();
100 if (aShape1 && aShape2){
101 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
102 std::shared_ptr<GeomAPI_Pnt> aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
103 aBoxAlgo = std::shared_ptr<GeomAlgoAPI_BoxPoints>(new GeomAlgoAPI_BoxPoints(aFirstPoint,aSecondPoint));
107 // These checks should be made to the GUI for the feature but the corresponding validator does not exist yet.
108 if (!aBoxAlgo->check()) {
109 // The error is not displayed in a popup window. It must be in the message console.
110 setError(aBoxAlgo->getError(), false);
117 // Check if the creation of the box
118 if(!aBoxAlgo->isDone()) {
119 // The error is not displayed in a popup window. It must be in the message console.
120 setError(aBoxAlgo->getError(), false);
123 if(!aBoxAlgo->checkValid("Box builder with two points")) {
124 // The error is not displayed in a popup window. It must be in the message console.
125 setError(aBoxAlgo->getError(), false);
129 int aResultIndex = 0;
130 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
131 loadNamingDS(aBoxAlgo, aResultBox);
132 setResult(aResultBox, aResultIndex);
135 //=================================================================================================
136 void PrimitivesPlugin_Box::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Box> theBoxAlgo,
137 std::shared_ptr<ModelAPI_ResultBody> theResultBox)
140 theResultBox->store(theBoxAlgo->shape());
142 // Prepare the naming
143 theBoxAlgo->prepareNamingFaces();
147 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces = theBoxAlgo->getCreatedFaces();
148 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
149 std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
150 theResultBox->generated(aFace, (*it).first, num++);