dxf2idf.cpp 7.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2014  Cirilo Bernardo
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <cstdio>
#include <iostream>
#include <libdxfrw.h>
#include <dxf2idf.h>

// differences in angle smaller than MIN_ANG are considered equal
#define MIN_ANG     (0.01)

DXF2IDF::~DXF2IDF()
{
    while( !lines.empty() )
    {
#ifdef DEBUG_IDF
        IDF3::printSeg( lines.back() );
#endif
        delete lines.back();
        lines.pop_back();
    }
}


bool DXF2IDF::ReadDxf( const std::string aFile )
{
    dxfRW* reader = new dxfRW( aFile.c_str() );

    if( !reader )
        return false;

    bool success = reader->read( this, true );

    delete reader;
    return success;
}


void DXF2IDF::addLine( const DRW_Line& data )
{
    IDF_POINT p1, p2;

    p1.x = data.basePoint.x;
    p1.y = data.basePoint.y;
    p2.x = data.secPoint.x;
    p2.y = data.secPoint.y;

    IDF_SEGMENT* seg = new IDF_SEGMENT( p1, p2 );

    if( !seg )
    {
        std::cerr << "* FAULT: could not add a linear segment to the outline\n";
    }
    else
    {
        lines.push_back( seg );
    }

    return;
}


void DXF2IDF::addCircle( const DRW_Circle& data )
{
    IDF_POINT p1, p2;

    p1.x = data.basePoint.x;
    p1.y = data.basePoint.y;

    p2.x = p1.x + data.radious;
    p2.y = p1.y;

    IDF_SEGMENT* seg = new IDF_SEGMENT( p1, p2, 360, true );

    if( !seg )
    {
        std::cerr << "* FAULT: could not add a linear segment to the outline\n";
    }
    else
    {
        lines.push_back( seg );
    }

    return;
}


void DXF2IDF::addArc( const DRW_Arc& data )
{
    IDF_POINT p1, p2;

    p1.x = data.basePoint.x;
    p1.y = data.basePoint.y;

    // note: DXF circles always run CCW
    double ea = data.endangle;

    while( ea < data.staangle )
        ea += M_PI;

    p2.x = p1.x + cos( data.staangle ) * data.radious;
    p2.y = p1.y + sin( data.staangle ) * data.radious;

    double angle = ( ea - data.staangle ) * 180.0 / M_PI;

    IDF_SEGMENT* seg = new IDF_SEGMENT( p1, p2, angle, true );

    if( !seg )
    {
        std::cerr << "* FAULT: could not add a linear segment to the outline\n";
    }
    else
    {
        lines.push_back( seg );
    }

    return;
}


bool DXF2IDF::WriteOutline( FILE* aFile, bool isInch )
{
    if( lines.empty() )
    {
        std::cerr << "* DXF2IDF: empty outline\n";
        return false;
    }

    // 1. find lowest X value
    // 2. string an outline together
    // 3. emit warnings if more than 1 outline
    IDF_OUTLINE outline;

    IDF3::GetOutline( lines, outline );

    if( outline.empty() )
    {
        std::cerr << "* DXF2IDF::WriteOutline(): no valid outline in file\n";
        return false;
    }

    if( !lines.empty() )
    {
        std::cerr << "* DXF2IDF::WriteOutline(): WARNING: more than 1 outline in file\n";
        std::cerr << "*                          Only the first outline will be used\n";
    }

    char loopDir = '1';

    if( outline.IsCCW() )
        loopDir = '0';

    std::list<IDF_SEGMENT*>::iterator bo;
    std::list<IDF_SEGMENT*>::iterator eo;

    if( outline.size() == 1 )
    {
        if( !outline.front()->IsCircle() )
        {
            std::cerr << "* DXF2IDF::WriteOutline(): bad outline\n";
            return false;
        }

            // NOTE: a circle always has an angle of 360, never -360,
            // otherwise SolidWorks chokes on the file.
            if( isInch )
            {
                fprintf( aFile, "%c %d %d 0\n", loopDir,
                         (int) (1000 * outline.front()->startPoint.x),
                         (int) (1000 * outline.front()->startPoint.y) );
                fprintf( aFile, "%c %d %d 360\n", loopDir,
                         (int) (1000 * outline.front()->endPoint.x),
                         (int) (1000 * outline.front()->endPoint.y) );
            }
            else
            {
                fprintf( aFile, "%c %.3f %.3f 0\n", loopDir,
                         outline.front()->startPoint.x, outline.front()->startPoint.y );
                fprintf( aFile, "%c %.3f %.3f 360\n", loopDir,
                         outline.front()->endPoint.x, outline.front()->endPoint.y );
            }

        return true;
    }

    // ensure that the very last point is the same as the very first point
    outline.back()-> endPoint = outline.front()->startPoint;

    bo  = outline.begin();
    eo  = outline.end();

    // for the first item we write out both points
    if( (*bo)->angle < MIN_ANG && (*bo)->angle > -MIN_ANG )
    {
        if( isInch )
        {
            fprintf( aFile, "%c %d %d 0\n", loopDir,
                     (int) (1000 * (*bo)->startPoint.x),
                     (int) (1000 * (*bo)->startPoint.y) );
            fprintf( aFile, "%c %d %d 0\n", loopDir,
                     (int) (1000 * (*bo)->endPoint.x),
                     (int) (1000 * (*bo)->endPoint.y) );
        }
        else
        {
            fprintf( aFile, "%c %.3f %.3f 0\n", loopDir,
                     (*bo)->startPoint.x, (*bo)->startPoint.y );
            fprintf( aFile, "%c %.3f %.3f 0\n", loopDir,
                     (*bo)->endPoint.x, (*bo)->endPoint.y );
        }
    }
    else
    {
        if( isInch )
        {
            fprintf( aFile, "%c %d %d 0\n", loopDir,
                     (int) (1000 * (*bo)->startPoint.x),
                     (int) (1000 * (*bo)->startPoint.y) );
            fprintf( aFile, "%c %d %d %.2f\n", loopDir,
                     (int) (1000 * (*bo)->endPoint.x),
                     (int) (1000 * (*bo)->endPoint.y),
                     (*bo)->angle );
        }
        else
        {
            fprintf( aFile, "%c %.3f %.3f 0\n", loopDir,
                     (*bo)->startPoint.x, (*bo)->startPoint.y );
            fprintf( aFile, "%c %.3f %.3f %.2f\n", loopDir,
                     (*bo)->endPoint.x, (*bo)->endPoint.y, (*bo)->angle );
        }
    }

    ++bo;

    // for all other segments we only write out the last point
    while( bo != eo )
    {
        if( isInch )
        {
            if( (*bo)->angle < MIN_ANG && (*bo)->angle > -MIN_ANG )
            {
                fprintf( aFile, "%c %d %d 0\n", loopDir,
                         (int) (1000 * (*bo)->endPoint.x),
                         (int) (1000 * (*bo)->endPoint.y) );
            }
            else
            {
                fprintf( aFile, "%c %d %d %.2f\n", loopDir,
                         (int) (1000 * (*bo)->endPoint.x),
                         (int) (1000 * (*bo)->endPoint.y),
                         (*bo)->angle );
            }
        }
        else
        {
            if( (*bo)->angle < MIN_ANG && (*bo)->angle > -MIN_ANG )
            {
                fprintf( aFile, "%c %.5f %.5f 0\n", loopDir,
                         (*bo)->endPoint.x, (*bo)->endPoint.y );
            }
            else
            {
                fprintf( aFile, "%c %.5f %.5f %.2f\n", loopDir,
                         (*bo)->endPoint.x, (*bo)->endPoint.y, (*bo)->angle );
            }
        }

        ++bo;
    }

    return true;
}