• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Convert a pixel value into a millimeter value using sAIUser->IUAIRealToStringUnits

Participant ,
Mar 16, 2023 Mar 16, 2023

Copy link to clipboard

Copied

Hi all,

I have a value with unit pixel or document points and want to convert it to a value with unit millimeter.

The only way I have found so far, is the following:

AIReal pixelValue = 0.316522;
ai::UnicodeString unicodeStr;
sAIUser->IUAIRealToStringUnits(pixelValue, -1, unicodeStr); 
//unicodeStr = "0,1117 mm"
AIReal millimeterValue = 0;
sAIUser->IUStringToAIReal(unicodeStr, &millimeterValue); 
//millimeterValue = 0.1117

That is exactly the result I want to have!

But when I change the ruler-unit in Illustrator from millimeter to e.g. pixel, I get this result:

AIReal pixelValue = 0.316522;
ai::UnicodeString unicodeStr;
sAIUser->IUAIRealToStringUnits(pixelValue, -1, unicodeStr); 
//unicodeStr = "0,3165 px"
AIReal millimeterValue = 0;
sAIUser->IUStringToAIReal(unicodeStr, &millimeterValue); 
//millimeterValue = 0.3165

What can I do to always convert the pixel value to the corresponding millimeter value, regardless of which ruler-unit is set in Illustrator?

Thank you very much in advance.

Best Regards,

Fabian

TOPICS
SDK , Third party plugins , Tools

Views

586

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Apr 06, 2023 Apr 06, 2023

Hello again,

now I have found the API function EvaluateExpression which is doing exactly what I need to do. This function converts a pixel-value into millimeter-value regardless of the current ruler setting in Illustrator. Maybe this is the helper function you were talking about.

I am using it the following way:

AIReal pixelValue = 0.3165;
ai::UnicodeString testUnicStr;
sAIUser->IUAIRealToStringWithLocale(localWidthInPts, 4, testUnicStr, ai::Locale::kSystem);
//testUnicStr = "0,3165" // I need the 
...

Votes

Translate

Translate
Adobe
Guide ,
Mar 16, 2023 Mar 16, 2023

Copy link to clipboard

Copied

There's no way to configure the destination unit for that function. If you want a specific unit, regardless of the current ruler, you need to do the calculation yourself and then use IUAIRealToString() if you need help formatting it for the currently selected locale.

 

I thought there were helper functions, but if there are I don't remember where to find them. I checked how we do this and we manage all unit-to-unit conversions ourselves (since it's fairly straight forward) without using the API, except to get the current ruler unit if that matters to the operation. That we're not using any helper function suggests to me that they don't exist or I assume I'd have written the code to use them 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 17, 2023 Mar 17, 2023

Copy link to clipboard

Copied

Hello Mr. Patterson,

thanks for the fast answer.

Could you show me how you are doing the unit-to-unit conversion yourself?

I mean, what mathematical formulas or functions do I have to use, to convert e.g. the pixel-value "0.316522" into the millimeter-value "0.1117"?

Thank you very much in advance.

Fabian Taubitz

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 06, 2023 Apr 06, 2023

Copy link to clipboard

Copied

LATEST

Hello again,

now I have found the API function EvaluateExpression which is doing exactly what I need to do. This function converts a pixel-value into millimeter-value regardless of the current ruler setting in Illustrator. Maybe this is the helper function you were talking about.

I am using it the following way:

AIReal pixelValue = 0.3165;
ai::UnicodeString testUnicStr;
sAIUser->IUAIRealToStringWithLocale(localWidthInPts, 4, testUnicStr, ai::Locale::kSystem);
//testUnicStr = "0,3165" // I need the decimal-comma, because I am using a german system.
AIExpressionOptions exprOptions;
exprOptions.unit = AIUnits::kAIMillimeterUnits;
exprOptions.minValue = 0;
exprOptions.maxValue = 1000000;
exprOptions.oldValue = 0;
exprOptions.precision = 4;
ai::UnicodeString evaluatedUnicodeStr;
AIBoolean isChanged = false;
AIDouble localWidthInMillimeter = 0;
sAIUser->EvaluateExpression(testUnicStr, exprOptions, evaluatedUnicodeStr, isChanged, localWidthInMillimeter);
//localWidthInMillimeter = 0.1117

The only question that remains for me is, if this code also works on an english system, which uses a decimal-point instead of a decimal-comma. I dont have such a system available right now to test it.

Could you tell me if it would work as it is or do I have to use different parameters?

Best regards,

Fabian Taubitz

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines