Welcome Guest! To enable all features please Login. New Registrations are disabled.

Notification

Icon
Error

Login


Options
Go to last post Go to first unread
Offline mkraska  
#1 Posted : 16 August 2017 20:03:05(UTC)
mkraska


Rank: Advanced Member

Groups: Registered
Joined: 15/04/2012(UTC)
Posts: 1,986
Germany

Was thanked: 1124 time(s) in 721 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

Martin Kraska

Pre-configured portable distribution of SMath Studio: https://smath.com/wiki/SMath_with_Plugins.ashx

Wanna join the discussion?! Login to your SMath Studio Forum forum account. New Registrations are disabled.

Offline mkraska  
#2 Posted : 16 August 2017 22:13:36(UTC)
mkraska


Rank: Advanced Member

Groups: Registered
Joined: 15/04/2012(UTC)
Posts: 1,986
Germany

Was thanked: 1124 time(s) in 721 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.

2017-08-16 20_54_49-SMath Studio 0.98.6398 - [svgtest.sm_].png

Edited by moderator 19 August 2017 19:30:29(UTC)  | Reason: Not specified

Martin Kraska

Pre-configured portable distribution of SMath Studio: https://smath.com/wiki/SMath_with_Plugins.ashx
Offline Davide Carpi  
#3 Posted : 19 August 2017 19:43:20(UTC)
Davide Carpi


Rank: Advanced Member

Groups: Registered, Advanced Member
Joined: 13/01/2012(UTC)
Posts: 2,647
Man
Italy
Location: Italy

Was thanked: 1329 time(s) in 875 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
thanks 1 user thanked Davide Carpi for this useful post.
on 20/08/2017(UTC)
Offline mkraska  
#4 Posted : 20 August 2017 09:47:15(UTC)
mkraska


Rank: Advanced Member

Groups: Registered
Joined: 15/04/2012(UTC)
Posts: 1,986
Germany

Was thanked: 1124 time(s) in 721 post(s)
Originally Posted by: Davide Carpi Go to Quoted Post


BTW in the code above I'd suggest:
Code:

TNumber Inch = Decision.NumericCalculation(new Entry("'in"), context);



Nice, Thanks.
Martin Kraska

Pre-configured portable distribution of SMath Studio: https://smath.com/wiki/SMath_with_Plugins.ashx
Users browsing this topic
Guest
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.