Mod/PartDesign/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 <TopExp_Explorer.hxx>
00027 # include <QMessageBox>
00028 #endif
00029 
00030 #include <Gui/Application.h>
00031 #include <Gui/Command.h>
00032 #include <Gui/Control.h>
00033 #include <Gui/Selection.h>
00034 #include <Gui/MainWindow.h>
00035 #include <Gui/FileDialog.h>
00036 
00037 #include <Mod/Part/App/Part2DObject.h>
00038 #include "TaskChamfer.h"
00039 
00040 
00041 using namespace std;
00042 
00043 //===========================================================================
00044 // Part_Pad
00045 //===========================================================================
00046 
00047 /* Sketch commands =======================================================*/
00048 //DEF_STD_CMD_A(CmdPartDesignNewSketch);
00049 //
00050 //CmdPartDesignNewSketch::CmdPartDesignNewSketch()
00051 //      :Command("PartDesign_NewSketch")
00052 //{
00053 //    sAppModule      = "PartDesign";
00054 //    sGroup          = QT_TR_NOOP("PartDesign");
00055 //    sMenuText       = QT_TR_NOOP("Create sketch");
00056 //    sToolTipText    = QT_TR_NOOP("Create a new sketch");
00057 //    sWhatsThis      = sToolTipText;
00058 //    sStatusTip      = sToolTipText;
00059 //    sPixmap         = "Sketcher_NewSketch";
00060 //}
00061 //
00062 //
00063 //void CmdPartDesignNewSketch::activated(int iMsg)
00064 //{
00065 //    const char camstring[] = "#Inventor V2.1 ascii \\n OrthographicCamera { \\n viewportMapping ADJUST_CAMERA \\n position 0 0 87 \\n orientation 0 0 1  0 \\n nearDistance 37 \\n farDistance 137 \\n aspectRatio 1 \\n focalDistance 87 \\n height 119 }";
00066 //
00067 //    std::string FeatName = getUniqueObjectName("Sketch");
00068 //
00069 //    std::string cam(camstring);
00070 //
00071 //    openCommand("Create a new Sketch");
00072 //    doCommand(Doc,"App.activeDocument().addObject('Sketcher::SketchObject','%s')",FeatName.c_str());
00073 //    doCommand(Gui,"Gui.activeDocument().activeView().setCamera('%s')",cam.c_str());
00074 //    doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
00075 //    
00076 //    //getDocument()->recompute();
00077 //}
00078 //
00079 //bool CmdPartDesignNewSketch::isActive(void)
00080 //{
00081 //    if (getActiveGuiDocument())
00082 //        return true;
00083 //    else
00084 //        return false;
00085 //}
00086 //
00087 
00088 //===========================================================================
00089 // PartDesign_Pad
00090 //===========================================================================
00091 DEF_STD_CMD_A(CmdPartDesignPad);
00092 
00093 CmdPartDesignPad::CmdPartDesignPad()
00094   : Command("PartDesign_Pad")
00095 {
00096     sAppModule    = "PartDesign";
00097     sGroup        = QT_TR_NOOP("PartDesign");
00098     sMenuText     = QT_TR_NOOP("Pad");
00099     sToolTipText  = QT_TR_NOOP("Pad a selected sketch");
00100     sWhatsThis    = sToolTipText;
00101     sStatusTip    = sToolTipText;
00102     sPixmap       = "PartDesign_Pad";
00103 }
00104 
00105 void CmdPartDesignPad::activated(int iMsg)
00106 {
00107     unsigned int n = getSelection().countObjectsOfType(Part::Part2DObject::getClassTypeId());
00108     if (n != 1) {
00109         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
00110             QObject::tr("Select a sketch or 2D object."));
00111         return;
00112     }
00113 
00114     std::string FeatName = getUniqueObjectName("Pad");
00115 
00116     std::vector<App::DocumentObject*> Sel = getSelection().getObjectsOfType(Part::Part2DObject::getClassTypeId());
00117     Part::Part2DObject* sketch = static_cast<Part::Part2DObject*>(Sel.front());
00118     const TopoDS_Shape& shape = sketch->Shape.getValue();
00119     if (shape.IsNull()) {
00120         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
00121             QObject::tr("The shape of the selected object is empty."));
00122         return;
00123     }
00124 
00125     // count free wires
00126     int ctWires=0;
00127     TopExp_Explorer ex;
00128     for (ex.Init(shape, TopAbs_WIRE); ex.More(); ex.Next()) {
00129         ctWires++;
00130     }
00131     if (ctWires == 0) {
00132         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
00133             QObject::tr("The shape of the selected object is not a wire."));
00134         return;
00135     }
00136 
00137     App::DocumentObject* support = sketch->Support.getValue();
00138 
00139     openCommand("Make Pad");
00140     doCommand(Doc,"App.activeDocument().addObject(\"PartDesign::Pad\",\"%s\")",FeatName.c_str());
00141     doCommand(Doc,"App.activeDocument().%s.Sketch = App.activeDocument().%s",FeatName.c_str(),sketch->getNameInDocument());
00142     doCommand(Doc,"App.activeDocument().%s.Length = 10.0",FeatName.c_str());
00143     updateActive();
00144     if (isActiveObjectValid()) {
00145         doCommand(Gui,"Gui.activeDocument().hide(\"%s\")",sketch->getNameInDocument());
00146         if (support)
00147             doCommand(Gui,"Gui.activeDocument().hide(\"%s\")",support->getNameInDocument());
00148     }
00149     doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
00150 
00151     //commitCommand();
00152     adjustCameraPosition();
00153 
00154     if (support) {
00155         copyVisual(FeatName.c_str(), "ShapeColor", support->getNameInDocument());
00156         copyVisual(FeatName.c_str(), "LineColor", support->getNameInDocument());
00157         copyVisual(FeatName.c_str(), "PointColor", support->getNameInDocument());
00158     }
00159 }
00160 
00161 bool CmdPartDesignPad::isActive(void)
00162 {
00163     return hasActiveDocument();
00164 }
00165 
00166 //===========================================================================
00167 // PartDesign_Pocket
00168 //===========================================================================
00169 DEF_STD_CMD_A(CmdPartDesignPocket);
00170 
00171 CmdPartDesignPocket::CmdPartDesignPocket()
00172   : Command("PartDesign_Pocket")
00173 {
00174     sAppModule    = "PartDesign";
00175     sGroup        = QT_TR_NOOP("PartDesign");
00176     sMenuText     = QT_TR_NOOP("Pocket");
00177     sToolTipText  = QT_TR_NOOP("create a pocket with the selected sketch");
00178     sWhatsThis    = sToolTipText;
00179     sStatusTip    = sToolTipText;
00180     sPixmap       = "PartDesign_Pocket";
00181 }
00182 
00183 void CmdPartDesignPocket::activated(int iMsg)
00184 {
00185     unsigned int n = getSelection().countObjectsOfType(Part::Part2DObject::getClassTypeId());
00186     if (n != 1) {
00187         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
00188             QObject::tr("Select a sketch or 2D object."));
00189         return;
00190     }
00191 
00192     std::string FeatName = getUniqueObjectName("Pocket");
00193 
00194     std::vector<App::DocumentObject*> Sel = getSelection().getObjectsOfType(Part::Part2DObject::getClassTypeId());
00195     Part::Part2DObject* sketch = static_cast<Part::Part2DObject*>(Sel.front());
00196     const TopoDS_Shape& shape = sketch->Shape.getValue();
00197     if (shape.IsNull()) {
00198         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
00199             QObject::tr("The shape of the selected object is empty."));
00200         return;
00201     }
00202 
00203     // count free wires
00204     int ctWires=0;
00205     TopExp_Explorer ex;
00206     for (ex.Init(shape, TopAbs_WIRE); ex.More(); ex.Next()) {
00207         ctWires++;
00208     }
00209     if (ctWires == 0) {
00210         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
00211             QObject::tr("The shape of the selected object is not a wire."));
00212         return;
00213     }
00214     App::DocumentObject* support = sketch->Support.getValue();
00215     if (support == 0) {
00216         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("No Support"),
00217             QObject::tr("The sketch has to have a support for the pocket feature.\nCreate the sketch on a face."));
00218         return;
00219     }
00220 
00221     openCommand("Make Pocket");
00222     doCommand(Doc,"App.activeDocument().addObject(\"PartDesign::Pocket\",\"%s\")",FeatName.c_str());
00223     doCommand(Doc,"App.activeDocument().%s.Sketch = App.activeDocument().%s",FeatName.c_str(),sketch->getNameInDocument());
00224     doCommand(Doc,"App.activeDocument().%s.Length = 5.0",FeatName.c_str());
00225     updateActive();
00226     if (isActiveObjectValid()) {
00227         doCommand(Gui,"Gui.activeDocument().hide(\"%s\")",sketch->getNameInDocument());
00228         doCommand(Gui,"Gui.activeDocument().hide(\"%s\")",support->getNameInDocument());
00229     }
00230     doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
00231 
00232     copyVisual(FeatName.c_str(), "ShapeColor", support->getNameInDocument());
00233     copyVisual(FeatName.c_str(), "LineColor", support->getNameInDocument());
00234     copyVisual(FeatName.c_str(), "PointColor", support->getNameInDocument());
00235 }
00236 
00237 bool CmdPartDesignPocket::isActive(void)
00238 {
00239     return hasActiveDocument();
00240 }
00241 
00242 //===========================================================================
00243 // PartDesign_Revolution
00244 //===========================================================================
00245 DEF_STD_CMD_A(CmdPartDesignRevolution);
00246 
00247 CmdPartDesignRevolution::CmdPartDesignRevolution()
00248   : Command("PartDesign_Revolution")
00249 {
00250     sAppModule    = "PartDesign";
00251     sGroup        = QT_TR_NOOP("PartDesign");
00252     sMenuText     = QT_TR_NOOP("Revolution");
00253     sToolTipText  = QT_TR_NOOP("Revolve a selected sketch");
00254     sWhatsThis    = sToolTipText;
00255     sStatusTip    = sToolTipText;
00256     sPixmap       = "PartDesign_Revolution";
00257 }
00258 
00259 void CmdPartDesignRevolution::activated(int iMsg)
00260 {
00261     unsigned int n = getSelection().countObjectsOfType(Part::Part2DObject::getClassTypeId());
00262     if (n != 1) {
00263         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
00264             QObject::tr("Select a sketch or 2D object."));
00265         return;
00266     }
00267 
00268     std::string FeatName = getUniqueObjectName("Revolution");
00269 
00270     std::vector<App::DocumentObject*> Sel = getSelection().getObjectsOfType(Part::Part2DObject::getClassTypeId());
00271     Part::Part2DObject* sketch = static_cast<Part::Part2DObject*>(Sel.front());
00272     const TopoDS_Shape& shape = sketch->Shape.getValue();
00273     if (shape.IsNull()) {
00274         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
00275             QObject::tr("The shape of the selected object is empty."));
00276         return;
00277     }
00278 
00279     // count free wires
00280     int ctWires=0;
00281     TopExp_Explorer ex;
00282     for (ex.Init(shape, TopAbs_WIRE); ex.More(); ex.Next()) {
00283         ctWires++;
00284     }
00285     if (ctWires == 0) {
00286         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
00287             QObject::tr("The shape of the selected object is not a wire."));
00288         return;
00289     }
00290 
00291     App::DocumentObject* support = sketch->Support.getValue();
00292 
00293     openCommand("Make Revolution");
00294     doCommand(Doc,"App.activeDocument().addObject(\"PartDesign::Revolution\",\"%s\")",FeatName.c_str());
00295     doCommand(Doc,"App.activeDocument().%s.Sketch = App.activeDocument().%s",FeatName.c_str(),sketch->getNameInDocument());
00296     doCommand(Doc,"App.activeDocument().%s.Axis = App.Vector(0,1,0)",FeatName.c_str());
00297     doCommand(Doc,"App.activeDocument().%s.Angle = 360.0",FeatName.c_str());
00298     updateActive();
00299     if (isActiveObjectValid()) {
00300         doCommand(Gui,"Gui.activeDocument().hide(\"%s\")",sketch->getNameInDocument());
00301         if (support)
00302             doCommand(Gui,"Gui.activeDocument().hide(\"%s\")",support->getNameInDocument());
00303     }
00304     doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
00305 
00306     if (support) {
00307         copyVisual(FeatName.c_str(), "ShapeColor", support->getNameInDocument());
00308         copyVisual(FeatName.c_str(), "LineColor", support->getNameInDocument());
00309         copyVisual(FeatName.c_str(), "PointColor", support->getNameInDocument());
00310     }
00311 }
00312 
00313 bool CmdPartDesignRevolution::isActive(void)
00314 {
00315     return hasActiveDocument();
00316 }
00317 
00318 //===========================================================================
00319 // PartDesign_Fillet
00320 //===========================================================================
00321 DEF_STD_CMD_A(CmdPartDesignFillet);
00322 
00323 CmdPartDesignFillet::CmdPartDesignFillet()
00324   :Command("PartDesign_Fillet")
00325 {
00326     sAppModule    = "PartDesign";
00327     sGroup        = QT_TR_NOOP("PartDesign");
00328     sMenuText     = QT_TR_NOOP("Fillet");
00329     sToolTipText  = QT_TR_NOOP("Make a fillet on an edge, face or body");
00330     sWhatsThis    = sToolTipText;
00331     sStatusTip    = sToolTipText;
00332     sPixmap       = "Part_Fillet";
00333 }
00334 
00335 void CmdPartDesignFillet::activated(int iMsg)
00336 {
00337     std::vector<Gui::SelectionObject> selection = getSelection().getSelectionEx();
00338 
00339     if (selection.size() != 1) {
00340         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong selection"),
00341             QObject::tr("Select an edge, face or body. Only one body is allowed."));
00342         return;
00343     }
00344 
00345     if (!selection[0].isObjectTypeOf(Part::Feature::getClassTypeId())){
00346         QMessageBox::warning(Gui::getMainWindow(), QObject::tr("Wrong object type"),
00347             QObject::tr("Fillet works only on parts"));
00348         return;
00349     }
00350     std::string SelString = selection[0].getAsPropertyLinkSubString();
00351     std::string FeatName = getUniqueObjectName("Fillet");
00352 
00353     openCommand("Make Fillet");
00354     doCommand(Doc,"App.activeDocument().addObject(\"PartDesign::Fillet\",\"%s\")",FeatName.c_str());
00355     doCommand(Doc,"App.activeDocument().%s.Base = %s",FeatName.c_str(),SelString.c_str());
00356     doCommand(Gui,"Gui.activeDocument().hide(\"%s\")",selection[0].getFeatName());
00357     doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
00358 
00359     copyVisual(FeatName.c_str(), "ShapeColor", selection[0].getFeatName());
00360     copyVisual(FeatName.c_str(), "LineColor",  selection[0].getFeatName());
00361     copyVisual(FeatName.c_str(), "PointColor", selection[0].getFeatName());
00362 }
00363 
00364 bool CmdPartDesignFillet::isActive(void)
00365 {
00366     return hasActiveDocument();
00367 }
00368 
00369 //===========================================================================
00370 // PartDesign_Chamfer
00371 //===========================================================================
00372 DEF_STD_CMD_A(CmdPartDesignChamfer);
00373 
00374 CmdPartDesignChamfer::CmdPartDesignChamfer()
00375   :Command("PartDesign_Chamfer")
00376 {
00377     sAppModule    = "PartDesign";
00378     sGroup        = QT_TR_NOOP("Part");
00379     sMenuText     = QT_TR_NOOP("Chamfer...");
00380     sToolTipText  = QT_TR_NOOP("Chamfer the selected edges of a shape");
00381     sWhatsThis    = sToolTipText;
00382     sStatusTip    = sToolTipText;
00383     sPixmap       = "Part_Chamfer";
00384 }
00385 
00386 void CmdPartDesignChamfer::activated(int iMsg)
00387 {
00388     Gui::Control().showDialog(new PartDesignGui::TaskChamfer());
00389 }
00390 
00391 bool CmdPartDesignChamfer::isActive(void)
00392 {
00393     return (hasActiveDocument() && !Gui::Control().activeDialog());
00394 }
00395 
00396 
00397 void CreatePartDesignCommands(void)
00398 {
00399     Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
00400 
00401     rcCmdMgr.addCommand(new CmdPartDesignPad());
00402     rcCmdMgr.addCommand(new CmdPartDesignPocket());
00403     rcCmdMgr.addCommand(new CmdPartDesignRevolution());
00404     rcCmdMgr.addCommand(new CmdPartDesignFillet());
00405     //rcCmdMgr.addCommand(new CmdPartDesignNewSketch());
00406     rcCmdMgr.addCommand(new CmdPartDesignChamfer());
00407  }

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