Code cleanup and minor bug fixes.

This commit is contained in:
Jeremy Mack Wright 2016-10-11 20:49:22 -04:00
parent 765c9d168d
commit 544faacaf2
5 changed files with 83 additions and 122 deletions

View File

@ -10,7 +10,7 @@ import module_locator
import Settings
import Shared
#Distinguish python built-in open function from the one declared here
# Distinguish python built-in open function from the one declared here
if open.__module__ == '__builtin__':
pythonopen = open
@ -26,12 +26,12 @@ class CadQueryClearOutput:
return True
def Activated(self):
#Grab our code editor so we can interact with it
# Grab our main window so we can interact with it
mw = FreeCADGui.getMainWindow()
reportView = mw.findChild(QtGui.QDockWidget, "Report view")
#Clear the view because it gets overwhelmed sometimes and won't scroll to the bottom
# Clear the view because it gets overwhelmed sometimes and won't scroll to the bottom
reportView.widget().clear()
@ -44,21 +44,18 @@ class CadQueryCloseScript:
def IsActive(self):
return True
# if FreeCAD.ActiveDocument is None:
# return False
# else:
# return True
def Activated(self):
#Grab our code editor so we can interact with it
mw = FreeCADGui.getMainWindow()
cqCodePane = mw.findChild(QtGui.QPlainTextEdit, "cqCodePane")
#If there's nothing open in the code pane, we don't need to close it
# Grab our code editor so we can interact with it
cqCodePane = Shared.getActiveCodePane()
# If there's nothing open in the code pane, we don't need to close it
if cqCodePane is None or len(cqCodePane.file.path) == 0:
return
#Check to see if we need to save the script
# Check to see if we need to save the script
if cqCodePane.dirty:
reply = QtGui.QMessageBox.question(cqCodePane, "Save CadQuery Script", "Save script before closing?",
QtGui.QMessageBox.Yes | QtGui.QMessageBox.No | QtGui.QMessageBox.Cancel)
@ -67,28 +64,28 @@ class CadQueryCloseScript:
return
if reply == QtGui.QMessageBox.Yes:
#If we've got a file name already save it there, otherwise give a save-as dialog
# If we've got a file name already save it there, otherwise give a save-as dialog
if len(cqCodePane.file.path) == 0:
filename = QtGui.QFileDialog.getSaveFileName(mw, mw.tr("Save CadQuery Script As"), "/home/",
mw.tr("CadQuery Files (*.py)"))
else:
filename = cqCodePane.file.path
#Make sure we got a valid file name
# Make sure we got a valid file name
if filename is not None:
ExportCQ.save(filename)
#Close the matching 3D view if it's open
# Close the matching 3D view if it's open
if cqCodePane.file.path is not None:
docname = os.path.splitext(os.path.basename(cqCodePane.file.path))[0]
try:
FreeCAD.closeDocument(docname)
except:
#Assume that the document has already been closed
# Assume that the document has already been closed
pass
#Clear our script and whatever was rendered by it out
# Clear our script and whatever was rendered by it out
cqCodePane.file.close()
class CadQueryExecuteExample:
@ -103,29 +100,19 @@ class CadQueryExecuteExample:
def Activated(self):
FreeCAD.Console.PrintMessage(self.exFile + "\r\n")
#So we can open the "Open File" dialog
# So we can open the "Open File" dialog
mw = FreeCADGui.getMainWindow()
#Start off defaulting to the Examples directory
# Start off defaulting to the Examples directory
module_base_path = module_locator.module_path()
exs_dir_path = os.path.join(module_base_path, 'Examples')
#Append this script's directory to sys.path
# 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
# 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"""
@ -140,19 +127,17 @@ class CadQueryExecuteScript:
return True
def Activated(self):
#Grab our code editor so we can interact with it
# Grab our code editor so we can interact with it
cqCodePane = Shared.getActiveCodePane()
#Clear the old render before re-rendering
# Clear the old render before re-rendering
Shared.clearActiveDocument()
#Save our code to a tempfile and render it
# Save our code to a tempfile and render it
tempFile = tempfile.NamedTemporaryFile(delete=False)
tempFile.write(cqCodePane.toPlainText().encode('utf-8'))
tempFile.close()
#mdi = mw.findChild(QtGui.QMdiArea)
#docname = mdi.currentSubWindow().getWindowTitle()
docname = os.path.splitext(os.path.basename(cqCodePane.file.path))[0]
# Make sure we replace any troublesome characters
@ -166,7 +151,7 @@ class CadQueryExecuteScript:
except:
FreeCAD.newDocument(docname)
#We import this way because using execfile() causes non-standard script execution in some situations
# We import this way because using execfile() causes non-standard script execution in some situations
imp.load_source('temp_module', tempFile.name)
msg = QtGui.QApplication.translate(
@ -189,13 +174,13 @@ class CadQueryNewScript:
return True
def Activated(self):
#We need to close any file that's already open in the editor window
# We need to close any file that's already open in the editor window
CadQueryCloseScript().Activated()
module_base_path = module_locator.module_path()
templ_dir_path = os.path.join(module_base_path, 'Templates')
#Use the library that FreeCAD can use as well to open CQ files
# Use the library that FreeCAD can use as well to open CQ files
ImportCQ.open(os.path.join(templ_dir_path, 'script_template.py'))
@ -213,12 +198,12 @@ class CadQueryOpenScript:
return True
def Activated(self):
#So we can open the "Open File" dialog
# So we can open the "Open File" dialog
mw = FreeCADGui.getMainWindow()
#Try to keep track of the previous path used to open as a convenience to the user
# Try to keep track of the previous path used to open as a convenience to the user
if self.previousPath is None:
#Start off defaulting to the Examples directory
# Start off defaulting to the Examples directory
module_base_path = module_locator.module_path()
exs_dir_path = os.path.join(module_base_path, 'Examples')
@ -227,29 +212,19 @@ class CadQueryOpenScript:
filename = QtGui.QFileDialog.getOpenFileName(mw, mw.tr("Open CadQuery Script"), self.previousPath,
mw.tr("CadQuery Files (*.py)"))
#Make sure the user didn't click cancel
# Make sure the user didn't click cancel
if filename[0]:
#We need to close any file that's already open in the editor window
# We need to close any file that's already open in the editor window
CadQueryCloseScript().Activated()
self.previousPath = filename[0]
#Append this script's directory to sys.path
# Append this script's directory to sys.path
sys.path.append(os.path.dirname(filename[0]))
#We've created a library that FreeCAD can use as well to open CQ files
# We've created a library that FreeCAD can use as well to open CQ files
ImportCQ.open(filename[0])
# docname = os.path.splitext(os.path.basename(filename[0]))[0]
# FreeCAD.newDocument(docname)
#Execute the script
# CadQueryExecuteScript().Activated()
#Get a nice view of our model
# FreeCADGui.activeDocument().activeView().viewAxometric()
# FreeCADGui.SendMsgToActiveView("ViewFit")
class CadQuerySaveScript:
"""CadQuery's command to save a script file"""
@ -264,10 +239,15 @@ class CadQuerySaveScript:
return True
def Activated(self):
#Grab our code editor so we can interact with it
# Grab our code editor so we can interact with it
cqCodePane = Shared.getActiveCodePane()
#If the code pane doesn't have a filename, we need to present the save as dialog
# If there are no windows open there is nothing to save
if cqCodePane == None:
FreeCAD.Console.PrintError("Nothing to save.\r\n")
return
# If the code pane doesn't have a filename, we need to present the save as dialog
if len(cqCodePane.file.path) == 0 or os.path.basename(cqCodePane.file.path) == 'script_template.py' \
or os.path.split(cqCodePane.file.path)[-2].endswith('Examples'):
FreeCAD.Console.PrintError("You cannot save over a blank file, example file or template file.\r\n")
@ -276,10 +256,10 @@ class CadQuerySaveScript:
return
#Rely on our export library to help us save the file
# Rely on our export library to help us save the file
ExportCQ.save()
#Execute the script if the user has asked for it
# Execute the script if the user has asked for it
if Settings.execute_on_save:
CadQueryExecuteScript().Activated()
@ -297,11 +277,15 @@ class CadQuerySaveAsScript:
return True
def Activated(self):
#So we can open the save-as dialog
# So we can open the save-as dialog
mw = FreeCADGui.getMainWindow()
cqCodePane = Shared.getActiveCodePane()
#Try to keep track of the previous path used to open as a convenience to the user
if cqCodePane == None:
FreeCAD.Console.PrintError("Nothing to save.\r\n")
return
# Try to keep track of the previous path used to open as a convenience to the user
if self.previousPath is None:
self.previousPath = "/home/"
@ -310,19 +294,19 @@ class CadQuerySaveAsScript:
self.previousPath = filename[0]
#Make sure the user didn't click cancel
# Make sure the user didn't click cancel
if filename[0]:
#Close the 3D view for the original script if it's open
# Close the 3D view for the original script if it's open
try:
docname = os.path.splitext(os.path.basename(cqCodePane.file.path))[0]
FreeCAD.closeDocument(docname)
except:
#Assume that there was no 3D view to close
# Assume that there was no 3D view to close
pass
# Change the name of our script window's tab
Shared.setActiveWindowTitle(os.path.basename(filename[0]))
#Save the file before closing the original and the re-rendering the new one
# Save the file before closing the original and the re-rendering the new one
ExportCQ.save(filename[0])
CadQueryExecuteScript().Activated()

View File

@ -45,7 +45,6 @@ def open(filename):
# Set up so that we can import from our embedded packages
module_base_path = module_locator.module_path()
libs_dir_path = os.path.join(module_base_path, 'Libs')
sys.path.insert(0, libs_dir_path)
from pyqode.core.modes import FileWatcherMode
from pyqode.core.modes import RightMarginMode
@ -59,7 +58,6 @@ def open(filename):
mw = FreeCADGui.getMainWindow()
# Grab just the file name from the path/file that's being executed
# docname = os.path.splitext(os.path.basename(filename))[0]
docname = os.path.basename(filename)
# Set up the text area for our CQ code

View File

@ -2,4 +2,32 @@
# (c) 2014-2016 Jeremy Wright Apache 2.0 License
#TODO: The FreeCAD devs like to put as much init code as possible in here so that the module can be used without the
#TODO: GUI if desired
#TODO: GUI if desired
import os
import sys
import module_locator
# Set up so that we can import from our embedded packages
module_base_path = module_locator.module_path()
libs_dir_path = os.path.join(module_base_path, 'Libs')
sys.path.insert(0, libs_dir_path)
# Tack on our CadQuery library git subtree
cq_lib_path = os.path.join(libs_dir_path, 'cadquery-lib')
sys.path.insert(1, cq_lib_path)
# Make sure we get the right libs under the FreeCAD installation
fc_base_path = os.path.dirname(os.path.dirname(module_base_path))
fc_lib_path = os.path.join(fc_base_path, 'lib')
fc_bin_path = os.path.join(fc_base_path, 'bin')
# Make sure that the directories exist before we add them to sys.path
# This could cause problems or solve them by overriding what CQ is setting for the paths
if os.path.exists(fc_lib_path):
sys.path.insert(1, fc_lib_path)
if os.path.exists(fc_bin_path):
sys.path.insert(1, fc_bin_path)
# Need to set this for PyQode
os.environ['QT_API'] = 'pyside'

View File

@ -15,11 +15,6 @@ class CadQueryWorkbench (Workbench):
closedWidgets = []
def Initialize(self):
import os
#Need to set this for PyQode
os.environ['QT_API'] = 'pyside'
#Turn off logging for now
#import logging
#logging.basicConfig(filename='C:\\Users\\Jeremy\\Documents\\', level=logging.DEBUG)
@ -39,31 +34,11 @@ class CadQueryWorkbench (Workbench):
self.appendMenu('CadQuery', ['Separator', 'CadQueryExecuteScript', 'CadQueryClearOutput'])
def Activated(self):
import os, sys
import os
import module_locator
from Gui import Command, ImportCQ
import Settings
from Gui import ImportCQ
#Set up so that we can import from our embedded packages
module_base_path = module_locator.module_path()
libs_dir_path = os.path.join(module_base_path, 'Libs')
sys.path.insert(0, libs_dir_path)
# Tack on our CadQuery library git subtree
cq_lib_path = os.path.join(libs_dir_path, 'cadquery-lib')
sys.path.insert(1, cq_lib_path)
#Make sure we get the right libs under the FreeCAD installation
fc_base_path = os.path.dirname(os.path.dirname(module_base_path))
fc_lib_path = os.path.join(fc_base_path, 'lib')
fc_bin_path = os.path.join(fc_base_path, 'bin')
#Make sure that the directories exist before we add them to sys.path
#This could cause problems or solve them by overriding what CQ is setting for the paths
if os.path.exists(fc_lib_path):
sys.path.insert(1, fc_lib_path)
if os.path.exists(fc_bin_path):
sys.path.insert(1, fc_bin_path)
import cadquery
from PySide import QtGui, QtCore
@ -71,7 +46,7 @@ class CadQueryWorkbench (Workbench):
msg = QtGui.QApplication.translate(
"cqCodeWidget",
"CadQuery " + cadquery.__version__ + "\r\n"
"CadQuery is a parametric scripting language "
"CadQuery is a parametric scripting API "
"for creating and traversing CAD models\r\n"
"Author: David Cowden\r\n"
"License: Apache-2.0\r\n"
@ -104,30 +79,7 @@ class CadQueryWorkbench (Workbench):
Gui.Command.CadQueryExecuteScript().Activated()
def Deactivated(self):
import Gui.Command
from PySide import QtGui
# msg = QtGui.QApplication.translate(
# "cqCodeWidget",
# "\r\nCadQuery Workbench Deactivated\r\n",
# None,
# QtGui.QApplication.UnicodeUTF8)
#
# #Put the UI back the way we found it
# FreeCAD.Console.PrintMessage(msg)
# # Getting the main window will allow us to start setting things up the way we want
# mw = FreeCADGui.getMainWindow()
#
# cqCodePane = mw.findChild(QtGui.QPlainTextEdit, "cqCodePane")
# cqCodePane.close()
# cqCodePane.setParent(None)
#
# dockWidgets = mw.findChildren(QtGui.QDockWidget)
#
# for widget in dockWidgets:
# if widget.objectName() == "cqCodeView":
# mw.removeDockWidget(widget)
pass
@staticmethod
def ListExamples():

View File

@ -25,14 +25,13 @@ def clearActiveDocument():
def getActiveCodePane():
"""Gets the currently active code pane, even if its 3D view is selected."""
# TODO: Make sure that the code pane is selected, even if the associated 3D view currently has focus
# Grab our code editor so we can interact with it
mw = FreeCADGui.getMainWindow()
mdi = mw.findChild(QtGui.QMdiArea)
# If our current subwindow doesn't contain a script, we need to find the one that does
mdiWin = mdi.currentSubWindow()
if mdiWin == None: return None # We need to warn the caller that there is no code pane
if mdiWin == 0 or ".py" not in mdiWin.windowTitle():
subList = mdi.subWindowList()