]> SALOME platform Git repositories - modules/shaper.git/blob - src/FeaturesPlugin/FeaturesPlugin_Extrusion.cpp
Salome HOME
27.10.2014. Naming data structure for Extrusion feature.
[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
49   boost::shared_ptr<GeomAPI_Shape> aContext = 
50     boost::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->context());
51
52   double aSize = data()->real(FeaturesPlugin_Extrusion::SIZE_ID())->value();
53   if (data()->boolean(FeaturesPlugin_Extrusion::REVERSE_ID())->value())
54     aSize = -aSize;
55
56   boost::shared_ptr<ModelAPI_ResultBody> aResultBody = document()->createBody(data());
57   //TCollection_AsciiString anError;
58   GeomAlgoAPI_Extrusion aFeature(aFace, aSize);
59   if(!aFeature.isDone()) {
60         std::string aFeatureError = "Extrusion algorithm failed";  
61     Events_Error::send(aFeatureError, this);
62         return;
63   }
64
65    // Check if shape is valid
66   if (!aFeature.shape()->impl<TopoDS_Shape>().IsNull()) {
67     std::string aShapeError = "Resulting shape is Null";     
68     Events_Error::send(aShapeError, this);
69  #ifdef _DEBUG
70     std::cerr << aShapeError << std::endl;
71  #endif
72     return;
73   }
74    if(!aFeature.isValid()) {
75         std::string aFeatureError = "Warning: resulting shape is not valid";  
76     Events_Error::send(aFeatureError, this);
77         return;
78   }  
79   //LoadNamingDS
80   LoadNamingDS(aFeature, aResultBody, aFace, aContext);
81
82   setResult(aResultBody);
83 }
84
85   //============================================================================
86 void FeaturesPlugin_Extrusion::LoadNamingDS(GeomAlgoAPI_Extrusion& theFeature, 
87                                      boost::shared_ptr<ModelAPI_ResultBody> theResultBody, 
88                                                                  boost::shared_ptr<GeomAPI_Shape> theBasis,
89                                      boost::shared_ptr<GeomAPI_Shape> theContext)
90 {  
91
92         
93     //load result
94         if(theBasis->impl<TopoDS_Shape>().IsEqual(theContext->impl<TopoDS_Shape>()))
95           theResultBody->store(theFeature.shape());
96         else
97           theResultBody->storeGenerated(theContext, theFeature.shape());
98
99         TopTools_DataMapOfShapeShape aSubShapes;
100         for (TopExp_Explorer Exp(theFeature.shape()->impl<TopoDS_Shape>(),TopAbs_FACE); Exp.More(); Exp.Next()) {
101           aSubShapes.Bind(Exp.Current(),Exp.Current());
102         }
103
104         //Insert lateral face : Face from Edge
105         //GeomAlgoAPI_DFLoader::loadAndOrientGeneratedShapes(*myBuilder, myBasis, TopAbs_EDGE, aLateralFaceBuilder, aSubShapes);
106
107
108   TopTools_MapOfShape aView;
109   TopExp_Explorer aShapeExplorer (theFeature.shape()->impl<TopoDS_Shape>(), TopAbs_EDGE);
110   for (; aShapeExplorer.More(); aShapeExplorer.Next ()) {
111     const TopoDS_Shape& aRoot = aShapeExplorer.Current ();
112     if (!aView.Add(aRoot)) continue;
113         boost::shared_ptr<GeomAPI_Shape> aRootG(new GeomAPI_Shape());
114         aRootG->setImpl((void *)&aRoot);
115         const ListOfShape& aShapes = theFeature.generated(aRootG);
116         std::list<boost::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = aShapes.begin(), aLast = aShapes.end();       
117         for (; anIt != aLast; anIt++) {
118       TopoDS_Shape aNewShape = (*anIt)->impl<TopoDS_Shape>(); 
119       if (aSubShapes.IsBound(aNewShape)) {
120         aNewShape.Orientation((aSubShapes(aNewShape)).Orientation());
121       }
122
123           if (!aRoot.IsSame (aNewShape)) {
124                 boost::shared_ptr<GeomAPI_Shape> aNew(new GeomAPI_Shape());
125             aNew->setImpl((void *)&aNewShape);
126                   theResultBody->generated(aRootG, aNew,_LATERAL_TAG); 
127           }
128         }
129   }
130
131   //Insert bottom face
132   const boost::shared_ptr<GeomAPI_Shape>& aBottomFace = theFeature.firstShape();
133   if (!aBottomFace->impl<TopoDS_Shape>().IsNull()) {
134           if (aSubShapes.IsBound(aBottomFace->impl<TopoDS_Shape>())) {
135                 aBottomFace->setImpl((void *)&aSubShapes(aBottomFace->impl<TopoDS_Shape>()));
136           }    
137   theResultBody->generated(aBottomFace, _FIRST_TAG);
138   }
139
140   
141
142  //Insert top face
143   boost::shared_ptr<GeomAPI_Shape> aTopFace = theFeature.lastShape();
144   if (!aTopFace->impl<TopoDS_Shape>().IsNull()) {
145         if (aSubShapes.IsBound(aTopFace->impl<TopoDS_Shape>())) {
146                 aTopFace->setImpl((void *)&aSubShapes(aTopFace->impl<TopoDS_Shape>()));
147         }
148     theResultBody->generated(aTopFace, _FIRST_TAG);
149   }
150
151 }