FeaturePad.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_Pln.hxx>
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 //# include <Geom_Plane.hxx>
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     //ADD_PROPERTY(Side,((long)0));
00058     //Side.setEnums(SideEnums);
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     // this is a workaround for an obscure OCC bug which leads to empty tessellations
00087     // for some faces. Making an explicit copy of the linked shape seems to fix it.
00088     // The error almost happens when re-computing the shape but sometimes also for the
00089     // first time
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 (/*shape.ShapeType() != TopAbs_WIRE*/wires.empty()) // there can be several wires
00101         return new App::DocumentObjectExecReturn("Linked shape object is not a wire");
00102 
00103     // get the Sketch plane
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()) // negative direction
00108         SketchOrientationVector *= -1;
00109     SketchOrientation.multVec(SketchOrientationVector,SketchOrientationVector);
00110 
00111     // get the support of the Sketch if any
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     // lengthen the vector
00123     SketchOrientationVector *= Length.getValue();
00124 
00125     try {
00126         // extrude the face to a solid
00127         gp_Vec vec(SketchOrientationVector.x,SketchOrientationVector.y,SketchOrientationVector.z);
00128         BRepPrimAPI_MakePrism PrismMaker(aFace,vec,0,1);
00129         if (PrismMaker.IsDone()) {
00130             // if the sketch has a support fuse them to get one result object (PAD!)
00131             if (SupportObject) {
00132                 // At this point the prism can be a compound
00133                 TopoDS_Shape result = PrismMaker.Shape();
00134                 // set the additive shape property for later usage in e.g. pattern
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                     // Let's call algorithm computing a fuse operation:
00149                     BRepAlgoAPI_Fuse mkFuse(support, result);
00150                     // Let's check if the fusion has been successful
00151                     if (!mkFuse.IsDone()) 
00152                         return new App::DocumentObjectExecReturn("Fusion with support failed");
00153                     result = mkFuse.Shape();
00154                     // we have to get the solids (fuse create seldomly compounds)
00155                     TopoDS_Shape solRes = this->getSolid(result);
00156                     // lets check if the result is a solid
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                 // set the additive shape property for later usage in e.g. pattern
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 

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