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>
12 #include <GeomAlgoAPI_PointBuilder.h>
14 #include <ModelAPI_AttributeDouble.h>
15 #include <ModelAPI_AttributeSelection.h>
16 #include <ModelAPI_ResultBody.h>
17 #include <ModelAPI_ResultConstruction.h>
18 #include <ModelAPI_Session.h>
20 //=================================================================================================
21 PrimitivesPlugin_Cone::PrimitivesPlugin_Cone()
25 //=================================================================================================
26 void PrimitivesPlugin_Cone::initAttributes()
28 data()->addAttribute(PrimitivesPlugin_Cone::BASE_POINT_ID(),
29 ModelAPI_AttributeSelection::typeId());
30 data()->addAttribute(PrimitivesPlugin_Cone::AXIS_ID(),
31 ModelAPI_AttributeSelection::typeId());
33 data()->addAttribute(PrimitivesPlugin_Cone::BASE_RADIUS_ID(),
34 ModelAPI_AttributeDouble::typeId());
35 data()->addAttribute(PrimitivesPlugin_Cone::TOP_RADIUS_ID(),
36 ModelAPI_AttributeDouble::typeId());
37 data()->addAttribute(PrimitivesPlugin_Cone::HEIGHT_ID(),
38 ModelAPI_AttributeDouble::typeId());
40 // Initialize the base point of the cone at the origin if the base point is not filled.
41 AttributeSelectionPtr aCenterPoint =
42 data()->selection(PrimitivesPlugin_Cone::BASE_POINT_ID());
43 if (!aCenterPoint->isInitialized()) {
44 ObjectPtr aPointObj = ModelAPI_Session::get()->moduleDocument()
45 ->objectByName(ModelAPI_ResultConstruction::group(), "Origin");
46 if (aPointObj.get()) {
47 ResultPtr aPointRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPointObj);
48 aCenterPoint->setValue(aPointRes, std::shared_ptr<GeomAPI_Shape>());
52 // Initialize the axis at the OZ axis if the axis is not filled.
53 AttributeSelectionPtr anAxis = data()->selection(PrimitivesPlugin_Cone::AXIS_ID());
54 if (!anAxis->isInitialized()) {
55 ObjectPtr anAxisObj = ModelAPI_Session::get()->moduleDocument()
56 ->objectByName(ModelAPI_ResultConstruction::group(), "OZ");
57 if (anAxisObj.get()) {
58 ResultPtr anAxisRes = std::dynamic_pointer_cast<ModelAPI_Result>(anAxisObj);
59 anAxis->setValue(anAxisRes, std::shared_ptr<GeomAPI_Shape>());
64 //=================================================================================================
65 void PrimitivesPlugin_Cone::execute()
67 // Getting base point.
68 std::shared_ptr<GeomAPI_Pnt> aBasePoint;
69 std::shared_ptr<ModelAPI_AttributeSelection> aPointRef =
70 selection(PrimitivesPlugin_Cone::BASE_POINT_ID());
71 if (aPointRef.get() != NULL) {
72 GeomShapePtr aShape1 = aPointRef->value();
74 aShape1 = aPointRef->context()->shape();
77 aBasePoint = GeomAlgoAPI_PointBuilder::point(aShape1);
82 std::shared_ptr<GeomAPI_Ax2> anAxis;
83 std::shared_ptr<GeomAPI_Edge> anEdge;
84 std::shared_ptr<ModelAPI_AttributeSelection> anEdgeRef =
85 selection(PrimitivesPlugin_Cone::AXIS_ID());
86 if(anEdgeRef && anEdgeRef->value() && anEdgeRef->value()->isEdge()) {
87 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->value()));
88 } else if (anEdgeRef && !anEdgeRef->value() && anEdgeRef->context() &&
89 anEdgeRef->context()->shape() && anEdgeRef->context()->shape()->isEdge()) {
90 anEdge = std::shared_ptr<GeomAPI_Edge>(new GeomAPI_Edge(anEdgeRef->context()->shape()));
93 anAxis = std::shared_ptr<GeomAPI_Ax2>(new GeomAPI_Ax2(aBasePoint,
94 anEdge->line()->direction()));
97 // Getting base radius, top radius and height
98 double aBaseRadius = real(PrimitivesPlugin_Cone::BASE_RADIUS_ID())->value();
99 double aTopRadius = real(PrimitivesPlugin_Cone::TOP_RADIUS_ID())->value();
100 double aHeight = real(PrimitivesPlugin_Cone::HEIGHT_ID())->value();
102 std::shared_ptr<GeomAlgoAPI_Cone> aConeAlgo =
103 std::shared_ptr<GeomAlgoAPI_Cone>(new GeomAlgoAPI_Cone(anAxis,
108 // These checks should be made to the GUI for the feature but
109 // the corresponding validator does not exist yet.
110 if (!aConeAlgo->check()) {
111 setError(aConeAlgo->getError());
118 // Check if the creation of the cylinder
119 if(!aConeAlgo->isDone()) {
120 setError(aConeAlgo->getError());
123 if(!aConeAlgo->checkValid("Cone builder")) {
124 setError(aConeAlgo->getError());
128 int aResultIndex = 0;
129 ResultBodyPtr aResultBox = document()->createBody(data(), aResultIndex);
130 loadNamingDS(aConeAlgo, aResultBox);
131 setResult(aResultBox, aResultIndex);
134 //=================================================================================================
135 void PrimitivesPlugin_Cone::loadNamingDS(std::shared_ptr<GeomAlgoAPI_Cone> theConeAlgo,
136 std::shared_ptr<ModelAPI_ResultBody> theResultCone)
139 theResultCone->store(theConeAlgo->shape());
141 // Prepare the naming
142 theConeAlgo->prepareNamingFaces();
146 std::map< std::string, std::shared_ptr<GeomAPI_Shape> > listOfFaces =
147 theConeAlgo->getCreatedFaces();
148 for (std::map< std::string, std::shared_ptr<GeomAPI_Shape> >::iterator
149 it=listOfFaces.begin(); it!=listOfFaces.end(); ++it) {
150 std::shared_ptr<GeomAPI_Shape> aFace = (*it).second;
151 theResultCone->generated(aFace, (*it).first, num++);