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.

391 lines
12 KiB

12 years ago
12 years ago
* 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
* 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
* 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
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
4 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2011-2013 Lorenzo Marcantonio <l.marcantonio@logossrl.com>
  5. * Copyright (C) 2004-2022 KiCad Developers, see change_log.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 export_d356.cpp
  26. * @brief Export IPC-D-356 test format
  27. */
  28. #include <confirm.h>
  29. #include <gestfich.h>
  30. #include <kiface_base.h>
  31. #include <pcb_edit_frame.h>
  32. #include <trigo.h>
  33. #include <build_version.h>
  34. #include <macros.h>
  35. #include <wildcards_and_files_ext.h>
  36. #include <locale_io.h>
  37. #include <pcbnew.h>
  38. #include <board.h>
  39. #include <board_design_settings.h>
  40. #include <footprint.h>
  41. #include <pad.h>
  42. #include <pcb_track.h>
  43. #include <vector>
  44. #include <cctype>
  45. #include <math/util.h> // for KiROUND
  46. #include <export_d356.h>
  47. #include <wx/filedlg.h>
  48. // Compute the access code for a pad. Returns -1 if there is no copper
  49. static int compute_pad_access_code( BOARD *aPcb, LSET aLayerMask )
  50. {
  51. // Non-copper is not interesting here
  52. aLayerMask &= LSET::AllCuMask();
  53. if( !aLayerMask.any() )
  54. return -1;
  55. // Traditional TH pad
  56. if( aLayerMask[F_Cu] && aLayerMask[B_Cu] )
  57. return 0;
  58. // Front SMD pad
  59. if( aLayerMask[F_Cu] )
  60. return 1;
  61. // Back SMD pad
  62. if( aLayerMask[B_Cu] )
  63. return aPcb->GetCopperLayerCount();
  64. // OK, we have an inner-layer only pad (and I have no idea about
  65. // what could be used for); anyway, find the first copper layer
  66. // it's on
  67. for( int layer = In1_Cu; layer < B_Cu; ++layer )
  68. {
  69. if( aLayerMask[layer] )
  70. return layer + 1;
  71. }
  72. // This shouldn't happen
  73. return -1;
  74. }
  75. /* Convert and clamp a size from IU to decimils */
  76. static int iu_to_d356(int iu, int clamp)
  77. {
  78. int val = KiROUND( iu / ( pcbIUScale.IU_PER_MILS / 10 ) );
  79. if( val > clamp ) return clamp;
  80. if( val < -clamp ) return -clamp;
  81. return val;
  82. }
  83. /* Extract the D356 record from the footprints (pads) */
  84. static void build_pad_testpoints( BOARD *aPcb, std::vector <D356_RECORD>& aRecords )
  85. {
  86. VECTOR2I origin = aPcb->GetDesignSettings().GetAuxOrigin();
  87. for( FOOTPRINT* footprint : aPcb->Footprints() )
  88. {
  89. for( PAD* pad : footprint->Pads() )
  90. {
  91. D356_RECORD rk;
  92. rk.access = compute_pad_access_code( aPcb, pad->GetLayerSet() );
  93. // It could be a mask only pad, we only handle pads with copper here
  94. if( rk.access != -1 )
  95. {
  96. rk.netname = pad->GetNetname();
  97. rk.pin = pad->GetNumber();
  98. rk.refdes = footprint->GetReference();
  99. rk.midpoint = false; // XXX MAYBE need to be computed (how?)
  100. const VECTOR2I& drill = pad->GetDrillSize();
  101. rk.drill = std::min( drill.x, drill.y );
  102. rk.hole = (rk.drill != 0);
  103. rk.smd = pad->GetAttribute() == PAD_ATTRIB::SMD
  104. || pad->GetAttribute() == PAD_ATTRIB::CONN;
  105. rk.mechanical = ( pad->GetAttribute() == PAD_ATTRIB::NPTH );
  106. rk.x_location = pad->GetPosition().x - origin.x;
  107. rk.y_location = origin.y - pad->GetPosition().y;
  108. rk.x_size = pad->GetSize().x;
  109. // Rule: round pads have y = 0
  110. if( pad->GetShape() == PAD_SHAPE::CIRCLE )
  111. rk.y_size = 0;
  112. else
  113. rk.y_size = pad->GetSize().y;
  114. rk.rotation = - pad->GetOrientation().AsDegrees();
  115. if( rk.rotation < 0 )
  116. rk.rotation += 360;
  117. // the value indicates which sides are *not* accessible
  118. rk.soldermask = 3;
  119. if( pad->GetLayerSet()[F_Mask] )
  120. rk.soldermask &= ~1;
  121. if( pad->GetLayerSet()[B_Mask] )
  122. rk.soldermask &= ~2;
  123. aRecords.push_back( rk );
  124. }
  125. }
  126. }
  127. }
  128. /* Compute the access code for a via. In D-356 layers are numbered from 1 up,
  129. where '1' is the 'primary side' (usually the component side);
  130. '0' means 'both sides', and other layers follows in an unspecified order */
  131. static int via_access_code( BOARD *aPcb, int top_layer, int bottom_layer )
  132. {
  133. // Easy case for through vias: top_layer is component, bottom_layer is
  134. // solder, access code is 0
  135. if( (top_layer == F_Cu) && (bottom_layer == B_Cu) )
  136. return 0;
  137. // Blind via, reachable from front
  138. if( top_layer == F_Cu )
  139. return 1;
  140. // Blind via, reachable from bottom
  141. if( bottom_layer == B_Cu )
  142. return aPcb->GetCopperLayerCount();
  143. // It's a buried via, accessible from some inner layer
  144. // (maybe could be used for testing before laminating? no idea)
  145. return bottom_layer + 1; // XXX is this correct?
  146. }
  147. /* Extract the D356 record from the vias */
  148. static void build_via_testpoints( BOARD *aPcb, std::vector <D356_RECORD>& aRecords )
  149. {
  150. VECTOR2I origin = aPcb->GetDesignSettings().GetAuxOrigin();
  151. // Enumerate all the track segments and keep the vias
  152. for( auto track : aPcb->Tracks() )
  153. {
  154. if( track->Type() == PCB_VIA_T )
  155. {
  156. PCB_VIA *via = static_cast<PCB_VIA*>( track );
  157. NETINFO_ITEM *net = track->GetNet();
  158. D356_RECORD rk;
  159. rk.smd = false;
  160. rk.hole = true;
  161. if( net )
  162. rk.netname = net->GetNetname();
  163. else
  164. rk.netname = wxEmptyString;
  165. rk.refdes = wxT("VIA");
  166. rk.pin = wxT("");
  167. rk.midpoint = true; // Vias are always midpoints
  168. rk.drill = via->GetDrillValue();
  169. rk.mechanical = false;
  170. PCB_LAYER_ID top_layer, bottom_layer;
  171. via->LayerPair( &top_layer, &bottom_layer );
  172. rk.access = via_access_code( aPcb, top_layer, bottom_layer );
  173. rk.x_location = via->GetPosition().x - origin.x;
  174. rk.y_location = origin.y - via->GetPosition().y;
  175. rk.x_size = via->GetWidth();
  176. rk.y_size = 0; // Round so height = 0
  177. rk.rotation = 0;
  178. rk.soldermask = 3; // XXX always tented?
  179. aRecords.push_back( rk );
  180. }
  181. }
  182. }
  183. /* Add a new netname to the d356 canonicalized list */
  184. static const wxString intern_new_d356_netname( const wxString &aNetname,
  185. std::map<wxString, wxString> &aMap, std::set<wxString> &aSet )
  186. {
  187. wxString canon;
  188. for( size_t ii = 0; ii < aNetname.Len(); ++ii )
  189. {
  190. // Rule: we can only use the standard ASCII, control excluded
  191. wxUniChar ch = aNetname[ii];
  192. if( ch > 126 || !std::isgraph( static_cast<unsigned char>( ch ) ) )
  193. ch = '?';
  194. canon += ch;
  195. }
  196. // Rule: only uppercase (unofficial, but known to give problems
  197. // otherwise)
  198. canon.MakeUpper();
  199. // Rule: maximum length is 14 characters, otherwise we keep the tail
  200. if( canon.size() > 14 )
  201. {
  202. canon = canon.Right( 14 );
  203. }
  204. // Check if it's still unique
  205. if( aSet.count( canon ) )
  206. {
  207. // Nope, need to uniquify it, trim it more and add a number
  208. wxString base( canon );
  209. if( base.size() > 10 )
  210. {
  211. base = base.Right( 10 );
  212. }
  213. int ctr = 0;
  214. do
  215. {
  216. ++ctr;
  217. canon = base;
  218. canon << '#' << ctr;
  219. } while ( aSet.count( canon ) );
  220. }
  221. // Register it
  222. aMap[aNetname] = canon;
  223. aSet.insert( canon );
  224. return canon;
  225. }
  226. /* Write all the accumuled data to the file in D356 format */
  227. void IPC356D_WRITER::write_D356_records( std::vector <D356_RECORD> &aRecords, FILE* aFile )
  228. {
  229. // Sanified and shorted network names and set of short names
  230. std::map<wxString, wxString> d356_net_map;
  231. std::set<wxString> d356_net_set;
  232. for( unsigned i = 0; i < aRecords.size(); i++ )
  233. {
  234. D356_RECORD &rk = aRecords[i];
  235. // Try to sanify the network name (there are limits on this), if
  236. // not already done. Also 'empty' net are marked as N/C, as
  237. // specified.
  238. wxString d356_net( wxT( "N/C" ) );
  239. if( !rk.netname.empty() )
  240. {
  241. d356_net = d356_net_map[rk.netname];
  242. if( d356_net.empty() )
  243. d356_net = intern_new_d356_netname( rk.netname, d356_net_map, d356_net_set );
  244. }
  245. // Choose the best record type
  246. int rktype;
  247. if( rk.smd )
  248. rktype = 327;
  249. else
  250. {
  251. if( rk.mechanical )
  252. rktype = 367;
  253. else
  254. rktype = 317;
  255. }
  256. // Operation code, signal and component
  257. fprintf( aFile, "%03d%-14.14s %-6.6s%c%-4.4s%c",
  258. rktype, TO_UTF8(d356_net),
  259. TO_UTF8(rk.refdes),
  260. rk.pin.empty()?' ':'-',
  261. TO_UTF8(rk.pin),
  262. rk.midpoint?'M':' ' );
  263. // Hole definition
  264. if( rk.hole )
  265. {
  266. fprintf( aFile, "D%04d%c",
  267. iu_to_d356( rk.drill, 9999 ),
  268. rk.mechanical ? 'U':'P' );
  269. }
  270. else
  271. fprintf( aFile, " " );
  272. // Test point access
  273. fprintf( aFile, "A%02dX%+07dY%+07dX%04dY%04dR%03d",
  274. rk.access,
  275. iu_to_d356( rk.x_location, 999999 ),
  276. iu_to_d356( rk.y_location, 999999 ),
  277. iu_to_d356( rk.x_size, 9999 ),
  278. iu_to_d356( rk.y_size, 9999 ),
  279. rk.rotation );
  280. // Soldermask
  281. fprintf( aFile, "S%d\n", rk.soldermask );
  282. }
  283. }
  284. void IPC356D_WRITER::Write( const wxString& aFilename )
  285. {
  286. FILE* file = nullptr;
  287. LOCALE_IO toggle; // Switch the locale to standard C
  288. if( ( file = wxFopen( aFilename, wxT( "wt" ) ) ) == nullptr )
  289. {
  290. wxString details;
  291. details.Printf( wxT( "The file %s could not be opened for writing." ), aFilename );
  292. DisplayErrorMessage( m_parent, wxT( "Could not write IPC-356D file!" ), details );
  293. return;
  294. }
  295. // This will contain everything needed for the 356 file
  296. std::vector<D356_RECORD> d356_records;
  297. build_via_testpoints( m_pcb, d356_records );
  298. build_pad_testpoints( m_pcb, d356_records );
  299. // Code 00 AFAIK is ASCII, CUST 0 is decimils/degrees
  300. // CUST 1 would be metric but gerbtool simply ignores it!
  301. fprintf( file, "P CODE 00\n" );
  302. fprintf( file, "P UNITS CUST 0\n" );
  303. fprintf( file, "P arrayDim N\n" );
  304. write_D356_records( d356_records, file );
  305. fprintf( file, "999\n" );
  306. fclose( file );
  307. }
  308. void PCB_EDIT_FRAME::GenD356File( wxCommandEvent& aEvent )
  309. {
  310. wxFileName fn = GetBoard()->GetFileName();
  311. wxString ext, wildcard;
  312. ext = IpcD356FileExtension;
  313. wildcard = IpcD356FileWildcard();
  314. fn.SetExt( ext );
  315. wxString pro_dir = wxPathOnly( Prj().GetProjectFullName() );
  316. wxFileDialog dlg( this, _( "Export D-356 Test File" ), pro_dir,
  317. fn.GetFullName(), wildcard,
  318. wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
  319. if( dlg.ShowModal() == wxID_CANCEL )
  320. return;
  321. IPC356D_WRITER writer( GetBoard(), this );
  322. writer.Write( dlg.GetPath() );
  323. }