You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

366 lines
14 KiB

* KIWAY Milestone A): Make major modules into DLL/DSOs. ! The initial testing of this commit should be done using a Debug build so that all the wxASSERT()s are enabled. Also, be sure and keep enabled the USE_KIWAY_DLLs option. The tree won't likely build without it. Turning it off is senseless anyways. If you want stable code, go back to a prior version, the one tagged with "stable". * Relocate all functionality out of the wxApp derivative into more finely targeted purposes: a) DLL/DSO specific b) PROJECT specific c) EXE or process specific d) configuration file specific data e) configuration file manipulations functions. All of this functionality was blended into an extremely large wxApp derivative and that was incompatible with the desire to support multiple concurrently loaded DLL/DSO's ("KIFACE")s and multiple concurrently open projects. An amazing amount of organization come from simply sorting each bit of functionality into the proper box. * Switch to wxConfigBase from wxConfig everywhere except instantiation. * Add classes KIWAY, KIFACE, KIFACE_I, SEARCH_STACK, PGM_BASE, PGM_KICAD, PGM_SINGLE_TOP, * Remove "Return" prefix on many function names. * Remove obvious comments from CMakeLists.txt files, and from else() and endif()s. * Fix building boost for use in a DSO on linux. * Remove some of the assumptions in the CMakeLists.txt files that windows had to be the host platform when building windows binaries. * Reduce the number of wxStrings being constructed at program load time via static construction. * Pass wxConfigBase* to all SaveSettings() and LoadSettings() functions so that these functions are useful even when the wxConfigBase comes from another source, as is the case in the KICAD_MANAGER_FRAME. * Move the setting of the KIPRJMOD environment variable into class PROJECT, so that it can be moved into a project variable soon, and out of FP_LIB_TABLE. * Add the KIWAY_PLAYER which is associated with a particular PROJECT, and all its child wxFrames and wxDialogs now have a Kiway() member function which returns a KIWAY& that that window tree branch is in support of. This is like wxWindows DNA in that child windows get this member with proper value at time of construction. * Anticipate some of the needs for milestones B) and C) and make code adjustments now in an effort to reduce work in those milestones. * No testing has been done for python scripting, since milestone C) has that being largely reworked and re-thought-out.
12 years ago
++PCBNew * Removed Pcb_Frame argument from BOARD() constructor, since it precludes having a BOARD being edited by more than one editor, it was a bad design. And this meant removing m_PcbFrame from BOARD. * removed BOARD::SetWindowFrame(), and BOARD::m_PcbFrame * Removed the global BOARD_DESIGN_SETTINGS which was in class_board.cpp * added BOARD_DESIGN_SETTINGS to the BOARD class, a full instance * a couple dialogs now only change BOARD_DESIGN_SETTINGS when OK is pressed, such as dialog_mask_clearance, dialog_drc, etc. * Removed common/pcbcommon.cpp's int g_CurrentVersionPCB = 1 and replaced it with build_version.h's #define BOARD_FILE_VERSION, although there may be a better place for this constant. * Made the public functions in PARAM_CFG_ARRAY be type const. void SaveParam(..) const and void ReadParam(..) const * PARAM_CFG_BASE now has virtual destructor since we have various way of destroying the derived class and boost::ptr_vector must be told about this. * Pass const PARAM_CFG_ARRAY& instead of PARAM_CFG_ARRAY so that we can use an automatic PARAM_CFG_ARRAY which is on the stack.\ * PCB_EDIT_FRAME::GetProjectFileParameters() may no longer cache the array, since it has to access the current BOARD and the BOARD can change. Remember BOARD_DESIGN_SETTINGS are now in the BOARD. * Made the m_BoundingBox member private, this was a brutally hard task, and indicative of the lack of commitment to accessors and object oriented design on the part of KiCad developers. We must do better. Added BOARD::GetBoundingBox, SetBoundingBox(), ComputeBoundingBox(). * Added PCB_BASE_FRAME::GetBoardBoundingBox() which calls BOARD::ComputeBoundingBox()
14 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2009 Jean-Pierre Charras, jean-pierre.charras@ujf-grenoble.fr
  5. * Copyright (C) 1992-2018 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file printout_controler.cpp
  26. * @brief Board print handler implementation file.
  27. */
  28. // Set this to 1 if you want to test PostScript printing under MSW.
  29. #define wxTEST_POSTSCRIPT_IN_MSW 1
  30. #include <fctsys.h>
  31. #include <pgm_base.h>
  32. #include <gr_basic.h>
  33. #include <class_drawpanel.h>
  34. #include <confirm.h>
  35. #include <base_units.h>
  36. #include <pcb_base_frame.h>
  37. #include <class_board.h>
  38. #include <pcbnew.h>
  39. #include <trace_helpers.h>
  40. #include <printout_controler.h>
  41. PRINT_PARAMETERS::PRINT_PARAMETERS()
  42. {
  43. m_PenDefaultSize = Millimeter2iu( 0.2 ); // A reasonable default value to draw items
  44. // which do not have a specified line width
  45. m_PrintScale = 1.0;
  46. m_XScaleAdjust = 1.0;
  47. m_YScaleAdjust = 1.0;
  48. m_Print_Sheet_Ref = false;
  49. m_PrintMaskLayer.set();
  50. m_PrintMirror = false;
  51. m_Print_Black_and_White = true;
  52. m_OptionPrintPage = 1;
  53. m_PageCount = 1;
  54. m_ForceCentered = false;
  55. m_Flags = 0;
  56. m_DrillShapeOpt = PRINT_PARAMETERS::SMALL_DRILL_SHAPE;
  57. m_PageSetupData = NULL;
  58. }
  59. BOARD_PRINTOUT_CONTROLLER::BOARD_PRINTOUT_CONTROLLER( const PRINT_PARAMETERS& aParams,
  60. EDA_DRAW_FRAME* aParent,
  61. const wxString& aTitle ) :
  62. wxPrintout( aTitle )
  63. {
  64. m_PrintParams = aParams; // Make a local copy of the print parameters.
  65. m_Parent = aParent;
  66. }
  67. bool BOARD_PRINTOUT_CONTROLLER::OnPrintPage( int aPage )
  68. {
  69. LSET lset = m_PrintParams.m_PrintMaskLayer;
  70. int pageCount = lset.count();
  71. wxString layer;
  72. PCB_LAYER_ID extractLayer;
  73. // compute layer mask from page number if we want one page per layer
  74. if( m_PrintParams.m_OptionPrintPage == 0 ) // One page per layer
  75. {
  76. // This sequence is TBD, call a different
  77. // sequencer if needed, such as Seq(). Could not find documentation on
  78. // page order.
  79. LSEQ seq = lset.UIOrder();
  80. // aPage starts at 1, not 0
  81. if( unsigned( aPage-1 ) < seq.size() )
  82. m_PrintParams.m_PrintMaskLayer = LSET( seq[aPage-1] );
  83. }
  84. if( !m_PrintParams.m_PrintMaskLayer.any() )
  85. return false;
  86. extractLayer = m_PrintParams.m_PrintMaskLayer.ExtractLayer();
  87. if( extractLayer == UNDEFINED_LAYER )
  88. layer = _( "Multiple Layers" );
  89. else
  90. layer = LSET::Name( extractLayer );
  91. // In Pcbnew we can want the layer EDGE always printed
  92. if( m_PrintParams.m_Flags == 1 )
  93. m_PrintParams.m_PrintMaskLayer.set( Edge_Cuts );
  94. DrawPage( layer, aPage, pageCount );
  95. m_PrintParams.m_PrintMaskLayer = lset;
  96. return true;
  97. }
  98. void BOARD_PRINTOUT_CONTROLLER::GetPageInfo( int* minPage, int* maxPage,
  99. int* selPageFrom, int* selPageTo )
  100. {
  101. *minPage = 1;
  102. *selPageFrom = 1;
  103. int icnt = 1;
  104. if( m_PrintParams.m_OptionPrintPage == 0 )
  105. icnt = m_PrintParams.m_PageCount;
  106. *maxPage = icnt;
  107. *selPageTo = icnt;
  108. }
  109. void BOARD_PRINTOUT_CONTROLLER::DrawPage( const wxString& aLayerName, int aPageNum, int aPageCount )
  110. {
  111. wxPoint offset;
  112. double userscale;
  113. EDA_RECT boardBoundingBox;
  114. EDA_RECT drawRect;
  115. wxDC* dc = GetDC();
  116. BASE_SCREEN* screen = m_Parent->GetScreen();
  117. bool printMirror = m_PrintParams.m_PrintMirror;
  118. wxSize pageSizeIU = m_Parent->GetPageSizeIU();
  119. int tempScreenNumber;
  120. int tempNumberOfScreens;
  121. wxBusyCursor dummy;
  122. BOARD* brd = ((PCB_BASE_FRAME*) m_Parent)->GetBoard();
  123. boardBoundingBox = brd->ComputeBoundingBox();
  124. const wxString& titleblockFilename = brd->GetFileName();
  125. // Use the page size as the drawing area when the board is shown or the user scale
  126. // is less than 1.
  127. if( m_PrintParams.PrintBorderAndTitleBlock() )
  128. boardBoundingBox = EDA_RECT( wxPoint( 0, 0 ), pageSizeIU );
  129. wxLogTrace( tracePrinting, wxT( "Drawing bounding box: x=%d, y=%d, w=%d, h=%d" ),
  130. boardBoundingBox.GetX(), boardBoundingBox.GetY(),
  131. boardBoundingBox.GetWidth(), boardBoundingBox.GetHeight() );
  132. // Compute the PCB size in internal units
  133. userscale = m_PrintParams.m_PrintScale;
  134. if( m_PrintParams.m_PrintScale == 0 ) // fit in page option
  135. {
  136. if( boardBoundingBox.GetWidth() && boardBoundingBox.GetHeight() )
  137. {
  138. int margin = Millimeter2iu( 10.0 ); // add a margin around the drawings
  139. double scaleX = (double)(pageSizeIU.x - (2 * margin)) /
  140. boardBoundingBox.GetWidth();
  141. double scaleY = (double)(pageSizeIU.y - (2 * margin)) /
  142. boardBoundingBox.GetHeight();
  143. userscale = (scaleX < scaleY) ? scaleX : scaleY;
  144. }
  145. else
  146. userscale = 1.0;
  147. }
  148. wxSize scaledPageSize = pageSizeIU;
  149. drawRect.SetSize( scaledPageSize );
  150. scaledPageSize.x = wxRound( scaledPageSize.x / userscale );
  151. scaledPageSize.y = wxRound( scaledPageSize.y / userscale );
  152. if( m_PrintParams.m_PageSetupData )
  153. {
  154. wxLogTrace( tracePrinting, wxT( "Fit size to page margins: x=%d, y=%d" ),
  155. scaledPageSize.x, scaledPageSize.y );
  156. // Always scale to the size of the paper.
  157. FitThisSizeToPageMargins( scaledPageSize, *m_PrintParams.m_PageSetupData );
  158. }
  159. // Compute Accurate scale 1
  160. if( m_PrintParams.m_PrintScale == 1.0 )
  161. {
  162. // We want a 1:1 scale, regardless the page setup
  163. // like page size, margin ...
  164. MapScreenSizeToPaper(); // set best scale and offset (scale is not used)
  165. int w, h;
  166. GetPPIPrinter( &w, &h );
  167. double accurate_Xscale = (double) w / (IU_PER_MILS*1000);
  168. double accurate_Yscale = (double) h / (IU_PER_MILS*1000);
  169. if( IsPreview() ) // Scale must take in account the DC size in Preview
  170. {
  171. // Get the size of the DC in pixels
  172. wxSize PlotAreaSize;
  173. dc->GetSize( &PlotAreaSize.x, &PlotAreaSize.y );
  174. GetPageSizePixels( &w, &h );
  175. accurate_Xscale *= (double)PlotAreaSize.x / w;
  176. accurate_Yscale *= (double)PlotAreaSize.y / h;
  177. }
  178. // Fine scale adjust
  179. accurate_Xscale *= m_PrintParams.m_XScaleAdjust;
  180. accurate_Yscale *= m_PrintParams.m_YScaleAdjust;
  181. // Set print scale for 1:1 exact scale
  182. dc->SetUserScale( accurate_Xscale, accurate_Yscale );
  183. }
  184. // Get the final size of the DC in pixels
  185. wxSize PlotAreaSizeInPixels;
  186. dc->GetSize( &PlotAreaSizeInPixels.x, &PlotAreaSizeInPixels.y );
  187. wxLogTrace( tracePrinting, wxT( "Plot area in pixels: x=%d, y=%d" ),
  188. PlotAreaSizeInPixels.x, PlotAreaSizeInPixels.y );
  189. double scalex, scaley;
  190. dc->GetUserScale( &scalex, &scaley );
  191. wxLogTrace( tracePrinting, wxT( "DC user scale: x=%g, y=%g" ),
  192. scalex, scaley );
  193. wxSize PlotAreaSizeInUserUnits;
  194. PlotAreaSizeInUserUnits.x = KiROUND( PlotAreaSizeInPixels.x / scalex );
  195. PlotAreaSizeInUserUnits.y = KiROUND( PlotAreaSizeInPixels.y / scaley );
  196. wxLogTrace( tracePrinting, wxT( "Scaled plot area in user units: x=%d, y=%d" ),
  197. PlotAreaSizeInUserUnits.x, PlotAreaSizeInUserUnits.y );
  198. // In module editor, the module is located at 0,0 but for printing
  199. // it is moved to pageSizeIU.x/2, pageSizeIU.y/2.
  200. // So the equivalent board must be moved to the center of the page:
  201. if( m_Parent->IsType( FRAME_PCB_MODULE_EDITOR ) )
  202. {
  203. boardBoundingBox.Move( wxPoint( pageSizeIU.x/2, pageSizeIU.y/2 ) );
  204. }
  205. // In some cases the plot origin is the centre of the board outline rather than the center
  206. // of the selected paper size.
  207. if( m_PrintParams.CenterOnBoardOutline() )
  208. {
  209. // Here we are only drawing the board and it's contents.
  210. drawRect = boardBoundingBox;
  211. offset.x += wxRound( (double) -scaledPageSize.x / 2.0 );
  212. offset.y += wxRound( (double) -scaledPageSize.y / 2.0 );
  213. wxPoint center = boardBoundingBox.Centre();
  214. if( printMirror )
  215. {
  216. // Calculate the mirrored center of the board.
  217. center.x = m_Parent->GetPageSizeIU().x - boardBoundingBox.Centre().x;
  218. }
  219. offset += center;
  220. }
  221. GRResetPenAndBrush( dc );
  222. EDA_DRAW_PANEL* panel = m_Parent->GetCanvas();
  223. EDA_RECT tmp = *panel->GetClipBox();
  224. // Set clip box to the max size
  225. #define MAX_VALUE (INT_MAX/2) // MAX_VALUE is the max we can use in an integer
  226. // and that allows calculations without overflow
  227. panel->SetClipBox( EDA_RECT( wxPoint( 0, 0 ), wxSize( MAX_VALUE, MAX_VALUE ) ) );
  228. screen->m_IsPrinting = true;
  229. COLOR4D bg_color = m_Parent->GetDrawBgColor();
  230. // Print frame reference, if requested, before
  231. if( m_PrintParams.m_Print_Black_and_White )
  232. GRForceBlackPen( true );
  233. if( m_PrintParams.PrintBorderAndTitleBlock() )
  234. {
  235. tempScreenNumber = screen->m_ScreenNumber;
  236. tempNumberOfScreens = screen->m_NumberOfScreens;
  237. screen->m_ScreenNumber = aPageNum;
  238. screen->m_NumberOfScreens = aPageCount;
  239. m_Parent->DrawWorkSheet( dc, screen, m_PrintParams.m_PenDefaultSize,
  240. IU_PER_MILS, titleblockFilename, aLayerName );
  241. screen->m_ScreenNumber = tempScreenNumber;
  242. screen->m_NumberOfScreens = tempNumberOfScreens;
  243. }
  244. if( printMirror )
  245. {
  246. // To plot mirror, we reverse the x axis, and modify the plot x origin
  247. dc->SetAxisOrientation( false, false);
  248. /* Plot offset x is moved by the x plot area size in order to have
  249. * the old draw area in the new draw area, because the draw origin has not moved
  250. * (this is the upper left corner) but the X axis is reversed, therefore the plotting area
  251. * is the x coordinate values from - PlotAreaSize.x to 0 */
  252. int x_dc_offset = PlotAreaSizeInPixels.x;
  253. x_dc_offset = KiROUND( x_dc_offset * userscale );
  254. dc->SetDeviceOrigin( x_dc_offset, 0 );
  255. wxLogTrace( tracePrinting, wxT( "Device origin: x=%d, y=%d" ),
  256. x_dc_offset, 0 );
  257. panel->SetClipBox( EDA_RECT( wxPoint( -MAX_VALUE/2, -MAX_VALUE/2 ),
  258. panel->GetClipBox()->GetSize() ) );
  259. }
  260. // screen->m_DrawOrg = offset;
  261. dc->SetLogicalOrigin( offset.x, offset.y );
  262. wxLogTrace( tracePrinting, wxT( "Logical origin: x=%d, y=%d" ),
  263. offset.x, offset.y );
  264. #if defined(wxUSE_LOG_TRACE) && defined( DEBUG )
  265. wxRect paperRect = GetPaperRectPixels();
  266. wxLogTrace( tracePrinting, wxT( "Paper rectangle: left=%d, top=%d, "
  267. "right=%d, bottom=%d" ),
  268. paperRect.GetLeft(), paperRect.GetTop(), paperRect.GetRight(),
  269. paperRect.GetBottom() );
  270. int devLeft = dc->LogicalToDeviceX( drawRect.GetX() );
  271. int devTop = dc->LogicalToDeviceY( drawRect.GetY() );
  272. int devRight = dc->LogicalToDeviceX( drawRect.GetRight() );
  273. int devBottom = dc->LogicalToDeviceY( drawRect.GetBottom() );
  274. wxLogTrace( tracePrinting, wxT( "Final device rectangle: left=%d, top=%d, "
  275. "right=%d, bottom=%d\n" ),
  276. devLeft, devTop, devRight, devBottom );
  277. #endif
  278. m_Parent->SetDrawBgColor( WHITE );
  279. /* when printing in color mode, we use the graphic OR mode that gives the same look as
  280. * the screen but because the background is white when printing, we must use a trick:
  281. * In order to plot on a white background in OR mode we must:
  282. * 1 - Plot all items in black, this creates a local black background
  283. * 2 - Plot in OR mode on black "local" background
  284. */
  285. if( !m_PrintParams.m_Print_Black_and_White )
  286. {
  287. // Creates a "local" black background
  288. GRForceBlackPen( true );
  289. m_Parent->PrintPage( dc, m_PrintParams.m_PrintMaskLayer,
  290. printMirror, &m_PrintParams );
  291. GRForceBlackPen( false );
  292. }
  293. else
  294. GRForceBlackPen( true );
  295. m_Parent->PrintPage( dc, m_PrintParams.m_PrintMaskLayer, printMirror,
  296. &m_PrintParams );
  297. m_Parent->SetDrawBgColor( bg_color );
  298. screen->m_IsPrinting = false;
  299. panel->SetClipBox( tmp );
  300. GRForceBlackPen( false );
  301. }