From dde81fe8f58c6ce1081c3ccaccc79403fee9aa08 Mon Sep 17 00:00:00 2001 From: jrheinlaender Date: Sun, 31 Mar 2013 17:43:37 +0430 Subject: [PATCH] Create standard XY, XZ, YZ planes when running the PartDesign_Body command if they don't exist yet --- src/Mod/PartDesign/Gui/Command.cpp | 45 +++++++++++++++++++++++++-- src/Mod/PartDesign/Gui/Workbench.cpp | 23 ++++++++------ src/Mod/Start/StartPage/PartDesign.py | 7 ++--- 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/src/Mod/PartDesign/Gui/Command.cpp b/src/Mod/PartDesign/Gui/Command.cpp index 132c17e6ad..2f5ca4e1e3 100644 --- a/src/Mod/PartDesign/Gui/Command.cpp +++ b/src/Mod/PartDesign/Gui/Command.cpp @@ -41,6 +41,7 @@ #include #include +#include #include #include #include @@ -100,10 +101,48 @@ CmdPartDesignBody::CmdPartDesignBody() void CmdPartDesignBody::activated(int iMsg) { - std::string FeatName = getUniqueObjectName("Body"); openCommand("Add a body feature"); - doCommand(Doc,"App.activeDocument().addObject('PartDesign::Body','%s')",FeatName.c_str()); + std::string FeatName = getUniqueObjectName("Body"); + // add the standard planes at the root of the feature tree + // first check if they already exist + // FIXME: If the user renames them, they won't be found... + bool found = false; + std::vector planes = getDocument()->getObjectsOfType(App::Plane::getClassTypeId()); + for (std::vector::const_iterator p = planes.begin(); p != planes.end(); p++) { + if ((strcmp("Body_PlaneXY", (*p)->getNameInDocument()) == 0) || + (strcmp("Body_PlaneYZ", (*p)->getNameInDocument()) == 0) || + (strcmp("Body_PlaneXZ", (*p)->getNameInDocument()) == 0)) { + found = true; + break; + } + } + + if (!found) { + // Add the planes ... + doCommand(Doc,"App.activeDocument().addObject('App::Plane','Body_PlaneXY')"); + doCommand(Doc,"App.activeDocument().ActiveObject.Label = 'XY-Plane'"); + doCommand(Doc,"App.activeDocument().addObject('App::Plane','Body_PlaneYZ')"); + doCommand(Doc,"App.activeDocument().ActiveObject.Placement = App.Placement(App.Vector(),App.Rotation(App.Vector(0,1,0),90))"); + doCommand(Doc,"App.activeDocument().ActiveObject.Label = 'YZ-Plane'"); + doCommand(Doc,"App.activeDocument().addObject('App::Plane','Body_PlaneXZ')"); + doCommand(Doc,"App.activeDocument().ActiveObject.Placement = App.Placement(App.Vector(),App.Rotation(App.Vector(1,0,0),90))"); + doCommand(Doc,"App.activeDocument().ActiveObject.Label = 'XZ-Plane'"); + // ... and put them in the 'Origin' group + doCommand(Doc,"App.activeDocument().addObject('App::DocumentObjectGroup','Origin')"); + doCommand(Doc,"App.activeDocument().Origin.addObject(App.activeDocument().getObject('Body_PlaneXY'))"); + doCommand(Doc,"App.activeDocument().Origin.addObject(App.activeDocument().getObject('Body_PlaneYZ'))"); + doCommand(Doc,"App.activeDocument().Origin.addObject(App.activeDocument().getObject('Body_PlaneXZ'))"); + // TODO: Fold the group (is that possible through the Python interface?) + } + + // add the Body feature itself, and make it active + doCommand(Doc,"App.activeDocument().addObject('PartDesign::Body','%s')",FeatName.c_str()); + doCommand(Gui,"PartDesignGui.setActivePart(App.ActiveDocument.ActiveObject)"); + // Make the "Create sketch" prompt appear in the task panel + doCommand(Gui,"Gui.Selection.addSelection(App.ActiveDocument.ActiveObject)"); + + updateActive(); } bool CmdPartDesignBody::isActive(void) @@ -960,7 +999,7 @@ bool CmdPartDesignDraft::isActive(void) //=========================================================================== // Common functions for all Transformed features //=========================================================================== -;;; + void prepareTransformed(Gui::Command* cmd, const std::string& which, std::vector& features, std::string& FeatName, std::vector& selList, std::string& selNames) diff --git a/src/Mod/PartDesign/Gui/Workbench.cpp b/src/Mod/PartDesign/Gui/Workbench.cpp index 3738605bac..4158d905a8 100644 --- a/src/Mod/PartDesign/Gui/Workbench.cpp +++ b/src/Mod/PartDesign/Gui/Workbench.cpp @@ -153,17 +153,22 @@ void Workbench::activated() // make the previously used active Body active again PartDesign::Body* activeBody = NULL; - std::vector bodies = App::GetApplication().getActiveDocument()->getObjectsOfType(PartDesign::Body::getClassTypeId()); - for (std::vector::const_iterator b = bodies.begin(); b != bodies.end(); b++) { - PartDesign::Body* body = static_cast(*b); - if (body->IsActive.getValue()) { - activeBody = body; - break; + App::Document* activeDocument = App::GetApplication().getActiveDocument(); + if (activeDocument != NULL) { + std::vector bodies = activeDocument->getObjectsOfType(PartDesign::Body::getClassTypeId()); + for (std::vector::const_iterator b = bodies.begin(); b != bodies.end(); b++) { + PartDesign::Body* body = static_cast(*b); + if (body->IsActive.getValue()) { + activeBody = body; + break; + } } + + // If there is only one body, make it active + if ((activeBody == NULL) && (bodies.size() == 1)) + activeBody = static_cast(bodies.front()); } - // If there is only one body, make it active - if ((activeBody == NULL) && (bodies.size() == 1)) - activeBody = static_cast(bodies.front()); + if (activeBody != NULL) { Gui::Command::doCommand(Gui::Command::Doc,"import PartDesignGui"); diff --git a/src/Mod/Start/StartPage/PartDesign.py b/src/Mod/Start/StartPage/PartDesign.py index 4ce8753289..3931e02570 100644 --- a/src/Mod/Start/StartPage/PartDesign.py +++ b/src/Mod/Start/StartPage/PartDesign.py @@ -21,10 +21,7 @@ #* * #*************************************************************************** -import FreeCADGui, PartDesignGui +import FreeCADGui FreeCADGui.activateWorkbench("PartDesignWorkbench") App.newDocument() -App.ActiveDocument.addObject("PartDesign::Body") -PartDesignGui.setActivePart(App.ActiveDocument.ActiveObject) -# Make the "Create sketch" prompt appear in the task panel -Gui.Selection.addSelection(App.ActiveDocument.ActiveObject) +FreeCADGui.runCommand('PartDesign_Body')