]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Extrusion.cpp
Salome HOME
b32f2e97a5e09841d36fb2602636c248b78121f4
[modules/shaper.git] / src / FeaturesPlugin / FeaturesPlugin_Extrusion.cpp
1 // File:        FeaturesPlugin_Extrusion.cpp
2 // Created:     30 May 2014
3 // Author:      Vitaly SMETANNIKOV
4
5 #include "FeaturesPlugin_Extrusion.h"
6 #include <ModelAPI_Session.h>
7 #include <ModelAPI_Document.h>
8 #include <ModelAPI_Data.h>
9 #include <ModelAPI_ResultConstruction.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_AttributeDouble.h>
12 #include <ModelAPI_AttributeSelection.h>
13 #include <ModelAPI_AttributeBoolean.h>
14 #include <Events_Error.h>
15 #include <GeomAlgoAPI_Extrusion.h>
16
17 using namespace std;
18 #define _LATERAL_TAG 1
19 #define _FIRST_TAG 2
20 #define _LAST_TAG 3
21 #ifdef _DEBUG
22 #include <iostream>
23 #include <ostream>
24 #endif
25
26 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
27 {
28 }
29
30 void FeaturesPlugin_Extrusion::initAttributes()
31 {
32   data()->addAttribute(FeaturesPlugin_Extrusion::FACE_ID(), ModelAPI_AttributeSelection::type());
33   data()->addAttribute(FeaturesPlugin_Extrusion::SIZE_ID(), ModelAPI_AttributeDouble::type());
34   data()->addAttribute(FeaturesPlugin_Extrusion::REVERSE_ID(), ModelAPI_AttributeBoolean::type());
35 }
36
37 void FeaturesPlugin_Extrusion::execute()
38 {
39   boost::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = boost::dynamic_pointer_cast<
40     ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Extrusion::FACE_ID()));
41   if (!aFaceRef)
42     return;
43
44   boost::shared_ptr<GeomAPI_Shape> aFace = 
45     boost::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
46   if (!aFace)
47     return;
48
49   boost::shared_ptr<GeomAPI_Shape> aContext;
50   ResultPtr aContextRes = aFaceRef->context();
51   if (aContextRes) {
52     if (aContextRes->groupName() == ModelAPI_ResultBody::group())
53       aContext = boost::dynamic_pointer_cast<ModelAPI_ResultBody>(aContextRes)->shape();
54     else if (aContextRes->groupName() == ModelAPI_ResultConstruction::group())
55       aContext = boost::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContextRes)->shape();
56   }
57   if (!aContext) {
58     std::string aContextError = "The selection context is bad";
59     Events_Error::send(aContextError, this);
60     return;
61   }
62
63   double aSize = data()->real(FeaturesPlugin_Extrusion::SIZE_ID())->value();
64   if (data()->boolean(FeaturesPlugin_Extrusion::REVERSE_ID())->value())
65     aSize = -aSize;
66
67   boost::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
68   //TCollection_AsciiString anError;
69   GeomAlgoAPI_Extrusion aFeature(aFace, aSize);
70   if(!aFeature.isDone()) {
71     std::string aFeatureError = "Extrusion algorithm failed";  
72     Events_Error::send(aFeatureError, this);
73     return;
74   }
75
76   // Check if shape is valid
77   if (aFeature.shape()->isNull()) {
78     std::string aShapeError = "Resulting shape is Null";     
79     Events_Error::send(aShapeError, this);
80 #ifdef _DEBUG
81     std::cerr << aShapeError << std::endl;
82 #endif
83     return;
84   }
85   if(!aFeature.isValid()) {
86     std::string aFeatureError = "Warning: resulting shape is not valid";  
87     Events_Error::send(aFeatureError, this);
88     return;
89   }  
90   //LoadNamingDS
91   LoadNamingDS(aFeature, aResultBody, aFace, aContext);
92
93   setResult(aResultBody);
94 }
95
96 //============================================================================
97 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Extrusion& theFeature, 
98   boost::shared_ptr<ModelAPI_ResultBody> theResultBody, 
99   boost::shared_ptr<GeomAPI_Shape> theBasis,
100   boost::shared_ptr<GeomAPI_Shape> theContext)
101 {  
102
103
104   //load result
105   if(theBasis->isEqual(theContext))
106     theResultBody->store(theFeature.shape());
107   else
108     theResultBody->storeGenerated(theContext, theFeature.shape());
109   /*
110   TopTools_DataMapOfShapeShape aSubShapes;
111   for (TopExp_Explorer Exp(theFeature.shape()->impl<TopoDS_Shape>(),TopAbs_FACE); Exp.More(); Exp.Next()) {
112     aSubShapes.Bind(Exp.Current(),Exp.Current());
113   }
114
115   //Insert lateral face : Face from Edge
116   //GeomAlgoAPI_DFLoader::loadAndOrientGeneratedShapes(*myBuilder, myBasis, TopAbs_EDGE, aLateralFaceBuilder, aSubShapes);
117
118
119   TopTools_MapOfShape aView;
120   TopExp_Explorer aShapeExplorer (theFeature.shape()->impl<TopoDS_Shape>(), TopAbs_EDGE);
121   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
122     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
123     if (!aView.Add(aRoot)) continue;
124     boost::shared_ptr<GeomAPI_Shape> aRootG(new GeomAPI_Shape());
125     aRootG->setImpl((void *)&aRoot);
126     const ListOfShape& aShapes = theFeature.generated(aRootG);
127     std::list<boost::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();   
128     for (; anIt != aLast; anIt++) {
129       TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>(); 
130       if (aSubShapes.IsBound(aNewShape)) {
131         aNewShape.Orientation((aSubShapes(aNewShape)).Orientation());
132       }
133
134       if (!aRoot.IsSame (aNewShape)) {
135         boost::shared_ptr<GeomAPI_Shape> aNew(new GeomAPI_Shape());
136         aNew->setImpl((void *)&aNewShape);
137         theResultBody->generated(aRootG, aNew,_LATERAL_TAG); 
138       }
139     }
140   }
141   //Insert bottom face
142   const boost::shared_ptr<GeomAPI_Shape>& aBottomFace = theFeature.firstShape();
143   if (!aBottomFace->isNull()) {
144     if (aSubShapes.IsBound(aBottomFace->impl<TopoDS_Shape>())) {
145       aBottomFace->setImpl((void *)&aSubShapes(aBottomFace->impl<TopoDS_Shape>()));
146     }    
147     theResultBody->generated(aBottomFace, _FIRST_TAG);
148   }
149
150
151
152   //Insert top face
153   boost::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
154   if (!aTopFace->isNull()) {
155     if (aSubShapes.IsBound(aTopFace->impl<TopoDS_Shape>())) {
156       aTopFace->setImpl((void *)&aSubShapes(aTopFace->impl<TopoDS_Shape>()));
157     }
158     theResultBody->generated(aTopFace, _FIRST_TAG);
159   }
160   */
161
162 }