Added examples submenu under the main CQ menu.

This commit is contained in:
Jeremy Wright 2015-01-14 12:29:38 -05:00
parent dc89c70d2f
commit ae51eb1cb1
2 changed files with 71 additions and 4 deletions

View File

@ -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"""

View File

@ -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())