SMath Studio Forum
»
SMath Studio
»
Questions
»
[Dev] How to acces the unit definitions for numeric computations?
Rank: Advanced Member Groups: Registered
Joined: 15/04/2012(UTC) Posts: 1,990 Was thanked: 1128 time(s) in 724 post(s)
|
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): Code:size = size * InchperMeter * GlobalProfile.ScreenDpi;
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 Code:
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. Edited by user 16 August 2017 20:29:02(UTC)
| Reason: Not specified |
|
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 15/04/2012(UTC) Posts: 1,990 Was thanked: 1128 time(s) in 724 post(s)
|
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. Code:
/// <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. Edited by moderator 19 August 2017 19:30:29(UTC)
| Reason: Not specified |
|
|
|
|
Rank: Advanced Member Groups: Registered, Advanced Member Joined: 13/01/2012(UTC) Posts: 2,654 Location: Italy Was thanked: 1340 time(s) in 879 post(s)
|
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: Code:UnitInfo ui;
UnitsManager.TryGetUnit(context.SessionProfile, "'in", out ui, true);
Entry Inch = ui.Relations["Metric"];
or: Code:UnitInfo ui = UnitsManager.BuiltInUnitsLinear["'in"];
Entry Inch = ui.Relations["Metric"];
BTW in the code above I'd suggest: Code:
TNumber Inch = Decision.NumericCalculation(new Entry("'in"), context);
instead of Code:TNumber Inch = Decision.NumericCalculation(Entry.Create(TermsConverter.ToTerms("'in")), context);
|
If you like my plugins consider to support SMath Studio buying a plan; to offer me a coffee: paypal.me/dcprojects |
1 user thanked Davide Carpi for this useful post.
|
|
|
Rank: Advanced Member Groups: Registered
Joined: 15/04/2012(UTC) Posts: 1,990 Was thanked: 1128 time(s) in 724 post(s)
|
Originally Posted by: Davide Carpi BTW in the code above I'd suggest: Code:
TNumber Inch = Decision.NumericCalculation(new Entry("'in"), context);
Nice, Thanks. |
|
|
|
|
SMath Studio Forum
»
SMath Studio
»
Questions
»
[Dev] How to acces the unit definitions for numeric computations?
Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.