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>
18 #define _LATERAL_TAG 1
26 FeaturesPlugin_Extrusion::FeaturesPlugin_Extrusion()
30 void FeaturesPlugin_Extrusion::initAttributes()
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());
37 void FeaturesPlugin_Extrusion::execute()
39 boost::shared_ptr<ModelAPI_AttributeSelection> aFaceRef = boost::dynamic_pointer_cast<
40 ModelAPI_AttributeSelection>(data()->attribute(FeaturesPlugin_Extrusion::FACE_ID()));
44 boost::shared_ptr<GeomAPI_Shape> aFace =
45 boost::dynamic_pointer_cast<GeomAPI_Shape>(aFaceRef->value());
49 boost::shared_ptr<GeomAPI_Shape> aContext;
50 ResultPtr aContextRes = aFaceRef->context();
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();
58 std::string aContextError = "The selection context is bad";
59 Events_Error::send(aContextError, this);
63 double aSize = data()->real(FeaturesPlugin_Extrusion::SIZE_ID())->value();
64 if (data()->boolean(FeaturesPlugin_Extrusion::REVERSE_ID())->value())
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);
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);
81 std::cerr << aShapeError << std::endl;
85 if(!aFeature.isValid()) {
86 std::string aFeatureError = "Warning: resulting shape is not valid";
87 Events_Error::send(aFeatureError, this);
91 LoadNamingDS(aFeature, aResultBody, aFace, aContext);
93 setResult(aResultBody);
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)
105 if(theBasis->isEqual(theContext))
106 theResultBody->store(theFeature.shape());
108 theResultBody->storeGenerated(theContext, theFeature.shape());
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());
115 //Insert lateral face : Face from Edge
116 //GeomAlgoAPI_DFLoader::loadAndOrientGeneratedShapes(*myBuilder, myBasis, TopAbs_EDGE, aLateralFaceBuilder, aSubShapes);
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());
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);
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>()));
147 theResultBody->generated(aBottomFace, _FIRST_TAG);
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>()));
158 theResultBody->generated(aTopFace, _FIRST_TAG);