FeaturePad.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
00027
00028 # include <BRep_Builder.hxx>
00029 # include <BRepBndLib.hxx>
00030 # include <BRepPrimAPI_MakePrism.hxx>
00031 # include <BRepBuilderAPI_Copy.hxx>
00032 # include <BRepBuilderAPI_MakeFace.hxx>
00033
00034 # include <Handle_Geom_Surface.hxx>
00035 # include <TopoDS.hxx>
00036 # include <TopoDS_Solid.hxx>
00037 # include <TopoDS_Face.hxx>
00038 # include <TopoDS_Wire.hxx>
00039 # include <TopExp_Explorer.hxx>
00040 # include <BRepAlgoAPI_Fuse.hxx>
00041 #endif
00042
00043 #include <Base/Placement.h>
00044 #include <Mod/Part/App/Part2DObject.h>
00045
00046 #include "FeaturePad.h"
00047
00048
00049 using namespace PartDesign;
00050
00051 const char* Pad::SideEnums[]= {"Positive","Negative",NULL};
00052
00053 PROPERTY_SOURCE(PartDesign::Pad, PartDesign::Additive)
00054
00055 Pad::Pad()
00056 {
00057
00058
00059 ADD_PROPERTY(Length,(100.0));
00060 ADD_PROPERTY(Reversed,(0));
00061 ADD_PROPERTY(MirroredExtent,(0));
00062
00063 }
00064
00065 short Pad::mustExecute() const
00066 {
00067 if (Sketch.isTouched() ||
00068 Length.isTouched() ||
00069 MirroredExtent.isTouched() ||
00070 Reversed.isTouched())
00071 return 1;
00072 return 0;
00073 }
00074
00075 App::DocumentObjectExecReturn *Pad::execute(void)
00076 {
00077 App::DocumentObject* link = Sketch.getValue();
00078 if (!link)
00079 return new App::DocumentObjectExecReturn("No sketch linked");
00080 if (!link->getTypeId().isDerivedFrom(Part::Part2DObject::getClassTypeId()))
00081 return new App::DocumentObjectExecReturn("Linked object is not a Sketch or Part2DObject");
00082 TopoDS_Shape shape = static_cast<Part::Part2DObject*>(link)->Shape.getShape()._Shape;
00083 if (shape.IsNull())
00084 return new App::DocumentObjectExecReturn("Linked shape object is empty");
00085
00086
00087
00088
00089
00090 BRepBuilderAPI_Copy copy(shape);
00091 shape = copy.Shape();
00092 if (shape.IsNull())
00093 return new App::DocumentObjectExecReturn("Linked shape object is empty");
00094
00095 TopExp_Explorer ex;
00096 std::vector<TopoDS_Wire> wires;
00097 for (ex.Init(shape, TopAbs_WIRE); ex.More(); ex.Next()) {
00098 wires.push_back(TopoDS::Wire(ex.Current()));
00099 }
00100 if (wires.empty())
00101 return new App::DocumentObjectExecReturn("Linked shape object is not a wire");
00102
00103
00104 Base::Placement SketchPos = static_cast<Part::Part2DObject*>(link)->Placement.getValue();
00105 Base::Rotation SketchOrientation = SketchPos.getRotation();
00106 Base::Vector3d SketchOrientationVector(0,0,1);
00107 if (Reversed.getValue())
00108 SketchOrientationVector *= -1;
00109 SketchOrientation.multVec(SketchOrientationVector,SketchOrientationVector);
00110
00111
00112 App::DocumentObject* SupportLink = static_cast<Part::Part2DObject*>(link)->Support.getValue();
00113 Part::Feature *SupportObject = 0;
00114 if (SupportLink && SupportLink->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()))
00115 SupportObject = static_cast<Part::Feature*>(SupportLink);
00116
00117
00118 TopoDS_Shape aFace = makeFace(wires);
00119 if (aFace.IsNull())
00120 return new App::DocumentObjectExecReturn("Creating a face from sketch failed");
00121
00122
00123 SketchOrientationVector *= Length.getValue();
00124
00125 try {
00126
00127 gp_Vec vec(SketchOrientationVector.x,SketchOrientationVector.y,SketchOrientationVector.z);
00128 BRepPrimAPI_MakePrism PrismMaker(aFace,vec,0,1);
00129 if (PrismMaker.IsDone()) {
00130
00131 if (SupportObject) {
00132
00133 TopoDS_Shape result = PrismMaker.Shape();
00134
00135 this->AddShape.setValue(result);
00136
00137 const TopoDS_Shape& support = SupportObject->Shape.getValue();
00138 bool isSolid = false;
00139 if (!support.IsNull()) {
00140 TopExp_Explorer xp;
00141 xp.Init(support,TopAbs_SOLID);
00142 for (;xp.More(); xp.Next()) {
00143 isSolid = true;
00144 break;
00145 }
00146 }
00147 if (isSolid) {
00148
00149 BRepAlgoAPI_Fuse mkFuse(support, result);
00150
00151 if (!mkFuse.IsDone())
00152 return new App::DocumentObjectExecReturn("Fusion with support failed");
00153 result = mkFuse.Shape();
00154
00155 TopoDS_Shape solRes = this->getSolid(result);
00156
00157 if (solRes.IsNull())
00158 return new App::DocumentObjectExecReturn("Resulting shape is not a solid");
00159 this->Shape.setValue(solRes);
00160 }
00161 else
00162 return new App::DocumentObjectExecReturn("Support is not a solid");
00163 }
00164 else {
00165 TopoDS_Shape result = this->getSolid(PrismMaker.Shape());
00166
00167 this->AddShape.setValue(result);
00168 this->Shape.setValue(result);
00169 }
00170 }
00171 else
00172 return new App::DocumentObjectExecReturn("Could not extrude the sketch!");
00173
00174 return App::DocumentObject::StdReturn;
00175 }
00176 catch (Standard_Failure) {
00177 Handle_Standard_Failure e = Standard_Failure::Caught();
00178 return new App::DocumentObjectExecReturn(e->GetMessageString());
00179 }
00180 }
00181