~metacollin/kicad/osx_distribution

« back to all changes in this revision

Viewing changes to utils/idftools/dxf2idfmain.cpp

  • Committer: jean-pierre charras
  • Author(s): Cirilo Bernardo
  • Date: 2014-02-05 09:27:21 UTC
  • mto: This revision was merged to the branch mainline in revision 4660.
  • Revision ID: jp.charras@wanadoo.fr-20140205092721-l33hnnhoqogdaajq
Apply IDF tools patch from Cirilo Bernardo

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This program source code file is part of KiCad, a free EDA CAD application.
 
3
 *
 
4
 * Copyright (C) 2014  Cirilo Bernardo
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU General Public License
 
8
 * as published by the Free Software Foundation; either version 2
 
9
 * of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, you may find one here:
 
18
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 
19
 * or you may search the http://www.gnu.org website for the version 2 license,
 
20
 * or you may write to the Free Software Foundation, Inc.,
 
21
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 
22
 */
 
23
 
 
24
#include <cstdio>
 
25
#include <iostream>
 
26
#include <sstream>
 
27
#include <string>
 
28
#include <list>
 
29
#include <dxf2idf.h>
 
30
 
 
31
using namespace std;
 
32
 
 
33
int main( int argc, char **argv )
 
34
{
 
35
    list< string > comments;
 
36
    string line;
 
37
    stringstream tstr;
 
38
 
 
39
    string dname;           // DXF filename
 
40
    string gname;           // Geometry Name
 
41
    string pname;           // Part Name
 
42
    double height;          // extrusion height
 
43
    bool   inch = false;    // true = inches, false = mm
 
44
    bool   ok;
 
45
 
 
46
    if( argc == 1 )
 
47
    {
 
48
        // no arguments; print out usage information
 
49
        cout << "dxf2idf: this program takes line, arc, and circle segments\n";
 
50
        cout << "         from a DXF file and creates an IDF component outline file.\n\n";
 
51
        cout << "Input:\n";
 
52
        cout << "         DXF filename: the input file, must end in '.dxf'\n";
 
53
        cout << "         Units: mm, in (millimeters or inches)\n";
 
54
        cout << "         Geometry Name: string, as per IDF version 3.0 specification\n";
 
55
        cout << "         Part Name: as per IDF version 3.0 specification of Part Number\n";
 
56
        cout << "         Height: extruded height of the outline\n";
 
57
        cout << "         Comments: all non-empty lines are comments to be added to\n";
 
58
        cout << "                   the IDF file. An empty line signifies the end of\n";
 
59
        cout << "                   the comment block.\n";
 
60
        cout << "         File name: output filename, must end in '.idf'\n\n";
 
61
    }
 
62
 
 
63
    line.clear();
 
64
    while( line.empty() || line.find( ".dxf" ) == string::npos )
 
65
    {
 
66
        cout << "* DXF filename: ";
 
67
 
 
68
        line.clear();
 
69
        std::getline( cin, line );
 
70
    }
 
71
    dname = line;
 
72
 
 
73
    line.clear();
 
74
    while( line.compare( "mm" ) && line.compare( "in" )
 
75
        && line.compare( "MM" ) && line.compare( "IN" ) )
 
76
    {
 
77
        cout << "* Units (mm,in): ";
 
78
        line.clear();
 
79
        std::getline( cin, line );
 
80
    }
 
81
 
 
82
    if( line.compare( "mm" ) && line.compare( "MM" ) )
 
83
        inch = true;
 
84
 
 
85
    line.clear();
 
86
    while( line.empty() )
 
87
    {
 
88
        cout << "* Geometry name: ";
 
89
        line.clear();
 
90
        std::getline( cin, line );
 
91
 
 
92
        if( line.find( "\"" ) != string::npos )
 
93
        {
 
94
            cerr << "[INFO] geometry name may not contain quotation marks\n";
 
95
            line.clear();
 
96
        }
 
97
    }
 
98
    gname = line;
 
99
 
 
100
    line.clear();
 
101
    while( line.empty() )
 
102
    {
 
103
        cout << "* Part name: ";
 
104
        line.clear();
 
105
        std::getline( cin, line );
 
106
 
 
107
        if( line.find( "\"" ) != string::npos )
 
108
        {
 
109
            cerr << "[INFO] part name may not contain quotation marks\n";
 
110
            line.clear();
 
111
        }
 
112
    }
 
113
    pname = line;
 
114
 
 
115
    ok = false;
 
116
    while( !ok )
 
117
    {
 
118
        cout << "* Height: ";
 
119
 
 
120
        line.clear();
 
121
        std::getline( cin, line );
 
122
 
 
123
        tstr.clear();
 
124
        tstr.str( line );
 
125
 
 
126
        if( (tstr >> height ) && height > 0.001 )
 
127
            ok = true;
 
128
    }
 
129
 
 
130
    cout << "* COMMENTS: any non-blank line is a comment;\n";
 
131
    cout << "            a blank line signifies the end of comments.\n";
 
132
    ok = false;
 
133
    while( !ok )
 
134
    {
 
135
        line.clear();
 
136
        std::getline( cin, line );
 
137
 
 
138
        if( line.empty() )
 
139
        {
 
140
            ok = true;
 
141
        }
 
142
        else
 
143
        {
 
144
            if( line[0] != '#' )
 
145
                line.insert( 0, "# " );
 
146
 
 
147
            comments.push_back( line );
 
148
        }
 
149
    }
 
150
 
 
151
    line.clear();
 
152
    while( line.empty() || line.find( ".idf" ) == string::npos )
 
153
    {
 
154
        cout << "* File name (*.idf): ";
 
155
 
 
156
        line.clear();
 
157
        std::getline( cin, line );
 
158
    }
 
159
 
 
160
    DXF2IDF dxf;
 
161
 
 
162
    dxf.ReadDxf( dname.c_str() );
 
163
 
 
164
    FILE* fp = fopen( line.c_str(), "w" );
 
165
 
 
166
    list< string >::const_iterator scom = comments.begin();
 
167
    list< string >::const_iterator ecom = comments.end();
 
168
 
 
169
    while( scom != ecom )
 
170
    {
 
171
        fprintf( fp, "%s\n", (*scom).c_str() );
 
172
        ++scom;
 
173
    }
 
174
 
 
175
    fprintf( fp, ".ELECTRICAL\n" );
 
176
 
 
177
    if( inch )
 
178
        fprintf( fp, "\"%s\" \"%s\" THOU %d\n", gname.c_str(),
 
179
                 pname.c_str(), (int) (height * 1000.0) );
 
180
    else
 
181
        fprintf( fp, "\"%s\" \"%s\" MM %.3f\n", gname.c_str(),
 
182
                 pname.c_str(), height );
 
183
 
 
184
    dxf.WriteOutline( fp, inch );
 
185
 
 
186
    fprintf( fp, ".END_ELECTRICAL\n" );
 
187
 
 
188
    return 0;
 
189
}
 
 
b'\\ No newline at end of file'