From ae51eb1cb123a3c6e112d834443b64550ccb3e85 Mon Sep 17 00:00:00 2001 From: Jeremy Wright Date: Wed, 14 Jan 2015 12:29:38 -0500 Subject: [PATCH] Added examples submenu under the main CQ menu. --- CadQuery/Gui/Command.py | 39 +++++++++++++++++++++++++++++++++++++++ CadQuery/InitGui.py | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/CadQuery/Gui/Command.py b/CadQuery/Gui/Command.py index 1ba7b5f..e894e7f 100644 --- a/CadQuery/Gui/Command.py +++ b/CadQuery/Gui/Command.py @@ -100,6 +100,45 @@ class CadQueryCloseScript: cqCodePane.file.close() +class CadQueryExecuteExample: + exFile = None + + def __init__(self, exFile): + self.exFile = str(exFile) + + def GetResources(self): + return {"MenuText": str(self.exFile)} + + def Activated(self): + FreeCAD.Console.PrintMessage(self.exFile + "\r\n") + + #So we can open the "Open File" dialog + mw = FreeCADGui.getMainWindow() + + #Start off defaulting to the Examples directory + module_base_path = module_locator.module_path() + exs_dir_path = os.path.join(module_base_path, 'Examples') + + #We need to close any file that's already open in the editor window + CadQueryCloseScript().Activated() + + #Append this script's directory to sys.path + sys.path.append(os.path.dirname(exs_dir_path)) + + #We've created a library that FreeCAD can use as well to open CQ files + ImportCQ.open(os.path.join(exs_dir_path, self.exFile)) + + docname = os.path.splitext(os.path.basename(self.exFile))[0] + FreeCAD.newDocument(docname) + + #Execute the script + CadQueryExecuteScript().Activated() + + #Get a nice view of our model + FreeCADGui.activeDocument().activeView().viewAxometric() + FreeCADGui.SendMsgToActiveView("ViewFit") + + class CadQueryExecuteScript: """CadQuery's command to execute a script file""" diff --git a/CadQuery/InitGui.py b/CadQuery/InitGui.py index 918ee55..93273c6 100644 --- a/CadQuery/InitGui.py +++ b/CadQuery/InitGui.py @@ -26,11 +26,19 @@ class CadQueryWorkbench (Workbench): #import logging #logging.basicConfig(filename='C:\\Users\\Jeremy\\Documents\\', level=logging.DEBUG) #logging.basicConfig(filename='/home/jwright/Documents/log.txt', level=logging.DEBUG) - + submenu = [] + + dirs = self.ListExamples() + + # Step through and add an Examples submenu item for each example + for curFile in dirs: + submenu.append(str(curFile)) + #We have our own CQ menu that's added when the user chooses our workbench - commands = ['CadQueryNewScript', 'CadQueryOpenScript', 'CadQuerySaveScript', 'CadQuerySaveAsScript', - 'CadQueryCloseScript', 'Separator', 'CadQueryExecuteScript', 'CadQueryClearOutput'] - self.appendMenu('CadQuery', commands) + self.appendMenu('CadQuery', ['CadQueryNewScript', 'CadQueryOpenScript', 'CadQuerySaveScript', + 'CadQuerySaveAsScript', 'CadQueryCloseScript']) + self.appendMenu(['CadQuery', 'Examples'], submenu) + self.appendMenu('CadQuery', ['Separator', 'CadQueryExecuteScript', 'CadQueryClearOutput']) def Activated(self): import os, sys @@ -159,6 +167,21 @@ class CadQueryWorkbench (Workbench): Gui.Command.CadQueryCloseScript().Activated() + @staticmethod + def ListExamples(): + import os + import module_locator + + dirs = [] + + # List all of the example files in an order that makes sense + module_base_path = module_locator.module_path() + exs_dir_path = os.path.join(module_base_path, 'Examples') + dirs = os.listdir(exs_dir_path) + dirs.sort() + + return dirs + FreeCADGui.addCommand('CadQueryNewScript', CadQueryNewScript()) FreeCADGui.addCommand('CadQueryOpenScript', CadQueryOpenScript()) FreeCADGui.addCommand('CadQuerySaveScript', CadQuerySaveScript()) @@ -167,4 +190,9 @@ FreeCADGui.addCommand('CadQueryExecuteScript', CadQueryExecuteScript()) FreeCADGui.addCommand('CadQueryCloseScript', CadQueryCloseScript()) FreeCADGui.addCommand('CadQueryClearOutput', CadQueryClearOutput()) +# Step through and add an Examples submenu item for each example +dirs = CadQueryWorkbench.ListExamples() +for curFile in dirs: + FreeCADGui.addCommand(curFile, CadQueryExecuteExample(curFile)) + FreeCADGui.addWorkbench(CadQueryWorkbench())