dxf2idfmain.cpp 5.21 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
/*
 * 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 <sstream>
#include <string>
#include <list>
#include <dxf2idf.h>

using namespace std;

int main( int argc, char **argv )
{
    list< string > comments;
    string line;
    stringstream tstr;

    string dname;           // DXF filename
    string gname;           // Geometry Name
    string pname;           // Part Name
    double height;          // extrusion height
    bool   inch = false;    // true = inches, false = mm
    bool   ok;

    if( argc == 1 )
    {
        // no arguments; print out usage information
        cout << "dxf2idf: this program takes line, arc, and circle segments\n";
        cout << "         from a DXF file and creates an IDF component outline file.\n\n";
        cout << "Input:\n";
        cout << "         DXF filename: the input file, must end in '.dxf'\n";
        cout << "         Units: mm, in (millimeters or inches)\n";
        cout << "         Geometry Name: string, as per IDF version 3.0 specification\n";
        cout << "         Part Name: as per IDF version 3.0 specification of Part Number\n";
        cout << "         Height: extruded height of the outline\n";
        cout << "         Comments: all non-empty lines are comments to be added to\n";
        cout << "                   the IDF file. An empty line signifies the end of\n";
        cout << "                   the comment block.\n";
        cout << "         File name: output filename, must end in '.idf'\n\n";
    }

    line.clear();
    while( line.empty() || line.find( ".dxf" ) == string::npos )
    {
        cout << "* DXF filename: ";

        line.clear();
        std::getline( cin, line );
    }
    dname = line;

    line.clear();
    while( line.compare( "mm" ) && line.compare( "in" )
        && line.compare( "MM" ) && line.compare( "IN" ) )
    {
        cout << "* Units (mm,in): ";
        line.clear();
        std::getline( cin, line );
    }

    if( line.compare( "mm" ) && line.compare( "MM" ) )
        inch = true;

    line.clear();
    while( line.empty() )
    {
        cout << "* Geometry name: ";
        line.clear();
        std::getline( cin, line );

        if( line.find( "\"" ) != string::npos )
        {
            cerr << "[INFO] geometry name may not contain quotation marks\n";
            line.clear();
        }
    }
    gname = line;

    line.clear();
    while( line.empty() )
    {
        cout << "* Part name: ";
        line.clear();
        std::getline( cin, line );

        if( line.find( "\"" ) != string::npos )
        {
            cerr << "[INFO] part name may not contain quotation marks\n";
            line.clear();
        }
    }
    pname = line;

    ok = false;
    while( !ok )
    {
        cout << "* Height: ";

        line.clear();
        std::getline( cin, line );

        tstr.clear();
        tstr.str( line );

126 127
        tstr >> height;
        if( !tstr.fail() && height > 0.001 )
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
            ok = true;
    }

    cout << "* COMMENTS: any non-blank line is a comment;\n";
    cout << "            a blank line signifies the end of comments.\n";
    ok = false;
    while( !ok )
    {
        line.clear();
        std::getline( cin, line );

        if( line.empty() )
        {
            ok = true;
        }
        else
        {
            if( line[0] != '#' )
                line.insert( 0, "# " );

            comments.push_back( line );
        }
    }

    line.clear();
    while( line.empty() || line.find( ".idf" ) == string::npos )
    {
        cout << "* File name (*.idf): ";

        line.clear();
        std::getline( cin, line );
    }

    DXF2IDF dxf;

    dxf.ReadDxf( dname.c_str() );

    FILE* fp = fopen( line.c_str(), "w" );

    list< string >::const_iterator scom = comments.begin();
    list< string >::const_iterator ecom = comments.end();

    while( scom != ecom )
    {
        fprintf( fp, "%s\n", (*scom).c_str() );
        ++scom;
    }

    fprintf( fp, ".ELECTRICAL\n" );

    if( inch )
        fprintf( fp, "\"%s\" \"%s\" THOU %d\n", gname.c_str(),
                 pname.c_str(), (int) (height * 1000.0) );
    else
        fprintf( fp, "\"%s\" \"%s\" MM %.3f\n", gname.c_str(),
                 pname.c_str(), height );

    dxf.WriteOutline( fp, inch );

    fprintf( fp, ".END_ELECTRICAL\n" );

    return 0;
190
}