Implemented SendToPythonConsole FreeCAD-wide (edit and context menu)

This commit is contained in:
Yorik van Havre
2020-02-04 10:02:29 +01:00
parent fbd534a57b
commit 5eb1ddaeac
2 changed files with 64 additions and 3 deletions

View File

@@ -111,6 +111,63 @@ bool StdCmdRandomColor::isActive(void)
}
//===========================================================================
// Std_SendToPythonConsole
//===========================================================================
DEF_STD_CMD_A(StdCmdSendToPythonConsole)
StdCmdSendToPythonConsole::StdCmdSendToPythonConsole()
:Command("Std_SendToPythonConsole")
{
// setting the
sGroup = QT_TR_NOOP("Edit");
sMenuText = QT_TR_NOOP("&Send to Python Console");
sToolTipText = QT_TR_NOOP("Sends the selected object to the Python console");
sWhatsThis = "Std_SendToPythonConsole";
sStatusTip = QT_TR_NOOP("Sends the selected object to the Python console");
sPixmap = "applications-python";
sAccel = "Ctrl+Shift+P";
}
bool StdCmdSendToPythonConsole::isActive(void)
{
return (Gui::Selection().size() == 1);
}
void StdCmdSendToPythonConsole::activated(int iMsg)
{
Q_UNUSED(iMsg);
const std::vector<Gui::SelectionObject> &sels = Gui::Selection().getSelectionEx("*",App::DocumentObject::getClassTypeId(),true,true);
if (sels.empty())
return;
const App::DocumentObject *obj = sels[0].getObject();
QString docname = QString::fromLatin1(obj->getDocument()->getName());
QString objname = QString::fromLatin1(obj->getNameInDocument());
try {
QString cmd = QString::fromLatin1("obj = App.getDocument(\"%1\").getObject(\"%2\")").arg(docname,objname);
Gui::Command::runCommand(Gui::Command::Gui,cmd.toLatin1());
if (sels[0].hasSubNames()) {
std::vector<std::string> subnames = sels[0].getSubNames();
if (obj->getPropertyByName("Shape")) {
QString subname = QString::fromLatin1(subnames[0].c_str());
cmd = QString::fromLatin1("shp = App.getDocument(\"%1\").getObject(\"%2\").Shape")
.arg(docname, objname);
Gui::Command::runCommand(Gui::Command::Gui,cmd.toLatin1());
cmd = QString::fromLatin1("elt = App.getDocument(\"%1\").getObject(\"%2\").Shape.%4")
.arg(docname,objname,subname);
Gui::Command::runCommand(Gui::Command::Gui,cmd.toLatin1());
}
}
}
catch (const Base::Exception& e) {
e.ReportException();
}
}
namespace Gui {
void CreateFeatCommands(void)
@@ -119,6 +176,7 @@ void CreateFeatCommands(void)
rcCmdMgr.addCommand(new StdCmdFeatRecompute());
rcCmdMgr.addCommand(new StdCmdRandomColor());
rcCmdMgr.addCommand(new StdCmdSendToPythonConsole());
}
} // namespace Gui

View File

@@ -495,7 +495,8 @@ void StdWorkbench::setupContextMenu(const char* recipient, MenuItem* item) const
if (Gui::Selection().countObjectsOfType(App::DocumentObject::getClassTypeId()) > 0) {
*item << "Separator" << "Std_SetAppearance" << "Std_ToggleVisibility"
<< "Std_ToggleSelectability" << "Std_TreeSelection"
<< "Std_RandomColor" << "Separator" << "Std_Delete";
<< "Std_RandomColor" << "Separator" << "Std_Delete"
<< "Std_SendToPythonConsole";
}
}
else if (strcmp(recipient,"Tree") == 0)
@@ -504,7 +505,8 @@ void StdWorkbench::setupContextMenu(const char* recipient, MenuItem* item) const
*item << "Std_ToggleVisibility" << "Std_ShowSelection" << "Std_HideSelection"
<< "Std_ToggleSelectability" << "Std_TreeSelectAllInstances" << "Separator"
<< "Std_SetAppearance" << "Std_RandomColor" << "Separator"
<< "Std_Cut" << "Std_Copy" << "Std_Paste" << "Std_Delete" << "Separator";
<< "Std_Cut" << "Std_Copy" << "Std_Paste" << "Std_Delete"
<< "Std_SendToPythonConsole" << "Separator";
}
}
}
@@ -534,7 +536,8 @@ MenuItem* StdWorkbench::setupMenuBar() const
edit->setCommand("&Edit");
*edit << "Std_Undo" << "Std_Redo" << "Separator" << "Std_Cut" << "Std_Copy"
<< "Std_Paste" << "Std_DuplicateSelection" << "Separator"
<< "Std_Refresh" << "Std_BoxSelection" << "Std_BoxElementSelection" << "Std_SelectAll" << "Std_Delete"
<< "Std_Refresh" << "Std_BoxSelection" << "Std_BoxElementSelection"
<< "Std_SelectAll" << "Std_Delete" << "Std_SendToPythonConsole"
<< "Separator" << "Std_Placement" /*<< "Std_TransformManip"*/ << "Std_Alignment"
<< "Std_Edit" << "Separator" << "Std_DlgPreferences";