Skip to content

Commit

Permalink
Experimental support for FreeCAD 0.18.4
Browse files Browse the repository at this point in the history
  • Loading branch information
mnesarco committed Apr 5, 2020
1 parent 1e7f88d commit dc10818
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 31 deletions.
10 changes: 10 additions & 0 deletions src/Init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# -*- coding: utf-8 -*-
"""
Marz Workbench for FreeCAD 0.19+.
https://github.com/mnesarco/MarzWorkbench
"""

__author__ = "Frank D. Martinez. M."
__copyright__ = "Copyright 2020, Frank D. Martinez. M."
__license__ = "GPLv3"
__maintainer__ = "https://github.com/mnesarco"
6 changes: 4 additions & 2 deletions src/InitGui.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ def GetClassName(self):

def Initialize(self):
import marz_reloader
from marz_freecad import isVersion19
marz_reloader.reloadAll(lambda name: name.startswith('marz_cmd_'))
cmds = [
"MarzCmdCreateInstrument",
"MarzCmdCreateFretboard",
"MarzCmdCreateNeck",
"MarzCmdCreateBody",
"MarzCmdCreateConstructionLines",
#"MarzCmdCreateNeckPlanes",
"MarzCmdToggleAutocompute",
#"MarzCmdCreateNeckPlanes", TODO: Fix
]
if isVersion19():
cmds.append("MarzCmdToggleAutocompute")
self.appendToolbar("Marz", cmds)
self.appendMenu("Marz", cmds + ['MarzCmdShowAboutWindow'])

Expand Down
45 changes: 28 additions & 17 deletions src/marz_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import gc
import hashlib
from time import time
from marz_freecad import isVersion19

#!-----------------------------------------------------------------------------
#! Poor man's cache implementation. Not too good but better than nothing.
Expand All @@ -22,6 +23,10 @@
MAX_CACHE_SIZE = 100
MAIN_CACHE = {}

# For some Reason, Cache does not work with FreeCAD 0.18
# So, performance in 0.18 is significatively poor compared to 0.19
CACHE_ENABLED = isVersion19()

def cacheKey(name, *args, **kwargs):
segs = [name] + [repr(arg) for arg in args]
for (kw, arg) in sorted(kwargs.items()):
Expand Down Expand Up @@ -50,17 +55,20 @@ def cleanCache():
#! This decorator adds a small overhead to the function, so
#! use it only in costly functions.
def PureFunctionCache(f):
def wrapper(*args, **kwargs):
key = cacheKey(str(id(f)), *args, **kwargs)
(cached, _) = MAIN_CACHE.get(key) or (None, 0)
cleanCache()
if cached:
return cached
else:
cached = f(*args, **kwargs)
MAIN_CACHE[key] = (cached, time())
return cached
return wrapper
if CACHE_ENABLED:
def wrapper(*args, **kwargs):
key = cacheKey(str(id(f)), *args, **kwargs)
(cached, _) = MAIN_CACHE.get(key) or (None, 0)
cleanCache()
if cached:
return cached
else:
cached = f(*args, **kwargs)
MAIN_CACHE[key] = (cached, time())
return cached
return wrapper
else:
return f

def getCachedObject(baseName, *args):
"""This function is for direct calls to cache
Expand All @@ -74,9 +82,12 @@ def getCachedObject(baseName, *args):
Returns:
{tuple} -- (cachedObject, updateFunction)
"""
key = cacheKey(baseName, *args)
(cached, ts) = MAIN_CACHE.get(key) or (None, 0)
cleanCache()
def setf(v):
MAIN_CACHE[key] = (v, time())
return (cached, setf)
if CACHE_ENABLED:
key = cacheKey(baseName, *args)
(cached, ts) = MAIN_CACHE.get(key) or (None, 0)
cleanCache()
def setf(v):
MAIN_CACHE[key] = (v, time())
return (cached, setf)
else:
return (None, lambda x: None)
22 changes: 22 additions & 0 deletions src/marz_freecad.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
"""
Marz Workbench for FreeCAD 0.19+.
https://github.com/mnesarco/MarzWorkbench
"""

__author__ = "Frank D. Martinez. M."
__copyright__ = "Copyright 2020, Frank D. Martinez. M."
__license__ = "GPLv3"
__maintainer__ = "https://github.com/mnesarco"


import FreeCAD as App

FreeCadVersion = App.Version()

def isVersion19():
return FreeCadVersion[1] == '19'

def isVersion18():
return FreeCadVersion[1] == '18'

2 changes: 1 addition & 1 deletion src/marz_instrument_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def execute(self, obj):
finally:
App.ActiveDocument.RecomputesFrozen = disabledAutoRecompute
if not disabledAutoRecompute:
runDeferred(lambda: App.ActiveDocument.recompute(None, True, True), 500)
runDeferred(lambda: App.ActiveDocument.recompute(), 500)
bar.stop()

def onChanged(self, fp, prop):
Expand Down
8 changes: 5 additions & 3 deletions src/marz_neck_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def barrell(neckd, fret):
@PureFunctionCache
def cutHeadstockSides(line, lenght, width, transitionLength):
hsLine = line.lerpLineTo(-lenght)
hsTop = hsLine.rectSym(width-0.01)
hsTop = hsLine.rectSym(width-0.1)
# Side cut 1
(a,b,c,d,_) = hsTop
s1 = [vxy(a.x-transitionLength, a.y), vxy(a.x-transitionLength, 4*a.y), vxy(d.x, 4*d.y), d]
Expand Down Expand Up @@ -620,12 +620,14 @@ def heel(self, neckd, line):

# Then move and cut top
cutThickness = extrusionDepth * math.cos(neckAngleRad)
naSide = naSide.translate(Vector(
#naSide =
naSide.translate(Vector(
(inst.body.backThickness+cutThickness)*math.sin(neckAngleRad),
0,
(inst.body.backThickness+cutThickness)*math.cos(neckAngleRad)
))
naSide = naSide.translate(Vector(
#naSide =
naSide.translate(Vector(
(inst.body.length-lengthDelta)*math.cos(neckAngleRad),
0,
(inst.body.length-lengthDelta)*math.sin(neckAngleRad)
Expand Down
2 changes: 1 addition & 1 deletion src/marz_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def featureToBody(feature, name = None):
Gui.activeView().setActiveObject('pdbody', body)
Gui.Selection.clearSelection()
Gui.Selection.addSelection(body)
feature.Visibility = False
feature.ViewObject.Visibility = False

def runDeferred(block, delay=500):
QtCore.QTimer.singleShot(delay, block)
Expand Down
7 changes: 0 additions & 7 deletions src/marz_widget_about.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.close()

def paintEvent(self, event):
opt = QtGui.QStyleOption()
opt.init(self)
painter = QtGui.QPainter(self)
self.style().drawPrimitive(QtGui.QStyle.PE_Widget, opt, painter, self)


@classmethod
def execute(cls, frame = True, timeout = None):
MarzAboutWindow(frame, timeout).show()

0 comments on commit dc10818

Please sign in to comment.