1 // Copyright (C) 2017-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 // File: PrimitivesPlugin_Cone.cpp
21 // Created: 17 Mar 2017
22 // Author: Clarisse Genrault (CEA)
24 #include <PrimitivesPlugin_Cone.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Lin.h>
28 #include <GeomAPI_ShapeExplorer.h>
29 #include <GeomAPI_ShapeIterator.h>
31 #include <GeomAlgoAPI_PointBuilder.h>
33 #include <ModelAPI_AttributeDouble.h>
34 #include <ModelAPI_AttributeSelection.h>
35 #include <ModelAPI_ResultBody.h>
36 #include <ModelAPI_ResultConstruction.h>
37 #include <ModelAPI_Session.h>
41 //=================================================================================================
42 PrimitivesPlugin_Cone::PrimitivesPlugin_Cone()
46 //=================================================================================================
47 void PrimitivesPlugin_Cone::initAttributes()
49 data()->addAttribute(PrimitivesPlugin_Cone::BASE_POINT_ID(),
50 ModelAPI_AttributeSelection::typeId());
51 data()->addAttribute(PrimitivesPlugin_Cone::AXIS_ID(),
52 ModelAPI_AttributeSelection::typeId());
54 data()->addAttribute(PrimitivesPlugin_Cone::BASE_RADIUS_ID(),
55 ModelAPI_AttributeDouble::typeId());
56 data()->addAttribute(PrimitivesPlugin_Cone::TOP_RADIUS_ID(),
57 ModelAPI_AttributeDouble::typeId());
58 data()->addAttribute(PrimitivesPlugin_Cone::HEIGHT_ID(),
59 ModelAPI_AttributeDouble::typeId());
61 // Initialize the base point of the cone at the origin if the base point is not filled.
62 AttributeSelectionPtr aCenterPoint =
63 data()->selection(PrimitivesPlugin_Cone::BASE_POINT_ID());
64 if (!aCenterPoint->isInitialized()) {
65 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
66 ->objectByName(ModelAPI_ResultConstruction::group(), L"Origin");
67 if (aPointObj.get()) {
68 ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
69 aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
73 // Initialize the axis at the OZ axis if the axis is not filled.
74 AttributeSelectionPtr anAxis = data()->selection(PrimitivesPlugin_Cone::AXIS_ID());
75 if (!anAxis->isInitialized()) {
76 ObjectPtr anAxisObj = ModelAPI_Session::get()->moduleDocument()
77 ->objectByName(ModelAPI_ResultConstruction::group(), L"OZ");
78 if (anAxisObj.get()) {
79 ResultPtr anAxisRes = std::dynamic_pointer_cast<ModelAPI_Result>(anAxisObj);
80 anAxis->setValue(anAxisRes, std::shared_ptr<GeomAPI_Shape>());
85 //=================================================================================================
86 void PrimitivesPlugin_Cone::execute()
88 // Getting base point.
89 std::shared_ptr<GeomAPI_Pnt> aBasePoint;
90 std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
91 selection(PrimitivesPlugin_Cone::BASE_POINT_ID());
92 if (aPointRef.get() != NULL) {
93 GeomShapePtr aShape1 = aPointRef->value();
95 aShape1 = aPointRef->context()->shape();
98 aBasePoint = GeomAlgoAPI_PointBuilder::point(aShape1);
103 static const std::string aSelectionError = "Error: The axis shape selection is bad.";
104 std::shared_ptr<ModelAPI_AttributeSelection> anEdgeRef = selection(AXIS_ID());
105 GeomShapePtr aShape = anEdgeRef->value();
107 if (anEdgeRef->context().get()) {
108 aShape = anEdgeRef->context()->shape();
112 setError(aSelectionError);
115 std::shared_ptr<GeomAPI_Edge> anEdge;
116 if (aShape->isEdge())
118 anEdge = aShape->edge();
120 else if (aShape->isCompound())
122 GeomAPI_ShapeIterator anIt(aShape);
123 anEdge = anIt.current()->edge();
127 setError(aSelectionError);
133 setError(aSelectionError);
137 std::shared_ptr<GeomAPI_Ax2> anAxis(new GeomAPI_Ax2(aBasePoint,
138 anEdge->line()->direction()));
140 // Getting base radius, top radius and height
141 double aBaseRadius = real(PrimitivesPlugin_Cone::BASE_RADIUS_ID())->value();
142 double aTopRadius = real(PrimitivesPlugin_Cone::TOP_RADIUS_ID())->value();
143 double aHeight = real(PrimitivesPlugin_Cone::HEIGHT_ID())->value();
145 std::shared_ptr<GeomAlgoAPI_Cone> aConeAlgo =
146 std::shared_ptr<GeomAlgoAPI_Cone>(new GeomAlgoAPI_Cone(anAxis,
151 // These checks should be made to the GUI for the feature but
152 // the corresponding validator does not exist yet.
153 if (!aConeAlgo->check()) {
154 setError(aConeAlgo->getError());
161 // Check if the creation of the cylinder
162 if(!aConeAlgo->isDone()) {
163 setError(aConeAlgo->getError());
166 if(!aConeAlgo->checkValid("Cone builder")) {
167 setError(aConeAlgo->getError());
171 int aResultIndex = 0;
172 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
173 loadNamingDS(aConeAlgo, aResultBox);
174 setResult(aResultBox, aResultIndex);
177 //=================================================================================================
178 void PrimitivesPlugin_Cone::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Cone> theConeAlgo,
179 std::shared_ptr<ModelAPI_ResultBody> theResultCone)
182 theResultCone->store(theConeAlgo->shape());
184 // Prepare the naming
185 theConeAlgo->prepareNamingFaces();
188 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
189 theConeAlgo->getCreatedFaces();
191 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator
192 it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
193 theResultCone->generated((*it).second, (*it).first);
199 GeomAPI_DataMapOfShapeShape aVertices;
201 for (GeomAPI_ShapeExplorer aVertExp(theConeAlgo->shape(), GeomAPI_Shape::VERTEX);
205 if (!aVertices.isBound(aVertExp.current())) {
206 std::ostringstream aStream;
207 aStream<<"Vertex_"<<anIndex++;
208 theResultCone->generated(aVertExp.current(), aStream.str());
209 aVertices.bind(aVertExp.current(), aVertExp.current());