Add EDA_UNIT_UTILS::ParseInternalUnits

This commit is contained in:
Alex Shvartzkop 2023-10-05 10:52:23 +03:00 committed by dsa-t
parent b5ab807568
commit 471bfb1131
2 changed files with 67 additions and 2 deletions

View File

@ -1,7 +1,7 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -25,6 +25,7 @@
#include <fmt/core.h>
#include <math/util.h> // for KiROUND
#include <macros.h>
#include <charconv>
bool EDA_UNIT_UTILS::IsImperialUnit( EDA_UNITS aUnit )
{
@ -180,6 +181,44 @@ std::string EDA_UNIT_UTILS::FormatInternalUnits( const EDA_IU_SCALE& aIuScale,
}
bool EDA_UNIT_UTILS::ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
int& aOut )
{
double value;
if( std::from_chars( aInput.data(), aInput.data() + aInput.size(), value ).ec != std::errc() )
return false;
aOut = value * aIuScale.IU_PER_MM;
return true;
}
bool EDA_UNIT_UTILS::ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
VECTOR2I& aOut )
{
size_t pos = aInput.find( ' ' );
if( pos == std::string::npos )
return false;
std::string first = aInput.substr( 0, pos );
std::string second = aInput.substr( pos + 1 );
VECTOR2I vec;
if( !ParseInternalUnits( first, aIuScale, vec.x ) )
return false;
if( !ParseInternalUnits( second, aIuScale, vec.y ) )
return false;
aOut = vec;
return true;
}
#define IU_TO_MM( x, scale ) ( x / scale.IU_PER_MM )
#define IU_TO_IN( x, scale ) ( x / scale.IU_PER_MILS / 1000 )
#define IU_TO_MILS( x, scale ) ( x / scale.IU_PER_MILS )

View File

@ -1,7 +1,7 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 1992-2022 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -122,6 +122,32 @@ namespace EDA_UNIT_UTILS
KICOMMON_API std::string FormatInternalUnits( const EDA_IU_SCALE& aIuScale,
const VECTOR2I& aPoint );
/**
* Converts \a aInput string to internal units when reading from a file.
*
* This should only be used for reading from files as it ignores locale
*
* @param aInput is std::string to parse.
* @param aIuScale is the scale to use.
* @param aOut is the output reference.
* @return true if the parsing was successful.
*/
KICOMMON_API bool ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
int& aOut );
/**
* Converts \a aInput string to internal units vector when reading from a file.
*
* This should only be used for reading from files as it ignores locale
*
* @param aInput is std::string to parse.
* @param aIuScale is the scale to use.
* @param aOut is the output reference vector.
* @return true if the parsing was successful.
*/
KICOMMON_API bool ParseInternalUnits( const std::string& aInput, const EDA_IU_SCALE& aIuScale,
VECTOR2I& aOut );
constexpr inline int Mils2IU( const EDA_IU_SCALE& aIuScale, int mils )
{
double x = mils * aIuScale.IU_PER_MILS;