From 5917d21395e867f2a330c8dc12148f0314acecb8 Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 3 Jun 2014 14:54:27 +0200 Subject: [PATCH] + implement hexagon sketch --- src/Gui/Command.cpp | 17 ++++++++++++- src/Gui/DocumentPy.xml | 8 ++++++ src/Gui/DocumentPyImp.cpp | 13 ++++++++++ src/Mod/Sketcher/Gui/CMakeLists.txt | 4 +-- src/Mod/Sketcher/ProfileLib/Hexagon.py | 34 ++++++++++++++++++++++++-- src/Mod/Sketcher/Profiles.py | 24 +++++++++++++++--- 6 files changed, 92 insertions(+), 8 deletions(-) diff --git a/src/Gui/Command.cpp b/src/Gui/Command.cpp index be4d9f955a..4c78916644 100644 --- a/src/Gui/Command.cpp +++ b/src/Gui/Command.cpp @@ -778,6 +778,21 @@ PythonCommand::PythonCommand(const char* name,PyObject * pcPyCommand, const char // check if the "GetResources()" method returns a Dict object if (!PyDict_Check(_pcPyResourceDict)) throw Base::Exception("PythonCommand::PythonCommand(): Method GetResources() of the Python command object returns the wrong type (has to be Py Dictonary)"); + + // check for command type + std::string cmdType = getResource("CmdType"); + if (!cmdType.empty()) { + int type = 0; + if (cmdType.find("AlterDoc") != std::string::npos) + type += int(AlterDoc); + if (cmdType.find("Alter3DView") != std::string::npos) + type += int(Alter3DView); + if (cmdType.find("AlterSelection") != std::string::npos) + type += int(AlterSelection); + if (cmdType.find("ForEdit") != std::string::npos) + type += int(ForEdit); + eType = type; + } } const char* PythonCommand::getResource(const char* sName) const @@ -796,7 +811,7 @@ const char* PythonCommand::getResource(const char* sName) const void PythonCommand::activated(int iMsg) { - if (Activation == "") { + if (Activation.empty()) { try { Interpreter().runMethodVoid(_pcPyCommand, "Activated"); } diff --git a/src/Gui/DocumentPy.xml b/src/Gui/DocumentPy.xml index 629a69ed4e..5e5608f915 100644 --- a/src/Gui/DocumentPy.xml +++ b/src/Gui/DocumentPy.xml @@ -36,6 +36,14 @@ + + + + getInEdit() + Returns the current object in edit mode or None if there is no such object + + + diff --git a/src/Gui/DocumentPyImp.cpp b/src/Gui/DocumentPyImp.cpp index de06e242b9..ec5af663b9 100644 --- a/src/Gui/DocumentPyImp.cpp +++ b/src/Gui/DocumentPyImp.cpp @@ -119,6 +119,19 @@ PyObject* DocumentPy::setEdit(PyObject *args) Py_Return; } +PyObject* DocumentPy::getInEdit(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return NULL; + + ViewProvider* vp = getDocumentPtr()->getInEdit(); + if (vp) { + return vp->getPyObject(); + } + + Py_Return; +} + PyObject* DocumentPy::resetEdit(PyObject *args) { if (!PyArg_ParseTuple(args, ";No arguments allowed")) // convert args: Python->C diff --git a/src/Mod/Sketcher/Gui/CMakeLists.txt b/src/Mod/Sketcher/Gui/CMakeLists.txt index cd34b9eedf..9a71556506 100644 --- a/src/Mod/Sketcher/Gui/CMakeLists.txt +++ b/src/Mod/Sketcher/Gui/CMakeLists.txt @@ -61,7 +61,7 @@ SET(SketcherGui_SRCS Command.cpp CommandCreateGeo.cpp CommandConstraints.cpp - CommandAlterGeometry.cpp + CommandAlterGeometry.cpp Resources/Sketcher.qrc PreCompiled.cpp PreCompiled.h @@ -103,7 +103,7 @@ SET(SketcherGui_SRCS SET(SketcherGui_Scripts InitGui.py - TestSketcherGui.py + TestSketcherGui.py ) add_library(SketcherGui SHARED ${SketcherGui_SRCS}) diff --git a/src/Mod/Sketcher/ProfileLib/Hexagon.py b/src/Mod/Sketcher/ProfileLib/Hexagon.py index 2686687417..1a8ddc4d04 100644 --- a/src/Mod/Sketcher/ProfileLib/Hexagon.py +++ b/src/Mod/Sketcher/ProfileLib/Hexagon.py @@ -20,14 +20,44 @@ #* * #*************************************************************************** -import FreeCAD, FreeCADGui, Sketcher +import FreeCAD, FreeCADGui, Sketcher, Part __title__="Hexagon profile lib" __author__ = "Juergen Riegel" __url__ = "http://www.freecadweb.org" +App = FreeCAD +Gui = FreeCADGui -def makeHexagonSimple(): +def makeHexagonSimple(sketchName=None): + if not sketchName: + sketch = App.ActiveDocument.addObject("Sketcher::SketchObject","Hexagon") + 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')) return diff --git a/src/Mod/Sketcher/Profiles.py b/src/Mod/Sketcher/Profiles.py index d9dfd76954..38b69d8043 100644 --- a/src/Mod/Sketcher/Profiles.py +++ b/src/Mod/Sketcher/Profiles.py @@ -23,12 +23,16 @@ import FreeCAD, Sketcher if FreeCAD.GuiUp: - import FreeCADGui,SketcherGui + import FreeCADGui,SketcherGui,os from PySide import QtCore, QtGui from PySide.QtCore import Qt from PySide.QtGui import QApplication, QCursor from FreeCADGui import PySideUic as uic + #s=os.path.dirname(__file__) + #s=os.path.join(s,"ProfileLib") + #FreeCADGui.addIconPath(s) + __title__="Sketcher profile lib handling" __author__ = "Juergen Riegel" __url__ = "http://www.freecadweb.org" @@ -37,19 +41,33 @@ __url__ = "http://www.freecadweb.org" def isProfileActive(): return not FreeCAD.ActiveDocument is None +def getSketch(): + edit = FreeCADGui.ActiveDocument.getInEdit() + if edit and edit.isDerivedFrom('SketcherGui::ViewProviderSketch'): + return edit.Object + #act = FreeCAD.ActiveDocument.ActiveObject + #if act and act.isDerivedFrom('Sketcher::SketchObject'): + # return act + return None + 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"), 'Accel': "", + 'CmdType': "ForEdit", 'ToolTip': QtCore.QT_TRANSLATE_NOOP("Sketcher_ProfilesHexagon1","Creates a hexagon profile in the sketch")} def Activated(self): - FreeCAD.ActiveDocument.openTransaction("Create hexagon profile") FreeCADGui.addModule("ProfileLib.Hexagon") - FreeCADGui.doCommand("ProfileLib.Hexagon.makeHexagonSimple()") + sketch = getSketch() + if not sketch is None: + FreeCADGui.doCommand("ProfileLib.Hexagon.makeHexagonSimple('%s')" % (sketch.Name)) + else: + FreeCADGui.doCommand("ProfileLib.Hexagon.makeHexagonSimple()") + FreeCAD.ActiveDocument.recompute() def IsActive(self): return isProfileActive()