1 // Copyright (C) 2014-2020 CEA/DEN, EDF R&D
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
20 #include <PrimitivesPlugin_Box.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_AttributeDouble.h>
25 #include <ModelAPI_AttributeSelection.h>
26 #include <ModelAPI_AttributeString.h>
28 #include <GeomAlgoAPI_PointBuilder.h>
33 //=================================================================================================
34 PrimitivesPlugin_Box::PrimitivesPlugin_Box() // Nothing to do during instantiation
38 //=================================================================================================
39 void PrimitivesPlugin_Box::initAttributes()
41 data()->addAttribute(PrimitivesPlugin_Box::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
43 data()->addAttribute(PrimitivesPlugin_Box::DX_ID(), ModelAPI_AttributeDouble::typeId());
44 data()->addAttribute(PrimitivesPlugin_Box::DY_ID(), ModelAPI_AttributeDouble::typeId());
45 data()->addAttribute(PrimitivesPlugin_Box::DZ_ID(), ModelAPI_AttributeDouble::typeId());
47 data()->addAttribute(PrimitivesPlugin_Box::POINT_FIRST_ID(),
48 ModelAPI_AttributeSelection::typeId());
49 data()->addAttribute(PrimitivesPlugin_Box::POINT_SECOND_ID(),
50 ModelAPI_AttributeSelection::typeId());
53 //=================================================================================================
54 void PrimitivesPlugin_Box::execute()
56 AttributeStringPtr aMethodTypeAttr = string(PrimitivesPlugin_Box::CREATION_METHOD());
57 std::string aMethodType = aMethodTypeAttr->value();
59 if (aMethodType == CREATION_METHOD_BY_DIMENSIONS())
60 createBoxByDimensions();
62 if (aMethodType == CREATION_METHOD_BY_TWO_POINTS())
63 createBoxByTwoPoints();
66 //=================================================================================================
67 void PrimitivesPlugin_Box::createBoxByDimensions()
69 double aDx = real(PrimitivesPlugin_Box::DX_ID())->value();
70 double aDy = real(PrimitivesPlugin_Box::DY_ID())->value();
71 double aDz = real(PrimitivesPlugin_Box::DZ_ID())->value();
73 std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo(new GeomAlgoAPI_Box(aDx,aDy,aDz));
75 // These checks should be made to the GUI for the feature but
76 // the corresponding validator does not exist yet.
77 if (!aBoxAlgo->check()) {
78 setError(aBoxAlgo->getError());
85 // Check if the creation of the box
86 if(!aBoxAlgo->isDone()) {
87 setError(aBoxAlgo->getError());
90 if(!aBoxAlgo->checkValid("Box builder with dimensions")) {
91 setError(aBoxAlgo->getError());
96 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
97 loadNamingDS(aBoxAlgo, aResultBox);
98 setResult(aResultBox, aResultIndex);
101 //=================================================================================================
102 void PrimitivesPlugin_Box::createBoxByTwoPoints()
104 AttributeSelectionPtr aRef1 = data()->selection(PrimitivesPlugin_Box::POINT_FIRST_ID());
105 AttributeSelectionPtr aRef2 = data()->selection(PrimitivesPlugin_Box::POINT_SECOND_ID());
107 std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo;
109 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
110 GeomShapePtr aShape1 = aRef1->value();
111 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
112 aShape1 = aRef1->context()->shape();
113 GeomShapePtr aShape2 = aRef2->value();
115 aShape2 = aRef2->context()->shape();
116 if (aShape1 && aShape2){
117 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
118 std::shared_ptr<GeomAPI_Pnt> aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
119 aBoxAlgo = std::shared_ptr<GeomAlgoAPI_Box>(new GeomAlgoAPI_Box(aFirstPoint,aSecondPoint));
123 // These checks should be made to the GUI for the feature but
124 // the corresponding validator does not exist yet.
125 if (!aBoxAlgo->check()) {
126 setError(aBoxAlgo->getError());
133 // Check if the creation of the box
134 if(!aBoxAlgo->isDone()) {
135 // The error is not displayed in a popup window. It must be in the message console.
136 setError(aBoxAlgo->getError());
139 if(!aBoxAlgo->checkValid("Box builder with two points")) {
140 // The error is not displayed in a popup window. It must be in the message console.
141 setError(aBoxAlgo->getError());
145 int aResultIndex = 0;
146 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
147 loadNamingDS(aBoxAlgo, aResultBox);
148 setResult(aResultBox, aResultIndex);
151 //=================================================================================================
152 void PrimitivesPlugin_Box::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Box> theBoxAlgo,
153 std::shared_ptr<ModelAPI_ResultBody> theResultBox)
156 theResultBox->store(theBoxAlgo->shape());
158 // Prepare the naming
159 theBoxAlgo->prepareNamingFaces();
162 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
163 theBoxAlgo->getCreatedFaces();
164 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator it = listOfFaces.begin();
165 it != listOfFaces.end();
168 theResultBox->generated((*it).second, (*it).first);