FeaturePocket.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <Bnd_Box.hxx>
00027 # include <gp_Dir.hxx>
00028 # include <gp_Pln.hxx>
00029 # include <BRep_Builder.hxx>
00030 # include <BRepAdaptor_Surface.hxx>
00031 # include <BRepBndLib.hxx>
00032 # include <BRepPrimAPI_MakePrism.hxx>
00033 # include <BRepBuilderAPI_Copy.hxx>
00034 # include <BRepBuilderAPI_MakeFace.hxx>
00035 # include <Geom_Plane.hxx>
00036 # include <Handle_Geom_Surface.hxx>
00037 # include <TopoDS.hxx>
00038 # include <TopoDS_Face.hxx>
00039 # include <TopoDS_Wire.hxx>
00040 # include <TopoDS_Solid.hxx>
00041 # include <TopExp_Explorer.hxx>
00042 # include <BRepAlgoAPI_Cut.hxx>
00043 #endif
00044
00045 #include <Base/Placement.h>
00046 #include <Mod/Part/App/Part2DObject.h>
00047
00048 #include "FeaturePocket.h"
00049
00050
00051 using namespace PartDesign;
00052
00053 const char* Pocket::TypeEnums[]= {"Length","UpToLast","UpToFirst",NULL};
00054
00055 PROPERTY_SOURCE(PartDesign::Pocket, PartDesign::SketchBased)
00056
00057 Pocket::Pocket()
00058 {
00059 ADD_PROPERTY(Type,((long)0));
00060 Type.setEnums(TypeEnums);
00061 ADD_PROPERTY(Length,(100.0));
00062 }
00063
00064 short Pocket::mustExecute() const
00065 {
00066 if (Sketch.isTouched() ||
00067 Length.isTouched())
00068 return 1;
00069 return 0;
00070 }
00071
00072 App::DocumentObjectExecReturn *Pocket::execute(void)
00073 {
00074 App::DocumentObject* link = Sketch.getValue();
00075 if (!link)
00076 return new App::DocumentObjectExecReturn("No sketch linked");
00077 if (!link->getTypeId().isDerivedFrom(Part::Part2DObject::getClassTypeId()))
00078 return new App::DocumentObjectExecReturn("Linked object is not a Sketch or Part2DObject");
00079 TopoDS_Shape shape = static_cast<Part::Part2DObject*>(link)->Shape.getShape()._Shape;
00080 if (shape.IsNull())
00081 return new App::DocumentObjectExecReturn("Linked shape object is empty");
00082
00083
00084
00085
00086
00087 BRepBuilderAPI_Copy copy(shape);
00088 shape = copy.Shape();
00089 if (shape.IsNull())
00090 return new App::DocumentObjectExecReturn("Linked shape object is empty");
00091
00092 TopExp_Explorer ex;
00093 std::vector<TopoDS_Wire> wires;
00094 for (ex.Init(shape, TopAbs_WIRE); ex.More(); ex.Next()) {
00095 wires.push_back(TopoDS::Wire(ex.Current()));
00096 }
00097 if (wires.empty())
00098 return new App::DocumentObjectExecReturn("Linked shape object is not a wire");
00099
00100
00101 Base::Placement SketchPos = static_cast<Part::Part2DObject*>(link)->Placement.getValue();
00102 Base::Rotation SketchOrientation = SketchPos.getRotation();
00103 Base::Vector3d SketchVector(0,0,1);
00104 SketchOrientation.multVec(SketchVector,SketchVector);
00105
00106
00107 App::DocumentObject* SupportLink = static_cast<Part::Part2DObject*>(link)->Support.getValue();
00108 Part::Feature *SupportObject = 0;
00109 if (SupportLink && SupportLink->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()))
00110 SupportObject = static_cast<Part::Feature*>(SupportLink);
00111
00112 if (!SupportObject)
00113 return new App::DocumentObjectExecReturn("No support in Sketch!");
00114
00115 TopoDS_Shape aFace = makeFace(wires);
00116 if (aFace.IsNull())
00117 return new App::DocumentObjectExecReturn("Creating a face from sketch failed");
00118
00119
00120
00121
00122 gp_Trsf mov;
00123 mov.SetTranslation(gp_Vec(SketchVector.x,SketchVector.y,SketchVector.z));
00124 TopLoc_Location loc(mov);
00125 aFace.Move(loc);
00126
00127
00128 SketchVector *= (Length.getValue()+1);
00129
00130
00131 SketchVector *= -1;
00132
00133
00134 gp_Vec vec(SketchVector.x,SketchVector.y,SketchVector.z);
00135 BRepPrimAPI_MakePrism PrismMaker(aFace,vec,0,1);
00136 if (PrismMaker.IsDone()) {
00137
00138 if (SupportObject) {
00139 const TopoDS_Shape& support = SupportObject->Shape.getValue();
00140 if (support.IsNull())
00141 return new App::DocumentObjectExecReturn("Support shape is invalid");
00142 TopExp_Explorer xp (support, TopAbs_SOLID);
00143 if (!xp.More())
00144 return new App::DocumentObjectExecReturn("Support shape is not a solid");
00145
00146 BRepAlgoAPI_Cut mkCut(support, PrismMaker.Shape());
00147
00148 if (!mkCut.IsDone())
00149 return new App::DocumentObjectExecReturn("Cut with support failed");
00150
00151
00152 TopoDS_Shape solRes = this->getSolid(mkCut.Shape());
00153 if (solRes.IsNull())
00154 return new App::DocumentObjectExecReturn("Resulting shape is not a solid");
00155 this->Shape.setValue(solRes);
00156 }
00157 else {
00158 return new App::DocumentObjectExecReturn("Cannot create a tool out of sketch with no support");
00159 }
00160 }
00161 else
00162 return new App::DocumentObjectExecReturn("Could not extrude the sketch!");
00163
00164 return App::DocumentObject::StdReturn;
00165 }
00166