1 // File: FeaturesPlugin_Extrusion.cpp
2 // Created: 30 May 2014
3 // Author: Vitaly SMETANNIKOV
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>
21 #define _LATERAL_TAG 1
28 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
32 void FeaturesPlugin_Extrusion::initAttributes()
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());
39 void FeaturesPlugin_Extrusion::execute()
41 boost::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = boost::dynamic_pointer_cast<
42 ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Extrusion::FACE_ID()));
46 boost::shared_ptr<GeomAPI_Shape> aFace =
47 boost::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
51 boost::shared_ptr<GeomAPI_Shape> aContext =
52 boost::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->context());
54 double aSize = data()->real(FeaturesPlugin_Extrusion::SIZE_ID())->value();
55 if (data()->boolean(FeaturesPlugin_Extrusion::REVERSE_ID())->value())
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);
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);
72 std::cerr << aShapeError << std::endl;
76 if(!aFeature.isValid()) {
77 std::string aFeatureError = "Warning: resulting shape is not valid";
78 Events_Error::send(aFeatureError, this);
82 LoadNamingDS(aFeature, aResultBody, aFace, aContext);
84 setResult(aResultBody);
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)
96 if(theBasis->impl<TopoDS_Shape>().IsEqual(theContext->impl<TopoDS_Shape>()))
97 theResultBody->store(theFeature.shape());
99 theResultBody->storeGenerated(theContext, theFeature.shape());
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());
106 //Insert lateral face : Face from Edge
107 //GeomAlgoAPI_DFLoader::loadAndOrientGeneratedShapes(*myBuilder, myBasis, TopAbs_EDGE, aLateralFaceBuilder, aSubShapes);
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());
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);
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>()));
139 theResultBody->generated(aBottomFace, _FIRST_TAG);
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>()));
150 theResultBody->generated(aTopFace, _FIRST_TAG);