diff --git a/src/Mod/PartDesign/Gui/Command.cpp b/src/Mod/PartDesign/Gui/Command.cpp index a1136add0..093c97889 100644 --- a/src/Mod/PartDesign/Gui/Command.cpp +++ b/src/Mod/PartDesign/Gui/Command.cpp @@ -48,6 +48,7 @@ #include #include +#include #include #include #include @@ -75,7 +76,7 @@ #include #include #include - +#include "Workbench.h" #include "FeaturePickDialog.h" #include "Workbench.h" @@ -105,37 +106,14 @@ void CmdPartDesignBody::activated(int iMsg) openCommand("Add a body feature"); 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++) { - for (unsigned i = 0; i < 3; i++) { - if (strcmp(PartDesignGui::BaseplaneNames[i], (*p)->getNameInDocument()) == 0) { - found = true; - break; - } - } - if (found) break; - } + // first check if Part is already created: + App::Part *actPart = getDocument()->Tip.getValue(); - if (!found) { - // Add the planes ... - doCommand(Doc,"App.activeDocument().addObject('App::Plane','%s')", PartDesignGui::BaseplaneNames[0]); - doCommand(Doc,"App.activeDocument().ActiveObject.Label = '%s'", QObject::tr("XY-Plane").toStdString().c_str()); - doCommand(Doc,"App.activeDocument().addObject('App::Plane','%s')", PartDesignGui::BaseplaneNames[1]); - doCommand(Doc,"App.activeDocument().ActiveObject.Placement = App.Placement(App.Vector(),App.Rotation(App.Vector(1,0,0),-90))"); - doCommand(Doc,"App.activeDocument().ActiveObject.Label = '%s'", QObject::tr("XZ-Plane").toStdString().c_str()); - doCommand(Doc,"App.activeDocument().addObject('App::Plane','%s')", PartDesignGui::BaseplaneNames[2]); - doCommand(Doc,"App.activeDocument().ActiveObject.Placement = App.Placement(App.Vector(),App.Rotation(App.Vector(0,1,0),90))"); - doCommand(Doc,"App.activeDocument().ActiveObject.Label = '%s'", QObject::tr("YZ-Plane").toStdString().c_str()); - // ... and put them in the 'Origin' group - doCommand(Doc,"App.activeDocument().addObject('App::DocumentObjectGroup','%s')", QObject::tr("Origin").toStdString().c_str()); - for (unsigned i = 0; i < 3; i++) - doCommand(Doc,"App.activeDocument().Origin.addObject(App.activeDocument().getObject('%s'))", PartDesignGui::BaseplaneNames[i]); - // TODO: Fold the group (is that possible through the Python interface?) - } + if(!actPart){ + std::string PartName = getUniqueObjectName("Part"); + doCommand(Doc,"App.activeDocument().addObject('App::Part','%s')",PartName.c_str()); + PartDesignGui::Workbench::setUpPart(dynamic_cast(getDocument()->getObject(PartName.c_str()))); + } // add the Body feature itself, and make it active doCommand(Doc,"App.activeDocument().addObject('PartDesign::Body','%s')",FeatName.c_str()); diff --git a/src/Mod/PartDesign/Gui/Workbench.cpp b/src/Mod/PartDesign/Gui/Workbench.cpp index 021875d3c..5257e17ca 100644 --- a/src/Mod/PartDesign/Gui/Workbench.cpp +++ b/src/Mod/PartDesign/Gui/Workbench.cpp @@ -35,6 +35,7 @@ #include "Workbench.h" #include +#include #include #include #include @@ -95,6 +96,45 @@ Workbench::~Workbench() { } + +PartDesign::Body *Workbench::setUpPart(const App::Part *part) +{ + // 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 = part->getObjectsOfType(App::Plane::getClassTypeId()); + for (std::vector::const_iterator p = planes.begin(); p != planes.end(); p++) { + for (unsigned i = 0; i < 3; i++) { + if (strcmp(PartDesignGui::BaseplaneNames[i], (*p)->getNameInDocument()) == 0) { + found = true; + break; + } + } + if (found) break; + } + + if (!found) { + // Add the planes ... + Gui::Command::doCommand( Gui::Command::Doc,"App.activeDocument().addObject('App::Plane','%s')", PartDesignGui::BaseplaneNames[0]); + Gui::Command::doCommand(Gui::Command::Doc,"App.activeDocument().ActiveObject.Label = '%s'", QObject::tr("XY-Plane").toStdString().c_str()); + Gui::Command::doCommand(Gui::Command::Doc,"App.activeDocument().addObject('App::Plane','%s')", PartDesignGui::BaseplaneNames[1]); + Gui::Command::doCommand(Gui::Command::Doc,"App.activeDocument().ActiveObject.Placement = App.Placement(App.Vector(),App.Rotation(App.Vector(1,0,0),-90))"); + Gui::Command::doCommand(Gui::Command::Doc,"App.activeDocument().ActiveObject.Label = '%s'", QObject::tr("XZ-Plane").toStdString().c_str()); + Gui::Command::doCommand(Gui::Command::Doc,"App.activeDocument().addObject('App::Plane','%s')", PartDesignGui::BaseplaneNames[2]); + Gui::Command::doCommand(Gui::Command::Doc,"App.activeDocument().ActiveObject.Placement = App.Placement(App.Vector(),App.Rotation(App.Vector(0,1,0),90))"); + Gui::Command::doCommand(Gui::Command::Doc,"App.activeDocument().ActiveObject.Label = '%s'", QObject::tr("YZ-Plane").toStdString().c_str()); + // ... and put them in the 'Origin' group + Gui::Command::doCommand(Gui::Command::Doc,"App.activeDocument().addObject('App::DocumentObjectGroup','%s')", QObject::tr("Origin").toStdString().c_str()); + for (unsigned i = 0; i < 3; i++) + Gui::Command::doCommand(Gui::Command::Doc,"App.activeDocument().Origin.addObject(App.activeDocument().getObject('%s'))", PartDesignGui::BaseplaneNames[i]); + // TODO: Fold the group (is that possible through the Python interface?) + } + + return NULL; +} + + void switchToDocument(const App::Document* doc) { if (doc == NULL) return; diff --git a/src/Mod/PartDesign/Gui/Workbench.h b/src/Mod/PartDesign/Gui/Workbench.h index bfaf79011..3e9cd2853 100644 --- a/src/Mod/PartDesign/Gui/Workbench.h +++ b/src/Mod/PartDesign/Gui/Workbench.h @@ -35,9 +35,11 @@ class ViewProviderDocumentObject; } namespace PartDesign { + class Body; +} -class Body; - +namespace App { + class Part; } namespace PartDesignGui { @@ -72,6 +74,16 @@ public: /// Add custom entries to the context menu virtual void setupContextMenu(const char* recipient, Gui::MenuItem*) const; + /** Setup a Part for PartDesign + * This methode is use to populate a Part object with all + * necesarry PartDesign and base objects to allow the use + * in PartDesign. Its called from within PartDesign as well + * as from other modules which wish to set up a Part for PartDesin + * (e.g. Assembly): + */ + static PartDesign::Body *setUpPart(const App::Part *); + + protected: Gui::MenuItem* setupMenuBar() const; Gui::ToolBarItem* setupToolBars() const;