1 // Copyright (C) 2014-201x CEA/DEN, EDF R&D
3 // File: PrimitivesPlugin_Cone.cpp
4 // Created: 17 Mar 2017
5 // Author: Clarisse Genrault (CEA)
7 #include <PrimitivesPlugin_Cone.h>
9 #include <GeomAPI_Edge.h>
10 #include <GeomAPI_Lin.h>
11 #include <GeomAPI_ShapeExplorer.h>
13 #include <GeomAlgoAPI_PointBuilder.h>
15 #include <ModelAPI_AttributeDouble.h>
16 #include <ModelAPI_AttributeSelection.h>
17 #include <ModelAPI_ResultBody.h>
18 #include <ModelAPI_ResultConstruction.h>
19 #include <ModelAPI_Session.h>
23 //=================================================================================================
24 PrimitivesPlugin_Cone::PrimitivesPlugin_Cone()
28 //=================================================================================================
29 void PrimitivesPlugin_Cone::initAttributes()
31 data()->addAttribute(PrimitivesPlugin_Cone::BASE_POINT_ID(),
32 ModelAPI_AttributeSelection::typeId());
33 data()->addAttribute(PrimitivesPlugin_Cone::AXIS_ID(),
34 ModelAPI_AttributeSelection::typeId());
36 data()->addAttribute(PrimitivesPlugin_Cone::BASE_RADIUS_ID(),
37 ModelAPI_AttributeDouble::typeId());
38 data()->addAttribute(PrimitivesPlugin_Cone::TOP_RADIUS_ID(),
39 ModelAPI_AttributeDouble::typeId());
40 data()->addAttribute(PrimitivesPlugin_Cone::HEIGHT_ID(),
41 ModelAPI_AttributeDouble::typeId());
43 // Initialize the base point of the cone at the origin if the base point is not filled.
44 AttributeSelectionPtr aCenterPoint =
45 data()->selection(PrimitivesPlugin_Cone::BASE_POINT_ID());
46 if (!aCenterPoint->isInitialized()) {
47 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
48 ->objectByName(ModelAPI_ResultConstruction::group(), "Origin");
49 if (aPointObj.get()) {
50 ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
51 aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
55 // Initialize the axis at the OZ axis if the axis is not filled.
56 AttributeSelectionPtr anAxis = data()->selection(PrimitivesPlugin_Cone::AXIS_ID());
57 if (!anAxis->isInitialized()) {
58 ObjectPtr anAxisObj = ModelAPI_Session::get()->moduleDocument()
59 ->objectByName(ModelAPI_ResultConstruction::group(), "OZ");
60 if (anAxisObj.get()) {
61 ResultPtr anAxisRes = std::dynamic_pointer_cast<ModelAPI_Result>(anAxisObj);
62 anAxis->setValue(anAxisRes, std::shared_ptr<GeomAPI_Shape>());
67 //=================================================================================================
68 void PrimitivesPlugin_Cone::execute()
70 // Getting base point.
71 std::shared_ptr<GeomAPI_Pnt> aBasePoint;
72 std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
73 selection(PrimitivesPlugin_Cone::BASE_POINT_ID());
74 if (aPointRef.get() != NULL) {
75 GeomShapePtr aShape1 = aPointRef->value();
77 aShape1 = aPointRef->context()->shape();
80 aBasePoint = GeomAlgoAPI_PointBuilder::point(aShape1);
85 std::shared_ptr<GeomAPI_Ax2> anAxis;
86 std::shared_ptr<GeomAPI_Edge> anEdge;
87 std::shared_ptr<ModelAPI_AttributeSelection> anEdgeRef =
88 selection(PrimitivesPlugin_Cone::AXIS_ID());
89 if(anEdgeRef && anEdgeRef->value() && anEdgeRef->value()->isEdge()) {
90 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->value()));
91 } else if (anEdgeRef && !anEdgeRef->value() && anEdgeRef->context() &&
92 anEdgeRef->context()->shape() && anEdgeRef->context()->shape()->isEdge()) {
93 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->context()->shape()));
96 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
97 anEdge->line()->direction()));
100 // Getting base radius, top radius and height
101 double aBaseRadius = real(PrimitivesPlugin_Cone::BASE_RADIUS_ID())->value();
102 double aTopRadius = real(PrimitivesPlugin_Cone::TOP_RADIUS_ID())->value();
103 double aHeight = real(PrimitivesPlugin_Cone::HEIGHT_ID())->value();
105 std::shared_ptr<GeomAlgoAPI_Cone> aConeAlgo =
106 std::shared_ptr<GeomAlgoAPI_Cone>(new GeomAlgoAPI_Cone(anAxis,
111 // These checks should be made to the GUI for the feature but
112 // the corresponding validator does not exist yet.
113 if (!aConeAlgo->check()) {
114 setError(aConeAlgo->getError());
121 // Check if the creation of the cylinder
122 if(!aConeAlgo->isDone()) {
123 setError(aConeAlgo->getError());
126 if(!aConeAlgo->checkValid("Cone builder")) {
127 setError(aConeAlgo->getError());
131 int aResultIndex = 0;
132 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
133 loadNamingDS(aConeAlgo, aResultBox);
134 setResult(aResultBox, aResultIndex);
137 //=================================================================================================
138 void PrimitivesPlugin_Cone::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Cone> theConeAlgo,
139 std::shared_ptr<ModelAPI_ResultBody> theResultCone)
142 theResultCone->store(theConeAlgo->shape());
144 // Prepare the naming
145 theConeAlgo->prepareNamingFaces();
149 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
150 theConeAlgo->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 theResultCone->generated(aFace, (*it).first, num++);
161 GeomAPI_DataMapOfShapeShape aVertices;
162 GeomAPI_ShapeExplorer aVertExp(theConeAlgo->shape(), GeomAPI_Shape::VERTEX);
163 for(int anIndex = 1; aVertExp.more(); aVertExp.next()) {
164 if (!aVertices.isBound(aVertExp.current())) {
165 std::ostringstream aStream;
166 aStream<<"Vertex_"<<anIndex++;
167 theResultCone->generated(aVertExp.current(), aStream.str(), num++);
168 aVertices.bind(aVertExp.current(), aVertExp.current());