import FreeCAD
import FreeCADGui
from PySide import QtGui, QtCore

class DatumTool:
    """
    A tool for creating datums in existing models
    """
    def __init__(self):
        self.active = False

    def activate(self):
        self.active = True
        FreeCAD.Console.PrintMessage("Datum tool activatedn")

    def deactivate(self):
        self.active = False
        FreeCAD.Console.PrintMessage("Datum tool deactivatedn")

    def mousePressEvent(self, event):
        if self.active:
            # Create a datum at the position of the mouse click
            pos = FreeCADGui.ActiveDocument.ActiveView.getCursorPos()
            point = FreeCADGui.ActiveDocument.ActiveView.getPoint(pos)
            datum = FreeCAD.ActiveDocument.addObject("Part::Datum", "Datum")
            datum.Placement.Base = point
            datum.ViewObject.ShapeColor = (0.0, 1.0, 0.0) # Set the color of the datum to green
            FreeCAD.ActiveDocument.recompute()

class DatumCommand:
    """
    A command for activating and deactivating the datum tool
    """
    def __init__(self):
        self.tool = DatumTool()

    def Activated(self):
        self.tool.activate()
        FreeCADGui.ActiveDocument.ActiveView.addEventCallback("SoMouseButtonEvent", self.tool.mousePressEvent)

    def Deactivated(self):
        self.tool.deactivate()
        FreeCADGui.ActiveDocument.ActiveView.removeEventCallback("SoMouseButtonEvent", self.tool.mousePressEvent)

    def GetResources(self):
        return {'Pixmap': 'path/to/icon.png', 'MenuText': 'Datum Tool', 'ToolTip': 'Creates datum elements in existing models'}

# Add the command to the Draft Workbench
FreeCADGui.addCommand('DatumCommand', DatumCommand())