FeaturePocket.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2010 Juergen Riegel <FreeCAD@juergen-riegel.net>        *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
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     // this is a workaround for an obscure OCC bug which leads to empty tessellations
00084     // for some faces. Making an explicit copy of the linked shape seems to fix it.
00085     // The error almost happens when re-computing the shape but sometimes also for the
00086     // first time
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()) // there can be several wires
00098         return new App::DocumentObjectExecReturn("Linked shape object is not a wire");
00099 
00100     // get the Sketch plane
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     // get the support of the Sketch if any
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     // This is a trick to avoid problems with the cut operation. Sometimes a cut doesn't
00120     // work as expected if faces or coincident. Thus, we move the face in normal direction
00121     // but make it longer by one unit in the opposite direction.
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     // lengthen the vector
00128     SketchVector *= (Length.getValue()+1);
00129 
00130     // turn around for pockets
00131     SketchVector *= -1;
00132 
00133     // extrude the face to a solid
00134     gp_Vec vec(SketchVector.x,SketchVector.y,SketchVector.z);
00135     BRepPrimAPI_MakePrism PrismMaker(aFace,vec,0,1);
00136     if (PrismMaker.IsDone()) {
00137         // if the sketch has a support fuse them to get one result object (PAD!)
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             // Let's call algorithm computing a fuse operation:
00146             BRepAlgoAPI_Cut mkCut(support, PrismMaker.Shape());
00147             // Let's check if the fusion has been successful
00148             if (!mkCut.IsDone()) 
00149                 return new App::DocumentObjectExecReturn("Cut with support failed");
00150 
00151             // we have to get the solids (fuse create seldomly compounds)
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 

Generated on Wed Nov 23 19:00:13 2011 for FreeCAD by  doxygen 1.6.1