mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-10-01 20:49:44 +00:00
Improved validation for inputs in polyline wall and measure tool.
This commit is contained in:
@@ -294,7 +294,27 @@ class DrawPolylineWall(bpy.types.Operator):
|
|||||||
self.action_count = 0
|
self.action_count = 0
|
||||||
self.visible_objs = []
|
self.visible_objs = []
|
||||||
self.objs_2d_bbox = []
|
self.objs_2d_bbox = []
|
||||||
self.number_options = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", " ", ".", "+", "-", "*", "/", "'", "\"", "="}
|
self.number_options = {
|
||||||
|
"0",
|
||||||
|
"1",
|
||||||
|
"2",
|
||||||
|
"3",
|
||||||
|
"4",
|
||||||
|
"5",
|
||||||
|
"6",
|
||||||
|
"7",
|
||||||
|
"8",
|
||||||
|
"9",
|
||||||
|
" ",
|
||||||
|
".",
|
||||||
|
"+",
|
||||||
|
"-",
|
||||||
|
"*",
|
||||||
|
"/",
|
||||||
|
"'",
|
||||||
|
'"',
|
||||||
|
"=",
|
||||||
|
}
|
||||||
self.number_input = []
|
self.number_input = []
|
||||||
self.number_output = ""
|
self.number_output = ""
|
||||||
self.number_is_negative = False
|
self.number_is_negative = False
|
||||||
@@ -311,6 +331,7 @@ class DrawPolylineWall(bpy.types.Operator):
|
|||||||
self.input_panel[self.input_type] = self.number_output
|
self.input_panel[self.input_type] = self.number_output
|
||||||
if not is_valid:
|
if not is_valid:
|
||||||
self.report({"WARNING"}, "The number typed is not valid.")
|
self.report({"WARNING"}, "The number typed is not valid.")
|
||||||
|
return is_valid
|
||||||
else:
|
else:
|
||||||
if self.input_type in {"X", "Y"}:
|
if self.input_type in {"X", "Y"}:
|
||||||
self.input_panel = PolylineDecorator.calculate_distance_and_angle(context, self.is_input_on)
|
self.input_panel = PolylineDecorator.calculate_distance_and_angle(context, self.is_input_on)
|
||||||
@@ -321,6 +342,7 @@ class DrawPolylineWall(bpy.types.Operator):
|
|||||||
|
|
||||||
PolylineDecorator.set_input_panel(self.input_panel, self.input_type)
|
PolylineDecorator.set_input_panel(self.input_panel, self.input_type)
|
||||||
tool.Blender.update_viewport()
|
tool.Blender.update_viewport()
|
||||||
|
return is_valid
|
||||||
|
|
||||||
# TODO This is creating a hack in generate function from DumbWallGenerator
|
# TODO This is creating a hack in generate function from DumbWallGenerator
|
||||||
# Come up with a better solution
|
# Come up with a better solution
|
||||||
@@ -448,8 +470,9 @@ class DrawPolylineWall(bpy.types.Operator):
|
|||||||
return {"FINISHED"}
|
return {"FINISHED"}
|
||||||
|
|
||||||
if self.is_input_on and event.value == "RELEASE" and event.type in {"RET", "NUMPAD_ENTER", "RIGHTMOUSE"}:
|
if self.is_input_on and event.value == "RELEASE" and event.type in {"RET", "NUMPAD_ENTER", "RIGHTMOUSE"}:
|
||||||
self.recalculate_inputs(context)
|
is_valid = self.recalculate_inputs(context)
|
||||||
tool.Snap.insert_polyline_point(self.input_panel)
|
if is_valid:
|
||||||
|
tool.Snap.insert_polyline_point(self.input_panel)
|
||||||
self.is_input_on = False
|
self.is_input_on = False
|
||||||
self.input_type = "OFF"
|
self.input_type = "OFF"
|
||||||
self.number_input = []
|
self.number_input = []
|
||||||
|
|||||||
@@ -2303,7 +2303,27 @@ class MeasureTool(bpy.types.Operator):
|
|||||||
self.action_count = 0
|
self.action_count = 0
|
||||||
self.visible_objs = []
|
self.visible_objs = []
|
||||||
self.objs_2d_bbox = []
|
self.objs_2d_bbox = []
|
||||||
self.number_options = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "+", "-", "*", "-", "/"}
|
self.number_options = {
|
||||||
|
"0",
|
||||||
|
"1",
|
||||||
|
"2",
|
||||||
|
"3",
|
||||||
|
"4",
|
||||||
|
"5",
|
||||||
|
"6",
|
||||||
|
"7",
|
||||||
|
"8",
|
||||||
|
"9",
|
||||||
|
" ",
|
||||||
|
".",
|
||||||
|
"+",
|
||||||
|
"-",
|
||||||
|
"*",
|
||||||
|
"/",
|
||||||
|
"'",
|
||||||
|
'"',
|
||||||
|
"=",
|
||||||
|
}
|
||||||
self.number_input = []
|
self.number_input = []
|
||||||
self.number_output = ""
|
self.number_output = ""
|
||||||
self.number_is_negative = False
|
self.number_is_negative = False
|
||||||
@@ -2320,6 +2340,7 @@ class MeasureTool(bpy.types.Operator):
|
|||||||
self.input_panel[self.input_type] = self.number_output
|
self.input_panel[self.input_type] = self.number_output
|
||||||
if not is_valid:
|
if not is_valid:
|
||||||
self.report({"WARNING"}, "The number typed is not valid.")
|
self.report({"WARNING"}, "The number typed is not valid.")
|
||||||
|
return is_valid
|
||||||
else:
|
else:
|
||||||
if self.input_type in {"X", "Y", "Z"}:
|
if self.input_type in {"X", "Y", "Z"}:
|
||||||
self.input_panel = PolylineDecorator.calculate_distance_and_angle(context, self.is_input_on)
|
self.input_panel = PolylineDecorator.calculate_distance_and_angle(context, self.is_input_on)
|
||||||
@@ -2330,6 +2351,7 @@ class MeasureTool(bpy.types.Operator):
|
|||||||
|
|
||||||
PolylineDecorator.set_input_panel(self.input_panel, self.input_type)
|
PolylineDecorator.set_input_panel(self.input_panel, self.input_type)
|
||||||
tool.Blender.update_viewport()
|
tool.Blender.update_viewport()
|
||||||
|
return is_valid
|
||||||
|
|
||||||
def modal(self, context, event):
|
def modal(self, context, event):
|
||||||
|
|
||||||
@@ -2424,8 +2446,9 @@ class MeasureTool(bpy.types.Operator):
|
|||||||
tool.Blender.update_viewport()
|
tool.Blender.update_viewport()
|
||||||
|
|
||||||
if self.is_input_on and event.value == "RELEASE" and event.type in {"RET", "NUMPAD_ENTER", "RIGHTMOUSE"}:
|
if self.is_input_on and event.value == "RELEASE" and event.type in {"RET", "NUMPAD_ENTER", "RIGHTMOUSE"}:
|
||||||
self.recalculate_inputs(context)
|
is_valid = self.recalculate_inputs(context)
|
||||||
tool.Snap.insert_polyline_point(self.input_panel)
|
if is_valid:
|
||||||
|
tool.Snap.insert_polyline_point(self.input_panel)
|
||||||
self.is_input_on = False
|
self.is_input_on = False
|
||||||
self.input_type = "OFF"
|
self.input_type = "OFF"
|
||||||
self.number_input = []
|
self.number_input = []
|
||||||
|
|||||||
@@ -456,19 +456,36 @@ class Snap(bonsai.core.tool.Snap):
|
|||||||
@classmethod
|
@classmethod
|
||||||
def validate_input(cls, input_number):
|
def validate_input(cls, input_number):
|
||||||
|
|
||||||
grammar = """
|
grammar_imperial = """
|
||||||
start: FORMULA? dim expr?
|
start: FORMULA? dim expr?
|
||||||
dim: metric | imperial
|
dim: imperial
|
||||||
|
|
||||||
FORMULA: "="
|
FORMULA: "="
|
||||||
|
|
||||||
metric: NUMBER
|
|
||||||
|
|
||||||
imperial: feet? "-"? inches?
|
imperial: feet? "-"? inches?
|
||||||
feet: NUMBER? " "? fraction? "'"
|
feet: NUMBER? " "? fraction? "'"
|
||||||
inches: NUMBER? " "? fraction? "\\""
|
inches: NUMBER? " "? fraction? "\\""
|
||||||
fraction: NUMBER "/" NUMBER
|
fraction: NUMBER "/" NUMBER
|
||||||
|
|
||||||
|
expr: (ADD | SUB) dim | (MUL | DIV) NUMBER
|
||||||
|
|
||||||
|
NUMBER: /-?\\d+(?:\\.\\d+)?/
|
||||||
|
ADD: "+"
|
||||||
|
SUB: "-"
|
||||||
|
MUL: "*"
|
||||||
|
DIV: "/"
|
||||||
|
|
||||||
|
%ignore " "
|
||||||
|
"""
|
||||||
|
|
||||||
|
grammar_metric = """
|
||||||
|
start: FORMULA? dim expr?
|
||||||
|
dim: metric
|
||||||
|
|
||||||
|
FORMULA: "="
|
||||||
|
|
||||||
|
metric: NUMBER
|
||||||
|
|
||||||
expr: (ADD | SUB | MUL | DIV) dim
|
expr: (ADD | SUB | MUL | DIV) dim
|
||||||
|
|
||||||
NUMBER: /-?\\d+(?:\\.\\d+)?/
|
NUMBER: /-?\\d+(?:\\.\\d+)?/
|
||||||
@@ -502,7 +519,7 @@ class Snap(bonsai.core.tool.Snap):
|
|||||||
result = (args[0] + args[1])
|
result = (args[0] + args[1])
|
||||||
else:
|
else:
|
||||||
result = args[0]
|
result = args[0]
|
||||||
return result * 0.3048
|
return result
|
||||||
|
|
||||||
def metric(self, args):
|
def metric(self, args):
|
||||||
return args[0]
|
return args[0]
|
||||||
@@ -535,13 +552,19 @@ class Snap(bonsai.core.tool.Snap):
|
|||||||
dimension = args[i]
|
dimension = args[i]
|
||||||
if len(args) > i+1:
|
if len(args) > i+1:
|
||||||
expression = args[i + 1]
|
expression = args[i + 1]
|
||||||
return expression(dimension)
|
return expression(dimension) * factor
|
||||||
else:
|
else:
|
||||||
return dimension
|
return dimension * factor
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
parser = Lark(grammar)
|
if bpy.context.scene.unit_settings.system == 'IMPERIAL':
|
||||||
|
parser = Lark(grammar_imperial)
|
||||||
|
factor = 0.3048
|
||||||
|
else:
|
||||||
|
parser = Lark(grammar_metric)
|
||||||
|
factor = 1
|
||||||
|
|
||||||
parse_tree = parser.parse(input_number)
|
parse_tree = parser.parse(input_number)
|
||||||
|
|
||||||
transformer = InputTransform()
|
transformer = InputTransform()
|
||||||
|
|||||||
Reference in New Issue
Block a user