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());
49 boost::shared_ptr<GeomAPI_Shape> aContext =
50 boost::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->context());
52 double aSize = data()->real(FeaturesPlugin_Extrusion::SIZE_ID())->value();
53 if (data()->boolean(FeaturesPlugin_Extrusion::REVERSE_ID())->value())
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);
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);
70 std::cerr << aShapeError << std::endl;
74 if(!aFeature.isValid()) {
75 std::string aFeatureError = "Warning: resulting shape is not valid";
76 Events_Error::send(aFeatureError, this);
80 LoadNamingDS(aFeature, aResultBody, aFace, aContext);
82 setResult(aResultBody);
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)
94 if(theBasis->impl<TopoDS_Shape>().IsEqual(theContext->impl<TopoDS_Shape>()))
95 theResultBody->store(theFeature.shape());
97 theResultBody->storeGenerated(theContext, theFeature.shape());
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());
104 //Insert lateral face : Face from Edge
105 //GeomAlgoAPI_DFLoader::loadAndOrientGeneratedShapes(*myBuilder, myBasis, TopAbs_EDGE, aLateralFaceBuilder, aSubShapes);
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());
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);
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>()));
137 theResultBody->generated(aBottomFace, _FIRST_TAG);
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>()));
148 theResultBody->generated(aTopFace, _FIRST_TAG);