ProjectionAlgos.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2002     *
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 
00026 #ifndef _PreComp_
00027 # include <sstream>
00028 # include <BRepAdaptor_Curve.hxx>
00029 # include <Geom_Circle.hxx>
00030 # include <gp_Circ.hxx>
00031 # include <gp_Elips.hxx>
00032 #endif
00033 
00034 #include <Bnd_Box.hxx>
00035 #include <BRepBndLib.hxx>
00036 #include <BRepBuilderAPI_Transform.hxx>
00037 #include <HLRBRep_Algo.hxx>
00038 #include <TopoDS_Shape.hxx>
00039 #include <HLRTopoBRep_OutLiner.hxx>
00040 //#include <BRepAPI_MakeOutLine.hxx>
00041 #include <HLRAlgo_Projector.hxx>
00042 #include <HLRBRep_ShapeBounds.hxx>
00043 #include <HLRBRep_HLRToShape.hxx>
00044 #include <gp_Ax2.hxx>
00045 #include <gp_Pnt.hxx>
00046 #include <gp_Dir.hxx>
00047 #include <gp_Vec.hxx>
00048 #include <Poly_Polygon3D.hxx>
00049 #include <Poly_Triangulation.hxx>
00050 #include <Poly_PolygonOnTriangulation.hxx>
00051 #include <TopoDS.hxx>
00052 #include <TopoDS_Face.hxx>
00053 #include <TopoDS_Edge.hxx>
00054 #include <TopoDS_Vertex.hxx>
00055 #include <TopExp.hxx>
00056 #include <TopExp_Explorer.hxx>
00057 #include <TopTools_IndexedMapOfShape.hxx>
00058 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
00059 #include <TopTools_ListOfShape.hxx>
00060 #include <TColgp_Array1OfPnt2d.hxx>
00061 #include <BRep_Tool.hxx>
00062 #include <BRepMesh.hxx>
00063 
00064 #include <BRepAdaptor_CompCurve.hxx>
00065 #include <Handle_BRepAdaptor_HCompCurve.hxx>
00066 #include <Approx_Curve3d.hxx>
00067 #include <BRepAdaptor_HCurve.hxx>
00068 #include <Handle_BRepAdaptor_HCurve.hxx>
00069 #include <Geom_BSplineCurve.hxx>
00070 #include <Handle_Geom_BSplineCurve.hxx>
00071 #include <Geom_BezierCurve.hxx>
00072 #include <GeomConvert_BSplineCurveToBezierCurve.hxx>
00073 #include <GeomConvert_BSplineCurveKnotSplitting.hxx>
00074 #include <Geom2d_BSplineCurve.hxx>
00075 
00076 #include <Base/Exception.h>
00077 #include <Base/FileInfo.h>
00078 #include <Base/Tools.h>
00079 #include <Mod/Part/App/PartFeature.h>
00080 
00081 #include "ProjectionAlgos.h"
00082 #include "DrawingExport.h"
00083 
00084 using namespace Drawing;
00085 using namespace std;
00086 
00087 //===========================================================================
00088 // ProjectionAlgos
00089 //===========================================================================
00090 
00091 
00092 
00093 ProjectionAlgos::ProjectionAlgos(const TopoDS_Shape &Input, const Base::Vector3f &Dir) 
00094   : Input(Input), Direction(Dir)
00095 {
00096     execute();
00097 }
00098 
00099 ProjectionAlgos::~ProjectionAlgos()
00100 {
00101 }
00102 
00103 TopoDS_Shape ProjectionAlgos::invertY(const TopoDS_Shape& shape)
00104 {
00105     // make sure to have the y coordinates inverted
00106     gp_Trsf mat;
00107     Bnd_Box bounds;
00108     BRepBndLib::Add(shape, bounds);
00109     bounds.SetGap(0.0);
00110     Standard_Real xMin, yMin, zMin, xMax, yMax, zMax;
00111     bounds.Get(xMin, yMin, zMin, xMax, yMax, zMax);
00112     mat.SetMirror(gp_Ax2(gp_Pnt((xMin+xMax)/2,(yMin+yMax)/2,(zMin+zMax)/2), gp_Dir(0,1,0)));
00113     BRepBuilderAPI_Transform mkTrf(shape, mat);
00114     return mkTrf.Shape();
00115 }
00116 
00117 void ProjectionAlgos::execute(void)
00118 {
00119     Handle( HLRBRep_Algo ) brep_hlr = new HLRBRep_Algo;
00120     brep_hlr->Add(Input);
00121 
00122     try {
00123 #if defined(__GNUC__) && defined (FC_OS_LINUX)
00124         Base::SignalException se;
00125 #endif
00126         gp_Ax2 transform(gp_Pnt(0,0,0),gp_Dir(Direction.x,Direction.y,Direction.z));
00127         HLRAlgo_Projector projector( transform );
00128         brep_hlr->Projector(projector);
00129         brep_hlr->Update();
00130         brep_hlr->Hide();
00131     }
00132     catch (...) {
00133         Standard_Failure::Raise("Fatal error occurred while projecting shape");
00134     }
00135 
00136     // extracting the result sets:
00137     HLRBRep_HLRToShape shapes( brep_hlr );
00138 
00139     V  = shapes.VCompound       ();// hard edge visibly
00140     V1 = shapes.Rg1LineVCompound();// Smoth edges visibly
00141     VN = shapes.RgNLineVCompound();// contour edges visibly
00142     VO = shapes.OutLineVCompound();// contours apparents visibly
00143     VI = shapes.IsoLineVCompound();// isoparamtriques   visibly
00144     H  = shapes.HCompound       ();// hard edge       invisibly
00145     H1 = shapes.Rg1LineHCompound();// Smoth edges  invisibly
00146     HN = shapes.RgNLineHCompound();// contour edges invisibly
00147     HO = shapes.OutLineHCompound();// contours apparents invisibly
00148     HI = shapes.IsoLineHCompound();// isoparamtriques   invisibly
00149 
00150 }
00151 
00152 std::string ProjectionAlgos::getSVG(SvgExtractionType type, float scale)
00153 {
00154     std::stringstream result;
00155     SVGOutput output;
00156     if (!H.IsNull() && (type & WithHidden)) {
00157         float width = 0.15f/scale;
00158         BRepMesh::Mesh(H,0.1);
00159         result  << "<g" 
00160                 //<< " id=\"" << ViewName << "\"" << endl
00161                 << "   stroke=\"rgb(0, 0, 0)\"" << endl 
00162                 << "   stroke-width=\"" << width << "\"" << endl
00163                 << "   stroke-linecap=\"butt\"" << endl
00164                 << "   stroke-linejoin=\"miter\"" << endl
00165                 << "   stroke-dasharray=\"5 3\"" << endl
00166                 << "   fill=\"none\"" << endl
00167                 << "  >" << endl
00168                 << output.exportEdges(H)
00169                 << "</g>" << endl;
00170     }
00171     if (!HO.IsNull() && (type & WithHidden)) {
00172         float width = 0.15f/scale;
00173         BRepMesh::Mesh(HO,0.1);
00174         result  << "<g" 
00175                 //<< " id=\"" << ViewName << "\"" << endl
00176                 << "   stroke=\"rgb(0, 0, 0)\"" << endl 
00177                 << "   stroke-width=\"" << width << "\"" << endl
00178                 << "   stroke-linecap=\"butt\"" << endl
00179                 << "   stroke-linejoin=\"miter\"" << endl
00180                 << "   stroke-dasharray=\"5 3\"" << endl
00181                 << "   fill=\"none\"" << endl
00182                 << "  >" << endl
00183                 << output.exportEdges(HO)
00184                 << "</g>" << endl;
00185     }
00186     if (!VO.IsNull()) {
00187         float width = 0.35f/scale;
00188         BRepMesh::Mesh(VO,0.1);
00189         result  << "<g" 
00190                 //<< " id=\"" << ViewName << "\"" << endl
00191                 << "   stroke=\"rgb(0, 0, 0)\"" << endl 
00192                 << "   stroke-width=\"" << width << "\"" << endl
00193                 << "   stroke-linecap=\"butt\"" << endl
00194                 << "   stroke-linejoin=\"miter\"" << endl
00195                 << "   fill=\"none\"" << endl
00196                 << "  >" << endl
00197                 << output.exportEdges(VO)
00198                 << "</g>" << endl;
00199     }
00200     if (!V.IsNull()) {
00201         float width = 0.35f/scale;
00202         BRepMesh::Mesh(V,0.1);
00203         result  << "<g" 
00204                 //<< " id=\"" << ViewName << "\"" << endl
00205                 << "   stroke=\"rgb(0, 0, 0)\"" << endl 
00206                 << "   stroke-width=\"" << width << "\"" << endl
00207                 << "   stroke-linecap=\"butt\"" << endl
00208                 << "   stroke-linejoin=\"miter\"" << endl
00209                 << "   fill=\"none\"" << endl
00210                 << "  >" << endl
00211                 << output.exportEdges(V)
00212                 << "</g>" << endl;
00213     }
00214     if (!V1.IsNull() && (type & WithSmooth)) {
00215         float width = 0.35f/scale;
00216         BRepMesh::Mesh(V1,0.1);
00217         result  << "<g" 
00218                 //<< " id=\"" << ViewName << "\"" << endl
00219                 << "   stroke=\"rgb(0, 0, 0)\"" << endl 
00220                 << "   stroke-width=\"" << width << "\"" << endl
00221                 << "   stroke-linecap=\"butt\"" << endl
00222                 << "   stroke-linejoin=\"miter\"" << endl
00223                 << "   fill=\"none\"" << endl
00224                 << "  >" << endl
00225                 << output.exportEdges(V1)
00226                 << "</g>" << endl;
00227     }
00228     if (!H1.IsNull() && (type & WithSmooth) && (type & WithHidden)) {
00229         float width = 0.15f/scale;
00230         BRepMesh::Mesh(H1,0.1);
00231         result  << "<g" 
00232                 //<< " id=\"" << ViewName << "\"" << endl
00233                 << "   stroke=\"rgb(0, 0, 0)\"" << endl 
00234                 << "   stroke-width=\"" << width << "\"" << endl
00235                 << "   stroke-linecap=\"butt\"" << endl
00236                 << "   stroke-linejoin=\"miter\"" << endl
00237                 << "   stroke-dasharray=\"5 3\"" << endl
00238                 << "   fill=\"none\"" << endl
00239                 << "  >" << endl
00240                 << output.exportEdges(H1)
00241                 << "</g>" << endl;
00242     }
00243         /*result << "0"          << endl
00244         << "SECTION"  << endl
00245         << "2"          << endl
00246         << "ENTITIES" << endl;*/
00247     return result.str();
00248 }
00249 
00250 /* dxf output section - Dan Falck 2011/09/25  */
00251 
00252 std::string ProjectionAlgos::getDXF(SvgExtractionType type, float scale)
00253 {
00254     std::stringstream result;
00255     DXFOutput output;
00256     
00257     result << "0"          << endl
00258         << "SECTION"  << endl
00259 
00260         << "2"          << endl
00261         << "ENTITIES" << endl;
00262 
00263     if (!H.IsNull() && (type & WithHidden)) {
00264         float width = 0.15f/scale;
00265         BRepMesh::Mesh(H,0.1);
00266         result  //<< "<g" 
00267                 //<< " id=\"" << ViewName << "\"" << endl
00268                 /*<< "   stroke=\"rgb(0, 0, 0)\"" << endl 
00269                 << "   stroke-width=\"" << width << "\"" << endl
00270                 << "   stroke-linecap=\"butt\"" << endl
00271                 << "   stroke-linejoin=\"miter\"" << endl
00272                 << "   stroke-dasharray=\"5 3\"" << endl
00273                 << "   fill=\"none\"" << endl
00274                 << "  >" << endl*/
00275                 << output.exportEdges(H);
00276                 //<< "</g>" << endl;
00277     }
00278     if (!HO.IsNull() && (type & WithHidden)) {
00279         float width = 0.15f/scale;
00280         BRepMesh::Mesh(HO,0.1);
00281         result  //<< "<g" 
00282                 //<< " id=\"" << ViewName << "\"" << endl
00283                 /*<< "   stroke=\"rgb(0, 0, 0)\"" << endl 
00284                 << "   stroke-width=\"" << width << "\"" << endl
00285                 << "   stroke-linecap=\"butt\"" << endl
00286                 << "   stroke-linejoin=\"miter\"" << endl
00287                 << "   stroke-dasharray=\"5 3\"" << endl
00288                 << "   fill=\"none\"" << endl
00289                 << "  >" << endl*/
00290                 << output.exportEdges(HO);
00291                 //<< "</g>" << endl;
00292     }
00293     if (!VO.IsNull()) {
00294         float width = 0.35f/scale;
00295         BRepMesh::Mesh(VO,0.1);
00296         result  //<< "<g" 
00297                 //<< " id=\"" << ViewName << "\"" << endl
00298 
00299                 /*<< "   stroke=\"rgb(0, 0, 0)\"" << endl 
00300                 << "   stroke-width=\"" << width << "\"" << endl
00301                 << "   stroke-linecap=\"butt\"" << endl
00302                 << "   stroke-linejoin=\"miter\"" << endl
00303                 << "   fill=\"none\"" << endl
00304                 << "  >" << endl*/
00305 
00306                 << output.exportEdges(VO);
00307                 //<< "</g>" << endl;
00308     }
00309     if (!V.IsNull()) {
00310         float width = 0.35f/scale;
00311 
00312         BRepMesh::Mesh(V,0.1);
00313         result  //<< "<g" 
00314                 //<< " id=\"" << ViewName << "\"" << endl
00315                 /*<< "   stroke=\"rgb(0, 0, 0)\"" << endl 
00316                 << "   stroke-width=\"" << width << "\"" << endl
00317                 << "   stroke-linecap=\"butt\"" << endl
00318 
00319                 << "   stroke-linejoin=\"miter\"" << endl
00320                 << "   fill=\"none\"" << endl
00321                 << "  >" << endl*/
00322                 << output.exportEdges(V);
00323                 //<< "</g>" << endl;
00324 
00325     }
00326     if (!V1.IsNull() && (type & WithSmooth)) {
00327         float width = 0.35f/scale;
00328         BRepMesh::Mesh(V1,0.1);
00329         result  //<< "<g" 
00330 
00331                 //<< " id=\"" << ViewName << "\"" << endl
00332                /* << "   stroke=\"rgb(0, 0, 0)\"" << endl 
00333                 << "   stroke-width=\"" << width << "\"" << endl
00334                 << "   stroke-linecap=\"butt\"" << endl
00335                 << "   stroke-linejoin=\"miter\"" << endl
00336                 << "   fill=\"none\"" << endl
00337 
00338                 << "  >" << endl*/
00339                 << output.exportEdges(V1);
00340                 //<< "</g>" << endl;
00341     }
00342     if (!H1.IsNull() && (type & WithSmooth) && (type & WithHidden)) {
00343 
00344         float width = 0.15f/scale;
00345         BRepMesh::Mesh(H1,0.1);
00346         result  //<< "<g" 
00347                 //<< " id=\"" << ViewName << "\"" << endl
00348                 /*<< "   stroke=\"rgb(0, 0, 0)\"" << endl 
00349                 << "   stroke-width=\"" << width << "\"" << endl
00350 
00351                 << "   stroke-linecap=\"butt\"" << endl
00352                 << "   stroke-linejoin=\"miter\"" << endl
00353                 << "   stroke-dasharray=\"5 3\"" << endl
00354                 << "   fill=\"none\"" << endl
00355                 << "  >" << endl*/
00356 
00357                 << output.exportEdges(H1);
00358                 //<< "</g>" << endl;
00359     }
00360 
00361 
00362     result       << 0          << endl
00363                 << "ENDSEC"   << endl
00364                 << 0          << endl
00365                 << "EOF";
00366 
00367     return result.str();
00368 }

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