FeatureViewPart.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 #endif
00029 
00030 
00031 #include <HLRBRep_Algo.hxx>
00032 #include <TopoDS_Shape.hxx>
00033 #include <HLRTopoBRep_OutLiner.hxx>
00034 //#include <BRepAPI_MakeOutLine.hxx>
00035 #include <HLRAlgo_Projector.hxx>
00036 #include <HLRBRep_ShapeBounds.hxx>
00037 #include <HLRBRep_HLRToShape.hxx>
00038 #include <gp_Ax2.hxx>
00039 #include <gp_Pnt.hxx>
00040 #include <gp_Dir.hxx>
00041 #include <Poly_Polygon3D.hxx>
00042 #include <Poly_Triangulation.hxx>
00043 #include <Poly_PolygonOnTriangulation.hxx>
00044 #include <TopoDS.hxx>
00045 #include <TopoDS_Face.hxx>
00046 #include <TopoDS_Edge.hxx>
00047 #include <TopoDS_Vertex.hxx>
00048 #include <TopExp.hxx>
00049 #include <TopExp_Explorer.hxx>
00050 #include <TopTools_IndexedMapOfShape.hxx>
00051 #include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
00052 #include <TopTools_ListOfShape.hxx>
00053 #include <TColgp_Array1OfPnt2d.hxx>
00054 #include <BRep_Tool.hxx>
00055 #include <BRepMesh.hxx>
00056 
00057 
00058 #include <Base/Exception.h>
00059 #include <Base/FileInfo.h>
00060 #include <Mod/Part/App/PartFeature.h>
00061 
00062 #include "FeatureViewPart.h"
00063 #include "ProjectionAlgos.h"
00064 
00065 using namespace Drawing;
00066 using namespace std;
00067 
00068 
00069 //===========================================================================
00070 // FeatureViewPart
00071 //===========================================================================
00072 
00073 PROPERTY_SOURCE(Drawing::FeatureViewPart, Drawing::FeatureView)
00074 
00075 
00076 FeatureViewPart::FeatureViewPart(void) 
00077 {
00078     static const char *group = "Shape view";
00079 
00080     ADD_PROPERTY_TYPE(Direction ,(0,0,1.0),group,App::Prop_None,"Projection direction");
00081     ADD_PROPERTY_TYPE(Source ,(0),group,App::Prop_None,"Shape to view");
00082     ADD_PROPERTY_TYPE(ShowHiddenLines ,(false),group,App::Prop_None,"Control the appearance of the dashed hidden lines");
00083     ADD_PROPERTY_TYPE(ShowSmoothLines ,(false),group,App::Prop_None,"Control the appearance of the smooth lines");
00084 }
00085 
00086 FeatureViewPart::~FeatureViewPart()
00087 {
00088 }
00089 
00090 #if 0 
00091 
00092 App::DocumentObjectExecReturn *FeatureViewPart::execute(void)
00093 {
00094     std::stringstream result;
00095         std::string ViewName = Label.getValue();
00096 
00097     App::DocumentObject* link = Source.getValue();
00098     if (!link)
00099         return new App::DocumentObjectExecReturn("No object linked");
00100     if (!link->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()))
00101         return new App::DocumentObjectExecReturn("Linked object is not a Part object");
00102     TopoDS_Shape shape = static_cast<Part::Feature*>(link)->Shape.getShape()._Shape;
00103     if (shape.IsNull())
00104         return new App::DocumentObjectExecReturn("Linked shape object is empty");
00105 
00106     Handle( HLRBRep_Algo ) brep_hlr = new HLRBRep_Algo;
00107     brep_hlr->Add( shape );
00108 
00109     gp_Ax2 transform(gp_Pnt(0,0,0),gp_Dir(0,0,1));
00110     HLRAlgo_Projector projector( transform );
00111     brep_hlr->Projector( projector );
00112     brep_hlr->Update();
00113     brep_hlr->Hide();
00114 
00115     // extracting the result sets:
00116     HLRBRep_HLRToShape shapes( brep_hlr );
00117 
00118     TopoDS_Shape VisiblyEdges;
00119     VisiblyEdges = shapes.VCompound();
00120 
00121     TopoDS_Shape HiddenEdges;
00122     HiddenEdges = shapes.HCompound();
00123 
00124         BRepMesh::Mesh(VisiblyEdges,0.1);
00125     //HLRBRep_HLRToShape Tool(Hider);
00126     //TopoDS_Shape V  = Tool.VCompound       ();// artes vives       vues
00127     //TopoDS_Shape V1 = Tool.Rg1LineVCompound();// artes rgulires  vues
00128     //TopoDS_Shape VN = Tool.RgNLineVCompound();// artes de couture  vues
00129     //TopoDS_Shape VO = Tool.OutLineVCompound();// contours apparents vus
00130     //TopoDS_Shape VI = Tool.IsoLineVCompound();// isoparamtriques   vues
00131     //TopoDS_Shape H  = Tool.HCompound       ();// artes vives       caches
00132     //TopoDS_Shape H1 = Tool.Rg1LineHCompound();// artes rgulires  caches
00133     //TopoDS_Shape HN = Tool.RgNLineHCompound();// artes de couture  caches
00134     //TopoDS_Shape HO = Tool.OutLineHCompound();// contours apparents cachs
00135     //TopoDS_Shape HI = Tool.IsoLineHCompound();// isoparamtriques   caches
00136 
00137     result  << "<g" 
00138             << " id=\"" << ViewName << "\"" << endl
00139                     << "   stroke=\"rgb(0, 0, 0)\"" << endl 
00140                         << "   stroke-width=\"0.35\"" << endl
00141                         << "   stroke-linecap=\"butt\"" << endl
00142                         << "   stroke-linejoin=\"miter\"" << endl
00143                         << "   transform=\"translate("<< X.getValue()<<","<<Y.getValue()<<") scale("<< Scale.getValue()<<","<<Scale.getValue()<<")\"" << endl
00144             << "   fill=\"none\"" << endl
00145             << "  >" << endl;
00146 
00147     TopExp_Explorer edges( VisiblyEdges, TopAbs_EDGE );
00148     for (int i = 1 ; edges.More(); edges.Next(),i++ ) {
00149       TopoDS_Edge edge = TopoDS::Edge( edges.Current() );
00150       TopLoc_Location location;
00151       Handle( Poly_Polygon3D ) polygon = BRep_Tool::Polygon3D( edge, location );
00152       if ( !polygon.IsNull() ) {
00153         const TColgp_Array1OfPnt& nodes = polygon->Nodes();
00154          char c = 'M';
00155         result << "<path id= \"" << ViewName << i << "\" d=\" "; 
00156         for ( int i = nodes.Lower(); i<= nodes.Upper(); i++ ){
00157             result << c << " " << nodes(i).X() << " " << nodes(i).Y()<< " " ; 
00158             c = 'L';
00159         }
00160         result << "\" />" << endl;
00161       }
00162     }
00163 
00164     result << "</g>" << endl;
00165 
00166     // Apply the resulting fragment
00167     ViewResult.setValue(result.str().c_str());
00168 
00169     return App::DocumentObject::StdReturn;
00170 }
00171 #else 
00172 App::DocumentObjectExecReturn *FeatureViewPart::execute(void)
00173 {
00174     std::stringstream result;
00175     std::string ViewName = Label.getValue();
00176 
00177     App::DocumentObject* link = Source.getValue();
00178     if (!link)
00179         return new App::DocumentObjectExecReturn("No object linked");
00180     if (!link->getTypeId().isDerivedFrom(Part::Feature::getClassTypeId()))
00181         return new App::DocumentObjectExecReturn("Linked object is not a Part object");
00182     TopoDS_Shape shape = static_cast<Part::Feature*>(link)->Shape.getShape()._Shape;
00183     if (shape.IsNull())
00184         return new App::DocumentObjectExecReturn("Linked shape object is empty");
00185     Base::Vector3f Dir = Direction.getValue();
00186     bool hidden = ShowHiddenLines.getValue();
00187     bool smooth = ShowSmoothLines.getValue();
00188 
00189     try {
00190         ProjectionAlgos Alg(ProjectionAlgos::invertY(shape),Dir);
00191         result  << "<g" 
00192                 << " id=\"" << ViewName << "\"" << endl
00193                 << "   transform=\"rotate("<< Rotation.getValue() << ","<< X.getValue()<<","<<Y.getValue()<<") translate("<< X.getValue()<<","<<Y.getValue()<<") scale("<< Scale.getValue()<<","<<Scale.getValue()<<")\"" << endl
00194                 << "  >" << endl;
00195 
00196         ProjectionAlgos::SvgExtractionType type = ProjectionAlgos::Plain;
00197         if (hidden) type = (ProjectionAlgos::SvgExtractionType)(type|ProjectionAlgos::WithHidden);
00198         if (smooth) type = (ProjectionAlgos::SvgExtractionType)(type|ProjectionAlgos::WithSmooth);
00199         result << Alg.getSVG(type, this->Scale.getValue());
00200 
00201         result << "</g>" << endl;
00202 
00203         // Apply the resulting fragment
00204         ViewResult.setValue(result.str().c_str());
00205 
00206         return App::DocumentObject::StdReturn;
00207     }
00208     catch (Standard_Failure) {
00209         Handle_Standard_Failure e = Standard_Failure::Caught();
00210         return new App::DocumentObjectExecReturn(e->GetMessageString());
00211     }
00212 }
00213 
00214 #endif 
00215 
00216 
00217 // Python Drawing feature ---------------------------------------------------------
00218 
00219 namespace App {
00221 PROPERTY_SOURCE_TEMPLATE(Drawing::FeatureViewPartPython, Drawing::FeatureViewPart)
00222 template<> const char* Drawing::FeatureViewPartPython::getViewProviderName(void) const {
00223     return "DrawingGui::ViewProviderDrawingView";
00224 }
00226 
00227 // explicit template instantiation
00228 template class DrawingExport FeaturePythonT<Drawing::FeatureViewPart>;
00229 }

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