plot_schematic_HPGL.cpp 7.47 KB
Newer Older
jean-pierre charras's avatar
jean-pierre charras committed
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
/** @file plot_schematic_HPGL.cpp
 */

/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 1992-2010 Jean-Pierre Charras <jean-pierre.charras@gipsa-lab.inpg.fr
 * 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 <plot_common.h>
#include <class_sch_screen.h>
#include <wxEeschemaStruct.h>
#include <base_units.h>
#include <sch_sheet_path.h>
34
#include <project.h>
jean-pierre charras's avatar
jean-pierre charras committed
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

#include <dialog_plot_schematic.h>


enum HPGL_PAGEZ_T {
    PAGE_DEFAULT = 0,
    HPGL_PAGE_SIZE_A4,
    HPGL_PAGE_SIZE_A3,
    HPGL_PAGE_SIZE_A2,
    HPGL_PAGE_SIZE_A1,
    HPGL_PAGE_SIZE_A0,
    HPGL_PAGE_SIZE_A,
    HPGL_PAGE_SIZE_B,
    HPGL_PAGE_SIZE_C,
    HPGL_PAGE_SIZE_D,
    HPGL_PAGE_SIZE_E,
};


static const wxChar* plot_sheet_list( int aSize )
{
    const wxChar* ret;

    switch( aSize )
    {
    default:
    case PAGE_DEFAULT:
        ret = NULL;         break;

    case HPGL_PAGE_SIZE_A4:
        ret = wxT( "A4" );  break;

    case HPGL_PAGE_SIZE_A3:
        ret = wxT( "A3" );  break;

    case HPGL_PAGE_SIZE_A2:
        ret = wxT( "A2" );  break;

    case HPGL_PAGE_SIZE_A1:
        ret = wxT( "A1" );  break;

    case HPGL_PAGE_SIZE_A0:
        ret = wxT( "A0" );  break;

    case HPGL_PAGE_SIZE_A:
        ret = wxT( "A" );   break;

    case HPGL_PAGE_SIZE_B:
        ret = wxT( "B" );   break;

    case HPGL_PAGE_SIZE_C:
        ret = wxT( "C" );   break;

    case HPGL_PAGE_SIZE_D:
        ret = wxT( "D" );   break;

    case HPGL_PAGE_SIZE_E:
        ret = wxT( "E" );   break;
    }

    return ret;
};


void DIALOG_PLOT_SCHEMATIC::SetHPGLPenWidth()
{
101
    m_HPGLPenSize = ValueFromTextCtrl( *m_penHPGLWidthCtrl );
jean-pierre charras's avatar
jean-pierre charras committed
102

103 104
    if( m_HPGLPenSize > Millimeter2iu( 2 ) )
        m_HPGLPenSize = Millimeter2iu( 2 );
jean-pierre charras's avatar
jean-pierre charras committed
105

106 107
    if( m_HPGLPenSize < Millimeter2iu( 0.01 ) )
        m_HPGLPenSize = Millimeter2iu( 0.01 );
jean-pierre charras's avatar
jean-pierre charras committed
108 109 110
}


111
void DIALOG_PLOT_SCHEMATIC::createHPGLFile( bool aPlotAll, bool aPlotFrameRef )
jean-pierre charras's avatar
jean-pierre charras committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
{
    SCH_SCREEN*     screen = m_parent->GetScreen();
    SCH_SHEET_PATH* sheetpath;
    SCH_SHEET_PATH  oldsheetpath = m_parent->GetCurrentSheet();

    /* When printing all pages, the printed page is not the current page.
     *  In complex hierarchies, we must setup references and other 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;
127
    WX_TEXT_CTRL_REPORTER reporter(m_MessagesBox);
jean-pierre charras's avatar
jean-pierre charras committed
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

    SetHPGLPenWidth();

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

            list.Clear();

            if( list.BuildSheetPathInfoFromSheetPathValue( sheetpath->Path() ) )
            {
                m_parent->SetCurrentSheet( list );
                m_parent->GetCurrentSheet().UpdateAllScreenReferences();
                m_parent->SetSheetNumberAndCount();

                screen = m_parent->GetCurrentSheet().LastScreen();

                if( !screen ) // LastScreen() may return NULL
                    screen = m_parent->GetScreen();
            }
            else // Should not happen
                return;

            sheetpath = SheetList.GetNext();
        }

        const PAGE_INFO&    curPage = screen->GetPageSettings();

        PAGE_INFO           plotPage = curPage;

        // if plotting on a page size other than curPage
        if( m_HPGLPaperSizeOption->GetSelection() != PAGE_DEFAULT )
            plotPage.SetType( plot_sheet_list( m_HPGLPaperSizeOption->GetSelection() ) );

        // Calculation of conversion scales.
        double  plot_scale = (double) plotPage.GetWidthMils() / curPage.GetWidthMils();

        // Calculate offsets
        wxPoint plotOffset;
170
        wxString msg;
jean-pierre charras's avatar
jean-pierre charras committed
171 172 173

        if( GetPlotOriginCenter() )
        {
174
            plotOffset.x    = plotPage.GetWidthIU() / 2;
jean-pierre charras's avatar
jean-pierre charras committed
175 176 177
            plotOffset.y    = -plotPage.GetHeightIU() / 2;
        }

178 179 180 181 182 183
        try
        {
            wxString fname = m_parent->GetUniqueFilenameForCurrentSheet();
            wxString ext = HPGL_PLOTTER::GetDefaultFileExtension();
            wxFileName plotFileName = createPlotFileName( m_outputDirectoryName, fname,
                                                          ext, &reporter );
jean-pierre charras's avatar
jean-pierre charras committed
184

185
            LOCALE_IO toggle;
186

187 188 189 190 191
            if( Plot_1_Page_HPGL( plotFileName.GetFullPath(), screen, plotPage, plotOffset,
                                plot_scale, aPlotFrameRef ) )
                msg.Printf( _( "Plot: '%s' OK\n" ), GetChars( plotFileName.GetFullPath() ) );
            else    // Error
                msg.Printf( _( "Unable to create '%s'\n" ), GetChars( plotFileName.GetFullPath() ) );
jean-pierre charras's avatar
jean-pierre charras committed
192

193
            m_MessagesBox->AppendText( msg );
194

195 196 197
            if( !aPlotAll )
                break;
        }
198
        catch( IO_ERROR& e )
199
        {
200
            msg.Printf( wxT( "HPGL Plotter Exception : '%s'"), GetChars( e.errorText ) );
201 202
            m_MessagesBox->AppendText( msg );
        }
jean-pierre charras's avatar
jean-pierre charras committed
203 204 205 206 207 208 209 210 211

    }

    m_parent->SetCurrentSheet( oldsheetpath );
    m_parent->GetCurrentSheet().UpdateAllScreenReferences();
    m_parent->SetSheetNumberAndCount();
}


212 213 214 215 216 217
bool DIALOG_PLOT_SCHEMATIC::Plot_1_Page_HPGL( const wxString&   aFileName,
                                              SCH_SCREEN*       aScreen,
                                              const PAGE_INFO&  aPageInfo,
                                              wxPoint           aPlot0ffset,
                                              double            aScale,
                                              bool              aPlotFrameRef )
jean-pierre charras's avatar
jean-pierre charras committed
218 219 220
{
    HPGL_PLOTTER* plotter = new HPGL_PLOTTER();

221 222
    plotter->SetPageSettings( aPageInfo );
    plotter->SetViewport( aPlot0ffset, IU_PER_DECIMILS, aScale, false );
jean-pierre charras's avatar
jean-pierre charras committed
223 224 225

    // Init :
    plotter->SetCreator( wxT( "Eeschema-HPGL" ) );
226 227 228 229 230 231 232 233 234

    if( ! plotter->OpenFile( aFileName ) )
    {
        delete plotter;
        return false;
    }

    LOCALE_IO toggle;

235 236 237 238
    // Pen num and pen speed are not initialized here.
    // Default HPGL driver values are used
    plotter->SetPenDiameter( m_HPGLPenSize );
    plotter->SetPenOverlap( m_HPGLPenSize / 4 );
239
    plotter->StartPlot();
jean-pierre charras's avatar
jean-pierre charras committed
240 241 242 243 244 245

    plotter->SetColor( BLACK );

    if( getPlotFrameRef() )
        PlotWorkSheet( plotter, m_parent->GetTitleBlock(),
                       m_parent->GetPageSettings(),
246
                       aScreen->m_ScreenNumber, aScreen->m_NumberOfScreens,
jean-pierre charras's avatar
jean-pierre charras committed
247
                       m_parent->GetScreenDesc(),
248
                       aScreen->GetFileName() );
jean-pierre charras's avatar
jean-pierre charras committed
249

250
    aScreen->Plot( plotter );
jean-pierre charras's avatar
jean-pierre charras committed
251 252 253 254

    plotter->EndPlot();
    delete plotter;

255
    return true;
jean-pierre charras's avatar
jean-pierre charras committed
256
}