Skip to content

Commit

Permalink
Fix rounding problem as reported here: https://forum.freecadweb.org/v…
Browse files Browse the repository at this point in the history
…iewtopic.php?f=23&t=44863&p=393334#p393177

Use precision from user preference of FreeCAD
  • Loading branch information
SurajDadral committed Apr 30, 2020
1 parent 6025855 commit 44f16cb
Showing 1 changed file with 85 additions and 36 deletions.
121 changes: 85 additions & 36 deletions BillOfMaterial/BillOfMaterial_SVG.py
Expand Up @@ -231,9 +231,10 @@ def getColumnHeadersSVG(
column_width,
row_height,
font_size,
dia_precision,
):
"""getColumnHeadersSVG(ColumnHeaders, DiameterList, YOffset, ColumnWidth,
RowHeight, FontSize):
RowHeight, FontSize, DiameterPrecision):
column_headers is a dictionary with keys: "Mark", "RebarsCount", "Diameter",
"RebarLength", "RebarsTotalLength" and values are tuple of column_header and
its sequence number.
Expand Down Expand Up @@ -290,7 +291,10 @@ def getColumnHeadersSVG(
y_offset + row_height,
column_width,
row_height,
"#" + str(dia.Value).rstrip("0").rstrip("."),
"#"
+ str(round(dia.Value, dia_precision))
.rstrip("0")
.rstrip("."),
font_size,
).replace("\n", "\n ").rstrip(" ")
column_offset += column_width
Expand Down Expand Up @@ -484,13 +488,19 @@ def makeBillOfMaterialSVG(
svg_output += indent_level + '<g id="BOM">\n'
indent_level += " "

# Get user preferred unit precision
precision = FreeCAD.ParamGet(
"User parameter:BaseApp/Preferences/Units"
).GetInt("Decimals")

column_headers_svg = getColumnHeadersSVG(
column_headers,
diameter_list,
y_offset,
column_width,
row_height,
font_size,
dia_precision=precision,
)
svg_output += indent_level + column_headers_svg.replace(
"\n", "\n" + indent_level
Expand Down Expand Up @@ -557,15 +567,20 @@ def makeBillOfMaterialSVG(
if "Diameter" in column_units:
disp_diameter = (
str(
disp_diameter.getValueAs(column_units["Diameter"]).Value
round(
disp_diameter.getValueAs(
column_units["Diameter"]
).Value,
precision,
)
)
.rstrip("0")
.rstrip(".")
+ " "
+ column_units["Diameter"]
)
else:
disp_diameter = disp_diameter.toStr()
disp_diameter = str(round(disp_diameter, precision))
column_offset = getColumnOffset(
column_headers, diameter_list, "Diameter", column_width
)
Expand All @@ -588,17 +603,22 @@ def makeBillOfMaterialSVG(
if "RebarLength" in column_units:
disp_base_rebar_length = (
str(
base_rebar_length.getValueAs(
column_units["RebarLength"]
).Value
round(
base_rebar_length.getValueAs(
column_units["RebarLength"]
).Value,
precision,
)
)
.rstrip("0")
.rstrip(".")
+ " "
+ column_units["RebarLength"]
)
else:
disp_base_rebar_length = disp_base_rebar_length.toStr()
disp_base_rebar_length = str(
round(disp_base_rebar_length, precision)
)

column_offset = getColumnOffset(
column_headers, diameter_list, "RebarLength", column_width
Expand All @@ -622,17 +642,22 @@ def makeBillOfMaterialSVG(
if "RebarsTotalLength" in column_units:
disp_rebar_total_length = (
str(
rebar_total_length.getValueAs(
column_units["RebarsTotalLength"]
).Value
round(
rebar_total_length.getValueAs(
column_units["RebarsTotalLength"]
).Value,
precision,
)
)
.rstrip("0")
.rstrip(".")
+ " "
+ column_units["RebarsTotalLength"]
)
else:
disp_rebar_total_length = disp_rebar_total_length.toStr()
disp_rebar_total_length = str(
round(disp_rebar_total_length, precision)
)

column_offset = getColumnOffset(
column_headers, diameter_list, "RebarsTotalLength", column_width
Expand Down Expand Up @@ -719,17 +744,22 @@ def makeBillOfMaterialSVG(
if "RebarsTotalLength" in column_units:
disp_dia_total_length = (
str(
disp_dia_total_length.getValueAs(
column_units["RebarsTotalLength"]
).Value
round(
disp_dia_total_length.getValueAs(
column_units["RebarsTotalLength"]
).Value,
precision,
)
)
.rstrip("0")
.rstrip(".")
+ " "
+ column_units["RebarsTotalLength"]
)
else:
disp_dia_total_length = disp_dia_total_length.toStr()
disp_dia_total_length = str(
round(disp_dia_total_length, precision)
)

svg_output += indent_level + getSVGDataCell(
rebar_total_length_offset + i * column_width,
Expand All @@ -745,17 +775,21 @@ def makeBillOfMaterialSVG(
if "RebarsTotalLength" in column_units:
disp_dia_weight = (
str(
disp_dia_weight.getValueAs(
"kg/" + column_units["RebarsTotalLength"]
).Value
round(
disp_dia_weight.getValueAs(
"kg/"
+ column_units["RebarsTotalLength"]
).Value,
precision,
)
)
.rstrip("0")
.rstrip(".")
+ " "
+ column_units["RebarsTotalLength"]
)
else:
disp_dia_weight = disp_dia_weight.toStr()
disp_dia_weight = str(round(disp_dia_weight, precision))

svg_output += indent_level + getSVGDataCell(
rebar_total_length_offset + i * column_width,
Expand All @@ -770,10 +804,13 @@ def makeBillOfMaterialSVG(
y_offset + 2 * row_height,
column_width,
row_height,
(
DIA_WEIGHT_MAP[dia.Value]
* dia_total_length_dict[dia.Value]
).toStr(),
str(
round(
DIA_WEIGHT_MAP[dia.Value]
* dia_total_length_dict[dia.Value],
precision,
)
),
font_size,
).replace("\n", "\n" + indent_level).rstrip(indent_level)
else:
Expand Down Expand Up @@ -822,17 +859,22 @@ def makeBillOfMaterialSVG(
if "RebarsTotalLength" in column_units:
disp_dia_total_length = (
str(
disp_dia_total_length.getValueAs(
column_units["RebarsTotalLength"]
).Value
round(
disp_dia_total_length.getValueAs(
column_units["RebarsTotalLength"]
).Value,
precision,
)
)
.rstrip("0")
.rstrip(".")
+ " "
+ column_units["RebarsTotalLength"]
)
else:
disp_dia_total_length = disp_dia_total_length.toStr()
disp_dia_total_length = str(
round(disp_dia_total_length, precision)
)

svg_output += indent_level + getSVGDataCell(
i * column_width,
Expand All @@ -848,17 +890,21 @@ def makeBillOfMaterialSVG(
if "RebarsTotalLength" in column_units:
disp_dia_weight = (
str(
disp_dia_weight.getValueAs(
"kg/" + column_units["RebarsTotalLength"]
).Value
round(
disp_dia_weight.getValueAs(
"kg/"
+ column_units["RebarsTotalLength"]
).Value,
precision,
)
)
.rstrip("0")
.rstrip(".")
+ " "
+ column_units["RebarsTotalLength"]
)
else:
disp_dia_weight = disp_dia_weight.toStr()
disp_dia_weight = str(round(disp_dia_weight, precision))

svg_output += indent_level + getSVGDataCell(
i * column_width,
Expand All @@ -873,10 +919,13 @@ def makeBillOfMaterialSVG(
y_offset + 2 * row_height,
column_width,
row_height,
(
DIA_WEIGHT_MAP[dia.Value]
* dia_total_length_dict[dia.Value]
).toStr(),
str(
round(
DIA_WEIGHT_MAP[dia.Value]
* dia_total_length_dict[dia.Value],
precision,
)
),
font_size,
).replace("\n", "\n" + indent_level).rstrip(indent_level)

Expand Down

0 comments on commit 44f16cb

Please sign in to comment.