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>
20 //=================================================================================================
21 PrimitivesPlugin_Box::PrimitivesPlugin_Box() // Nothing to do during instantiation
25 //=================================================================================================
26 void PrimitivesPlugin_Box::initAttributes()
28 data()->addAttribute(PrimitivesPlugin_Box::CREATION_METHOD(), ModelAPI_AttributeString::typeId());
30 data()->addAttribute(PrimitivesPlugin_Box::DX_ID(), ModelAPI_AttributeDouble::typeId());
31 data()->addAttribute(PrimitivesPlugin_Box::DY_ID(), ModelAPI_AttributeDouble::typeId());
32 data()->addAttribute(PrimitivesPlugin_Box::DZ_ID(), ModelAPI_AttributeDouble::typeId());
34 data()->addAttribute(PrimitivesPlugin_Box::POINT_FIRST_ID(),
35 ModelAPI_AttributeSelection::typeId());
36 data()->addAttribute(PrimitivesPlugin_Box::POINT_SECOND_ID(),
37 ModelAPI_AttributeSelection::typeId());
40 //=================================================================================================
41 void PrimitivesPlugin_Box::execute()
43 AttributeStringPtr aMethodTypeAttr = string(PrimitivesPlugin_Box::CREATION_METHOD());
44 std::string aMethodType = aMethodTypeAttr->value();
46 if (aMethodType == CREATION_METHOD_BY_DIMENSIONS())
47 createBoxByDimensions();
49 if (aMethodType == CREATION_METHOD_BY_TWO_POINTS())
50 createBoxByTwoPoints();
53 //=================================================================================================
54 void PrimitivesPlugin_Box::createBoxByDimensions()
56 double aDx = real(PrimitivesPlugin_Box::DX_ID())->value();
57 double aDy = real(PrimitivesPlugin_Box::DY_ID())->value();
58 double aDz = real(PrimitivesPlugin_Box::DZ_ID())->value();
60 std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo(new GeomAlgoAPI_Box(aDx,aDy,aDz));
62 // These checks should be made to the GUI for the feature but
63 // the corresponding validator does not exist yet.
64 if (!aBoxAlgo->check()) {
65 setError(aBoxAlgo->getError());
72 // Check if the creation of the box
73 if(!aBoxAlgo->isDone()) {
74 setError(aBoxAlgo->getError());
77 if(!aBoxAlgo->checkValid("Box builder with dimensions")) {
78 setError(aBoxAlgo->getError());
83 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
84 loadNamingDS(aBoxAlgo, aResultBox);
85 setResult(aResultBox, aResultIndex);
88 //=================================================================================================
89 void PrimitivesPlugin_Box::createBoxByTwoPoints()
91 AttributeSelectionPtr aRef1 = data()->selection(PrimitivesPlugin_Box::POINT_FIRST_ID());
92 AttributeSelectionPtr aRef2 = data()->selection(PrimitivesPlugin_Box::POINT_SECOND_ID());
94 std::shared_ptr<GeomAlgoAPI_Box> aBoxAlgo;
96 if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) {
97 GeomShapePtr aShape1 = aRef1->value();
98 if (!aShape1.get()) //If we can't get the points directly, try getting them from the context
99 aShape1 = aRef1->context()->shape();
100 GeomShapePtr aShape2 = aRef2->value();
102 aShape2 = aRef2->context()->shape();
103 if (aShape1 && aShape2){
104 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1);
105 std::shared_ptr<GeomAPI_Pnt> aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2);
106 aBoxAlgo = std::shared_ptr<GeomAlgoAPI_Box>(new GeomAlgoAPI_Box(aFirstPoint,aSecondPoint));
110 // These checks should be made to the GUI for the feature but
111 // the corresponding validator does not exist yet.
112 if (!aBoxAlgo->check()) {
113 setError(aBoxAlgo->getError());
120 // Check if the creation of the box
121 if(!aBoxAlgo->isDone()) {
122 // The error is not displayed in a popup window. It must be in the message console.
123 setError(aBoxAlgo->getError());
126 if(!aBoxAlgo->checkValid("Box builder with two points")) {
127 // The error is not displayed in a popup window. It must be in the message console.
128 setError(aBoxAlgo->getError());
132 int aResultIndex = 0;
133 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
134 loadNamingDS(aBoxAlgo, aResultBox);
135 setResult(aResultBox, aResultIndex);
138 //=================================================================================================
139 void PrimitivesPlugin_Box::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Box> theBoxAlgo,
140 std::shared_ptr<ModelAPI_ResultBody> theResultBox)
143 theResultBox->store(theBoxAlgo->shape());
145 // Prepare the naming
146 theBoxAlgo->prepareNamingFaces();
150 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
151 theBoxAlgo->getCreatedFaces();
152 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator
153 it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
154 std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
155 theResultBox->generated(aFace, (*it).first, num++);