dialog_plot_schematic_HPGL.cpp 12 KB
Newer Older
1 2 3 4 5 6
/** @file dialog_plot_schematic_HPGL.cpp
 */

/*
 * This program source code file is part of KICAD, a free EDA CAD application.
 *
7
 * Copyright (C) 1992-2010 Jean-Pierre Charras <jean-pierre.charras@gipsa-lab.inpg.fr
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
 * Copyright (C) 1992-2010 Kicad Developers, see change_log.txt for contributors.
 *
 * 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 "fctsys.h"
#include "gr_basic.h"
#include "common.h"
#include "confirm.h"
#include "plot_common.h"
#include "worksheet.h"
34 35
#include "class_sch_screen.h"
#include "wxEeschemaStruct.h"
36 37 38

#include "general.h"
#include "protos.h"
39
#include "sch_sheet_path.h"
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
#include "dialog_plot_schematic_HPGL_base.h"

enum PageFormatReq
{
    PAGE_DEFAULT = 0,
    PAGE_SIZE_A4,
    PAGE_SIZE_A3,
    PAGE_SIZE_A2,
    PAGE_SIZE_A1,
    PAGE_SIZE_A0,
    PAGE_SIZE_A,
    PAGE_SIZE_B,
    PAGE_SIZE_C,
    PAGE_SIZE_D,
    PAGE_SIZE_E
};

static Ki_PageDescr* Plot_sheet_list[] =
{
    NULL,
    &g_Sheet_A4,
    &g_Sheet_A3,
    &g_Sheet_A2,
    &g_Sheet_A1,
    &g_Sheet_A0,
    &g_Sheet_A,
    &g_Sheet_B,
    &g_Sheet_C,
    &g_Sheet_D,
    &g_Sheet_E,
    &g_Sheet_GERBER,
    &g_Sheet_user
};

class DIALOG_PLOT_SCHEMATIC_HPGL : public DIALOG_PLOT_SCHEMATIC_HPGL_BASE
{
private:
78
    SCH_EDIT_FRAME* m_Parent;
79 80

public:
81
   DIALOG_PLOT_SCHEMATIC_HPGL( SCH_EDIT_FRAME* parent );
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

private:
    static PageFormatReq  m_pageSizeSelect;
    static bool m_plot_Sheet_Ref;
    bool        m_select_PlotAll;

private:
    void OnPageSelected( wxCommandEvent& event );
    void OnPlotCurrent( wxCommandEvent& event );
    void OnPlotAll( wxCommandEvent& event );
    void OnCancelClick( wxCommandEvent& event );
    void AcceptPlotOffset( wxCommandEvent& event );

    void initDlg();
    void SetPenSpeed();
    void SetPenNum();
    void SetPenWidth();
    void SetPageOffsetValue();
    void HPGL_Plot( bool aPlotAll );
    void Plot_Schematic_HPGL( bool aPlotAll, int HPGL_SheetSize );
    void Plot_1_Page_HPGL( const wxString& FileName,
                           SCH_SCREEN* screen, Ki_PageDescr* sheet,
                           wxPoint& offset, double plot_scale );
105
    void ReturnSheetDims( SCH_SCREEN* screen, wxSize& SheetSize, wxPoint& SheetOffset );
106 107 108 109 110 111
};
/* static members (static to remember last state): */
PageFormatReq DIALOG_PLOT_SCHEMATIC_HPGL:: m_pageSizeSelect = PAGE_DEFAULT;
bool DIALOG_PLOT_SCHEMATIC_HPGL::m_plot_Sheet_Ref = true;


112
void SCH_EDIT_FRAME::ToPlot_HPGL( wxCommandEvent& event )
113 114 115 116 117 118
{
    DIALOG_PLOT_SCHEMATIC_HPGL dlg( this );
    dlg.ShowModal();
}


119
DIALOG_PLOT_SCHEMATIC_HPGL::DIALOG_PLOT_SCHEMATIC_HPGL( SCH_EDIT_FRAME* parent )
120 121 122 123 124 125 126 127
    :DIALOG_PLOT_SCHEMATIC_HPGL_BASE(parent)
{
    m_Parent = parent;
    initDlg();
    SetPageOffsetValue();

    GetSizer()->SetSizeHints( this );
    Centre();
128
    m_buttonPlotAll->SetDefault();
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
}


void DIALOG_PLOT_SCHEMATIC_HPGL::initDlg()
{

    SetFocus(); // Make ESC key working

    // Set validators
    m_SizeOption->SetSelection( m_pageSizeSelect );
    AddUnitSymbol( *m_penWidthTitle, g_UserUnit );
    PutValueInLocalUnits( *m_penWidthCtrl, g_HPGL_Pen_Descr. m_Pen_Diam, EESCHEMA_INTERNAL_UNIT );
    m_penSpeedCtrl->SetValue( g_HPGL_Pen_Descr. m_Pen_Speed );
    m_penNumCtrl->SetValue( g_HPGL_Pen_Descr. m_Pen_Num );

}


void DIALOG_PLOT_SCHEMATIC_HPGL::OnPlotCurrent( wxCommandEvent& event )
{
    HPGL_Plot( false );
}

void DIALOG_PLOT_SCHEMATIC_HPGL::OnPlotAll( wxCommandEvent& event )
{
    HPGL_Plot( true );
}

void DIALOG_PLOT_SCHEMATIC_HPGL::OnCancelClick( wxCommandEvent& event )
{
    EndModal( 0 );
}

void DIALOG_PLOT_SCHEMATIC_HPGL::SetPageOffsetValue()
{
    wxString msg;

    m_pageSizeSelect = (PageFormatReq) m_SizeOption->GetSelection();
    if( m_pageSizeSelect != PAGE_DEFAULT )
    {
        msg = ReturnStringFromValue( g_UserUnit,
                                     Plot_sheet_list[m_pageSizeSelect]->m_Offset.x,
                                     EESCHEMA_INTERNAL_UNIT );
        m_PlotOrgPosition_X->SetValue( msg );
        msg = ReturnStringFromValue( g_UserUnit,
                                     Plot_sheet_list[m_pageSizeSelect]-> m_Offset.y,
                                     EESCHEMA_INTERNAL_UNIT );
        m_PlotOrgPosition_Y->SetValue( msg );

        m_PlotOrgPosition_X->Enable( TRUE );
        m_PlotOrgPosition_Y->Enable( TRUE );
    }
    else
    {
        m_PlotOrgPosition_X->Enable( FALSE );
        m_PlotOrgPosition_Y->Enable( FALSE );
    }
}


void DIALOG_PLOT_SCHEMATIC_HPGL::AcceptPlotOffset( wxCommandEvent& event )
{
    m_pageSizeSelect = (PageFormatReq) m_SizeOption->GetSelection();

    if( m_pageSizeSelect != PAGE_DEFAULT )
    {
        wxString msg = m_PlotOrgPosition_X->GetValue();
        Plot_sheet_list[m_pageSizeSelect]->m_Offset.x =
            ReturnValueFromString( g_UserUnit, msg, EESCHEMA_INTERNAL_UNIT );
        msg = m_PlotOrgPosition_Y->GetValue();
        Plot_sheet_list[m_pageSizeSelect]->m_Offset.y =
            ReturnValueFromString( g_UserUnit, msg, EESCHEMA_INTERNAL_UNIT );
    }
}


void DIALOG_PLOT_SCHEMATIC_HPGL::SetPenWidth( )
{
    g_HPGL_Pen_Descr.m_Pen_Diam = ReturnValueFromTextCtrl( *m_penWidthCtrl,
                                                           EESCHEMA_INTERNAL_UNIT);
    if( g_HPGL_Pen_Descr.m_Pen_Diam > 100 )
        g_HPGL_Pen_Descr.m_Pen_Diam = 100;
211

212 213 214 215 216 217 218 219
    if( g_HPGL_Pen_Descr.m_Pen_Diam < 1 )
        g_HPGL_Pen_Descr.m_Pen_Diam = 1;
}


void DIALOG_PLOT_SCHEMATIC_HPGL::SetPenSpeed(  )
{
    g_HPGL_Pen_Descr.m_Pen_Speed = m_penSpeedCtrl->GetValue();
220

221 222
    if( g_HPGL_Pen_Descr.m_Pen_Speed > 40 )
        g_HPGL_Pen_Descr.m_Pen_Speed = 40;
223

224 225 226 227 228 229 230 231
    if( g_HPGL_Pen_Descr.m_Pen_Speed < 1 )
        g_HPGL_Pen_Descr.m_Pen_Speed = 1;
}


void DIALOG_PLOT_SCHEMATIC_HPGL::SetPenNum(  )
{
    g_HPGL_Pen_Descr.m_Pen_Num = m_penNumCtrl->GetValue();
232

233 234
    if( g_HPGL_Pen_Descr.m_Pen_Num > 8 )
        g_HPGL_Pen_Descr.m_Pen_Num = 8;
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
    if( g_HPGL_Pen_Descr.m_Pen_Num < 1 )
        g_HPGL_Pen_Descr.m_Pen_Num = 1;
}


void DIALOG_PLOT_SCHEMATIC_HPGL::HPGL_Plot( bool aPlotAll )
{
    SetPenWidth( );
    SetPenNum( );
    SetPenSpeed( );

    if( m_pageSizeSelect != PAGE_DEFAULT )
    {
        Ki_PageDescr* plot_sheet = Plot_sheet_list[m_pageSizeSelect];
        wxString msg = m_PlotOrgPosition_X->GetValue();
        plot_sheet->m_Offset.x =
            ReturnValueFromString( g_UserUnit, msg, EESCHEMA_INTERNAL_UNIT );
        msg = m_PlotOrgPosition_Y->GetValue();
        plot_sheet->m_Offset.y =
            ReturnValueFromString( g_UserUnit, msg, EESCHEMA_INTERNAL_UNIT );
    }

    Plot_Schematic_HPGL( aPlotAll, m_pageSizeSelect );
}


/* Function calculates the offsets and dimensions of any trace of the
 * selected sheet
 */
265 266 267
void DIALOG_PLOT_SCHEMATIC_HPGL::ReturnSheetDims( SCH_SCREEN* screen,
                                                  wxSize&     SheetSize,
                                                  wxPoint&    SheetOffset )
268 269 270 271
{
    Ki_PageDescr* PlotSheet;

    if( screen == NULL )
272
        screen = m_Parent->GetScreen();
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

    PlotSheet = screen->m_CurrentSheetDesc;

    SheetSize   = PlotSheet->m_Size;
    SheetOffset = PlotSheet->m_Offset;
}


void DIALOG_PLOT_SCHEMATIC_HPGL::Plot_Schematic_HPGL( bool aPlotAll, int HPGL_SheetSize )
{
    wxString               PlotFileName;
    SCH_SCREEN*            screen    = m_Parent->GetScreen();
    SCH_SHEET_PATH*        sheetpath, * oldsheetpath = m_Parent->GetSheet();
    Ki_PageDescr*          PlotSheet;
    wxSize                 SheetSize;
    wxPoint                SheetOffset, PlotOffset;

    /* When printing all pages, the printed page is not the current page.
     *  In complex hierarchies, we must setup references and others parameters
     *  in the printed SCH_SCREEN
     *  because in complex hierarchies a SCH_SCREEN (a schematic drawings)
     *  is shared between many sheets
     */
    SCH_SHEET_LIST SheetList( NULL );

    sheetpath = SheetList.GetFirst();
    SCH_SHEET_PATH list;

    while( true )
    {
        if( aPlotAll )
        {
            if( sheetpath == NULL )
                break;
307

308
            list.Clear();
309

310 311 312 313 314 315 316 317 318
            if( list.BuildSheetPathInfoFromSheetPathValue( sheetpath->Path() ) )
            {
                m_Parent->m_CurrentSheet = &list;
                m_Parent->m_CurrentSheet->UpdateAllScreenReferences();
                m_Parent->SetSheetNumberAndCount();
                screen = m_Parent->m_CurrentSheet->LastScreen();
            }
            else  // Should not happen
                return;
319

320 321
            sheetpath = SheetList.GetNext();
        }
322

323
        ReturnSheetDims( screen, SheetSize, SheetOffset );
324

325 326 327 328 329
        /* Calculation of conversion scales. */
        if( HPGL_SheetSize )
            PlotSheet = Plot_sheet_list[HPGL_SheetSize];
        else
            PlotSheet = screen->m_CurrentSheetDesc;
330

331
        /* 10x because eeschema works in mils, not decimals */
332
        double plot_scale = 10 * (double) PlotSheet->m_Size.x / (double) SheetSize.x;
333 334 335 336 337

        /* Calculate offsets */
        PlotOffset.x = -SheetOffset.x;
        PlotOffset.y = -SheetOffset.y;

338
        PlotFileName = m_Parent->GetUniqueFilenameForCurrentSheet() + wxT( ".plt" );
339 340

        SetLocaleTo_C_standard();
341
        Plot_1_Page_HPGL( PlotFileName, screen, PlotSheet, PlotOffset, plot_scale );
342 343 344 345 346 347 348 349 350 351 352 353 354
        SetLocaleTo_Default();

        if( !aPlotAll )
            break;
    }

    m_Parent->m_CurrentSheet = oldsheetpath;
    m_Parent->m_CurrentSheet->UpdateAllScreenReferences();
    m_Parent->SetSheetNumberAndCount();
}


void DIALOG_PLOT_SCHEMATIC_HPGL::Plot_1_Page_HPGL( const wxString& FileName,
355 356 357 358
                                                   SCH_SCREEN*     screen,
                                                   Ki_PageDescr*   sheet,
                                                   wxPoint&        offset,
                                                   double          plot_scale )
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
{
    wxString msg;

    FILE*    output_file = wxFopen( FileName, wxT( "wt" ) );

    if( output_file == NULL )
    {
        msg  = wxT( "\n** " );
        msg += _( "Unable to create " ) + FileName + wxT( " **\n" );
        m_MsgBox->AppendText( msg );
        wxBell();
        return;
    }

    SetLocaleTo_C_standard();
    msg.Printf( _( "Plot: %s " ), FileName.GetData() );
    m_MsgBox->AppendText( msg );

    HPGL_PLOTTER* plotter = new HPGL_PLOTTER();
    plotter->set_paper_size( sheet );
    plotter->set_viewport( offset, plot_scale, 0 );
    plotter->set_default_line_width( g_DrawDefaultLineThickness );
    /* Init : */
    plotter->set_creator( wxT( "EESchema-HPGL" ) );
    plotter->set_filename( FileName );
    plotter->set_pen_speed( g_HPGL_Pen_Descr.m_Pen_Speed );
    plotter->set_pen_number( g_HPGL_Pen_Descr.m_Pen_Num );
    plotter->set_pen_diameter( g_HPGL_Pen_Descr.m_Pen_Diam );
    plotter->set_pen_overlap( g_HPGL_Pen_Descr.m_Pen_Diam / 2 );
    plotter->start_plot( output_file );

    plotter->set_color( BLACK );
391

392 393 394
    if( m_plot_Sheet_Ref )
        m_Parent->PlotWorkSheet( plotter, screen );

395
    PlotDrawlist( plotter, screen->GetDrawItems() );
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

    plotter->end_plot();
    delete plotter;
    SetLocaleTo_Default();

    m_MsgBox->AppendText( wxT( "Ok\n" ) );
}


/* Event handler for page size option
 */
void DIALOG_PLOT_SCHEMATIC_HPGL::OnPageSelected( wxCommandEvent& event )
{
    m_pageSizeSelect = (PageFormatReq) m_SizeOption->GetSelection();
    SetPageOffsetValue();
}