Skip to content

Commit

Permalink
PartDesign: Adopt python interface to body group
Browse files Browse the repository at this point in the history
  • Loading branch information
ickby authored and wwmayer committed Jan 4, 2017
1 parent b4a569e commit d2764a3
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 104 deletions.
2 changes: 1 addition & 1 deletion src/Mod/PartDesign/App/Body.cpp
Expand Up @@ -62,7 +62,7 @@ Body::Body() {
// Note: The following code will catch Python Document::removeObject() modifications. If the object removed is
// a member of the Body::Group, then it will be automatically removed from the Group property which triggers the
// following two methods
// But since we require the Python user to call both Document::addObject() and Body::addFeature(), we should
// But since we require the Python user to call both Document::addObject() and Body::addObject(), we should
// also require calling both Document::removeObject and Body::removeFeature() in order to be consistent
void Body::onBeforeChange(const App::Property *prop)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/PartDesign/App/Body.h
Expand Up @@ -79,7 +79,7 @@ class PartDesignExport Body : public Part::BodyBase
* and into the begin if where is InsertAfter.
* @param after if true insert the feature after the target. Default is false.
*
* @note the methode doesn't modifies the Tip unlike addFeature()
* @note the methode doesn't modifies the Tip unlike addObject()
*/
void insertObject(App::DocumentObject* feature, App::DocumentObject* target, bool after=false);

Expand Down
21 changes: 3 additions & 18 deletions src/Mod/PartDesign/App/BodyPy.xml
Expand Up @@ -13,14 +13,9 @@
<Author Licence="LGPL" Name="Juergen Riegel" EMail="FreeCAD@juergen-riegel.net" />
<UserDocu>PartDesign body class</UserDocu>
</Documentation>
<Methode Name="addFeature">
<Methode Name="insertObject">
<Documentation>
<UserDocu>addFeature(feat) - Add the given feature after the current Tip feature</UserDocu>
</Documentation>
</Methode>
<Methode Name="insertFeature">
<Documentation>
<UserDocu>insertFeatureAfter(feature, target, after=False)
<UserDocu>insertObject(feature, target, after=False)
Insert the feature into the body after the given feature.

@param feature The feature to insert into the body
Expand All @@ -29,19 +24,9 @@
and into the begin if where is InsertAfter.
@param after if true insert the feature after the target. Default is false.

@note the methode doesn't modifies the Tip unlike addFeature()
@note the methode doesn't modifies the Tip unlike addObject()
</UserDocu>
</Documentation>
</Methode>
<Methode Name="removeFeature">
<Documentation>
<UserDocu>removeFeature(feat) - Remove the given feature from the Body</UserDocu>
</Documentation>
</Methode>
<Methode Name="removeModelFromDocument">
<Documentation>
<UserDocu>Delets all the objects linked to the model.</UserDocu>
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>
56 changes: 1 addition & 55 deletions src/Mod/PartDesign/App/BodyPyImp.cpp
Expand Up @@ -52,32 +52,7 @@ int BodyPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
return 0;
}

PyObject* BodyPy::addFeature(PyObject *args)
{
PyObject* featurePy;
if (!PyArg_ParseTuple(args, "O!", &(App::DocumentObjectPy::Type), &featurePy))
return 0;

App::DocumentObject* feature = static_cast<App::DocumentObjectPy*>(featurePy)->getDocumentObjectPtr();

if (!Body::isAllowed(feature)) {
PyErr_SetString(PyExc_SystemError, "Only PartDesign features, datum features and sketches can be inserted into a Body");
return 0;
}

Body* body = this->getBodyPtr();

try {
body->addObject(feature);
} catch (Base::Exception& e) {
PyErr_SetString(PyExc_SystemError, e.what());
return 0;
}

Py_Return;
}

PyObject* BodyPy::insertFeature(PyObject *args)
PyObject* BodyPy::insertObject(PyObject *args)
{
PyObject* featurePy;
PyObject* targetPy;
Expand Down Expand Up @@ -115,32 +90,3 @@ PyObject* BodyPy::insertFeature(PyObject *args)

Py_Return;
}

PyObject* BodyPy::removeFeature(PyObject *args)
{
PyObject* featurePy;
if (!PyArg_ParseTuple(args, "O!", &(App::DocumentObjectPy::Type), &featurePy))
return 0;

App::DocumentObject* feature = static_cast<App::DocumentObjectPy*>(featurePy)->getDocumentObjectPtr();
Body* body = this->getBodyPtr();

try {
body->removeObject(feature);
} catch (Base::Exception& e) {
PyErr_SetString(PyExc_SystemError, e.what());
return 0;
}

Py_Return;
}

PyObject* BodyPy::removeModelFromDocument(PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return 0;

getBodyPtr()->removeObjectsFromDocument();
Py_Return;
}

10 changes: 5 additions & 5 deletions src/Mod/PartDesign/FeatureHole/TaskHole.py
Expand Up @@ -54,18 +54,18 @@ def reject(self):
sketch = groove.Sketch
plane = sketch.Support[0]
axis = plane.References[0][0]
body.removeFeature(self.feature)
body.removeObject(self.feature)
document.removeObject(self.feature.Name)
body.removeFeature(groove)
body.removeObject(groove)
document.removeObject(groove.Name)
body.removeFeature(sketch)
body.removeObject(sketch)
try:
document.removeObject(sketch.Name)
except:
pass # This always throws an exception: "Sketch support has been deleted" from SketchObject::execute()
body.removeFeature(plane)
body.removeObject(plane)
document.removeObject(plane.Name)
body.removeFeature(axis)
body.removeObject(axis)
document.removeObject(axis.Name)
FreeCADGui.ActiveDocument.resetEdit()
FreeCADGui.Control.closeDialog(self)
Expand Down
14 changes: 7 additions & 7 deletions src/Mod/PartDesign/Gui/Command.cpp
Expand Up @@ -125,7 +125,7 @@ void UnifiedDatumCommand(Gui::Command &cmd, Base::Type type, std::string name)
}
}
if (pcActiveBody) {
cmd.doCommand(Gui::Command::Doc,"App.activeDocument().%s.addFeature(App.activeDocument().%s)",
cmd.doCommand(Gui::Command::Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",
pcActiveBody->getNameInDocument(), FeatName.c_str());
}
cmd.doCommand(Gui::Command::Doc,"App.activeDocument().recompute()"); // recompute the feature based on its references
Expand Down Expand Up @@ -278,7 +278,7 @@ void CmdPartDesignShapeBinder::activated(int iMsg)
doCommand(Gui::Command::Doc,"App.activeDocument().%s.Support = %s",
FeatName.c_str(), support.getPyReprString().c_str());
}
doCommand(Gui::Command::Doc,"App.activeDocument().%s.addFeature(App.activeDocument().%s)",
doCommand(Gui::Command::Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",
pcActiveBody->getNameInDocument(), FeatName.c_str());
doCommand(Gui::Command::Doc,"App.activeDocument().recompute()"); // recompute the feature based on its references
doCommand(Gui::Command::Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
Expand Down Expand Up @@ -429,7 +429,7 @@ void CmdPartDesignNewSketch::activated(int iMsg)
doCommand(Doc,"App.activeDocument().addObject('Sketcher::SketchObject','%s')",FeatName.c_str());
doCommand(Doc,"App.activeDocument().%s.Support = %s",FeatName.c_str(),supportString.c_str());
doCommand(Doc,"App.activeDocument().%s.MapMode = '%s'",FeatName.c_str(),Attacher::AttachEngine::getModeName(Attacher::mmFlatFace).c_str());
doCommand(Doc,"App.activeDocument().%s.addFeature(App.activeDocument().%s)",
doCommand(Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",
pcActiveBody->getNameInDocument(), FeatName.c_str());
doCommand(Gui,"App.activeDocument().recompute()"); // recompute the sketch placement based on its support
//doCommand(Gui,"Gui.activeDocument().activeView().setCamera('%s')",cam.c_str());
Expand Down Expand Up @@ -521,7 +521,7 @@ void CmdPartDesignNewSketch::activated(int iMsg)
Gui::Command::doCommand(Doc,"App.activeDocument().%s.Support = %s",FeatName.c_str(),supportString.c_str());
Gui::Command::doCommand(Doc,"App.activeDocument().%s.MapMode = '%s'",FeatName.c_str(),Attacher::AttachEngine::getModeName(Attacher::mmFlatFace).c_str());
Gui::Command::updateActive(); // Make sure the Support's Placement property is updated
Gui::Command::doCommand(Doc,"App.activeDocument().%s.addFeature(App.activeDocument().%s)",
Gui::Command::doCommand(Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",
pcActiveBody->getNameInDocument(), FeatName.c_str());
//doCommand(Gui,"Gui.activeDocument().activeView().setCamera('%s')",cam.c_str());
Gui::Command::doCommand(Gui,"Gui.activeDocument().setEdit('%s')",FeatName.c_str());
Expand Down Expand Up @@ -584,13 +584,13 @@ void finishFeature(const Gui::Command* cmd, const std::string& FeatName,
App::DocumentObject* lastSolidFeature = pcActiveBody->Tip.getValue();
if (!prevSolidFeature || prevSolidFeature == lastSolidFeature) {
// If the previous feature not given or is the Tip add Feature after it.
cmd->doCommand(cmd->Doc,"App.activeDocument().%s.addFeature(App.activeDocument().%s)",
cmd->doCommand(cmd->Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",
pcActiveBody->getNameInDocument(), FeatName.c_str());
prevSolidFeature = lastSolidFeature;
} else {
// Insert the feature into the body after the given one.
cmd->doCommand(cmd->Doc,
"App.activeDocument().%s.insertFeature(App.activeDocument().%s, App.activeDocument().%s, True)",
"App.activeDocument().%s.insertObject(App.activeDocument().%s, App.activeDocument().%s, True)",
pcActiveBody->getNameInDocument(), FeatName.c_str(), prevSolidFeature->getNameInDocument());
}
}
Expand Down Expand Up @@ -1855,7 +1855,7 @@ void CmdPartDesignMultiTransform::activated(int iMsg)

// Remove the Transformed feature from the Body
if(pcActiveBody)
doCommand(Doc, "App.activeDocument().%s.removeFeature(App.activeDocument().%s)",
doCommand(Doc, "App.activeDocument().%s.removeObject(App.activeDocument().%s)",
pcActiveBody->getNameInDocument(), trFeat->getNameInDocument());

// Create a MultiTransform feature and move the Transformed feature inside it
Expand Down
14 changes: 7 additions & 7 deletions src/Mod/PartDesign/Gui/CommandBody.cpp
Expand Up @@ -401,7 +401,7 @@ void CmdPartDesignMigrate::activated(int iMsg)
PartDesign::ProfileBased *sketchBased = static_cast<PartDesign::ProfileBased *> ( feature );
Part::Part2DObject *sketch = sketchBased->getVerifiedSketch( /*silent =*/ true);
if ( sketch ) {
doCommand ( Doc,"App.activeDocument().%s.addFeature(App.activeDocument().%s)",
doCommand ( Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",
bodyName.c_str (), sketch->getNameInDocument() );

if ( sketch->isDerivedFrom ( Sketcher::SketchObject::getClassTypeId() ) ) {
Expand All @@ -419,7 +419,7 @@ void CmdPartDesignMigrate::activated(int iMsg)
}
}
}
doCommand ( Doc,"App.activeDocument().%s.addFeature(App.activeDocument().%s)",
doCommand ( Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",
bodyName.c_str (), feature->getNameInDocument() );

PartDesignGui::relinkToBody ( feature );
Expand Down Expand Up @@ -552,7 +552,7 @@ void CmdPartDesignDuplicateSelection::activated(int iMsg)

for (auto feature : newFeatures) {
if (PartDesign::Body::isAllowed(feature)) {
doCommand(Doc,"App.activeDocument().%s.addFeature(App.activeDocument().%s)",
doCommand(Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",
pcActiveBody->getNameInDocument(), feature->getNameInDocument());
doCommand(Gui,"Gui.activeDocument().hide(\"%s\")", feature->getNameInDocument());
}
Expand Down Expand Up @@ -658,14 +658,14 @@ void CmdPartDesignMoveFeature::activated(int iMsg)
// Remove from the source body if the feature belonged to a body
if (source) {
featureWasTip = (source->Tip.getValue() == feat);
doCommand(Doc,"App.activeDocument().%s.removeFeature(App.activeDocument().%s)",
doCommand(Doc,"App.activeDocument().%s.removeObject(App.activeDocument().%s)",
source->getNameInDocument(), (feat)->getNameInDocument());
}

App::DocumentObject* targetOldTip = target->Tip.getValue();

// Add to target body (always at the Tip)
doCommand(Doc,"App.activeDocument().%s.addFeature(App.activeDocument().%s)",
doCommand(Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",
target->getNameInDocument(), (feat)->getNameInDocument());
// Recompute to update the shape
doCommand(Gui,"App.activeDocument().recompute()");
Expand Down Expand Up @@ -798,9 +798,9 @@ void CmdPartDesignMoveFeatureInTree::activated(int iMsg)
// Remove and re-insert the feature to/from the Body
// TODO if tip was moved the new position of tip is quite undetermined (2015-08-07, Fat-Zer)
// TODO warn the user if we are moving an object to some place before the object's link (2015-08-07, Fat-Zer)
doCommand ( Doc,"App.activeDocument().%s.removeFeature(App.activeDocument().%s)",
doCommand ( Doc,"App.activeDocument().%s.removeObject(App.activeDocument().%s)",
body->getNameInDocument(), feat->getNameInDocument() );
doCommand ( Doc, "App.activeDocument().%s.insertFeature(App.activeDocument().%s, %s, True)",
doCommand ( Doc, "App.activeDocument().%s.insertObject(App.activeDocument().%s, %s, True)",
body->getNameInDocument(), feat->getNameInDocument(), targetStr.c_str () );
}

Expand Down
4 changes: 2 additions & 2 deletions src/Mod/PartDesign/Gui/CommandPrimitive.cpp
Expand Up @@ -133,7 +133,7 @@ void CmdPrimtiveCompAdditive::activated(int iMsg)
}


Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.addFeature(App.activeDocument().%s)"
Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.addObject(App.activeDocument().%s)"
,pcActiveBody->getNameInDocument(), FeatName.c_str());
Gui::Command::updateActive();

Expand Down Expand Up @@ -322,7 +322,7 @@ void CmdPrimtiveCompSubtractive::activated(int iMsg)
FeatName.c_str());
}

Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.addFeature(App.activeDocument().%s)"
Gui::Command::doCommand(Doc,"App.ActiveDocument.%s.addObject(App.activeDocument().%s)"
,pcActiveBody->getNameInDocument(), FeatName.c_str());
Gui::Command::updateActive();

Expand Down
2 changes: 1 addition & 1 deletion src/Mod/PartDesign/Gui/Utils.cpp
Expand Up @@ -221,7 +221,7 @@ void fixSketchSupport (Sketcher::SketchObject* sketch)
Gui::Command::doCommand(Gui::Command::Doc,"App.activeDocument().%s.superPlacement.Base.z = %f",
Datum.c_str(), offset);
Gui::Command::doCommand(Gui::Command::Doc,
"App.activeDocument().%s.insertFeature(App.activeDocument().%s, App.activeDocument().%s)",
"App.activeDocument().%s.insertObject(App.activeDocument().%s, App.activeDocument().%s)",
body->getNameInDocument(), Datum.c_str(), sketch->getNameInDocument());
Gui::Command::doCommand(Gui::Command::Doc,
"App.activeDocument().%s.Support = (App.activeDocument().%s,[''])",
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/PartDesign/Gui/ViewProviderBody.cpp
Expand Up @@ -283,7 +283,7 @@ std::vector<App::DocumentObject*> ViewProviderBody::claimChildren3D(void)const
bool ViewProviderBody::onDelete ( const std::vector<std::string> &) {
// TODO May be do it conditionally? (2015-09-05, Fat-Zer)
Gui::Command::doCommand(Gui::Command::Doc,
"App.getDocument(\"%s\").getObject(\"%s\").removeGroupFromDocument()"
"App.getDocument(\"%s\").getObject(\"%s\").removeObjectsFromDocument()"
,getObject()->getDocument()->getName(), getObject()->getNameInDocument());
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Mod/PartDesign/Gui/Workbench.cpp
Expand Up @@ -151,7 +151,7 @@ void Workbench::slotNewObject(const App::DocumentObject& obj)
if ((obj.getDocument() == ActiveAppDoc) && (ActivePartObject != NULL)) {
// Add the new object to the active Body
// Note: Will this break Undo? But how else can we catch Edit->Duplicate selection?
Gui::Command::doCommand(Gui::Command::Doc,"App.activeDocument().%s.addFeature(App.activeDocument().%s)",
Gui::Command::doCommand(Gui::Command::Doc,"App.activeDocument().%s.addObject(App.activeDocument().%s)",
ActivePartObject->getNameInDocument(), obj.getNameInDocument());
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/Mod/PartDesign/TestPartDesignGui.py
Expand Up @@ -72,14 +72,14 @@ def testRefuseToMoveSingleFeature(self):
self.BoxObj.Length=10.0
self.BoxObj.Width=10.0
self.BoxObj.Height=10.0
self.BodySource.addFeature(self.BoxObj)
self.BodySource.addObject(self.BoxObj)

App.ActiveDocument.recompute()

self.Sketch = self.Doc.addObject('Sketcher::SketchObject','Sketch')
self.Sketch.Support = (self.BoxObj, ('Face3',))
self.Sketch.MapMode = 'FlatFace'
self.BodySource.addFeature(self.Sketch)
self.BodySource.addObject(self.Sketch)

geoList = []
geoList.append(Part.LineSegment(App.Vector(2.0,8.0,0),App.Vector(8.0,8.0,0)))
Expand Down Expand Up @@ -108,7 +108,7 @@ def testRefuseToMoveSingleFeature(self):
self.Pad.Midplane = 0
self.Pad.Offset = 0.000000

self.BodySource.addFeature(self.Pad)
self.BodySource.addObject(self.Pad)

self.Doc.recompute()
Gui.SendMsgToActiveView("ViewFit")
Expand All @@ -131,7 +131,7 @@ def testMoveSingleFeature(self):
self.Sketch = self.Doc.addObject('Sketcher::SketchObject','Sketch')
self.Sketch.Support = (self.Doc.XY_Plane, [''])
self.Sketch.MapMode = 'FlatFace'
self.BodySource.addFeature(self.Sketch)
self.BodySource.addObject(self.Sketch)

geoList = []
geoList.append(Part.LineSegment(App.Vector(-10.000000,10.000000,0),App.Vector(10.000000,10.000000,0)))
Expand Down Expand Up @@ -160,7 +160,7 @@ def testMoveSingleFeature(self):
self.Pad.Midplane = 0
self.Pad.Offset = 0.000000

self.BodySource.addFeature(self.Pad)
self.BodySource.addObject(self.Pad)

self.Doc.recompute()
Gui.SendMsgToActiveView("ViewFit")
Expand Down

0 comments on commit d2764a3

Please sign in to comment.