Mod/Sketcher/Gui/Command.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2008 Jürgen Riegel (juergen.riegel@web.de)              *
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 <TopoDS_Shape.hxx>
00027 # include <TopoDS_Face.hxx>
00028 # include <TopoDS.hxx>
00029 # include <BRepAdaptor_Surface.hxx>
00030 # include <QApplication>
00031 # include <QInputDialog>
00032 # include <QMessageBox>
00033 #endif
00034 
00035 #include <Gui/Application.h>
00036 #include <Gui/Document.h>
00037 #include <Gui/Command.h>
00038 #include <Gui/MainWindow.h>
00039 #include <Gui/DlgEditFileIncludeProptertyExternal.h>
00040 #include <Gui/SelectionFilter.h>
00041 
00042 #include <Mod/Sketcher/App/SketchObjectSF.h>
00043 #include <Mod/Sketcher/App/SketchObject.h>
00044 #include <Mod/Part/App/Part2DObject.h>
00045 
00046 #include "SketchOrientationDialog.h"
00047 #include "ViewProviderSketch.h"
00048 
00049 using namespace std;
00050 using namespace SketcherGui;
00051 using namespace Part;
00052 
00053 /* Sketch commands =======================================================*/
00054 DEF_STD_CMD_A(CmdSketcherNewSketch);
00055 
00056 CmdSketcherNewSketch::CmdSketcherNewSketch()
00057         :Command("Sketcher_NewSketch")
00058 {
00059     sAppModule      = "Sketcher";
00060     sGroup          = QT_TR_NOOP("Sketcher");
00061     sMenuText       = QT_TR_NOOP("Create sketch");
00062     sToolTipText    = QT_TR_NOOP("Create a new or edit the selected sketch");
00063     sWhatsThis      = sToolTipText;
00064     sStatusTip      = sToolTipText;
00065     sPixmap         = "Sketcher_NewSketch";
00066 }
00067 
00068 void CmdSketcherNewSketch::activated(int iMsg)
00069 {
00070     Gui::SelectionFilter SketchFilter("SELECT Sketcher::SketchObject COUNT 1");
00071     Gui::SelectionFilter FaceFilter  ("SELECT Part::Feature SUBELEMENT Face COUNT 1");
00072 
00073     if (SketchFilter.match()) {
00074         Sketcher::SketchObject *Sketch = static_cast<Sketcher::SketchObject*>(SketchFilter.Result[0][0].getObject());
00075         openCommand("Edit Sketch");
00076         doCommand(Gui,"Gui.activeDocument().setEdit('%s')",Sketch->getNameInDocument());
00077     }
00078     else if (FaceFilter.match()) {
00079         // get the selected object
00080         Part::Feature *part = static_cast<Part::Feature*>(FaceFilter.Result[0][0].getObject());
00081         Base::Placement ObjectPos = part->Placement.getValue();
00082         const std::vector<std::string> &sub = FaceFilter.Result[0][0].getSubNames();
00083         if (sub.size() > 1){
00084             // No assert for wrong user input!
00085             QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Several sub-elements selected"),
00086                 QObject::tr("You have to select a single face as support for a sketch!"));
00087             return;
00088         }
00089         // get the selected sub shape (a Face)
00090         const Part::TopoShape &shape = part->Shape.getValue();
00091         TopoDS_Shape sh = shape.getSubShape(sub[0].c_str());
00092         const TopoDS_Face& face = TopoDS::Face(sh);
00093         if (face.IsNull()){
00094             // No assert for wrong user input!
00095             QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No support face selected"),
00096                 QObject::tr("You have to select a face as support for a sketch!"));
00097             return;
00098         }
00099 
00100         BRepAdaptor_Surface adapt(face);
00101         if (adapt.GetType() != GeomAbs_Plane){
00102             QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No planar support"),
00103                 QObject::tr("You need a planar face as support for a sketch!"));
00104             return;
00105         }
00106         Base::Placement placement = Part2DObject::positionBySupport(face,ObjectPos);
00107         double a,b,c;
00108         placement.getRotation().getYawPitchRoll(a,b,c);
00109 
00110         std::string supportString = FaceFilter.Result[0][0].getAsPropertyLinkSubString();
00111  
00112         // create Sketch on Face
00113         std::string FeatName = getUniqueObjectName("Sketch");
00114 
00115         openCommand("Create a Sketch on Face");
00116         doCommand(Doc,"App.activeDocument().addObject('Sketcher::SketchObject','%s')",FeatName.c_str());
00117         doCommand(Gui,"App.activeDocument().%s.Placement = FreeCAD.Placement(FreeCAD.Vector(%f,%f,%f),FreeCAD.Rotation(%f,%f,%f))",FeatName.c_str(),placement.getPosition().x,placement.getPosition().y,placement.getPosition().z,a,b,c);
00118         doCommand(Gui,"App.activeDocument().%s.Support = %s",FeatName.c_str(),supportString.c_str());
00119         //doCommand(Gui,"Gui.activeDocument().activeView().setCamera('%s')",cam.c_str());
00120         doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
00121     }
00122     else {
00123         // ask user for orientation
00124         SketchOrientationDialog Dlg;
00125 
00126         if (Dlg.exec() != QDialog::Accepted)
00127             return; // canceled
00128         Base::Vector3d p = Dlg.Pos.getPosition();
00129         Base::Rotation r = Dlg.Pos.getRotation();
00130 
00131         // do the right view direction
00132         std::string camstring;
00133         switch(Dlg.DirType){
00134             case 0:
00135                 camstring = "#Inventor V2.1 ascii \\n OrthographicCamera {\\n viewportMapping ADJUST_CAMERA \\n position 0 0 87 \\n orientation 0 0 1  0 \\n nearDistance -112.88701 \\n farDistance 287.28702 \\n aspectRatio 1 \\n focalDistance 87 \\n height 143.52005 }";
00136                 break;
00137             case 1:
00138                 camstring = "#Inventor V2.1 ascii \\n OrthographicCamera {\\n viewportMapping ADJUST_CAMERA \\n position 0 0 -87 \\n orientation -1 0 0  3.1415927 \\n nearDistance -112.88701 \\n farDistance 287.28702 \\n aspectRatio 1 \\n focalDistance 87 \\n height 143.52005 }";
00139                 break;
00140             case 2:
00141                 camstring = "#Inventor V2.1 ascii \\n OrthographicCamera {\\n viewportMapping ADJUST_CAMERA\\n  position 0 -87 0 \\n  orientation -1 0 0  4.712389\\n  nearDistance -112.88701\\n  farDistance 287.28702\\n  aspectRatio 1\\n  focalDistance 87\\n  height 143.52005\\n\\n}";
00142                 break;
00143             case 3:
00144                 camstring = "#Inventor V2.1 ascii \\n OrthographicCamera {\\n viewportMapping ADJUST_CAMERA\\n  position 0 87 0 \\n  orientation 0 0.70710683 0.70710683  3.1415927\\n  nearDistance -112.88701\\n  farDistance 287.28702\\n  aspectRatio 1\\n  focalDistance 87\\n  height 143.52005\\n\\n}";
00145                 break;
00146             case 4:
00147                 camstring = "#Inventor V2.1 ascii \\n OrthographicCamera {\\n viewportMapping ADJUST_CAMERA\\n  position 87 0 0 \\n  orientation 0.57735026 0.57735026 0.57735026  2.0943952 \\n  nearDistance -112.887\\n  farDistance 287.28699\\n  aspectRatio 1\\n  focalDistance 87\\n  height 143.52005\\n\\n}";
00148                 break;
00149             case 5:
00150                 camstring = "#Inventor V2.1 ascii \\n OrthographicCamera {\\n viewportMapping ADJUST_CAMERA\\n  position -87 0 0 \\n  orientation -0.57735026 0.57735026 0.57735026  4.1887903 \\n  nearDistance -112.887\\n  farDistance 287.28699\\n  aspectRatio 1\\n  focalDistance 87\\n  height 143.52005\\n\\n}";
00151                 break;
00152         }
00153         std::string FeatName = getUniqueObjectName("Sketch");
00154 
00155         openCommand("Create a new Sketch");
00156         doCommand(Doc,"App.activeDocument().addObject('Sketcher::SketchObject','%s')",FeatName.c_str());
00157         doCommand(Doc,"App.activeDocument().%s.Placement = App.Placement(App.Vector(%f,%f,%f),App.Rotation(%f,%f,%f,%f))",FeatName.c_str(),p.x,p.y,p.z,r[0],r[1],r[2],r[3]);
00158         doCommand(Gui,"Gui.activeDocument().activeView().setCamera('%s')",camstring.c_str());
00159         doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
00160     }
00161  
00162 }
00163 
00164 bool CmdSketcherNewSketch::isActive(void)
00165 {
00166     if (getActiveGuiDocument())
00167         return true;
00168     else
00169         return false;
00170 }
00171 
00172 DEF_STD_CMD_A(CmdSketcherMapSketch);
00173 
00174 CmdSketcherMapSketch::CmdSketcherMapSketch()
00175   : Command("Sketcher_MapSketch")
00176 {
00177     sAppModule      = "Sketcher";
00178     sGroup          = QT_TR_NOOP("Sketcher");
00179     sMenuText       = QT_TR_NOOP("Map sketch to face...");
00180     sToolTipText    = QT_TR_NOOP("Map a sketch to a face");
00181     sWhatsThis      = sToolTipText;
00182     sStatusTip      = sToolTipText;
00183 }
00184 
00185 void CmdSketcherMapSketch::activated(int iMsg)
00186 {
00187     App::Document* doc = App::GetApplication().getActiveDocument();
00188     std::vector<App::DocumentObject*> sel = doc->getObjectsOfType(Sketcher::SketchObject::getClassTypeId());
00189     if (sel.empty()) {
00190         QMessageBox::warning(Gui::getMainWindow(),
00191             qApp->translate(className(), "No sketch found"),
00192             qApp->translate(className(), "The document doesn't have a sketch"));
00193         return;
00194     }
00195 
00196     bool ok;
00197     QStringList items;
00198     for (std::vector<App::DocumentObject*>::iterator it = sel.begin(); it != sel.end(); ++it)
00199         items.push_back(QString::fromUtf8((*it)->Label.getValue()));
00200     QString text = QInputDialog::getItem(Gui::getMainWindow(),
00201         qApp->translate(className(), "Select sketch"),
00202         qApp->translate(className(), "Select a sketch from the list"),
00203         items, 0, false, &ok);
00204     if (!ok) return;
00205     int index = items.indexOf(text);
00206 
00207     std::string featName = sel[index]->getNameInDocument();
00208     Gui::SelectionFilter FaceFilter  ("SELECT Part::Feature SUBELEMENT Face COUNT 1");
00209     if (FaceFilter.match()) {
00210         // get the selected object
00211         Part::Feature *part = static_cast<Part::Feature*>(FaceFilter.Result[0][0].getObject());
00212         Base::Placement ObjectPos = part->Placement.getValue();
00213         const std::vector<std::string> &sub = FaceFilter.Result[0][0].getSubNames();
00214         if (sub.size() > 1){
00215             // No assert for wrong user input!
00216             QMessageBox::warning(Gui::getMainWindow(),
00217                 qApp->translate(className(),"Several sub-elements selected"),
00218                 qApp->translate(className(),"You have to select a single face as support for a sketch!"));
00219             return;
00220         }
00221         // get the selected sub shape (a Face)
00222         const Part::TopoShape &shape = part->Shape.getValue();
00223         TopoDS_Shape sh = shape.getSubShape(sub[0].c_str());
00224         const TopoDS_Face& face = TopoDS::Face(sh);
00225         if (face.IsNull()) {
00226             // No assert for wrong user input!
00227             QMessageBox::warning(Gui::getMainWindow(),
00228                 qApp->translate(className(),"No support face selected"),
00229                 qApp->translate(className(),"You have to select a face as support for a sketch!"));
00230             return;
00231         }
00232 
00233         BRepAdaptor_Surface adapt(face);
00234         if (adapt.GetType() != GeomAbs_Plane){
00235             QMessageBox::warning(Gui::getMainWindow(),
00236                 qApp->translate(className(),"No planar support"),
00237                 qApp->translate(className(),"You need a planar face as support for a sketch!"));
00238             return;
00239         }
00240         Base::Placement placement = Part2DObject::positionBySupport(face,ObjectPos);
00241         double a,b,c;
00242         placement.getRotation().getYawPitchRoll(a,b,c);
00243 
00244         std::string supportString = FaceFilter.Result[0][0].getAsPropertyLinkSubString();
00245 
00246         openCommand("Map a Sketch on Face");
00247         doCommand(Gui,"App.activeDocument().%s.Placement = FreeCAD.Placement(FreeCAD.Vector(%f,%f,%f),FreeCAD.Rotation(%f,%f,%f))",featName.c_str(),placement.getPosition().x,placement.getPosition().y,placement.getPosition().z,a,b,c);
00248         doCommand(Gui,"App.activeDocument().%s.Support = %s",featName.c_str(),supportString.c_str());
00249         doCommand(Gui,"Gui.activeDocument().setEdit('%s')",featName.c_str());
00250     }
00251     else {
00252         QMessageBox::warning(Gui::getMainWindow(),
00253             qApp->translate(className(), "No face selected"),
00254             qApp->translate(className(), "No face was selected to map the sketch to"));
00255     }
00256 }
00257 
00258 bool CmdSketcherMapSketch::isActive(void)
00259 {
00260     return getActiveGuiDocument() != 0;
00261 }
00262 
00263 
00264 DEF_STD_CMD_A(CmdSketcherLeaveSketch);
00265 
00266 CmdSketcherLeaveSketch::CmdSketcherLeaveSketch()
00267   : Command("Sketcher_LeaveSketch")
00268 {
00269     sAppModule      = "Sketcher";
00270     sGroup          = QT_TR_NOOP("Sketcher");
00271     sMenuText       = QT_TR_NOOP("Leave sketch");
00272     sToolTipText    = QT_TR_NOOP("Close the editing of the sketch");
00273     sWhatsThis      = sToolTipText;
00274     sStatusTip      = sToolTipText;
00275     sPixmap         = "Sketcher_LeaveSketch";
00276     eType           = 0;
00277 }
00278 
00279 void CmdSketcherLeaveSketch::activated(int iMsg)
00280 {
00281     openCommand("Sketch changed");
00282     doCommand(Gui,"Gui.activeDocument().resetEdit()");
00283     doCommand(Doc,"App.ActiveDocument.recompute()");
00284     commitCommand();
00285 
00286 }
00287 
00288 bool CmdSketcherLeaveSketch::isActive(void)
00289 {
00290     Gui::Document *doc = getActiveGuiDocument();
00291     if (doc) {
00292         // checks if a Sketch Viewprovider is in Edit and is in no special mode
00293         SketcherGui::ViewProviderSketch* vp = dynamic_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());
00294         if (vp && vp->getSketchMode() == ViewProviderSketch::STATUS_NONE)
00295             return true;
00296     }
00297     return false;
00298 }
00299 
00300 DEF_STD_CMD_A(CmdSketcherViewSketch);
00301 
00302 CmdSketcherViewSketch::CmdSketcherViewSketch()
00303   : Command("Sketcher_ViewSketch")
00304 {
00305     sAppModule      = "Sketcher";
00306     sGroup          = QT_TR_NOOP("Sketcher");
00307     sMenuText       = QT_TR_NOOP("View sketch");
00308     sToolTipText    = QT_TR_NOOP("View sketch perpendicular to sketch plane");
00309     sWhatsThis      = sToolTipText;
00310     sStatusTip      = sToolTipText;
00311     eType           = 0;
00312 }
00313 
00314 void CmdSketcherViewSketch::activated(int iMsg)
00315 {
00316     Gui::Document *doc = getActiveGuiDocument();
00317     SketcherGui::ViewProviderSketch* vp = dynamic_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());
00318     doCommand(Gui,"Gui.ActiveDocument.ActiveView.setCameraOrientation(App.ActiveDocument.%s.Placement.Rotation.Q)"
00319                  ,vp->getObject()->getNameInDocument());
00320 }
00321 
00322 bool CmdSketcherViewSketch::isActive(void)
00323 {
00324     Gui::Document *doc = getActiveGuiDocument();
00325     if (doc) {
00326         // checks if a Sketch Viewprovider is in Edit and is in no special mode
00327         SketcherGui::ViewProviderSketch* vp = dynamic_cast<SketcherGui::ViewProviderSketch*>(doc->getInEdit());
00328         if (vp && vp->getSketchMode() == ViewProviderSketch::STATUS_NONE)
00329             return true;
00330     }
00331     return false;
00332 }
00333 
00334 
00335 
00336 
00337 
00338 void CreateSketcherCommands(void)
00339 {
00340     Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
00341 
00342     rcCmdMgr.addCommand(new CmdSketcherNewSketch());
00343     rcCmdMgr.addCommand(new CmdSketcherMapSketch());
00344     rcCmdMgr.addCommand(new CmdSketcherLeaveSketch());
00345     rcCmdMgr.addCommand(new CmdSketcherViewSketch());
00346 }

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