common_plotDXF_functions.cpp 11.2 KB
Newer Older
charras's avatar
charras committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/******************************************/
/* Kicad: Common plot DXF Routines */
/******************************************/

#include "fctsys.h"
#include "gr_basic.h"
#include "trigo.h"
#include "wxstruct.h"
#include "base_struct.h"
#include "plot_common.h"
#include "macros.h"
#include "kicad_string.h"

/***********************************************************************************/
15
void DXF_PLOTTER::set_viewport( wxPoint offset,
charras's avatar
charras committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
                                double aScale, int orient )
/***********************************************************************************/

/* Set the plot offset for the current plotting
 */
{
    wxASSERT( !output_file );
    plot_offset  = offset;
    plot_scale   = aScale;
    device_scale = 1;
    set_default_line_width( 0 );    /* No line width on DXF */
    plot_orient_options = 0;        /* No mirroring on DXF */
    current_color = BLACK;
}


/*****************************************************************/
33
void DXF_PLOTTER::start_plot( FILE* fout )
charras's avatar
charras committed
34 35 36 37 38 39 40 41 42 43 44 45
/*****************************************************************/
{
    wxASSERT( !output_file );
    output_file = fout;
    /* DXF HEADER - Boilerplate */
    fputs(
        "0\nSECTION\n2\nHEADER\n9\n$ANGBASE\n50\n0.0\n9\n$ANGDIR\n70\n0\n0\nENDSEC\n0\nSECTION\n2\nTABLES\n0\nTABLE\n2\nLTYPE\n70\n1\n0\nLTYPE\n2\nCONTINUOUS\n70\n0\n3\nSolid line\n72\n65\n73\n0\n40\n0.0\n0\nENDTAB\n",
        output_file );
    /* Layer table - one layer per color */
    fprintf( output_file, "0\nTABLE\n2\nLAYER\n70\n%d\n", NBCOLOR );
    for( int i = 0; i<NBCOLOR; i++ )
    {
46
        wxString cname = ColorRefs[i].m_Name;
charras's avatar
charras committed
47
        fprintf( output_file, "0\nLAYER\n2\n%s\n70\n0\n62\n%d\n6\nCONTINUOUS\n",
48
                 CONV_TO_UTF8( cname ), i + 1 );
charras's avatar
charras committed
49 50 51 52 53 54 55 56
    }

    /* End of layer table, begin entities */
    fputs( "0\nENDTAB\n0\nENDSEC\n0\nSECTION\n2\nENTITIES\n", output_file );
}


/**********************************/
57
void DXF_PLOTTER::end_plot()
charras's avatar
charras committed
58 59 60 61 62 63 64 65 66 67 68
/**********************************/
{
    wxASSERT( output_file );
    /* DXF FOOTER */
    fputs( "0\nENDSEC\n0\nEOF\n", output_file );
    fclose( output_file );
    output_file = 0;
}


/******************************/
69
void DXF_PLOTTER::set_color( int color )
charras's avatar
charras committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
/******************************/

/*
 * color = color index in ColorRefs[]
 */
{
    wxASSERT( output_file );
    if( (color >= 0 && color_mode)
       || (color == BLACK)
       || (color == WHITE) )
    {
        current_color = color;
    }
}


/************************************************************/
87
void DXF_PLOTTER::rect( wxPoint p1, wxPoint p2, FILL_T fill, int width )
charras's avatar
charras committed
88 89 90 91 92 93 94 95 96 97 98 99
/************************************************************/
{
    wxASSERT( output_file );
    move_to( p1 );
    line_to( wxPoint( p1.x, p2.y ) );
    line_to( wxPoint( p2.x, p2.y ) );
    line_to( wxPoint( p2.x, p1.y ) );
    finish_to( wxPoint( p1.x, p1.y ) );
}


/************************************************************/
100
void DXF_PLOTTER::circle( wxPoint centre, int diameter, FILL_T fill, int width )
charras's avatar
charras committed
101 102 103 104 105 106 107
/************************************************************/
{
    wxASSERT( output_file );
    double rayon = user_to_device_size( diameter / 2 );
    user_to_device_coordinates( centre );
    if( rayon > 0 )
    {
108
        wxString cname = ColorRefs[current_color].m_Name;
charras's avatar
charras committed
109
        fprintf( output_file, "0\nCIRCLE\n8\n%s\n10\n%d.0\n20\n%d.0\n40\n%g\n",
110
                 CONV_TO_UTF8( cname ),
charras's avatar
charras committed
111 112 113 114 115 116
                 centre.x, centre.y, rayon );
    }
}


/*****************************************************/
117
void DXF_PLOTTER::poly( int nb, int* coord, FILL_T fill, int width )
charras's avatar
charras committed
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
/*****************************************************/

/* Trace un polygone (ferme si rempli) en format DXF
 * coord = tableau des coord des sommets
 * nb = nombre de coord ( 1 coord = 2 elements: X et Y du tableau )
 * fill : si != 0 polygone rempli
 */
{
    wxASSERT( output_file );
    if( nb <= 1 )
        return;

    move_to( wxPoint( coord[0], coord[1] ) );
    for( int ii = 1; ii < nb; ii++ )
        line_to( wxPoint( coord[ii * 2], coord[(ii * 2) + 1] ) );

    /* Fermeture eventuelle du polygone */
    if( fill )
    {
        int ii = (nb - 1) * 2;
        if( (coord[ii] != coord[0] ) || (coord[ii + 1] != coord[1]) )
            line_to( wxPoint( coord[0], coord[1] ) );
    }
    pen_finish();
}


/**********************************************/
146
void DXF_PLOTTER::pen_to( wxPoint pos, char plume )
charras's avatar
charras committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
/**********************************************/

/*
 * deplace la plume levee (plume = 'U') ou baissee (plume = 'D')
 * en position x,y
 * Unites en Unites DESSIN
 * Si plume = 'Z' lever de plume sans deplacement
 */
{
    wxASSERT( output_file );
    if( plume == 'Z' )
    {
        return;
    }
    user_to_device_coordinates( pos );

    if( pen_lastpos != pos && plume == 'D' )
    {
        /* DXF LINE */
166
        wxString cname = ColorRefs[current_color].m_Name;
charras's avatar
charras committed
167
        fprintf( output_file, "0\nLINE\n8\n%s\n10\n%d.0\n20\n%d.0\n11\n%d.0\n21\n%d.0\n",
168
                 CONV_TO_UTF8( cname ),
charras's avatar
charras committed
169 170 171 172 173 174
                 pen_lastpos.x, pen_lastpos.y, pos.x, pos.y );
    }
    pen_lastpos = pos;
}


175
void DXF_PLOTTER::set_dash( bool dashed )
charras's avatar
charras committed
176 177 178 179 180 181
{
    /* NOP for now */
    wxASSERT( output_file );
}


182
void DXF_PLOTTER::thick_segment( wxPoint start, wxPoint end, int width,
charras's avatar
charras committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
                                 GRTraceMode tracemode )

/** Function Plot a filled segment (track)
 * @param start = starting point
 * @param end = ending point
 * @param aWidth = segment width (thickness)
 * @param aPlotMode = FILLED, SKETCH ..
 */
{
    wxASSERT( output_file );

    if( tracemode == FILAIRE )  /* just a line is Ok */
    {
        move_to( start );
        finish_to( end );
    }
    else
        segment_as_oval( start, end, width, tracemode );
}


/********************************************************************/
205
void DXF_PLOTTER::arc( wxPoint centre, int StAngle, int EndAngle, int rayon,
charras's avatar
charras committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
                       FILL_T fill, int width )
/********************************************************************/

/* trace d'un arc de cercle:
 * centre = coord du centre
 * StAngle, EndAngle = angle de debut et fin
 * rayon = rayon de l'arc
 */
{
    wxASSERT( output_file );

    if( rayon <= 0 )
        return;

    user_to_device_coordinates( centre );
    rayon = user_to_device_size( rayon );

    /* DXF ARC */
224
    wxString cname = ColorRefs[current_color].m_Name;
charras's avatar
charras committed
225
    fprintf( output_file, "0\nARC\n8\n%s\n10\n%d.0\n20\n%d.0\n40\n%d.0\n50\n%d.0\n51\n%d.0\n",
226
             CONV_TO_UTF8( cname ),
charras's avatar
charras committed
227 228 229 230 231 232
             centre.x, centre.y, rayon,
             StAngle / 10, EndAngle / 10 );
}


/***********************************************************************************/
233
void DXF_PLOTTER::flash_pad_oval( wxPoint pos, wxSize size, int orient,
charras's avatar
charras committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
                                  GRTraceMode trace_mode )
/************************************************************************************/
/* Trace 1 pastille PAD_OVAL en position pos_X,Y , de dim size.x, size.y */
{
    wxASSERT( output_file );

    /* la pastille est ramenee a une pastille ovale avec size.y > size.x
     *  ( ovale vertical en orientation 0 ) */
    if( size.x > size.y )
    {
        EXCHG( size.x, size.y ); orient += 900;
        if( orient >= 3600 )
            orient -= 3600;
    }
    sketch_oval( pos, size, orient, -1 );
}


/*******************************************************************************/
253
void DXF_PLOTTER::flash_pad_circle( wxPoint pos, int diametre,
charras's avatar
charras committed
254 255 256 257 258 259 260 261 262 263
                                    GRTraceMode trace_mode )
/*******************************************************************************/
/* Trace 1 pastille RONDE (via,pad rond) en position pos */
{
    wxASSERT( output_file );
    circle( pos, diametre, NO_FILL );
}


/**************************************************************************/
264
void DXF_PLOTTER::flash_pad_rect( wxPoint pos, wxSize padsize,
charras's avatar
charras committed
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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
                                  int orient, GRTraceMode trace_mode )
/**************************************************************************/

/*
 *  Trace 1 pad rectangulaire vertical ou horizontal ( Pad rectangulaire )
 *  donne par son centre et ses dimensions X et Y
 *  Units are user units
 */
{
    wxASSERT( output_file );
    wxSize size;
    int    ox, oy, fx, fy;

    size.x = padsize.x / 2;  size.y = padsize.y / 2;

    if( size.x < 0 )
        size.x = 0;
    if( size.y < 0 )
        size.y = 0;

    /* Si une des dimensions est nulle, le trace se reduit a 1 trait */
    if( size.x == 0 )
    {
        ox = pos.x; oy = pos.y - size.y;
        RotatePoint( &ox, &oy, pos.x, pos.y, orient );
        fx = pos.x; fy = pos.y + size.y;
        RotatePoint( &fx, &fy, pos.x, pos.y, orient );
        move_to( wxPoint( ox, oy ) );
        finish_to( wxPoint( fx, fy ) );
        return;
    }
    if( size.y == 0 )
    {
        ox = pos.x - size.x; oy = pos.y;
        RotatePoint( &ox, &oy, pos.x, pos.y, orient );
        fx = pos.x + size.x; fy = pos.y;
        RotatePoint( &fx, &fy, pos.x, pos.y, orient );
        move_to( wxPoint( ox, oy ) );
        finish_to( wxPoint( fx, fy ) );
        return;
    }

    ox = pos.x - size.x; oy = pos.y - size.y;
    RotatePoint( &ox, &oy, pos.x, pos.y, orient );
    move_to( wxPoint( ox, oy ) );

    fx = pos.x - size.x; fy = pos.y + size.y;
    RotatePoint( &fx, &fy, pos.x, pos.y, orient );
    line_to( wxPoint( fx, fy ) );

    fx = pos.x + size.x; fy = pos.y + size.y;
    RotatePoint( &fx, &fy, pos.x, pos.y, orient );
    line_to( wxPoint( fx, fy ) );

    fx = pos.x + size.x; fy = pos.y - size.y;
    RotatePoint( &fx, &fy, pos.x, pos.y, orient );
    line_to( wxPoint( fx, fy ) );

    finish_to( wxPoint( ox, oy ) );
}


/*******************************************************************/
328
void DXF_PLOTTER::flash_pad_trapez( wxPoint pos, wxSize size, wxSize delta,
charras's avatar
charras committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
                                    int orient, GRTraceMode trace_mode )
/*******************************************************************/

/*
 *  Trace 1 pad trapezoidal donne par :
 *  son centre pos.x,pos.y
 *  ses dimensions dimX et dimY
 *  les variations deltaX et deltaY
 *  son orientation orient et 0.1 degres
 *  le mode de trace (FILLED, SKETCH, FILAIRE)
 *  Le trace n'est fait que pour un trapeze, c.a.d que deltaX ou deltaY
 *  = 0.
 *
 *  les notation des sommets sont ( vis a vis de la table tracante )
 *      0 ------------- 3
 *        .			   .
 *          .		  .
 *           .		 .
 *            1 --- 2
 */
{
    wxASSERT( output_file );
    wxPoint polygone[4];    /* coord des sommets / centre du pad */
    wxPoint coord[4];       /* coord reelles des sommets du trapeze a tracer */
    int     moveX, moveY; /* variation de position plume selon axe X et Y , lors
                           *  du remplissage du trapeze */
    moveX = moveY = 0;

    size.x  /= 2;  size.y /= 2;
    delta.x /= 2; delta.y /= 2;

    polygone[0].x = -size.x - delta.y; polygone[0].y = +size.y + delta.x;
    polygone[1].x = -size.x + delta.y; polygone[1].y = -size.y - delta.x;
    polygone[2].x = +size.x - delta.y; polygone[2].y = -size.y + delta.x;
    polygone[3].x = +size.x + delta.y; polygone[3].y = +size.y - delta.x;

    for( int ii = 0; ii < 4; ii++ )
    {
        coord[ii].x = polygone[ii].x + pos.x;
        coord[ii].y = polygone[ii].y + pos.y;
        RotatePoint( &coord[ii], pos, orient );
    }

    // Plot edge:
    move_to( coord[0] );
    line_to( coord[1] );
    line_to( coord[2] );
    line_to( coord[3] );
    finish_to( coord[0] );
}