1 Pages (4 items)
[Dev] How to acces the unit definitions for numeric computations? - Messages
#1 Posted: 8/16/2017 2:03:05 PM
I want to provide the user with the option to specify an image size in length units but have to convert this internally to screen pixels. The idea is something like that (size is a TNumber expression with 'm as unit):
Thus, what is the best method to generate the factor InchperMeter? I know these ones:
- start SMath, compute the value and paste it in the equation.
- Write a string "'m/'in", convert that to a TNumber
Of course I ask this to better understand access to units in general, not just for this problem.
Thanks for any hints.
size = size * InchperMeter * GlobalProfile.ScreenDpi;
- start SMath, compute the value and paste it in the equation.
- Write a string "'m/'in", convert that to a TNumber
Decision.NumericCalculation(Entry.Create(TermsConverter.ToTerms("'m/'in"
));
Of course I ask this to better understand access to units in general, not just for this problem.
Thanks for any hints.
Martin Kraska
Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#2 Posted: 8/16/2017 4:13:36 PM
Here is how I actually did it.
I am a bit sceptical if the Inch definition and the result formatting couldn't be solved more elegantly.
The image shows four image regions with the same graph, format PNG (left) and PDF (right) and size specified in SMath length units (upper) and in pixels (lower). The image regions are reset to original size via context menu. My screen is 96 dpi and at 100% zoom level the scaling is perfect.
![2017-08-16 20_54_49-SMath Studio 0.98.6398 - [svgtest.sm_].png](/en-US/file/8U6YYM/2017-08-16-20_54_49-SMath-Studio-0_98_6398---_svgtest_sm___png)
I am a bit sceptical if the Inch definition and the result formatting couldn't be solved more elegantly.
/// <summary>
/// This returns a string for specification of the image size in Gnuplot.
/// </summary>
/// <param name="arg"></param>expression (list), size in meters or if without unit, in pixels
/// <param name="context"></param>
/// <param name="unit"></param> unit for gnuplot
/// <returns></returns>
public static string GetSizeString(Entry arg, Store context, SizeUnit resultunit)
{
TNumber Inch = Decision.NumericCalculation(Entry.Create(TermsConverter.ToTerms("'in"
), context);
TNumber size = Decision.NumericCalculation(arg, context);
if (size.El(1).obj.Units.ContainsUnits() && size.El(2).obj.Units.ContainsUnits())
{
if (size.El(1).obj.Units.Text == "'m" && size.El(2).obj.Units.Text == "'m" )
{
// compute the size string if given in m
if (resultunit == SizeUnit.Pixel) size = size /Inch * GlobalProfile.ScreenDpi;
else size = size / Inch;
}
else
{
// complain about non-consistent units.
}
}
else
{
// compute size given as pixels
if (resultunit == SizeUnit.Inch) size = size / GlobalProfile.ScreenDpi;
}
// Extract list entries to string
return size.El(1).obj.ToString(2, 10, FractionsType.Decimal, false) + ","
+ size.El(2).obj.ToString(2, 10, FractionsType.Decimal, false);
}
The image shows four image regions with the same graph, format PNG (left) and PDF (right) and size specified in SMath length units (upper) and in pixels (lower). The image regions are reset to original size via context menu. My screen is 96 dpi and at 100% zoom level the scaling is perfect.
Martin Kraska
Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
#3 Posted: 8/19/2017 1:43:20 PM
Units are available through the symbolic engine, therefore if this is not the most compact code it is close to it.
other ways to access units:
or:
BTW in the code above I'd suggest:
instead of
other ways to access units:
UnitInfo ui;
UnitsManager.TryGetUnit(context.SessionProfile, "'in", out ui, true);
Entry Inch = ui.Relations["Metric"];
or:
UnitInfo ui = UnitsManager.BuiltInUnitsLinear["'in"];
Entry Inch = ui.Relations["Metric"];
BTW in the code above I'd suggest:
TNumber Inch = Decision.NumericCalculation(new Entry("'in"
, context);
instead of
TNumber Inch = Decision.NumericCalculation(Entry.Create(TermsConverter.ToTerms("'in"
), context);
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 users liked this post
Martin Kraska 8/20/2017 3:43:00 AM
#4 Posted: 8/20/2017 3:47:15 AM
Wrote
BTW in the code above I'd suggest:TNumber Inch = Decision.NumericCalculation(new Entry("'in"
, context);
Nice, Thanks.
Martin Kraska
Pre-configured portable distribution of SMath Studio: https://en.smath.info/wiki/SMath%20with%20Plugins.ashx
1 Pages (4 items)
-
New Posts
-
No New Posts