CADSTAR Schematic Archive Importer: Fix Coordinate Rounding error

Rewrite of the calculation to convert CADSTAR to KiCad coordinates,
rounding half way cases away from zero.
Use integer division instead of double, to ensure precision.
This commit is contained in:
Roberto Fernandez Bautista 2021-02-18 18:02:49 +00:00 committed by Wayne Stambaugh
parent 842a930ced
commit ccbc0f73cc
4 changed files with 18 additions and 10 deletions

View File

@ -54,8 +54,8 @@ void CADSTAR_SCH_ARCHIVE_LOADER::Load( SCHEMATIC* aSchematic, SCH_SHEET* aRootSh
LONGPOINT designLimit = Assignments.Settings.DesignLimit;
//Note: can't use getKiCadPoint() due wxPoint being int - need long long to make the check
long long designSizeXkicad = (long long) designLimit.x * KiCadUnitMultiplier;
long long designSizeYkicad = (long long) designLimit.y * KiCadUnitMultiplier;
long long designSizeXkicad = (long long) designLimit.x / KiCadUnitDivider;
long long designSizeYkicad = (long long) designLimit.y / KiCadUnitDivider;
// Max size limited by the positive dimension of wxPoint (which is an int)
constexpr long long maxDesignSizekicad = std::numeric_limits<int>::max();
@ -2408,8 +2408,8 @@ wxPoint CADSTAR_SCH_ARCHIVE_LOADER::getKiCadPoint( wxPoint aCadstarPoint )
{
wxPoint retval;
retval.x = ( aCadstarPoint.x - m_designCenter.x ) * KiCadUnitMultiplier;
retval.y = -( aCadstarPoint.y - m_designCenter.y ) * KiCadUnitMultiplier;
retval.x = getKiCadLength( aCadstarPoint.x - m_designCenter.x );
retval.y = -getKiCadLength( aCadstarPoint.y - m_designCenter.y );
return retval;
}
@ -2420,8 +2420,8 @@ wxPoint CADSTAR_SCH_ARCHIVE_LOADER::getKiCadLibraryPoint(
{
wxPoint retval;
retval.x = ( aCadstarPoint.x - aCadstarCentre.x ) * KiCadUnitMultiplier;
retval.y = ( aCadstarPoint.y - aCadstarCentre.y ) * KiCadUnitMultiplier;
retval.x = getKiCadLength( aCadstarPoint.x - aCadstarCentre.x );
retval.y = getKiCadLength( aCadstarPoint.y - aCadstarCentre.y );
return retval;
}

View File

@ -214,7 +214,15 @@ private:
int getKiCadLength( long long aCadstarLength )
{
return aCadstarLength * KiCadUnitMultiplier;
int mod = aCadstarLength % KiCadUnitDivider;
int absmod = sign( mod ) * mod;
int offset = 0;
// Round half-way cases away from zero
if( absmod >= KiCadUnitDivider / 2 )
offset = sign( aCadstarLength );
return ( aCadstarLength / KiCadUnitDivider ) + offset;
}
/**

View File

@ -46,7 +46,7 @@ void CADSTAR_SCH_ARCHIVE_PARSER::Parse()
switch( Header.Resolution )
{
case RESOLUTION::HUNDREDTH_MICRON:
KiCadUnitMultiplier = SCH_IU_PER_MM / 1e5;
KiCadUnitDivider = (long) 1e5 / (long) SCH_IU_PER_MM;
break;
default:

View File

@ -36,7 +36,7 @@ class CADSTAR_SCH_ARCHIVE_PARSER : public CADSTAR_ARCHIVE_PARSER
{
public:
explicit CADSTAR_SCH_ARCHIVE_PARSER( wxString aFilename )
: CADSTAR_ARCHIVE_PARSER(), Filename( aFilename ), KiCadUnitMultiplier( 0.1 )
: CADSTAR_ARCHIVE_PARSER(), Filename( aFilename ), KiCadUnitDivider( 10 )
{
}
@ -452,7 +452,7 @@ public:
ATTRCOLORS AttrColors;
PARTNAMECOL SymbolPartNameColor;
double KiCadUnitMultiplier; ///<Use this value to convert units in this CSA file to KiCad units
int KiCadUnitDivider; ///<Use this value to convert units in this CSA file to KiCad units
}; //CADSTAR_SCH_ARCHIVE_PARSER