Update PartsLibrary.FCMacro

This commit is contained in:
microelly2 2015-06-08 00:13:44 +02:00
parent cd9e11dcb0
commit 3212cfa050

View file

@ -52,7 +52,17 @@ which you can browse and install items from the library.
LIBRARYPATH = "/home/yorik/Sources/FreeCAD-library" LIBRARYPATH = "/home/yorik/Sources/FreeCAD-library"
# END CONFIGURATION - THAT'S DONE, NO NEED TO EDIT ANYTHING MORE # END CONFIGURATION - THAT'S DONE, NO NEED TO EDIT ANYTHING MORE
import FreeCAD, FreeCADGui, Part, zipfile, tempfile
s=FreeCAD.ParamGet('User parameter:Plugins/partlib').GetString('destination')
print s
if s<>'':
LIBRARYPATH = s
else:
pass
import FreeCAD, FreeCADGui, Part
from PySide import QtGui, QtCore from PySide import QtGui, QtCore
class ExpFileSystemModel(QtGui.QFileSystemModel): class ExpFileSystemModel(QtGui.QFileSystemModel):
@ -69,7 +79,8 @@ class ExpFileSystemModel(QtGui.QFileSystemModel):
class ExpDockWidget(QtGui.QDockWidget): class ExpDockWidget(QtGui.QDockWidget):
"a library explorer dock widget" "a library explorer dock widget"
def __init__(self):
def __init__(self,LIBRARYPATH):
QtGui.QDockWidget.__init__(self) QtGui.QDockWidget.__init__(self)
self.setObjectName("PartsLibrary") self.setObjectName("PartsLibrary")
@ -78,52 +89,26 @@ class ExpDockWidget(QtGui.QDockWidget):
# setting up a directory model that shows only fcstd and step # setting up a directory model that shows only fcstd and step
self.dirmodel = ExpFileSystemModel() self.dirmodel = ExpFileSystemModel()
self.dirmodel.setRootPath(LIBRARYPATH) self.dirmodel.setRootPath(LIBRARYPATH)
self.dirmodel.setNameFilters(["*.fcstd","*.FcStd","*.FCSTD","*.stp","*.STP","*.step","*.STEP", "*.brp", "*.BRP", "*.brep", "*.BREP"]) self.dirmodel.setNameFilters(["*.fcstd","*.FcStd","*.FCSTD","*.stp","*.STP","*.step","*.STEP", "*.brep", "*.BREP"])
self.dirmodel.setNameFilterDisables(0) self.dirmodel.setNameFilterDisables(0)
container = QtGui.QWidget() self.folder_view = QtGui.QTreeView();
layout = QtGui.QVBoxLayout(container) self.folder_view.setModel(self.dirmodel)
folder = QtGui.QTreeView() self.folder_view.doubleClicked[QtCore.QModelIndex].connect(self.clicked)
folder.setModel(self.dirmodel)
folder.clicked[QtCore.QModelIndex].connect(self.clicked)
folder.doubleClicked[QtCore.QModelIndex].connect(self.doubleclicked)
# Don't show columns for size, file type, and last modified # Don't show columns for size, file type, and last modified
folder.setHeaderHidden(True) self.folder_view.setHeaderHidden(True)
folder.hideColumn(1) self.folder_view.hideColumn(1)
folder.hideColumn(2) self.folder_view.hideColumn(2)
folder.hideColumn(3) self.folder_view.hideColumn(3)
folder.setRootIndex(self.dirmodel.index(LIBRARYPATH)) self.folder_view.setRootIndex(self.dirmodel.index(LIBRARYPATH))
layout.addWidget(folder)
self.preview = QtGui.QLabel() self.setWidget(self.folder_view)
self.preview.setFixedHeight(128)
layout.addWidget(self.preview)
layout.setAlignment(self.preview, QtCore.Qt.AlignHCenter)
self.setWidget(container)
def clicked(self, index): def clicked(self, index):
path = self.dirmodel.filePath(index) path = self.dirmodel.filePath(index)
if path.lower().endswith(".fcstd"): if path.lower().endswith(".stp") or path.lower().endswith(".step") or path.lower().endswith(".brep"):
zfile=zipfile.ZipFile(path)
files=zfile.namelist()
# check for meta-file if it's really a FreeCAD document
if files[0] == "Document.xml":
image="thumbnails/Thumbnail.png"
if image in files:
image=zfile.read(image)
thumbfile = tempfile.mkstemp(suffix='.png')[1]
thumb = open(thumbfile,"wb")
thumb.write(image)
thumb.close()
im = QtGui.QPixmap(thumbfile)
self.preview.setPixmap(im)
return
self.preview.clear()
def doubleclicked(self, index):
path = self.dirmodel.filePath(index)
if path.lower().endswith(".stp") or path.lower().endswith(".step") or path.lower().endswith(".brp") or path.lower().endswith(".brep"):
Part.show(Part.read(path)) Part.show(Part.read(path))
elif path.lower().endswith(".fcstd"): else:
FreeCADGui.ActiveDocument.mergeProject(path) FreeCADGui.ActiveDocument.mergeProject(path)
if QtCore.QDir(LIBRARYPATH).exists(): if QtCore.QDir(LIBRARYPATH).exists():
@ -135,7 +120,7 @@ if QtCore.QDir(LIBRARYPATH).exists():
else: else:
w.show() w.show()
else: else:
m.addDockWidget(QtCore.Qt.RightDockWidgetArea,ExpDockWidget()) m.addDockWidget(QtCore.Qt.RightDockWidgetArea,ExpDockWidget(LIBRARYPATH))
else: else:
print "Library path ", LIBRARYPATH, "not found." print "Library path ", LIBRARYPATH, "not found."
print "Please set the correct path to your Parts library in the macro script" print "Please set the correct path to your Parts library in the macro script"