From 409285d53f2e6efdd6b867ceb8f960c8e6e62dd1 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 4 Jun 2014 12:05:41 +0200 Subject: [PATCH] + optimize creation of hexagonal sketch profile, add icon --- src/Mod/Sketcher/App/ConstraintPyImp.cpp | 20 ++- src/Mod/Sketcher/App/Sketch.cpp | 6 +- src/Mod/Sketcher/App/Sketch.h | 2 +- src/Mod/Sketcher/App/SketchObject.cpp | 11 +- src/Mod/Sketcher/App/SketchObjectPyImp.cpp | 53 +++++- src/Mod/Sketcher/App/SketchPyImp.cpp | 66 ++++--- src/Mod/Sketcher/Gui/Resources/Sketcher.qrc | 1 + .../icons/Sketcher_ProfilesHexagon1.svg | 162 ++++++++++++++++++ src/Mod/Sketcher/ProfileLib/Hexagon.py | 48 +++--- src/Mod/Sketcher/Profiles.py | 6 +- 10 files changed, 317 insertions(+), 58 deletions(-) create mode 100644 src/Mod/Sketcher/Gui/Resources/icons/Sketcher_ProfilesHexagon1.svg diff --git a/src/Mod/Sketcher/App/ConstraintPyImp.cpp b/src/Mod/Sketcher/App/ConstraintPyImp.cpp index 82225e3ad269..1c235cb1d703 100644 --- a/src/Mod/Sketcher/App/ConstraintPyImp.cpp +++ b/src/Mod/Sketcher/App/ConstraintPyImp.cpp @@ -22,11 +22,10 @@ #include "PreCompiled.h" #include -#include "Mod/Sketcher/App/Constraint.h" - -// inclusion of the generated files (generated out of ConstraintPy.xml) +#include "Constraint.h" #include "ConstraintPy.h" #include "ConstraintPy.cpp" +#include using namespace Sketcher; @@ -109,6 +108,11 @@ int ConstraintPy::PyInit(PyObject* args, PyObject* /*kwd*/) valid = true; } else if (strcmp("Angle",ConstraintType) == 0 ) { + if (PyObject_TypeCheck(index_or_value, &(Base::QuantityPy::Type))) { + Base::Quantity q = *(static_cast(index_or_value)->getQuantityPtr()); + if (q.getUnit() == Base::Unit::Angle) + Value = q.getValueAs(Base::Quantity::Radian); + } this->getConstraintPtr()->Type = Angle; valid = true; } @@ -172,6 +176,11 @@ int ConstraintPy::PyInit(PyObject* args, PyObject* /*kwd*/) //} //else if (strcmp("Angle",ConstraintType) == 0) { + if (PyObject_TypeCheck(index_or_value, &(Base::QuantityPy::Type))) { + Base::Quantity q = *(static_cast(index_or_value)->getQuantityPtr()); + if (q.getUnit() == Base::Unit::Angle) + Value = q.getValueAs(Base::Quantity::Radian); + } this->getConstraintPtr()->Type = Angle; this->getConstraintPtr()->First = FirstIndex; this->getConstraintPtr()->Second = SecondIndex; @@ -279,6 +288,11 @@ int ConstraintPy::PyInit(PyObject* args, PyObject* /*kwd*/) valid = true; } else if (strcmp("Angle",ConstraintType) == 0 ) { + if (PyObject_TypeCheck(index_or_value, &(Base::QuantityPy::Type))) { + Base::Quantity q = *(static_cast(index_or_value)->getQuantityPtr()); + if (q.getUnit() == Base::Unit::Angle) + Value = q.getValueAs(Base::Quantity::Radian); + } this->getConstraintPtr()->Type = Angle; valid = true; } diff --git a/src/Mod/Sketcher/App/Sketch.cpp b/src/Mod/Sketcher/App/Sketch.cpp index 566ebb3f34fa..fd13224f4c09 100644 --- a/src/Mod/Sketcher/App/Sketch.cpp +++ b/src/Mod/Sketcher/App/Sketch.cpp @@ -174,10 +174,12 @@ int Sketch::addGeometry(const Part::Geometry *geo, bool fixed) } } -void Sketch::addGeometry(const std::vector &geo, bool fixed) +int Sketch::addGeometry(const std::vector &geo, bool fixed) { + int ret = -1; for (std::vector::const_iterator it=geo.begin(); it != geo.end(); ++it) - addGeometry(*it, fixed); + ret = addGeometry(*it, fixed); + return ret; } int Sketch::addPoint(const Part::GeomPoint &point, bool fixed) diff --git a/src/Mod/Sketcher/App/Sketch.h b/src/Mod/Sketcher/App/Sketch.h index 851cb2443ae8..0afca80a78ef 100644 --- a/src/Mod/Sketcher/App/Sketch.h +++ b/src/Mod/Sketcher/App/Sketch.h @@ -73,7 +73,7 @@ class SketcherExport Sketch :public Base::Persistence /// add unspecified geometry int addGeometry(const Part::Geometry *geo, bool fixed=false); /// add unspecified geometry - void addGeometry(const std::vector &geo, bool fixed=false); + int addGeometry(const std::vector &geo, bool fixed=false); /// returns the actual geometry std::vector extractGeometry(bool withConstrucionElements=true, bool withExternalElements=false) const; diff --git a/src/Mod/Sketcher/App/SketchObject.cpp b/src/Mod/Sketcher/App/SketchObject.cpp index 71503392c1a8..7c0f8509c5c8 100644 --- a/src/Mod/Sketcher/App/SketchObject.cpp +++ b/src/Mod/Sketcher/App/SketchObject.cpp @@ -307,7 +307,16 @@ Base::Axis SketchObject::getAxis(int axId) const int SketchObject::addGeometry(const std::vector &geoList) { - return -1; + const std::vector< Part::Geometry * > &vals = getInternalGeometry(); + + std::vector< Part::Geometry * > newVals(vals); + for (std::vector::const_iterator it = geoList.begin(); it != geoList.end(); ++it) { + newVals.push_back(*it); + } + Geometry.setValues(newVals); + Constraints.acceptGeometry(getCompleteGeometry()); + rebuildVertexIndex(); + return Geometry.getSize()-1; } int SketchObject::addGeometry(const Part::Geometry *geo) diff --git a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp index 0c71ba7521ee..5b9100bffeec 100644 --- a/src/Mod/Sketcher/App/SketchObjectPyImp.cpp +++ b/src/Mod/Sketcher/App/SketchObjectPyImp.cpp @@ -25,7 +25,7 @@ # include #endif -#include "Mod/Sketcher/App/SketchObject.h" +#include #include #include #include @@ -34,6 +34,7 @@ #include #include #include +#include // inclusion of the generated files (generated out of SketchObjectSFPy.xml) #include "SketchObjectPy.h" @@ -66,7 +67,30 @@ PyObject* SketchObjectPy::addGeometry(PyObject *args) Part::Geometry *geo = static_cast(pcObj)->getGeometryPtr(); return Py::new_reference_to(Py::Int(this->getSketchObjectPtr()->addGeometry(geo))); } - Py_Return; + else if (PyObject_TypeCheck(pcObj, &(PyList_Type)) || + PyObject_TypeCheck(pcObj, &(PyTuple_Type))) { + std::vector geoList; + Py::Sequence list(pcObj); + for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { + if (PyObject_TypeCheck((*it).ptr(), &(Part::GeometryPy::Type))) { + Part::Geometry *geo = static_cast((*it).ptr())->getGeometryPtr(); + geoList.push_back(geo); + } + } + + int ret = this->getSketchObjectPtr()->addGeometry(geoList) + 1; + std::size_t numGeo = geoList.size(); + Py::Tuple tuple(numGeo); + for (std::size_t i=0; iob_type->tp_name; + throw Py::TypeError(error); } PyObject* SketchObjectPy::delGeometry(PyObject *args) @@ -130,7 +154,30 @@ PyObject* SketchObjectPy::addConstraint(PyObject *args) this->getSketchObjectPtr()->solve(); return Py::new_reference_to(Py::Int(ret)); } - Py_Return; + else if (PyObject_TypeCheck(pcObj, &(PyList_Type)) || + PyObject_TypeCheck(pcObj, &(PyTuple_Type))) { + std::vector values; + Py::Sequence list(pcObj); + for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { + if (PyObject_TypeCheck((*it).ptr(), &(ConstraintPy::Type))) { + Constraint *con = static_cast((*it).ptr())->getConstraintPtr(); + values.push_back(con); + } + } + + int ret = getSketchObjectPtr()->addConstraints(values) + 1; + std::size_t numCon = values.size(); + Py::Tuple tuple(numCon); + for (std::size_t i=0; iob_type->tp_name; + throw Py::TypeError(error); } PyObject* SketchObjectPy::delConstraint(PyObject *args) diff --git a/src/Mod/Sketcher/App/SketchPyImp.cpp b/src/Mod/Sketcher/App/SketchPyImp.cpp index 1030a5c51b68..3404a5dfbc9b 100644 --- a/src/Mod/Sketcher/App/SketchPyImp.cpp +++ b/src/Mod/Sketcher/App/SketchPyImp.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include "Sketch.h" #include "Constraint.h" @@ -71,50 +72,71 @@ PyObject* SketchPy::addGeometry(PyObject *args) if (!PyArg_ParseTuple(args, "O", &pcObj)) return 0; - if (PyObject_TypeCheck(pcObj, &(LinePy::Type))) { - GeomLineSegment *line = static_cast(pcObj)->getGeomLineSegmentPtr(); - return Py::new_reference_to(Py::Int(this->getSketchPtr()->addGeometry(line->clone()))); + if (PyObject_TypeCheck(pcObj, &(Part::GeometryPy::Type))) { + Part::Geometry *geo = static_cast(pcObj)->getGeometryPtr(); + return Py::new_reference_to(Py::Int(this->getSketchPtr()->addGeometry(geo))); } - Py_Return; + else if (PyObject_TypeCheck(pcObj, &(PyList_Type)) || + PyObject_TypeCheck(pcObj, &(PyTuple_Type))) { + std::vector geoList; + Py::Sequence list(pcObj); + for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { + if (PyObject_TypeCheck((*it).ptr(), &(Part::GeometryPy::Type))) { + Part::Geometry *geo = static_cast((*it).ptr())->getGeometryPtr(); + geoList.push_back(geo); + } + } + + int ret = this->getSketchPtr()->addGeometry(geoList) + 1; + std::size_t numGeo = geoList.size(); + Py::Tuple tuple(numGeo); + for (std::size_t i=0; iob_type->tp_name; + throw Py::TypeError(error); } PyObject* SketchPy::addConstraint(PyObject *args) { - int ret = -1; PyObject *pcObj; if (!PyArg_ParseTuple(args, "O", &pcObj)) return 0; - if (PyList_Check(pcObj)) { - Py_ssize_t nSize = PyList_Size(pcObj); + if (PyObject_TypeCheck(pcObj, &(PyList_Type)) || PyObject_TypeCheck(pcObj, &(PyTuple_Type))) { std::vector values; - values.resize(nSize); - - for (Py_ssize_t i=0; iob_type->tp_name; - throw Py::TypeError(error); + Py::Sequence list(pcObj); + for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) { + if (PyObject_TypeCheck((*it).ptr(), &(ConstraintPy::Type))) { + Constraint *con = static_cast((*it).ptr())->getConstraintPtr(); + values.push_back(con); } - - values[i] = static_cast(item)->getConstraintPtr(); } - ret = getSketchPtr()->addConstraints(values); + int ret = getSketchPtr()->addConstraints(values) + 1; + std::size_t numCon = values.size(); + Py::Tuple tuple(numCon); + for (std::size_t i=0; i(pcObj); - ret = getSketchPtr()->addConstraint(pcObject->getConstraintPtr()); + int ret = getSketchPtr()->addConstraint(pcObject->getConstraintPtr()); + return Py::new_reference_to(Py::Int(ret)); } else { std::string error = std::string("type must be 'Constraint' or list of 'Constraint', not "); error += pcObj->ob_type->tp_name; throw Py::TypeError(error); } - - return Py::new_reference_to(Py::Int(ret)); - } PyObject* SketchPy::clear(PyObject *args) diff --git a/src/Mod/Sketcher/Gui/Resources/Sketcher.qrc b/src/Mod/Sketcher/Gui/Resources/Sketcher.qrc index 81b791a2c75d..cb912585e93e 100644 --- a/src/Mod/Sketcher/Gui/Resources/Sketcher.qrc +++ b/src/Mod/Sketcher/Gui/Resources/Sketcher.qrc @@ -70,6 +70,7 @@ icons/Sketcher_Sketch.svg icons/Sketcher_ViewSketch.svg icons/Sketcher_AlterConstruction.svg + icons/Sketcher_ProfilesHexagon1.svg translations/Sketcher_af.qm translations/Sketcher_de.qm translations/Sketcher_fi.qm diff --git a/src/Mod/Sketcher/Gui/Resources/icons/Sketcher_ProfilesHexagon1.svg b/src/Mod/Sketcher/Gui/Resources/icons/Sketcher_ProfilesHexagon1.svg new file mode 100644 index 000000000000..f30265d763e6 --- /dev/null +++ b/src/Mod/Sketcher/Gui/Resources/icons/Sketcher_ProfilesHexagon1.svg @@ -0,0 +1,162 @@ + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + diff --git a/src/Mod/Sketcher/ProfileLib/Hexagon.py b/src/Mod/Sketcher/ProfileLib/Hexagon.py index 1a8ddc4d0404..db71ad273812 100644 --- a/src/Mod/Sketcher/ProfileLib/Hexagon.py +++ b/src/Mod/Sketcher/ProfileLib/Hexagon.py @@ -36,28 +36,30 @@ def makeHexagonSimple(sketchName=None): else: sketch = App.ActiveDocument.getObject(sketchName) - l1=sketch.addGeometry(Part.Line(App.Vector(-20.00,34.64,0),App.Vector(20.00,34.64,0))) - l2=sketch.addGeometry(Part.Line(App.Vector(20.00,34.64,0),App.Vector(47.082363,0.00,0))) - sketch.addConstraint(Sketcher.Constraint('Coincident',l1,2,l2,1)) - l3=sketch.addGeometry(Part.Line(App.Vector(40.00,0.00,0),App.Vector(20.00,-34.64,0))) - sketch.addConstraint(Sketcher.Constraint('Coincident',l2,2,l3,1)) - l4=sketch.addGeometry(Part.Line(App.Vector(20.00,-34.64,0),App.Vector(-20.00,-34.64,0))) - sketch.addConstraint(Sketcher.Constraint('Coincident',l3,2,l4,1)) - l5=sketch.addGeometry(Part.Line(App.Vector(-20.00,-34.64,0),App.Vector(-40.00,0.00,0))) - sketch.addConstraint(Sketcher.Constraint('Coincident',l4,2,l5,1)) - l6=sketch.addGeometry(Part.Line(App.Vector(-40.00,0.00,0),App.Vector(-20.00,34.64,0))) - sketch.addConstraint(Sketcher.Constraint('Coincident',l5,2,l6,1)) - sketch.addConstraint(Sketcher.Constraint('Coincident',l6,2,l1,1)) - sketch.addConstraint(Sketcher.Constraint('Equal',l1,l2)) - sketch.addConstraint(Sketcher.Constraint('Equal',l2,l3)) - sketch.addConstraint(Sketcher.Constraint('Equal',l3,l4)) - sketch.addConstraint(Sketcher.Constraint('Equal',l4,l5)) - sketch.addConstraint(Sketcher.Constraint('Equal',l5,l6)) - a1=sketch.addConstraint(Sketcher.Constraint('Angle',l1,2,l2,1,2.0943951023931953)) - sketch.setDatum(a1,App.Units.Quantity('120.000000 deg')) - a2=sketch.addConstraint(Sketcher.Constraint('Angle',l3,2,l4,1,2.0943951023931953)) - sketch.setDatum(a2,App.Units.Quantity('120.000000 deg')) - a3=sketch.addConstraint(Sketcher.Constraint('Angle',l5,2,l6,1,2.0943951023931953)) - sketch.setDatum(a3,App.Units.Quantity('120.000000 deg')) + geoList = [] + geoList.append(Part.Line(App.Vector(-20.00,34.64,0),App.Vector(20.00,34.64,0))) + geoList.append(Part.Line(App.Vector(20.00,34.64,0),App.Vector(47.082363,0.00,0))) + geoList.append(Part.Line(App.Vector(40.00,0.00,0),App.Vector(20.00,-34.64,0))) + geoList.append(Part.Line(App.Vector(20.00,-34.64,0),App.Vector(-20.00,-34.64,0))) + geoList.append(Part.Line(App.Vector(-20.00,-34.64,0),App.Vector(-40.00,0.00,0))) + geoList.append(Part.Line(App.Vector(-40.00,0.00,0),App.Vector(-20.00,34.64,0))) + (l1,l2,l3,l4,l5,l6) = sketch.addGeometry(geoList) + + conList = [] + conList.append(Sketcher.Constraint('Coincident',l1,2,l2,1)) + conList.append(Sketcher.Constraint('Coincident',l2,2,l3,1)) + conList.append(Sketcher.Constraint('Coincident',l3,2,l4,1)) + conList.append(Sketcher.Constraint('Coincident',l4,2,l5,1)) + conList.append(Sketcher.Constraint('Coincident',l5,2,l6,1)) + conList.append(Sketcher.Constraint('Coincident',l6,2,l1,1)) + conList.append(Sketcher.Constraint('Equal',l1,l2)) + conList.append(Sketcher.Constraint('Equal',l2,l3)) + conList.append(Sketcher.Constraint('Equal',l3,l4)) + conList.append(Sketcher.Constraint('Equal',l4,l5)) + conList.append(Sketcher.Constraint('Equal',l5,l6)) + conList.append(Sketcher.Constraint('Angle',l1,2,l2,1,App.Units.Quantity('120.000000 deg'))) + conList.append(Sketcher.Constraint('Angle',l3,2,l4,1,App.Units.Quantity('120.000000 deg'))) + conList.append(Sketcher.Constraint('Angle',l5,2,l6,1,App.Units.Quantity('120.000000 deg'))) + sketch.addConstraint(conList) return diff --git a/src/Mod/Sketcher/Profiles.py b/src/Mod/Sketcher/Profiles.py index 38b69d80435c..8431ec6eebcf 100644 --- a/src/Mod/Sketcher/Profiles.py +++ b/src/Mod/Sketcher/Profiles.py @@ -53,11 +53,11 @@ def getSketch(): class _CommandProfileHexagon1: "The basis hexagon profile command definition" def GetResources(self): - return {'Pixmap' : 'Sketcher_Hexagon', - 'MenuText': QtCore.QT_TRANSLATE_NOOP("Sketcher_ProfilesHexagon1","Creates a hexagon profile"), + return {'Pixmap' : 'Sketcher_ProfilesHexagon1', + 'MenuText': QtCore.QT_TRANSLATE_NOOP("Sketcher_ProfilesHexagon1","Creates a hexagonal profile"), 'Accel': "", 'CmdType': "ForEdit", - 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Sketcher_ProfilesHexagon1","Creates a hexagon profile in the sketch")} + 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Sketcher_ProfilesHexagon1","Creates a hexagonal profile in the sketch")} def Activated(self): FreeCAD.ActiveDocument.openTransaction("Create hexagon profile")