CommandSimple.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 #ifndef _PreComp_
00026 # include <QDir>
00027 # include <QFileInfo>
00028 # include <QLineEdit>
00029 # include <QInputDialog>
00030 # include <Standard_math.hxx>
00031 #endif
00032 
00033 #include <Base/Exception.h>
00034 #include <App/Document.h>
00035 #include <App/DocumentObject.h>
00036 #include <Gui/Application.h>
00037 #include <Gui/Command.h>
00038 #include <Gui/Document.h>
00039 #include <Gui/MainWindow.h>
00040 #include <Gui/Selection.h>
00041 #include <Gui/WaitCursor.h>
00042 
00043 #include "../App/PartFeature.h"
00044 #include "../App/TopoShape.h"
00045 #include "DlgPartCylinderImp.h"
00046 
00047 
00048 //===========================================================================
00049 // Part_SimpleCylinder
00050 //===========================================================================
00051 DEF_STD_CMD_A(CmdPartSimpleCylinder);
00052 
00053 CmdPartSimpleCylinder::CmdPartSimpleCylinder()
00054   :Command("Part_SimpleCylinder")
00055 {
00056     sAppModule    = "Part";
00057     sGroup        = QT_TR_NOOP("Part");
00058     sMenuText     = QT_TR_NOOP("Create Cylinder...");
00059     sToolTipText  = QT_TR_NOOP("Create a Cylinder");
00060     sWhatsThis    = sToolTipText;
00061     sStatusTip    = sToolTipText;
00062     sPixmap       = "Part_Cylinder";
00063 }
00064 
00065 void CmdPartSimpleCylinder::activated(int iMsg)
00066 {
00067     PartGui::DlgPartCylinderImp dlg(Gui::getMainWindow());
00068     if (dlg.exec()== QDialog::Accepted) {
00069         Base::Vector3f dir = dlg.getDirection();
00070         openCommand("Create Part Cylinder");
00071         doCommand(Doc,"from FreeCAD import Base");
00072         doCommand(Doc,"import Part");
00073         doCommand(Doc,"App.ActiveDocument.addObject(\"Part::Feature\",\"Cylinder\")"
00074                       ".Shape=Part.makeCylinder(%f,%f,"
00075                       "Base.Vector(%f,%f,%f),"
00076                       "Base.Vector(%f,%f,%f))"
00077                      ,dlg.radius->value()
00078                      ,dlg.length->value()
00079                      ,dlg.xPos->value()
00080                      ,dlg.yPos->value()
00081                      ,dlg.zPos->value()
00082                      ,dir.x,dir.y,dir.z);
00083         commitCommand();
00084         updateActive();
00085         doCommand(Gui, "Gui.SendMsgToActiveView(\"ViewFit\")");
00086     }
00087 }
00088 
00089 bool CmdPartSimpleCylinder::isActive(void)
00090 {
00091     if (getActiveGuiDocument())
00092         return true;
00093     else
00094         return false;
00095 }
00096 
00097 
00098 //===========================================================================
00099 // Part_ShapeFromMesh
00100 //===========================================================================
00101 DEF_STD_CMD_A(CmdPartShapeFromMesh);
00102 
00103 CmdPartShapeFromMesh::CmdPartShapeFromMesh()
00104   :Command("Part_ShapeFromMesh")
00105 {
00106     sAppModule    = "Part";
00107     sGroup        = QT_TR_NOOP("Part");
00108     sMenuText     = QT_TR_NOOP("Create shape from mesh...");
00109     sToolTipText  = QT_TR_NOOP("Create shape from selected mesh object");
00110     sWhatsThis    = sToolTipText;
00111     sStatusTip    = sToolTipText;
00112 }
00113 
00114 void CmdPartShapeFromMesh::activated(int iMsg)
00115 {
00116     bool ok;
00117     double tol = QInputDialog::getDouble(Gui::getMainWindow(), QObject::tr("Sewing Tolerance"),
00118         QObject::tr("Enter tolerance for sewing shape:"), 0.1, 0.01,10.0,2,&ok);
00119     if (!ok)
00120         return;
00121     Base::Type meshid = Base::Type::fromName("Mesh::Feature");
00122     std::vector<App::DocumentObject*> meshes;
00123     meshes = Gui::Selection().getObjectsOfType(meshid);
00124     Gui::WaitCursor wc;
00125     std::vector<App::DocumentObject*>::iterator it;
00126     openCommand("Convert mesh");
00127     for (it = meshes.begin(); it != meshes.end(); ++it) {
00128         App::Document* doc = (*it)->getDocument();
00129         std::string mesh = (*it)->getNameInDocument();
00130         std::string name = doc->getUniqueObjectName(mesh.c_str());
00131         doCommand(Doc,"import Part");
00132         doCommand(Doc,"FreeCAD.getDocument(\"%s\").addObject(\"Part::Feature\",\"%s\")"
00133                      ,doc->getName()
00134                      ,name.c_str());
00135         doCommand(Doc,"__shape__=Part.Shape()");
00136         doCommand(Doc,"__shape__.makeShapeFromMesh("
00137                       "FreeCAD.getDocument(\"%s\").getObject(\"%s\").Mesh.Topology,%f"
00138                       ")"
00139                      ,doc->getName()
00140                      ,mesh.c_str()
00141                      ,tol);
00142         doCommand(Doc,"FreeCAD.getDocument(\"%s\").getObject(\"%s\").Shape=__shape__"
00143                      ,doc->getName()
00144                      ,name.c_str());
00145         doCommand(Doc,"FreeCAD.getDocument(\"%s\").getObject(\"%s\").purgeTouched()"
00146                      ,doc->getName()
00147                      ,name.c_str());
00148         doCommand(Doc,"del __shape__");
00149     }
00150 
00151     commitCommand();
00152 }
00153 
00154 bool CmdPartShapeFromMesh::isActive(void)
00155 {
00156     Base::Type meshid = Base::Type::fromName("Mesh::Feature");
00157     return Gui::Selection().countObjectsOfType(meshid) > 0;
00158 }
00159 
00160 //===========================================================================
00161 // Part_SimpleCopy
00162 //===========================================================================
00163 DEF_STD_CMD_A(CmdPartSimpleCopy);
00164 
00165 CmdPartSimpleCopy::CmdPartSimpleCopy()
00166   : Command("Part_SimpleCopy")
00167 {
00168     sAppModule    = "Part";
00169     sGroup        = QT_TR_NOOP("Part");
00170     sMenuText     = QT_TR_NOOP("Create simple copy");
00171     sToolTipText  = QT_TR_NOOP("Create a simple non-parametric copy");
00172     sWhatsThis    = sToolTipText;
00173     sStatusTip    = sToolTipText;
00174 }
00175 
00176 void CmdPartSimpleCopy::activated(int iMsg)
00177 {
00178     Base::Type partid = Base::Type::fromName("Part::Feature");
00179     std::vector<App::DocumentObject*> objs = Gui::Selection().getObjectsOfType(partid);
00180     openCommand("Create Copy");
00181     for (std::vector<App::DocumentObject*>::iterator it = objs.begin(); it != objs.end(); ++it) {
00182         doCommand(Doc,"App.ActiveDocument.addObject('Part::Feature','%s').Shape="
00183                       "App.ActiveDocument.%s.Shape\n"
00184                       "App.ActiveDocument.ActiveObject.Label="
00185                       "App.ActiveDocument.%s.Label\n",
00186                       (*it)->getNameInDocument(),
00187                       (*it)->getNameInDocument(),
00188                       (*it)->getNameInDocument());
00189         copyVisual("ActiveObject", "ShapeColor", (*it)->getNameInDocument());
00190         copyVisual("ActiveObject", "LineColor", (*it)->getNameInDocument());
00191         copyVisual("ActiveObject", "PointColor", (*it)->getNameInDocument());
00192     }
00193     commitCommand();
00194     updateActive();
00195 }
00196 
00197 bool CmdPartSimpleCopy::isActive(void)
00198 {
00199     Base::Type partid = Base::Type::fromName("Part::Feature");
00200     return Gui::Selection().countObjectsOfType(partid) > 0;
00201 }
00202 
00203 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00204 
00205 void CreateSimplePartCommands(void)
00206 {
00207     Gui::CommandManager &rcCmdMgr = Gui::Application::Instance->commandManager();
00208     rcCmdMgr.addCommand(new CmdPartSimpleCylinder());
00209     rcCmdMgr.addCommand(new CmdPartShapeFromMesh());
00210     rcCmdMgr.addCommand(new CmdPartSimpleCopy());
00211 } 

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