1 // Copyright (C) 2014-2017 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
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <PrimitivesPlugin_Box.h>
23 #include <ModelAPI_Data.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_AttributeDouble.h>
26 #include <ModelAPI_AttributeSelection.h>
27 #include <ModelAPI_AttributeString.h>
29 #include <GeomAlgoAPI_PointBuilder.h>
34 //=================================================================================================
35 PrimitivesPlugin_Box::PrimitivesPlugin_Box() // Nothing to do during instantiation
39 //=================================================================================================
40 void PrimitivesPlugin_Box::initAttributes()
42 data()->addAttribute(PrimitivesPlugin_Box::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
44 data()->addAttribute(PrimitivesPlugin_Box::DX_ID(), ModelAPI_AttributeDouble::typeId());
45 data()->addAttribute(PrimitivesPlugin_Box::DY_ID(), ModelAPI_AttributeDouble::typeId());
46 data()->addAttribute(PrimitivesPlugin_Box::DZ_ID(), ModelAPI_AttributeDouble::typeId());
48 data()->addAttribute(PrimitivesPlugin_Box::POINT_FIRST_ID(),
49 ModelAPI_AttributeSelection::typeId());
50 data()->addAttribute(PrimitivesPlugin_Box::POINT_SECOND_ID(),
51 ModelAPI_AttributeSelection::typeId());
54 //=================================================================================================
55 void PrimitivesPlugin_Box::execute()
57 AttributeStringPtr aMethodTypeAttr = string(PrimitivesPlugin_Box::CREATION_METHOD());
58 std::string aMethodType = aMethodTypeAttr->value();
60 if (aMethodType == CREATION_METHOD_BY_DIMENSIONS())
61 createBoxByDimensions();
63 if (aMethodType == CREATION_METHOD_BY_TWO_POINTS())
64 createBoxByTwoPoints();
67 //=================================================================================================
68 void PrimitivesPlugin_Box::createBoxByDimensions()
70 double aDx = real(PrimitivesPlugin_Box::DX_ID())->value();
71 double aDy = real(PrimitivesPlugin_Box::DY_ID())->value();
72 double aDz = real(PrimitivesPlugin_Box::DZ_ID())->value();
74 std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo(new GeomAlgoAPI_Box(aDx,aDy,aDz));
76 // These checks should be made to the GUI for the feature but
77 // the corresponding validator does not exist yet.
78 if (!aBoxAlgo->check()) {
79 setError(aBoxAlgo->getError());
86 // Check if the creation of the box
87 if(!aBoxAlgo->isDone()) {
88 setError(aBoxAlgo->getError());
91 if(!aBoxAlgo->checkValid("Box builder with dimensions")) {
92 setError(aBoxAlgo->getError());
97 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
98 loadNamingDS(aBoxAlgo, aResultBox);
99 setResult(aResultBox, aResultIndex);
102 //=================================================================================================
103 void PrimitivesPlugin_Box::createBoxByTwoPoints()
105 AttributeSelectionPtr aRef1 = data()->selection(PrimitivesPlugin_Box::POINT_FIRST_ID());
106 AttributeSelectionPtr aRef2 = data()->selection(PrimitivesPlugin_Box::POINT_SECOND_ID());
108 std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo;
110 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
111 GeomShapePtr aShape1 = aRef1->value();
112 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
113 aShape1 = aRef1->context()->shape();
114 GeomShapePtr aShape2 = aRef2->value();
116 aShape2 = aRef2->context()->shape();
117 if (aShape1 && aShape2){
118 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
119 std::shared_ptr<GeomAPI_Pnt> aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
120 aBoxAlgo = std::shared_ptr<GeomAlgoAPI_Box>(new GeomAlgoAPI_Box(aFirstPoint,aSecondPoint));
124 // These checks should be made to the GUI for the feature but
125 // the corresponding validator does not exist yet.
126 if (!aBoxAlgo->check()) {
127 setError(aBoxAlgo->getError());
134 // Check if the creation of the box
135 if(!aBoxAlgo->isDone()) {
136 // The error is not displayed in a popup window. It must be in the message console.
137 setError(aBoxAlgo->getError());
140 if(!aBoxAlgo->checkValid("Box builder with two points")) {
141 // The error is not displayed in a popup window. It must be in the message console.
142 setError(aBoxAlgo->getError());
146 int aResultIndex = 0;
147 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
148 loadNamingDS(aBoxAlgo, aResultBox);
149 setResult(aResultBox, aResultIndex);
152 //=================================================================================================
153 void PrimitivesPlugin_Box::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Box> theBoxAlgo,
154 std::shared_ptr<ModelAPI_ResultBody> theResultBox)
157 theResultBox->store(theBoxAlgo->shape());
159 // Prepare the naming
160 theBoxAlgo->prepareNamingFaces();
164 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
165 theBoxAlgo->getCreatedFaces();
166 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator
167 it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
168 std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
169 theResultBox->generated(aFace, (*it).first, num++);