Create arguments from edge settings starting with FingerJointSettings

This commit is contained in:
Florian Festi 2016-10-31 18:12:39 +01:00
parent 5a721df82b
commit 3a0df10866
2 changed files with 42 additions and 16 deletions

View File

@ -191,18 +191,10 @@ class Boxes:
def __init__(self): def __init__(self):
self.formats = formats.Formats() self.formats = formats.Formats()
self.argparser = ArgumentParser(description=self.__doc__) self.argparser = ArgumentParser(description=self.__doc__)
self.edgesettings = {}
self.argparser._action_groups[1].title = "Generator Settings" self.argparser._action_groups[1].title = "Generator Settings"
fingergroup = self.argparser.add_argument_group( self.addSettingsArgs(edges.FingerJointSettings)
"Finger Joint Settings")
fingergroup.add_argument(
"--fingerjointfinger", action="store", type=float, default=1.0,
help="width of the fingers in multiples of thickness")
fingergroup.add_argument(
"--fingerjointspace", action="store", type=float, default=1.0,
help="width of the space between fingers in multiples of thickness")
fingergroup.add_argument(
"--fingerjointsurrounding", action="store", type=float, default=1.0,
help="amount of space needed at the end in multiples of normal spaces")
defaultgroup = self.argparser.add_argument_group( defaultgroup = self.argparser.add_argument_group(
"Default Settings") "Default Settings")
defaultgroup.add_argument( defaultgroup.add_argument(
@ -302,6 +294,12 @@ class Boxes:
else: else:
raise ValueError("No default for argument", arg) raise ValueError("No default for argument", arg)
def addSettingsArgs(self, settings, prefix=None):
prefix = prefix or settings.__name__[:-len("Settings")]
settings.parserArguments(self.argparser, prefix)
self.edgesettings[prefix] = {}
def parseArgs(self, args=None): def parseArgs(self, args=None):
""" """
Parse command line parameters Parse command line parameters
@ -310,6 +308,11 @@ class Boxes:
""" """
for key, value in vars(self.argparser.parse_args(args=args)).items(): for key, value in vars(self.argparser.parse_args(args=args)).items():
# treat edge settings separately
for setting in self.edgesettings:
if key.startswith(setting + '_'):
self.edgesettings[setting][key[len(setting)+1:]] = value
continue
setattr(self, key, value) setattr(self, key, value)
# Change file ending to format if not given explicitly # Change file ending to format if not given explicitly
@ -343,11 +346,8 @@ class Boxes:
# Finger joints # Finger joints
# Share settings object # Share settings object
s = edges.FingerJointSettings(self.thickness) s = edges.FingerJointSettings(self.thickness, True,
s.setValues(self.thickness, **self.edgesettings.get("FingerJoint", {}))
finger=getattr(self, "fingerjointfinger", 1.0),
space=getattr(self, "fingerjointspace", 1.0),
surroundingspaces=getattr(self, "fingerjointsurrounding", 1.0))
self.addPart(edges.FingerJointEdge(self, s)) self.addPart(edges.FingerJointEdge(self, s))
self.addPart(edges.FingerJointEdgeCounterPart(self, s)) self.addPart(edges.FingerJointEdgeCounterPart(self, s))
self.addPart(edges.FingerHoles(self, s), name="fingerHolesAt") self.addPart(edges.FingerHoles(self, s), name="fingerHolesAt")

View File

@ -17,6 +17,9 @@
import math import math
import inspect import inspect
import argparse
import re
from boxes import gears from boxes import gears
def getDescriptions(): def getDescriptions():
@ -125,6 +128,28 @@ class Settings(object):
absolute_params = {} absolute_params = {}
relative_params = {} relative_params = {}
@classmethod
def parserArguments(cls, parser, prefix=None):
prefix = prefix or cls.__name__[:-len("Settings")]
lines = cls.__doc__.split("\n")
# Parse doc string
descriptions = {}
r = re.compile(r"^ +\* +(\S+) +: .* : +(.*)")
for l in lines:
m = r.search(l)
if m:
descriptions[m.group(1)] = m.group(2)
group = parser.add_argument_group(lines[0] or lines[1])
for name, default in (list(cls.absolute_params.items()) +
list(cls.relative_params.items())):
group.add_argument("--%s_%s" % (prefix, name),
type=type(default),
action="store", default=default,
help=descriptions.get(name))
def __init__(self, thickness, relative=True, **kw): def __init__(self, thickness, relative=True, **kw):
self.values = self.absolute_params.copy() self.values = self.absolute_params.copy()
self.thickness = thickness self.thickness = thickness
@ -147,6 +172,7 @@ class Settings(object):
factor = 1.0 factor = 1.0
if relative: if relative:
factor = thickness factor = thickness
print(kw.items())
for name, value in kw.items(): for name, value in kw.items():
if name in self.absolute_params: if name in self.absolute_params:
self.values[name] = value self.values[name] = value