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.

1128 lines
33 KiB

14 years ago
14 years ago
14 years ago
15 years ago
15 years ago
15 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
15 years ago
15 years ago
15 years ago
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, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 2011 Wayne Stambaugh <stambaughw@verizon.net>
  6. * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, you may find one here:
  20. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  21. * or you may search the http://www.gnu.org website for the version 2 license,
  22. * or you may write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  24. */
  25. /**
  26. * @file muonde.cpp
  27. * @brief Microwave pcb layout code.
  28. */
  29. #include <fctsys.h>
  30. #include <class_drawpanel.h>
  31. #include <confirm.h>
  32. #include <trigo.h>
  33. #include <kicad_string.h>
  34. #include <gestfich.h>
  35. #include <wxPcbStruct.h>
  36. #include <dialog_helpers.h>
  37. #include <richio.h>
  38. #include <filter_reader.h>
  39. #include <gr_basic.h>
  40. #include <pcbcommon.h>
  41. #include <macros.h>
  42. #include <base_units.h>
  43. #include <class_board.h>
  44. #include <class_module.h>
  45. #include <class_edge_mod.h>
  46. #include <protos.h>
  47. #include <pcbnew.h>
  48. #define COEFF_COUNT 6
  49. static std::vector< double > PolyEdges;
  50. static double ShapeScaleX, ShapeScaleY;
  51. static wxSize ShapeSize;
  52. static int PolyShapeType;
  53. static void Exit_Self( EDA_DRAW_PANEL* Panel, wxDC* DC );
  54. static void gen_arc( std::vector <wxPoint>& aBuffer,
  55. wxPoint aStartPoint,
  56. wxPoint aCenter,
  57. int a_ArcAngle );
  58. static void ShowBoundingBoxMicroWaveInductor( EDA_DRAW_PANEL* apanel,
  59. wxDC* aDC,
  60. const wxPoint& aPosition,
  61. bool aErase );
  62. int BuildCornersList_S_Shape( std::vector <wxPoint>& aBuffer,
  63. wxPoint aStartPoint, wxPoint aEndPoint,
  64. int aLength, int aWidth );
  65. class SELFPCB
  66. {
  67. public:
  68. int forme; // Shape: coil, spiral, etc ..
  69. wxPoint m_Start;
  70. wxPoint m_End;
  71. wxSize m_Size;
  72. int lng; // Trace length.
  73. int m_Width; // Trace width.
  74. };
  75. static SELFPCB Mself;
  76. static int Self_On;
  77. /* This function shows on screen the bounding box of the inductor that will be
  78. * created at the end of the build inductor process
  79. */
  80. static void ShowBoundingBoxMicroWaveInductor( EDA_DRAW_PANEL* aPanel, wxDC* aDC,
  81. const wxPoint& aPosition, bool aErase )
  82. {
  83. /* Calculate the orientation and size of the box containing the inductor:
  84. * the box is a rectangle with height = lenght/2
  85. * the shape is defined by a rectangle, nor necessary horizontal or vertical
  86. */
  87. GRSetDrawMode( aDC, GR_XOR );
  88. wxPoint poly[5];
  89. wxPoint pt = Mself.m_End - Mself.m_Start;
  90. int angle = -wxRound( atan2( (double) pt.y, (double) pt.x ) * 1800.0 / M_PI );
  91. int len = wxRound( sqrt( (double) pt.x * pt.x + (double) pt.y * pt.y ) );
  92. // calculate corners
  93. pt.x = 0; pt.y = len / 4;
  94. RotatePoint( &pt, angle );
  95. poly[0] = Mself.m_Start + pt;
  96. poly[1] = Mself.m_End + pt;
  97. pt.x = 0; pt.y = -len / 4;
  98. RotatePoint( &pt, angle );
  99. poly[2] = Mself.m_End + pt;
  100. poly[3] = Mself.m_Start + pt;
  101. poly[4] = poly[0];
  102. if( aErase )
  103. {
  104. GRPoly( aPanel->GetClipBox(), aDC, 5, poly, false, 0, YELLOW, YELLOW );
  105. }
  106. Mself.m_End = aPanel->GetScreen()->GetCrossHairPosition();
  107. pt = Mself.m_End - Mself.m_Start;
  108. angle = -wxRound( atan2( (double) pt.y, (double) pt.x ) * 1800.0 / M_PI );
  109. len = wxRound( sqrt( (double) pt.x * pt.x + (double) pt.y * pt.y ) );
  110. // calculate new corners
  111. pt.x = 0; pt.y = len / 4;
  112. RotatePoint( &pt, angle );
  113. poly[0] = Mself.m_Start + pt;
  114. poly[1] = Mself.m_End + pt;
  115. pt.x = 0; pt.y = -len / 4;
  116. RotatePoint( &pt, angle );
  117. poly[2] = Mself.m_End + pt;
  118. poly[3] = Mself.m_Start + pt;
  119. poly[4] = poly[0];
  120. GRPoly( aPanel->GetClipBox(), aDC, 5, poly, false, 0, YELLOW, YELLOW );
  121. }
  122. void Exit_Self( EDA_DRAW_PANEL* Panel, wxDC* DC )
  123. {
  124. if( Self_On )
  125. {
  126. Self_On = 0;
  127. Panel->CallMouseCapture( DC, wxDefaultPosition, 0 );
  128. }
  129. }
  130. void PCB_EDIT_FRAME::Begin_Self( wxDC* DC )
  131. {
  132. if( Self_On )
  133. {
  134. Genere_Self( DC );
  135. return;
  136. }
  137. Mself.m_Start = GetScreen()->GetCrossHairPosition();
  138. Mself.m_End = Mself.m_Start;
  139. Self_On = 1;
  140. // Update the initial coordinates.
  141. GetScreen()->m_O_Curseur = GetScreen()->GetCrossHairPosition();
  142. UpdateStatusBar();
  143. m_canvas->SetMouseCapture( ShowBoundingBoxMicroWaveInductor, Exit_Self );
  144. m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
  145. }
  146. MODULE* PCB_EDIT_FRAME::Genere_Self( wxDC* DC )
  147. {
  148. D_PAD* pad;
  149. int ll;
  150. wxString msg;
  151. m_canvas->CallMouseCapture( DC, wxDefaultPosition, false );
  152. m_canvas->SetMouseCapture( NULL, NULL );
  153. if( Self_On == 0 )
  154. {
  155. DisplayError( this, wxT( "Starting point not init.." ) );
  156. return NULL;
  157. }
  158. Self_On = 0;
  159. Mself.m_End = GetScreen()->GetCrossHairPosition();
  160. wxPoint pt = Mself.m_End - Mself.m_Start;
  161. int min_len = wxRound( sqrt( (double) pt.x * pt.x + (double) pt.y * pt.y ) );
  162. Mself.lng = min_len;
  163. // Enter the desired length.
  164. msg = ReturnStringFromValue( g_UserUnit, Mself.lng );
  165. wxTextEntryDialog dlg( this, _( "Length:" ), _( "Length" ), msg );
  166. if( dlg.ShowModal() != wxID_OK )
  167. return NULL; // canceled by user
  168. msg = dlg.GetValue();
  169. Mself.lng = ReturnValueFromString( g_UserUnit, msg, GetScreen()->GetInternalUnits() );
  170. // Control values (ii = minimum length)
  171. if( Mself.lng < min_len )
  172. {
  173. DisplayError( this, _( "Requested length < minimum length" ) );
  174. return NULL;
  175. }
  176. // Calculate the elements.
  177. Mself.m_Width = GetBoard()->GetCurrentTrackWidth();
  178. std::vector <wxPoint> buffer;
  179. ll = BuildCornersList_S_Shape( buffer, Mself.m_Start, Mself.m_End, Mself.lng, Mself.m_Width );
  180. if( !ll )
  181. {
  182. DisplayError( this, _( "Requested length too large" ) );
  183. return NULL;
  184. }
  185. // Generate module.
  186. MODULE* module;
  187. module = Create_1_Module( wxEmptyString );
  188. if( module == NULL )
  189. return NULL;
  190. // here the module is already in the BOARD, Create_1_Module() does that.
  191. module->m_LibRef = wxT( "MuSelf" );
  192. module->m_Attributs = MOD_VIRTUAL | MOD_CMS;
  193. module->ClearFlags();
  194. module->m_Pos = Mself.m_End;
  195. // Generate segments
  196. for( unsigned jj = 1; jj < buffer.size(); jj++ )
  197. {
  198. EDGE_MODULE* PtSegm;
  199. PtSegm = new EDGE_MODULE( module );
  200. PtSegm->SetStart( buffer[jj - 1] );
  201. PtSegm->SetEnd( buffer[jj] );
  202. PtSegm->SetWidth( Mself.m_Width );
  203. PtSegm->SetLayer( module->GetLayer() );
  204. PtSegm->SetShape( S_SEGMENT );
  205. PtSegm->SetStart0( PtSegm->GetStart() - module->GetPosition() );
  206. PtSegm->SetEnd0( PtSegm->GetEnd() - module->GetPosition() );
  207. module->m_Drawings.PushBack( PtSegm );
  208. }
  209. // Place a pad on each end of coil.
  210. pad = new D_PAD( module );
  211. module->m_Pads.PushFront( pad );
  212. pad->SetPadName( wxT( "1" ) );
  213. pad->SetPosition( Mself.m_End );
  214. pad->SetPos0( pad->GetPosition() - module->GetPosition() );
  215. pad->SetSize( wxSize( Mself.m_Width, Mself.m_Width ) );
  216. pad->SetLayerMask( GetLayerMask( module->GetLayer() ) );
  217. pad->SetAttribute( PAD_SMD );
  218. pad->SetShape( PAD_CIRCLE );
  219. D_PAD* newpad = new D_PAD( *pad );
  220. module->m_Pads.Insert( newpad, pad->Next() );
  221. pad = newpad;
  222. pad->SetPadName( wxT( "2" ) );
  223. pad->SetPosition( Mself.m_Start );
  224. pad->SetPos0( pad->GetPosition() - module->GetPosition() );
  225. // Modify text positions.
  226. module->DisplayInfo( this );
  227. module->m_Value->m_Pos.x = module->m_Reference->m_Pos.x =
  228. ( Mself.m_Start.x + Mself.m_End.x ) / 2;
  229. module->m_Value->m_Pos.y = module->m_Reference->m_Pos.y =
  230. ( Mself.m_Start.y + Mself.m_End.y ) / 2;
  231. module->m_Reference->m_Pos.y -= module->m_Reference->m_Size.y;
  232. module->m_Value->m_Pos.y += module->m_Value->m_Size.y;
  233. module->m_Reference->SetPos0( module->m_Reference->m_Pos - module->m_Pos );
  234. module->m_Value->SetPos0( module->m_Value->m_Pos - module->m_Pos );
  235. module->CalculateBoundingBox();
  236. module->Draw( m_canvas, DC, GR_OR );
  237. return module;
  238. }
  239. /**
  240. * Function gen_arc
  241. * generates an arc using arc approximation by lines:
  242. * Center aCenter
  243. * Angle "angle" (in 0.1 deg)
  244. * @param aBuffer = a buffer to store points.
  245. * @param aStartPoint = starting point of arc.
  246. * @param aCenter = arc centre.
  247. * @param a_ArcAngle = arc length in 0.1 degrees.
  248. */
  249. static void gen_arc( std::vector <wxPoint>& aBuffer,
  250. wxPoint aStartPoint,
  251. wxPoint aCenter,
  252. int a_ArcAngle )
  253. {
  254. #define SEGM_COUNT_PER_360DEG 16
  255. wxPoint first_point = aStartPoint - aCenter;
  256. int seg_count = ( ( abs( a_ArcAngle ) ) * SEGM_COUNT_PER_360DEG ) / 3600;
  257. if( seg_count == 0 )
  258. seg_count = 1;
  259. double increment_angle = (double) a_ArcAngle * 3.14159 / 1800 / seg_count;
  260. // Creates nb_seg point to approximate arc by segments:
  261. for( int ii = 1; ii <= seg_count; ii++ )
  262. {
  263. double rot_angle = increment_angle * ii;
  264. double fcos = cos( rot_angle );
  265. double fsin = sin( rot_angle );
  266. wxPoint currpt;
  267. // Rotate current point:
  268. currpt.x = wxRound( ( first_point.x * fcos + first_point.y * fsin ) );
  269. currpt.y = wxRound( ( first_point.y * fcos - first_point.x * fsin ) );
  270. wxPoint corner = aCenter + currpt;
  271. aBuffer.push_back( corner );
  272. }
  273. }
  274. /**
  275. * Function BuildCornersList_S_Shape
  276. * Create a path like a S-shaped coil
  277. * @param aBuffer = a buffer where to store points (ends of segments)
  278. * @param aStartPoint = starting point of the path
  279. * @param aEndPoint = ending point of the path
  280. * @param aLength = full lenght of the path
  281. * @param aWidth = segment width
  282. */
  283. int BuildCornersList_S_Shape( std::vector <wxPoint>& aBuffer,
  284. wxPoint aStartPoint, wxPoint aEndPoint,
  285. int aLength, int aWidth )
  286. {
  287. /* We must determine:
  288. * segm_count = number of segments perpendicular to the direction
  289. * segm_len = length of a strand
  290. * radius = radius of rounded parts of the coil
  291. * stubs_len = length of the 2 stubs( segments parallel to the direction)
  292. * connecting the start point to the start point of the S shape
  293. * and the ending point to the end point of the S shape
  294. * The equations are (assuming the area size of the entire shape is Size:
  295. * Size.x = 2 * radius + segm_len
  296. * Size.y = (segm_count + 2 ) * 2 * radius + 2 * stubs_len
  297. * Mself.lng = 2 * delta // connections to the coil
  298. * + (segm_count-2) * segm_len // length of the strands except 1st and last
  299. * + (segm_count) * (PI * radius) // length of rounded
  300. * segm_len + / 2 - radius * 2) // length of 1st and last bit
  301. *
  302. * The constraints are:
  303. * segm_count >= 2
  304. * radius < m_Size.x
  305. * Size.y = (radius * 4) + (2 * stubs_len)
  306. * segm_len > radius * 2
  307. *
  308. * The calculation is conducted in the following way:
  309. * first:
  310. * segm_count = 2
  311. * radius = 4 * Size.x (arbitrarily fixed value)
  312. * Then:
  313. * Increasing the number of segments to the desired length
  314. * (radius decreases if necessary)
  315. */
  316. wxSize size;
  317. // This scale factor adjusts the arc length to handle
  318. // the arc to segment approximation.
  319. // because we use SEGM_COUNT_PER_360DEG segment to approximate a circle,
  320. // the trace len must be corrected when calculated using arcs
  321. // this factor adjust calculations and must be changed if SEGM_COUNT_PER_360DEG is modified
  322. // because trace using segment is shorter the corresponding arc
  323. // ADJUST_SIZE is the ratio between tline len and the arc len for an arc
  324. // of 360/ADJUST_SIZE angle
  325. #define ADJUST_SIZE 0.988
  326. wxPoint pt = aEndPoint - aStartPoint;
  327. int angle = -wxRound( atan2( (double) pt.y, (double) pt.x ) * 1800.0 / M_PI );
  328. int min_len = wxRound( sqrt( (double) pt.x * pt.x + (double) pt.y * pt.y ) );
  329. int segm_len = 0; // length of segments
  330. int full_len; // full len of shape (sum of lenght of all segments + arcs)
  331. /* Note: calculations are made for a vertical coil (more easy calculations)
  332. * and after points are rotated to their actual position
  333. * So the main direction is the Y axis.
  334. * the 2 stubs are on the Y axis
  335. * the others segments are parallel to the X axis.
  336. */
  337. // Calculate the size of area (for a vertical shape)
  338. size.x = min_len / 2;
  339. size.y = min_len;
  340. // Choose a reasonable starting value for the radius of the arcs.
  341. int radius = MIN( aWidth * 5, size.x / 4 );
  342. int segm_count; // number of full len segments
  343. // the half size segments (first and last segment) are not counted here
  344. int stubs_len = 0; // lenght of first or last segment (half size of others segments)
  345. for( segm_count = 0; ; segm_count++ )
  346. {
  347. stubs_len = ( size.y - ( radius * 2 * (segm_count + 2 ) ) ) / 2;
  348. if( stubs_len < size.y / 10 ) // Reduce radius.
  349. {
  350. stubs_len = size.y / 10;
  351. radius = ( size.y - (2 * stubs_len) ) / ( 2 * (segm_count + 2) );
  352. if( radius < aWidth ) // Radius too small.
  353. {
  354. // Unable to create line: Requested length value is too large for room
  355. return 0;
  356. }
  357. }
  358. segm_len = size.x - ( radius * 2 );
  359. full_len = 2 * stubs_len; // Length of coil connections.
  360. full_len += segm_len * segm_count; // Length of full length segments.
  361. full_len += wxRound( ( segm_count + 2 ) * M_PI * ADJUST_SIZE * radius ); // Ard arcs len
  362. full_len += segm_len - (2 * radius); // Length of first and last segments
  363. // (half size segments len = segm_len/2 - radius).
  364. if( full_len >= aLength )
  365. break;
  366. }
  367. // Adjust len by adjusting segm_len:
  368. int delta_size = full_len - aLength;
  369. // reduce len of the segm_count segments + 2 half size segments (= 1 full size segment)
  370. segm_len -= delta_size / (segm_count + 1);
  371. // Generate first line (the first stub) and first arc (90 deg arc)
  372. pt = aStartPoint;
  373. aBuffer.push_back( pt );
  374. pt.y += stubs_len;
  375. aBuffer.push_back( pt );
  376. wxPoint centre = pt;
  377. centre.x -= radius;
  378. gen_arc( aBuffer, pt, centre, -900 );
  379. pt = aBuffer.back();
  380. int half_size_seg_len = segm_len / 2 - radius;
  381. if( half_size_seg_len )
  382. {
  383. pt.x -= half_size_seg_len;
  384. aBuffer.push_back( pt );
  385. }
  386. // Create shape.
  387. int ii;
  388. int sign = 1;
  389. segm_count += 1; // increase segm_count to create the last half_size segment
  390. for( ii = 0; ii < segm_count; ii++ )
  391. {
  392. int arc_angle;
  393. if( ii & 1 ) // odd order arcs are greater than 0
  394. sign = -1;
  395. else
  396. sign = 1;
  397. arc_angle = 1800 * sign;
  398. centre = pt;
  399. centre.y += radius;
  400. gen_arc( aBuffer, pt, centre, arc_angle );
  401. pt = aBuffer.back();
  402. pt.x += segm_len * sign;
  403. aBuffer.push_back( pt );
  404. }
  405. // The last point is false:
  406. // it is the end of a full size segment, but must be
  407. // the end of the second half_size segment. Change it.
  408. sign *= -1;
  409. aBuffer.back().x = aStartPoint.x + radius * sign;
  410. // create last arc
  411. pt = aBuffer.back();
  412. centre = pt;
  413. centre.y += radius;
  414. gen_arc( aBuffer, pt, centre, 900 * sign ); pt = aBuffer.back();
  415. // Rotate point
  416. angle += 900;
  417. for( unsigned jj = 0; jj < aBuffer.size(); jj++ )
  418. {
  419. RotatePoint( &aBuffer[jj].x, &aBuffer[jj].y, aStartPoint.x, aStartPoint.y, angle );
  420. }
  421. // push last point (end point)
  422. aBuffer.push_back( aEndPoint );
  423. return 1;
  424. }
  425. MODULE* PCB_EDIT_FRAME::Create_MuWaveBasicShape( const wxString& name, int pad_count )
  426. {
  427. MODULE* module;
  428. int pad_num = 1;
  429. wxString Line;
  430. module = Create_1_Module( name );
  431. if( module == NULL )
  432. return NULL;
  433. #define DEFAULT_SIZE 30
  434. module->SetTimeStamp( GetNewTimeStamp() );
  435. module->m_Value->m_Size = wxSize( DEFAULT_SIZE, DEFAULT_SIZE );
  436. module->m_Value->SetPos0( wxPoint( 0, -DEFAULT_SIZE ) );
  437. module->m_Value->m_Pos.y += module->m_Value->GetPos0().y;
  438. module->m_Value->m_Thickness = DEFAULT_SIZE / 4;
  439. module->m_Reference->m_Size = wxSize( DEFAULT_SIZE, DEFAULT_SIZE );
  440. module->m_Reference->SetPos0( wxPoint( 0, DEFAULT_SIZE ) );
  441. module->m_Reference->m_Pos.y += module->m_Reference->GetPos0().y;
  442. module->m_Reference->m_Thickness = DEFAULT_SIZE / 4;
  443. // Create 2 pads used in gaps and stubs. The gap is between these 2 pads
  444. // the stub is the pad 2
  445. while( pad_count-- )
  446. {
  447. D_PAD* pad = new D_PAD( module );
  448. module->m_Pads.PushFront( pad );
  449. int tw = GetBoard()->GetCurrentTrackWidth();
  450. pad->SetSize( wxSize( tw, tw ) );
  451. pad->SetPosition( module->GetPosition() );
  452. pad->SetShape( PAD_RECT );
  453. pad->SetAttribute( PAD_SMD );
  454. pad->SetLayerMask( LAYER_FRONT );
  455. Line.Printf( wxT( "%d" ), pad_num );
  456. pad->SetPadName( Line );
  457. pad_num++;
  458. }
  459. return module;
  460. }
  461. MODULE* PCB_EDIT_FRAME::Create_MuWaveComponent( int shape_type )
  462. {
  463. int oX;
  464. D_PAD* pad;
  465. MODULE* module;
  466. wxString msg, cmp_name;
  467. int pad_count = 2;
  468. int angle = 0;
  469. // Enter the size of the gap or stub
  470. int gap_size = GetBoard()->GetCurrentTrackWidth();
  471. switch( shape_type )
  472. {
  473. case 0:
  474. msg = _( "Gap" );
  475. cmp_name = wxT( "GAP" );
  476. break;
  477. case 1:
  478. msg = _( "Stub" );
  479. cmp_name = wxT( "STUB" );
  480. pad_count = 2;
  481. break;
  482. case 2:
  483. msg = _( "Arc Stub" );
  484. cmp_name = wxT( "ASTUB" );
  485. pad_count = 1;
  486. break;
  487. default:
  488. msg = wxT( "???" );
  489. break;
  490. }
  491. wxString value = ReturnStringFromValue( g_UserUnit, gap_size );
  492. wxTextEntryDialog dlg( this, msg, _( "Create microwave module" ), value );
  493. if( dlg.ShowModal() != wxID_OK )
  494. {
  495. m_canvas->MoveCursorToCrossHair();
  496. return NULL; // cancelled by user
  497. }
  498. value = dlg.GetValue();
  499. gap_size = ReturnValueFromString( g_UserUnit, value, GetScreen()->GetInternalUnits() );
  500. bool abort = false;
  501. if( shape_type == 2 )
  502. {
  503. double fcoeff = 10.0, fval;
  504. msg.Printf( wxT( "%3.1f" ), angle / fcoeff );
  505. wxTextEntryDialog angledlg( this, _( "Angle (0.1deg):" ),
  506. _( "Create microwave module" ), msg );
  507. if( angledlg.ShowModal() != wxID_OK )
  508. {
  509. m_canvas->MoveCursorToCrossHair();
  510. return NULL; // cancelled by user
  511. }
  512. msg = angledlg.GetValue();
  513. if( !msg.ToDouble( &fval ) )
  514. {
  515. DisplayError( this, _( "Incorrect number, abort" ) );
  516. abort = true;
  517. }
  518. angle = ABS( wxRound( fval * fcoeff ) );
  519. if( angle > 1800 )
  520. angle = 1800;
  521. }
  522. if( abort )
  523. {
  524. m_canvas->MoveCursorToCrossHair();
  525. return NULL;
  526. }
  527. module = Create_MuWaveBasicShape( cmp_name, pad_count );
  528. pad = module->m_Pads;
  529. switch( shape_type )
  530. {
  531. case 0: //Gap :
  532. oX = -( gap_size + pad->GetSize().x ) / 2;
  533. pad->SetX0( oX );
  534. pad->SetX( pad->GetPos0().x + pad->GetPosition().x );
  535. pad = pad->Next();
  536. pad->SetX0( oX + gap_size + pad->GetSize().x );
  537. pad->SetX( pad->GetPos0().x + pad->GetPosition().x );
  538. break;
  539. case 1: //Stub :
  540. pad->SetPadName( wxT( "1" ) );
  541. pad = pad->Next();
  542. pad->SetY0( -( gap_size + pad->GetSize().y ) / 2 );
  543. pad->SetSize( wxSize( pad->GetSize().x, gap_size ) );
  544. pad->SetY( pad->GetPos0().y + pad->GetPosition().y );
  545. break;
  546. case 2: // Arc Stub created by a polygonal approach:
  547. {
  548. EDGE_MODULE* edge = new EDGE_MODULE( module );
  549. module->m_Drawings.PushFront( edge );
  550. edge->SetShape( S_POLYGON );
  551. edge->SetLayer( LAYER_N_FRONT );
  552. int numPoints = angle / 50 + 3; // Note: angles are in 0.1 degrees
  553. std::vector<wxPoint> polyPoints = edge->GetPolyPoints();
  554. polyPoints.reserve( numPoints );
  555. edge->m_Start0.y = -pad->GetSize().y / 2;
  556. polyPoints.push_back( wxPoint( 0, 0 ) );
  557. int theta = -angle / 2;
  558. for( int ii = 1; ii<numPoints - 1; ii++ )
  559. {
  560. wxPoint pt( 0, -gap_size );
  561. RotatePoint( &pt.x, &pt.y, theta );
  562. polyPoints.push_back( pt );
  563. theta += 50;
  564. if( theta > angle / 2 )
  565. theta = angle / 2;
  566. }
  567. // Close the polygon:
  568. polyPoints.push_back( polyPoints[0] );
  569. }
  570. break;
  571. default:
  572. break;
  573. }
  574. module->CalculateBoundingBox();
  575. GetBoard()->m_Status_Pcb = 0;
  576. OnModify();
  577. return module;
  578. }
  579. /**************** Polygon Shapes ***********************/
  580. enum id_mw_cmd {
  581. ID_READ_SHAPE_FILE = 1000
  582. };
  583. /* Setting polynomial form parameters
  584. */
  585. class WinEDA_SetParamShapeFrame : public wxDialog
  586. {
  587. private:
  588. PCB_EDIT_FRAME* m_Parent;
  589. wxRadioBox* m_ShapeOptionCtrl;
  590. EDA_SIZE_CTRL* m_SizeCtrl;
  591. public: WinEDA_SetParamShapeFrame( PCB_EDIT_FRAME* parent, const wxPoint& pos );
  592. ~WinEDA_SetParamShapeFrame() { };
  593. private:
  594. void OnOkClick( wxCommandEvent& event );
  595. void OnCancelClick( wxCommandEvent& event );
  596. /**
  597. * Function ReadDataShapeDescr
  598. * read a description shape file
  599. * File format is
  600. * Unit=MM
  601. * XScale=271.501
  602. * YScale=1.00133
  603. *
  604. * $COORD
  605. * 0 0.6112600148417837
  606. * 0.001851851851851852 0.6104800531118608
  607. * ....
  608. * $ENDCOORD
  609. *
  610. * Each line is the X Y coord (normalized units from 0 to 1)
  611. */
  612. void ReadDataShapeDescr( wxCommandEvent& event );
  613. void AcceptOptions( wxCommandEvent& event );
  614. DECLARE_EVENT_TABLE()
  615. };
  616. BEGIN_EVENT_TABLE( WinEDA_SetParamShapeFrame, wxDialog )
  617. EVT_BUTTON( wxID_OK, WinEDA_SetParamShapeFrame::OnOkClick )
  618. EVT_BUTTON( wxID_CANCEL, WinEDA_SetParamShapeFrame::OnCancelClick )
  619. EVT_BUTTON( ID_READ_SHAPE_FILE, WinEDA_SetParamShapeFrame::ReadDataShapeDescr )
  620. END_EVENT_TABLE()
  621. WinEDA_SetParamShapeFrame::WinEDA_SetParamShapeFrame( PCB_EDIT_FRAME* parent,
  622. const wxPoint& framepos ) :
  623. wxDialog( parent, -1, _( "Complex shape" ), framepos, wxSize( 350, 280 ), DIALOG_STYLE )
  624. {
  625. m_Parent = parent;
  626. PolyEdges.clear();
  627. wxBoxSizer* MainBoxSizer = new wxBoxSizer( wxHORIZONTAL );
  628. SetSizer( MainBoxSizer );
  629. wxBoxSizer* LeftBoxSizer = new wxBoxSizer( wxVERTICAL );
  630. wxBoxSizer* RightBoxSizer = new wxBoxSizer( wxVERTICAL );
  631. MainBoxSizer->Add( LeftBoxSizer, 0, wxGROW | wxALL, 5 );
  632. MainBoxSizer->Add( RightBoxSizer, 0, wxALIGN_CENTER_VERTICAL | wxALL, 5 );
  633. wxButton* Button = new wxButton( this, wxID_OK, _( "OK" ) );
  634. RightBoxSizer->Add( Button, 0, wxGROW | wxALL, 5 );
  635. Button = new wxButton( this, wxID_CANCEL, _( "Cancel" ) );
  636. RightBoxSizer->Add( Button, 0, wxGROW | wxALL, 5 );
  637. Button = new wxButton( this, ID_READ_SHAPE_FILE,
  638. _( "Read Shape Description File..." ) );
  639. RightBoxSizer->Add( Button, 0, wxGROW | wxALL, 5 );
  640. wxString shapelist[3] =
  641. {
  642. _( "Normal" ), _( "Symmetrical" ),
  643. _( "Mirrored" )
  644. };
  645. m_ShapeOptionCtrl = new wxRadioBox( this, -1, _( "Shape Option" ),
  646. wxDefaultPosition, wxDefaultSize, 3,
  647. shapelist, 1,
  648. wxRA_SPECIFY_COLS );
  649. LeftBoxSizer->Add( m_ShapeOptionCtrl, 0, wxGROW | wxALL, 5 );
  650. m_SizeCtrl = new EDA_SIZE_CTRL( this, _( "Size" ), ShapeSize,
  651. g_UserUnit, LeftBoxSizer,
  652. PCB_INTERNAL_UNIT );
  653. GetSizer()->Fit( this );
  654. GetSizer()->SetSizeHints( this );
  655. }
  656. void WinEDA_SetParamShapeFrame::OnCancelClick( wxCommandEvent& event )
  657. {
  658. PolyEdges.clear();
  659. EndModal( -1 );
  660. }
  661. void WinEDA_SetParamShapeFrame::OnOkClick( wxCommandEvent& event )
  662. {
  663. ShapeSize = m_SizeCtrl->GetValue();
  664. PolyShapeType = m_ShapeOptionCtrl->GetSelection();
  665. EndModal( 1 );
  666. }
  667. void WinEDA_SetParamShapeFrame::ReadDataShapeDescr( wxCommandEvent& event )
  668. {
  669. wxString FullFileName;
  670. wxString ext, mask;
  671. FILE* File;
  672. char* Line;
  673. double unitconv = 10000;
  674. char* param1, * param2;
  675. ext = wxT( ".txt" );
  676. mask = wxT( "*" ) + ext;
  677. FullFileName = EDA_FileSelector( _( "Read descr shape file" ),
  678. wxEmptyString,
  679. FullFileName,
  680. ext,
  681. mask,
  682. this,
  683. wxFD_OPEN,
  684. true );
  685. if( FullFileName.IsEmpty() )
  686. return;
  687. File = wxFopen( FullFileName, wxT( "rt" ) );
  688. if( File == NULL )
  689. {
  690. DisplayError( this, _( "File not found" ) );
  691. return;
  692. }
  693. FILE_LINE_READER fileReader( File, FullFileName );
  694. FILTER_READER reader( fileReader );
  695. LOCALE_IO toggle;
  696. while( reader.ReadLine() )
  697. {
  698. Line = reader.Line();
  699. param1 = strtok( Line, " =\n\r" );
  700. param2 = strtok( NULL, " \t\n\r" );
  701. if( strnicmp( param1, "Unit", 4 ) == 0 )
  702. {
  703. if( strnicmp( param2, "inch", 4 ) == 0 )
  704. unitconv = 10000;
  705. if( strnicmp( param2, "mm", 2 ) == 0 )
  706. unitconv = 10000 / 25.4;
  707. }
  708. if( strnicmp( param1, "$ENDCOORD", 8 ) == 0 )
  709. break;
  710. if( strnicmp( param1, "$COORD", 6 ) == 0 )
  711. {
  712. while( reader.ReadLine() )
  713. {
  714. Line = reader.Line();
  715. param1 = strtok( Line, " \t\n\r" );
  716. param2 = strtok( NULL, " \t\n\r" );
  717. if( strnicmp( param1, "$ENDCOORD", 8 ) == 0 )
  718. break;
  719. PolyEdges.push_back( atof( param1 ) );
  720. PolyEdges.push_back( atof( param2 ) );
  721. }
  722. }
  723. if( strnicmp( Line, "XScale", 6 ) == 0 )
  724. {
  725. ShapeScaleX = atof( param2 );
  726. }
  727. if( strnicmp( Line, "YScale", 6 ) == 0 )
  728. {
  729. ShapeScaleY = atof( param2 );
  730. }
  731. }
  732. ShapeScaleX *= unitconv;
  733. ShapeScaleY *= unitconv;
  734. m_SizeCtrl->SetValue( (int) ShapeScaleX, (int) ShapeScaleY );
  735. }
  736. MODULE* PCB_EDIT_FRAME::Create_MuWavePolygonShape()
  737. {
  738. D_PAD* pad1, * pad2;
  739. MODULE* module;
  740. wxString cmp_name;
  741. int pad_count = 2;
  742. EDGE_MODULE* edge;
  743. WinEDA_SetParamShapeFrame* frame = new WinEDA_SetParamShapeFrame( this, wxPoint( -1, -1 ) );
  744. int ok = frame->ShowModal();
  745. frame->Destroy();
  746. m_canvas->MoveCursorToCrossHair();
  747. if( ok != 1 )
  748. {
  749. PolyEdges.clear();
  750. }
  751. if( PolyShapeType == 2 ) // mirrored
  752. ShapeScaleY = -ShapeScaleY;
  753. ShapeSize.x = wxRound( ShapeScaleX );
  754. ShapeSize.y = wxRound( ShapeScaleY );
  755. if( ( ShapeSize.x ) == 0 || ( ShapeSize.y == 0 ) )
  756. {
  757. DisplayError( this, _( "Shape has a null size!" ) );
  758. return NULL;
  759. }
  760. if( PolyEdges.size() == 0 )
  761. {
  762. DisplayError( this, _( "Shape has no points!" ) );
  763. return NULL;
  764. }
  765. cmp_name = wxT( "POLY" );
  766. module = Create_MuWaveBasicShape( cmp_name, pad_count );
  767. pad1 = module->m_Pads;
  768. pad1->SetX0( -ShapeSize.x / 2 );
  769. pad1->SetX( pad1->GetPos0().x + pad1->GetPosition().x );
  770. pad2 = (D_PAD*) pad1->Next();
  771. pad2->SetX0( pad1->GetPos0().x + ShapeSize.x );
  772. pad2->SetX( pad2->GetPos0().x + pad2->GetPosition().x );
  773. edge = new EDGE_MODULE( module );
  774. module->m_Drawings.PushFront( edge );
  775. edge->SetShape( S_POLYGON );
  776. edge->SetLayer( LAYER_N_FRONT );
  777. std::vector<wxPoint> polyPoints = edge->GetPolyPoints();
  778. polyPoints.reserve( 2 * PolyEdges.size() + 2 );
  779. // Init start point coord:
  780. polyPoints.push_back( wxPoint( pad1->GetPos0().x, 0 ) );
  781. wxPoint first_coordinate, last_coordinate;
  782. for( unsigned ii = 0; ii < PolyEdges.size(); ii++ ) // Copy points
  783. {
  784. last_coordinate.x = wxRound( PolyEdges[ii] * ShapeScaleX ) + pad1->GetPos0().x;
  785. last_coordinate.y = -wxRound( PolyEdges[ii] * ShapeScaleY );
  786. polyPoints.push_back( last_coordinate );
  787. }
  788. first_coordinate.y = polyPoints[1].y;
  789. switch( PolyShapeType )
  790. {
  791. case 0: // Single
  792. case 2: // Single mirrored
  793. // Init end point coord:
  794. pad2->SetX0( last_coordinate.x );
  795. polyPoints.push_back( wxPoint( last_coordinate.x, 0 ) );
  796. pad1->SetSize( wxSize( ABS( first_coordinate.y ), ABS( first_coordinate.y ) ) );
  797. pad2->SetSize( wxSize( ABS( last_coordinate.y ), ABS( last_coordinate.y ) ) );
  798. pad1->SetY0( first_coordinate.y / 2 );
  799. pad2->SetY0( last_coordinate.y / 2 );
  800. pad1->SetY( pad1->GetPos0().y + module->GetPosition().y );
  801. pad2->SetY( pad2->GetPos0().y + module->GetPosition().y );
  802. break;
  803. case 1: // Symmetric
  804. for( int ndx = polyPoints.size() - 1; ndx>=0; --ndx )
  805. {
  806. wxPoint pt = polyPoints[ndx];
  807. pt.y = -pt.y; // mirror about X axis
  808. polyPoints.push_back( pt );
  809. }
  810. pad1->SetSize( wxSize( 2 * ABS( first_coordinate.y ), 2 * ABS( first_coordinate.y ) ) );
  811. pad2->SetSize( wxSize( 2 * ABS( last_coordinate.y ), 2 * ABS( last_coordinate.y ) ) );
  812. break;
  813. }
  814. PolyEdges.clear();
  815. module->CalculateBoundingBox();
  816. GetBoard()->m_Status_Pcb = 0;
  817. OnModify();
  818. return module;
  819. }
  820. void PCB_EDIT_FRAME::Edit_Gap( wxDC* DC, MODULE* aModule )
  821. {
  822. int gap_size, oX;
  823. D_PAD* pad, * next_pad;
  824. wxString msg;
  825. if( aModule == NULL )
  826. return;
  827. // Test if module is a gap type (name begins with GAP, and has 2 pads).
  828. msg = aModule->m_Reference->m_Text.Left( 3 );
  829. if( msg != wxT( "GAP" ) )
  830. return;
  831. pad = aModule->m_Pads;
  832. if( pad == NULL )
  833. {
  834. DisplayError( this, _( "No pad for this module" ) );
  835. return;
  836. }
  837. next_pad = (D_PAD*) pad->Next();
  838. if( next_pad == NULL )
  839. {
  840. DisplayError( this, _( "Only one pad for this module" ) );
  841. return;
  842. }
  843. aModule->Draw( m_canvas, DC, GR_XOR );
  844. // Calculate the current dimension.
  845. gap_size = next_pad->GetPos0().x - pad->GetPos0().x - pad->GetSize().x;
  846. // Entrer the desired length of the gap.
  847. msg = ReturnStringFromValue( g_UserUnit, gap_size );
  848. wxTextEntryDialog dlg( this, _( "Gap:" ), _( "Create Microwave Gap" ), msg );
  849. if( dlg.ShowModal() != wxID_OK )
  850. return; // cancelled by user
  851. msg = dlg.GetValue();
  852. gap_size = ReturnValueFromString( g_UserUnit, msg, GetScreen()->GetInternalUnits() );
  853. // Updating sizes of pads forming the gap.
  854. int tw = GetBoard()->GetCurrentTrackWidth();
  855. pad->SetSize( wxSize( tw, tw ) );
  856. pad->SetY0( 0 );
  857. oX = -( gap_size + pad->GetSize().x ) / 2;
  858. pad->SetX0( oX );
  859. wxPoint padpos = pad->GetPos0() + aModule->GetPosition();
  860. RotatePoint( &padpos.x, &padpos.y,
  861. aModule->m_Pos.x, aModule->m_Pos.y, aModule->GetOrientation() );
  862. pad->SetPosition( padpos );
  863. tw = GetBoard()->GetCurrentTrackWidth();
  864. next_pad->SetSize( wxSize( tw, tw ) );
  865. next_pad->SetY0( 0 );
  866. next_pad->SetX0( oX + gap_size + next_pad->GetSize().x );
  867. padpos = next_pad->GetPos0() + aModule->GetPosition();
  868. RotatePoint( &padpos.x, &padpos.y,
  869. aModule->m_Pos.x, aModule->m_Pos.y, aModule->GetOrientation() );
  870. next_pad->SetPosition( padpos );
  871. aModule->Draw( m_canvas, DC, GR_OR );
  872. }