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.

450 lines
12 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
15 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
17 years ago
18 years ago
18 years ago
// Dick Hollenbeck's KiROUND R&D // This provides better project control over rounding to int from double // than wxRound() did. This scheme provides better logging in Debug builds // and it provides for compile time calculation of constants. #include <stdio.h> #include <assert.h> #include <limits.h> //-----<KiROUND KIT>------------------------------------------------------------ /** * KiROUND * rounds a floating point number to an int using * "round halfway cases away from zero". * In Debug build an assert fires if will not fit into an int. */ #if defined( DEBUG ) // DEBUG: a macro to capture line and file, then calls this inline static inline int KiRound( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v ); } else if( v < INT_MIN - 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v ); } return int( v ); } #define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) #else // RELEASE: a macro so compile can pre-compute constants. #define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) #endif //-----</KiROUND KIT>----------------------------------------------------------- // Only a macro is compile time calculated, an inline function causes a static constructor // in a situation like this. // Therefore the Release build is best done with a MACRO not an inline function. int Computed = KiROUND( 14.3 * 8 ); int main( int argc, char** argv ) { for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 ) { int i = KiROUND( d ); printf( "t: %d %.16g\n", i, d ); } return 0; }
14 years ago
18 years ago
18 years ago
  1. /**
  2. * @file 3d_frame.cpp
  3. */
  4. /*
  5. * This program source code file is part of KiCad, a free EDA CAD application.
  6. *
  7. * Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, you may find one here:
  21. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  22. * or you may search the http://www.gnu.org website for the version 2 license,
  23. * or you may write to the Free Software Foundation, Inc.,
  24. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  25. */
  26. #include <fctsys.h>
  27. #include <appl_wxstruct.h>
  28. #include <3d_viewer.h>
  29. #include <info3d_visu.h>
  30. #include <trackball.h>
  31. #include <wx/colordlg.h>
  32. #include <wxstruct.h>
  33. #include <3d_viewer_id.h>
  34. INFO3D_VISU g_Parm_3D_Visu;
  35. double DataScale3D; // 3D conversion units.
  36. BEGIN_EVENT_TABLE( EDA_3D_FRAME, wxFrame )
  37. EVT_ACTIVATE( EDA_3D_FRAME::OnActivate )
  38. EVT_TOOL_RANGE( ID_ZOOM_IN, ID_ZOOM_PAGE, EDA_3D_FRAME::Process_Zoom )
  39. EVT_TOOL_RANGE( ID_START_COMMAND_3D, ID_END_COMMAND_3D,
  40. EDA_3D_FRAME::Process_Special_Functions )
  41. EVT_MENU( wxID_EXIT, EDA_3D_FRAME::Exit3DFrame )
  42. EVT_MENU( ID_MENU_SCREENCOPY_PNG, EDA_3D_FRAME::Process_Special_Functions )
  43. EVT_MENU( ID_MENU_SCREENCOPY_JPEG, EDA_3D_FRAME::Process_Special_Functions )
  44. EVT_MENU_RANGE( ID_MENU3D_GRID, ID_MENU3D_GRID_END,
  45. EDA_3D_FRAME::On3DGridSelection )
  46. EVT_CLOSE( EDA_3D_FRAME::OnCloseWindow )
  47. END_EVENT_TABLE()
  48. EDA_3D_FRAME::EDA_3D_FRAME( PCB_BASE_FRAME* parent, const wxString& title, long style ) :
  49. wxFrame( parent, DISPLAY3D_FRAME, title, wxPoint( -1, -1 ), wxSize( -1, -1 ), style )
  50. {
  51. m_FrameName = wxT( "Frame3D" );
  52. m_Canvas = NULL;
  53. m_HToolBar = NULL;
  54. m_VToolBar = NULL;
  55. m_reloadRequest = false;
  56. // Give it an icon
  57. wxIcon icon;
  58. icon.CopyFromBitmap( KiBitmap( icon_3d_xpm ) );
  59. SetIcon( icon );
  60. GetSettings();
  61. SetSize( m_FramePos.x, m_FramePos.y, m_FrameSize.x, m_FrameSize.y );
  62. // Create the status line
  63. static const int dims[5] = { -1, 100, 100, 100, 140 };
  64. CreateStatusBar( 5 );
  65. SetStatusWidths( 5, dims );
  66. ReCreateMenuBar();
  67. ReCreateHToolbar();
  68. // ReCreateAuxiliaryToolbar();
  69. ReCreateVToolbar();
  70. // Make a EDA_3D_CANVAS
  71. int attrs[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 16, 0 };
  72. m_Canvas = new EDA_3D_CANVAS( this, attrs );
  73. m_auimgr.SetManagedWindow( this );
  74. EDA_PANEINFO horiz;
  75. horiz.HorizontalToolbarPane();
  76. m_auimgr.AddPane( m_HToolBar,
  77. wxAuiPaneInfo( horiz ).Name( wxT( "m_HToolBar" ) ).Top() );
  78. m_auimgr.AddPane( m_Canvas,
  79. wxAuiPaneInfo().Name( wxT( "DrawFrame" ) ).CentrePane() );
  80. m_auimgr.Update();
  81. // Fixes bug in Windows (XP and possibly others) where the canvas requires the focus
  82. // in order to receive mouse events. Otherwise, the user has to click somewhere on
  83. // the canvas before it will respond to mouse wheel events.
  84. m_Canvas->SetFocus();
  85. }
  86. void EDA_3D_FRAME::Exit3DFrame( wxCommandEvent& event )
  87. {
  88. Close( true );
  89. }
  90. void EDA_3D_FRAME::OnCloseWindow( wxCloseEvent& Event )
  91. {
  92. SaveSettings();
  93. if( Parent() )
  94. {
  95. Parent()->m_Draw3DFrame = NULL;
  96. }
  97. Destroy();
  98. }
  99. void EDA_3D_FRAME::GetSettings()
  100. {
  101. wxString text;
  102. wxConfig* config = wxGetApp().GetSettings(); // Current config used by application
  103. if( config )
  104. {
  105. text = m_FrameName + wxT( "Pos_x" );
  106. config->Read( text, &m_FramePos.x );
  107. text = m_FrameName + wxT( "Pos_y" );
  108. config->Read( text, &m_FramePos.y );
  109. text = m_FrameName + wxT( "Size_x" );
  110. config->Read( text, &m_FrameSize.x, 600 );
  111. text = m_FrameName + wxT( "Size_y" );
  112. config->Read( text, &m_FrameSize.y, 400 );
  113. config->Read( wxT( "BgColor_Red" ), &g_Parm_3D_Visu.m_BgColor.m_Red, 0.0 );
  114. config->Read( wxT( "BgColor_Green" ), &g_Parm_3D_Visu.m_BgColor.m_Green, 0.0 );
  115. config->Read( wxT( "BgColor_Blue" ), &g_Parm_3D_Visu.m_BgColor.m_Blue, 0.0 );
  116. }
  117. #if defined( __WXMAC__ )
  118. // for macOSX, the window must be below system (macOSX) toolbar
  119. if( m_FramePos.y < 20 )
  120. m_FramePos.y = 20;
  121. #endif
  122. }
  123. void EDA_3D_FRAME::SaveSettings()
  124. {
  125. wxString text;
  126. wxConfig* Config = wxGetApp().GetSettings(); // Current config used by application
  127. if( !Config )
  128. return;
  129. Config->Write( wxT( "BgColor_Red" ), g_Parm_3D_Visu.m_BgColor.m_Red );
  130. Config->Write( wxT( "BgColor_Green" ), g_Parm_3D_Visu.m_BgColor.m_Green );
  131. Config->Write( wxT( "BgColor_Blue" ), g_Parm_3D_Visu.m_BgColor.m_Blue );
  132. if( IsIconized() )
  133. return;
  134. m_FrameSize = GetSize();
  135. m_FramePos = GetPosition();
  136. text = m_FrameName + wxT( "Pos_x" );
  137. Config->Write( text, (long) m_FramePos.x );
  138. text = m_FrameName + wxT( "Pos_y" );
  139. Config->Write( text, (long) m_FramePos.y );
  140. text = m_FrameName + wxT( "Size_x" );
  141. Config->Write( text, (long) m_FrameSize.x );
  142. text = m_FrameName + wxT( "Size_y" );
  143. Config->Write( text, (long) m_FrameSize.y );
  144. }
  145. void EDA_3D_FRAME::Process_Zoom( wxCommandEvent& event )
  146. {
  147. int ii;
  148. switch( event.GetId() )
  149. {
  150. case ID_ZOOM_PAGE:
  151. for( ii = 0; ii < 4; ii++ )
  152. g_Parm_3D_Visu.m_Rot[ii] = 0.0;
  153. g_Parm_3D_Visu.m_Zoom = 1.0;
  154. m_Canvas->SetOffset(0.0, 0.0);
  155. trackball( g_Parm_3D_Visu.m_Quat, 0.0, 0.0, 0.0, 0.0 );
  156. break;
  157. case ID_ZOOM_IN:
  158. g_Parm_3D_Visu.m_Zoom /= 1.2;
  159. if( g_Parm_3D_Visu.m_Zoom <= 0.01 )
  160. g_Parm_3D_Visu.m_Zoom = 0.01;
  161. break;
  162. case ID_ZOOM_OUT:
  163. g_Parm_3D_Visu.m_Zoom *= 1.2;
  164. break;
  165. case ID_ZOOM_REDRAW:
  166. break;
  167. default:
  168. return;
  169. }
  170. m_Canvas->Refresh( false );
  171. m_Canvas->DisplayStatus();
  172. }
  173. void EDA_3D_FRAME::OnLeftClick( wxDC* DC, const wxPoint& MousePos )
  174. {
  175. }
  176. void EDA_3D_FRAME::OnRightClick( const wxPoint& MousePos, wxMenu* PopMenu )
  177. {
  178. }
  179. double EDA_3D_FRAME::BestZoom()
  180. {
  181. return 1.0;
  182. }
  183. void EDA_3D_FRAME::RedrawActiveWindow( wxDC* DC, bool EraseBg )
  184. {
  185. }
  186. void EDA_3D_FRAME::Process_Special_Functions( wxCommandEvent& event )
  187. {
  188. #define ROT_ANGLE 10.0
  189. int id = event.GetId();
  190. bool isChecked = event.IsChecked();
  191. switch( id )
  192. {
  193. case ID_RELOAD3D_BOARD:
  194. NewDisplay();
  195. return;
  196. break;
  197. case ID_ROTATE3D_X_POS:
  198. g_Parm_3D_Visu.m_ROTX += ROT_ANGLE;
  199. break;
  200. case ID_ROTATE3D_X_NEG:
  201. g_Parm_3D_Visu.m_ROTX -= ROT_ANGLE;
  202. break;
  203. case ID_ROTATE3D_Y_POS:
  204. g_Parm_3D_Visu.m_ROTY += ROT_ANGLE;
  205. break;
  206. case ID_ROTATE3D_Y_NEG:
  207. g_Parm_3D_Visu.m_ROTY -= ROT_ANGLE;
  208. break;
  209. case ID_ROTATE3D_Z_POS:
  210. g_Parm_3D_Visu.m_ROTZ += ROT_ANGLE;
  211. break;
  212. case ID_ROTATE3D_Z_NEG:
  213. g_Parm_3D_Visu.m_ROTZ -= ROT_ANGLE;
  214. break;
  215. case ID_MOVE3D_LEFT:
  216. m_Canvas->SetView3D( WXK_LEFT );
  217. return;
  218. case ID_MOVE3D_RIGHT:
  219. m_Canvas->SetView3D( WXK_RIGHT );
  220. return;
  221. case ID_MOVE3D_UP:
  222. m_Canvas->SetView3D( WXK_UP );
  223. return;
  224. case ID_MOVE3D_DOWN:
  225. m_Canvas->SetView3D( WXK_DOWN );
  226. return;
  227. case ID_ORTHO:
  228. m_Canvas->ToggleOrtho();
  229. return;
  230. case ID_TOOL_SCREENCOPY_TOCLIBBOARD:
  231. case ID_MENU_SCREENCOPY_PNG:
  232. case ID_MENU_SCREENCOPY_JPEG:
  233. m_Canvas->TakeScreenshot( event );
  234. break;
  235. case ID_MENU3D_BGCOLOR_SELECTION:
  236. Set3DBgColor();
  237. return;
  238. case ID_MENU3D_AXIS_ONOFF:
  239. g_Parm_3D_Visu.m_DrawFlags[g_Parm_3D_Visu.FL_AXIS] = isChecked;
  240. NewDisplay();
  241. return;
  242. case ID_MENU3D_MODULE_ONOFF:
  243. g_Parm_3D_Visu.m_DrawFlags[g_Parm_3D_Visu.FL_MODULE] = isChecked;
  244. NewDisplay();
  245. return;
  246. case ID_MENU3D_ZONE_ONOFF:
  247. g_Parm_3D_Visu.m_DrawFlags[g_Parm_3D_Visu.FL_ZONE] = isChecked;
  248. NewDisplay();
  249. return;
  250. case ID_MENU3D_COMMENTS_ONOFF:
  251. g_Parm_3D_Visu.m_DrawFlags[g_Parm_3D_Visu.FL_COMMENTS] = isChecked;
  252. NewDisplay();
  253. return;
  254. case ID_MENU3D_DRAWINGS_ONOFF:
  255. g_Parm_3D_Visu.m_DrawFlags[g_Parm_3D_Visu.FL_DRAWINGS] = isChecked;
  256. NewDisplay();
  257. return;
  258. case ID_MENU3D_ECO1_ONOFF:
  259. g_Parm_3D_Visu.m_DrawFlags[g_Parm_3D_Visu.FL_ECO1] = isChecked;
  260. NewDisplay();
  261. return;
  262. case ID_MENU3D_ECO2_ONOFF:
  263. g_Parm_3D_Visu.m_DrawFlags[g_Parm_3D_Visu.FL_ECO2] = isChecked;
  264. NewDisplay();
  265. return;
  266. default:
  267. wxLogMessage( wxT( "EDA_3D_FRAME::Process_Special_Functions() error: unknown command" ) );
  268. return;
  269. }
  270. m_Canvas->Refresh( true );
  271. m_Canvas->DisplayStatus();
  272. }
  273. void EDA_3D_FRAME::On3DGridSelection( wxCommandEvent& event )
  274. {
  275. int id = event.GetId();
  276. for( int ii = ID_MENU3D_GRID; ii < ID_MENU3D_GRID_END; ii++ )
  277. {
  278. if( event.GetId() == ii )
  279. continue;
  280. GetMenuBar()->Check( ii, false );
  281. }
  282. switch( id )
  283. {
  284. case ID_MENU3D_GRID_NOGRID:
  285. g_Parm_3D_Visu.m_DrawFlags[g_Parm_3D_Visu.FL_GRID] = false;
  286. break;
  287. case ID_MENU3D_GRID_10_MM:
  288. g_Parm_3D_Visu.m_DrawFlags[g_Parm_3D_Visu.FL_GRID] = true;
  289. g_Parm_3D_Visu.m_3D_Grid = 10.0;
  290. break;
  291. case ID_MENU3D_GRID_5_MM:
  292. g_Parm_3D_Visu.m_DrawFlags[g_Parm_3D_Visu.FL_GRID] = true;
  293. g_Parm_3D_Visu.m_3D_Grid = 5.0;
  294. break;
  295. case ID_MENU3D_GRID_2P5_MM:
  296. g_Parm_3D_Visu.m_DrawFlags[g_Parm_3D_Visu.FL_GRID] = true;
  297. g_Parm_3D_Visu.m_3D_Grid = 2.5;
  298. break;
  299. case ID_MENU3D_GRID_1_MM:
  300. g_Parm_3D_Visu.m_DrawFlags[g_Parm_3D_Visu.FL_GRID] = true;
  301. g_Parm_3D_Visu.m_3D_Grid = 1.0;
  302. break;
  303. default:
  304. wxLogMessage( wxT( "EDA_3D_FRAME::On3DGridSelection() error: unknown command" ) );
  305. return;
  306. }
  307. NewDisplay();
  308. }
  309. void EDA_3D_FRAME::NewDisplay()
  310. {
  311. m_reloadRequest = false;
  312. m_Canvas->ClearLists();
  313. m_Canvas->CreateDrawGL_List();
  314. // m_Canvas->InitGL();
  315. m_Canvas->Refresh( true );
  316. m_Canvas->DisplayStatus();
  317. }
  318. void EDA_3D_FRAME::OnActivate( wxActivateEvent& event )
  319. {
  320. // Reload data if 3D frame shows a footprint,
  321. // because it can be changed since last frame activation
  322. if( m_reloadRequest )
  323. NewDisplay();
  324. event.Skip(); // required under wxMAC
  325. }
  326. /* called to set the background color of the 3D scene
  327. */
  328. void EDA_3D_FRAME::Set3DBgColor()
  329. {
  330. S3D_COLOR color;
  331. wxColour newcolor, oldcolor;
  332. oldcolor.Set( KiROUND( g_Parm_3D_Visu.m_BgColor.m_Red * 255 ),
  333. KiROUND( g_Parm_3D_Visu.m_BgColor.m_Green * 255 ),
  334. KiROUND( g_Parm_3D_Visu.m_BgColor.m_Blue * 255 ) );
  335. newcolor = wxGetColourFromUser( this, oldcolor );
  336. if( newcolor != oldcolor )
  337. {
  338. g_Parm_3D_Visu.m_BgColor.m_Red = (double) newcolor.Red() / 255.0;
  339. g_Parm_3D_Visu.m_BgColor.m_Green = (double) newcolor.Green() / 255.0;
  340. g_Parm_3D_Visu.m_BgColor.m_Blue = (double) newcolor.Blue() / 255.0;
  341. NewDisplay();
  342. }
  343. }