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