├── .gitignore ├── LICENSE ├── README.md ├── docs └── samples │ └── generic_plot.kiplot.yaml ├── setup.cfg ├── setup.py ├── src └── kiplot │ ├── __init__.py │ ├── __main__.py │ ├── __version__.py │ ├── config_reader.py │ ├── error.py │ ├── kiplot.py │ └── plot_config.py └── tests ├── board_samples └── simple_2layer.kicad_pcb ├── conftest.py ├── test_plot ├── README.md ├── __init__.py ├── plotting_test_utils.py └── test_simple_2layer.py ├── test_yaml.py └── yaml_samples └── simple_2layer.kiplot.yaml /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | 29 | .pytest_cache 30 | 31 | *.kicad_pcb-bak 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KiPlot 2 | 3 | KiPlot is a program which helps you to plot your KiCad PCBs to output 4 | formats easily, repeatable, and most of all, scriptably. This means you 5 | can use a Makefile to export your KiCad PCBs just as needed. 6 | 7 | For example, it's common that you might want for each board rev: 8 | 9 | * Check DRC one last time (currently not possible) 10 | * Gerbers, drills and drill maps for a fab in their favourite format 11 | * Fab docs for the assembler 12 | * Pick and place files 13 | 14 | You want to do this in a one-touch way, and make sure everything you need to 15 | do so it securely saved in version control, not on the back of an old 16 | datasheet. 17 | 18 | KiPlot lets you do this. 19 | 20 | As a side effect of providing a scriptable plot driver for KiCad, KiPlot also 21 | allows functional testing of KiCad plot functions, which would otherwise be 22 | somewhat unwieldy to write. 23 | 24 | ## Using KiPlot 25 | 26 | You can call `kiplot` directly, passing a PCB file and a config file: 27 | 28 | ``` 29 | -b $(PCB) -c $(KIPLOT_CFG) -v 30 | ``` 31 | 32 | A simple target can be added to your `makefile`, so you can just run 33 | `make pcb_files` or integrate into your current build process. 34 | 35 | ``` 36 | pcb_files: 37 | kiplot -b $(PCB) -c $(KIPLOT_CFG) -v 38 | ``` 39 | 40 | ## Installing 41 | 42 | ### Set up a virtualenv (if you installed KiCad normally) 43 | 44 | If you installed KiCad from a package manager, or you used `make install`, 45 | you probably have the packages and libraries on you system paths. 46 | 47 | ``` 48 | virtualenv --python /usr/bin/python2.7 --system-site-packages ~/venv/kiplot 49 | ``` 50 | 51 | ### Set up a virtualenv (if you installed KiCad locally) 52 | 53 | 54 | If the `pcbnew` Python package is *not* installed at a system level (e.g. if 55 | you are building locally and not installing to the system, you should not 56 | need any system packages: 57 | 58 | ``` 59 | virtualenv --python /usr/bin/python2.7 ~/venv/kiplot 60 | ``` 61 | However, you must make sure `pcbnew` is accessible to KiPlot. 62 | You might need to add it to the `PYTHONPATH`. 63 | 64 | You might also need to set `LD_LIBRARY_PATH` (you need to be able to load 65 | `libkicad_3dsg.so`). 66 | 67 | For example, if you installed in `~/local/kicad`, you might have: 68 | 69 | ``` 70 | export PYTHONPATH=~/local/kicad/lib/python2.7/site-packages 71 | export LD_LIBRARY_PATH=~/local/kicad/lib64 72 | ``` 73 | 74 | ### Install KiPlot to the virtualenv 75 | 76 | Activate the virtualenv: 77 | 78 | ``` 79 | source ~/venv/kiplot/bin/activate 80 | ``` 81 | 82 | Install `kiplot` with `pip -e`: 83 | 84 | ``` 85 | cd path/to/kiplot 86 | pip install -e . 87 | ``` 88 | 89 | ## Testing 90 | 91 | There are some tests. Run them with pytest: 92 | 93 | ``` 94 | pytest 95 | ``` 96 | 97 | # TODO list 98 | 99 | There are some things that still need work: 100 | 101 | * DRC checking - that can't be done over the Python interface yet. If/when 102 | this is added to KiCad, KiPlot will be able to also be used for DRC 103 | functional tests instead of a complex additonal test harness in C++. 104 | -------------------------------------------------------------------------------- /docs/samples/generic_plot.kiplot.yaml: -------------------------------------------------------------------------------- 1 | # Example KiPlot config file 2 | kiplot: 3 | version: 1 4 | 5 | preflight: 6 | 7 | # one day.... 8 | check_zone_fills: false 9 | run_drc: false 10 | 11 | outputs: 12 | 13 | - name: 'gerbers' 14 | comment: "Gerbers for the board house" 15 | type: gerber 16 | dir: gerberdir 17 | options: 18 | # generic layer options 19 | exclude_edge_layer: false 20 | exclude_pads_from_silkscreen: false 21 | use_aux_axis_as_origin: false 22 | plot_sheet_reference: false 23 | plot_footprint_refs: true 24 | plot_footprint_values: true 25 | force_plot_invisible_refs_vals: false 26 | tent_vias: true 27 | check_zone_fills: true 28 | 29 | # gerber options 30 | line_width: 0.15 31 | subtract_mask_from_silk: true 32 | use_protel_extensions: false 33 | gerber_precision: 4.5 34 | create_gerber_job_file: true 35 | use_gerber_x2_attributes: true 36 | use_gerber_net_attributes: false 37 | 38 | layers: 39 | - layer: F.Cu 40 | suffix: F_Cu 41 | - layer: F.SilkS 42 | suffix: F_SilkS 43 | # - layer: Inner.1 44 | # suffix: Inner_1 45 | 46 | - name: excellon_drill 47 | comment: "Excellon drill files" 48 | type: excellon 49 | dir: gerberdir 50 | options: 51 | metric_units: true 52 | pth_and_npth_single_file: true 53 | use_aux_axis_as_origin: false 54 | minimal_header: false 55 | mirror_y_axis: false 56 | report: 57 | filename: 'drill_report.rpt' 58 | map: 59 | type: 'pdf' 60 | 61 | - name: gerber drills 62 | comment: "Gerber drill files" 63 | type: gerb_drill 64 | dir: gerberdir 65 | options: 66 | use_aux_axis_as_origin: false 67 | 68 | - name: postscript 69 | comment: "Postscript files" 70 | type: ps 71 | dir: gerberdir 72 | options: 73 | exclude_edge_layer: false 74 | exclude_pads_from_silkscreen: false 75 | use_aux_axis_as_origin: false 76 | plot_sheet_reference: false 77 | plot_footprint_refs: true 78 | plot_footprint_values: true 79 | force_plot_invisible_refs_vals: false 80 | tent_vias: true 81 | check_zone_fills: true 82 | 83 | # PS options 84 | line_width: 0.15 85 | drill_marks: full 86 | scaling: 2 87 | sketch_plot: true 88 | scale_adjust_x: 1.0 89 | scale_adjust_y: 1.0 90 | width_adjust: 0 91 | mirror_plot: false 92 | negative_plot: false 93 | a4_output: true 94 | layers: 95 | - layer: F.Cu 96 | suffix: F_Cu 97 | - layer: F.Fab 98 | suffix: F_Fab 99 | # - layer: Inner.1 100 | # suffix: Inner_1 101 | # 102 | - name: DXF 103 | comment: "DXF files" 104 | type: dxf 105 | dir: gerberdir 106 | options: 107 | exclude_edge_layer: false 108 | exclude_pads_from_silkscreen: false 109 | use_aux_axis_as_origin: false 110 | plot_sheet_reference: false 111 | plot_footprint_refs: true 112 | plot_footprint_values: true 113 | force_plot_invisible_refs_vals: false 114 | tent_vias: true 115 | check_zone_fills: true 116 | 117 | # PS options 118 | drill_marks: full 119 | sketch_plot: true 120 | use_aux_axis_as_origin: false 121 | polygon_mode: true 122 | layers: 123 | - layer: F.Cu 124 | suffix: F_Cu 125 | - layer: F.Fab 126 | suffix: F_Fab 127 | 128 | - name: SVG 129 | comment: "SVG files" 130 | type: svg 131 | dir: gerberdir 132 | options: 133 | exclude_edge_layer: false 134 | exclude_pads_from_silkscreen: false 135 | use_aux_axis_as_origin: false 136 | plot_sheet_reference: false 137 | plot_footprint_refs: true 138 | plot_footprint_values: true 139 | force_plot_invisible_refs_vals: false 140 | tent_vias: true 141 | check_zone_fills: true 142 | 143 | # SVG options 144 | line_width: 0.25 145 | drill_marks: full 146 | mirror_plot: true 147 | negative_plot: true 148 | layers: 149 | - layer: F.Cu 150 | suffix: F_Cu 151 | - layer: F.Fab 152 | suffix: F_Fab 153 | 154 | - name: HPGL 155 | comment: "HPGL files" 156 | type: hpgl 157 | dir: gerberdir 158 | options: 159 | exclude_edge_layer: false 160 | exclude_pads_from_silkscreen: false 161 | use_aux_axis_as_origin: false 162 | plot_sheet_reference: false 163 | plot_footprint_refs: true 164 | plot_footprint_values: true 165 | force_plot_invisible_refs_vals: false 166 | tent_vias: true 167 | check_zone_fills: true 168 | 169 | # HPGL options 170 | drill_marks: full 171 | mirror_plot: true 172 | sketch_plot: true 173 | scaling: 0 # auto 174 | pen_width: 0.5 175 | layers: 176 | - layer: F.Cu 177 | suffix: F_Cu 178 | - layer: B.SilkS 179 | suffix: B_Silks 180 | 181 | - name: PDF 182 | comment: "PDF files" 183 | type: pdf 184 | dir: gerberdir 185 | options: 186 | exclude_edge_layer: false 187 | exclude_pads_from_silkscreen: false 188 | use_aux_axis_as_origin: false 189 | plot_sheet_reference: false 190 | plot_footprint_refs: true 191 | plot_footprint_values: true 192 | force_plot_invisible_refs_vals: false 193 | tent_vias: true 194 | check_zone_fills: true 195 | 196 | # PDF options 197 | drill_marks: small 198 | mirror_plot: false 199 | negative_plot: false 200 | line_width: 0.01 201 | layers: 202 | - layer: F.Cu 203 | suffix: F_Cu 204 | - layer: B.SilkS 205 | suffix: B_Silks -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [aliases] 2 | test=pytest -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import io 4 | import os 5 | 6 | from setuptools import setup, find_packages 7 | 8 | # Package meta-data. 9 | NAME = 'kiplot' 10 | DESCRIPTION = 'Plotting driver for KiCad' 11 | URL = 'https://github.com/johnbeard/kiplot' 12 | EMAIL = 'john.j.beard@gmail.com' 13 | AUTHOR = 'John Beard' 14 | REQUIRES_PYTHON = '>=2.7.0' 15 | 16 | 17 | # What packages are required for this module to be executed? 18 | REQUIRED = [ 19 | 'pyyaml', 20 | # 'pcbnew' 21 | ] 22 | 23 | here = os.path.abspath(os.path.dirname(__file__)) 24 | 25 | # Import the README and use it as the long-description. 26 | # Note: this will only work if 'README.md' is present in your MANIFEST.in file! 27 | with io.open(os.path.join(here, 'README.md'), encoding='utf-8') as f: 28 | long_description = '\n' + f.read() 29 | 30 | 31 | about = {} 32 | with open(os.path.join(here, 'src', NAME, '__version__.py')) as f: 33 | exec(f.read(), about) 34 | 35 | 36 | # Where the magic happens: 37 | setup( 38 | name=NAME, 39 | version=about['__version__'], 40 | description=DESCRIPTION, 41 | long_description=long_description, 42 | long_description_content_type='text/markdown', 43 | author=AUTHOR, 44 | author_email=EMAIL, 45 | python_requires=REQUIRES_PYTHON, 46 | url=URL, 47 | packages=find_packages('src'), 48 | package_dir={'': 'src'}, 49 | # If your package is a single module, use this instead of 'packages': 50 | # py_modules=['mypackage'], 51 | 52 | entry_points={ 53 | 'console_scripts': ['kiplot=kiplot.__main__:main'], 54 | }, 55 | install_requires=REQUIRED, 56 | include_package_data=True, 57 | license='MIT', 58 | classifiers=[ 59 | # Trove classifiers 60 | # Full list: https://pypi.python.org/pypi?%3Aaction=list_classifiers 61 | 'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)', 62 | 'Programming Language :: Python', 63 | 'Programming Language :: Python :: 2', 64 | 'Programming Language :: Python :: 2.7', 65 | 'Programming Language :: Python :: Implementation :: CPython', 66 | 'Programming Language :: Python :: Implementation :: PyPy', 67 | 'Topic :: Scientific/Engineering :: Electronic Design Automation (EDA)', 68 | ], 69 | setup_requires=['pytest-runner'], 70 | tests_require=['pytest'], 71 | ) 72 | -------------------------------------------------------------------------------- /src/kiplot/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/johnbeard/kiplot/f1c85dbdba44a417a0f42d5a209381e569764b78/src/kiplot/__init__.py -------------------------------------------------------------------------------- /src/kiplot/__main__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import argparse 4 | import logging 5 | import os 6 | import sys 7 | 8 | from . import kiplot 9 | from . import config_reader 10 | 11 | 12 | def main(): 13 | 14 | EXIT_BAD_ARGS = 1 15 | EXIT_BAD_CONFIG = 2 16 | 17 | parser = argparse.ArgumentParser( 18 | description='Command-line Plotting for KiCad') 19 | parser.add_argument('-v', '--verbose', action='store_true', 20 | help='show debugging information') 21 | parser.add_argument('-b', '--board-file', required=True, 22 | help='The PCB .kicad-pcb board file') 23 | parser.add_argument('-c', '--plot-config', required=True, 24 | help='The plotting config file to use') 25 | parser.add_argument('-d', '--out-dir', default='.', 26 | help='The output directory (cwd if not given)') 27 | 28 | args = parser.parse_args() 29 | 30 | log_level = logging.DEBUG if args.verbose else logging.INFO 31 | logging.basicConfig(level=log_level) 32 | 33 | if not os.path.isfile(args.board_file): 34 | logging.error("Board file not found: {}".format(args.board_file)) 35 | 36 | if not os.path.isfile(args.plot_config): 37 | logging.error("Plot config file not found: {}" 38 | .format(args.plot_config)) 39 | sys.exit(EXIT_BAD_ARGS) 40 | 41 | cr = config_reader.CfgYamlReader() 42 | 43 | with open(args.plot_config) as cf_file: 44 | cfg = cr.read(cf_file) 45 | 46 | # relative to CWD (absolute path overrides) 47 | outdir = os.path.join(os.getcwd(), args.out_dir) 48 | cfg.outdir = outdir 49 | 50 | # Finally, once all value are in, check they make sense 51 | errs = cfg.validate() 52 | 53 | if errs: 54 | logging.error('Invalid config:\n\n' + "\n".join(errs)) 55 | sys.exit(EXIT_BAD_CONFIG) 56 | 57 | # Set up the plotter and do it 58 | plotter = kiplot.Plotter(cfg) 59 | plotter.plot(args.board_file) 60 | 61 | 62 | if __name__ == "__main__": 63 | main() 64 | -------------------------------------------------------------------------------- /src/kiplot/__version__.py: -------------------------------------------------------------------------------- 1 | VERSION = (0, 1, 0) 2 | 3 | __version__ = '.'.join(map(str, VERSION)) 4 | -------------------------------------------------------------------------------- /src/kiplot/config_reader.py: -------------------------------------------------------------------------------- 1 | """ 2 | Class to read KiPlot config files 3 | """ 4 | 5 | import logging 6 | import yaml 7 | import os 8 | import re 9 | 10 | import pcbnew 11 | 12 | from . import plot_config as PC 13 | from . import error 14 | 15 | 16 | class CfgReader(object): 17 | 18 | def __init__(self): 19 | pass 20 | 21 | 22 | class YamlError(error.KiPlotError): 23 | pass 24 | 25 | 26 | class CfgYamlReader(CfgReader): 27 | 28 | def __init__(self): 29 | super(CfgYamlReader, self).__init__() 30 | 31 | def _check_version(self, data): 32 | 33 | try: 34 | version = data['kiplot']['version'] 35 | except KeyError: 36 | raise YamlError("YAML config needs kiplot.version.") 37 | return None 38 | 39 | if version != 1: 40 | raise YamlError("Unknown KiPlot config version: {}" 41 | .format(version)) 42 | return None 43 | 44 | return version 45 | 46 | def _get_required(self, data, key): 47 | 48 | try: 49 | val = data[key] 50 | except KeyError: 51 | raise YamlError("Value is needed for {}".format(key)) 52 | 53 | return val 54 | 55 | def _parse_drill_map(self, map_opts): 56 | 57 | mo = PC.DrillMapOptions() 58 | 59 | TYPES = { 60 | 'hpgl': pcbnew.PLOT_FORMAT_HPGL, 61 | 'ps': pcbnew.PLOT_FORMAT_POST, 62 | 'gerber': pcbnew.PLOT_FORMAT_GERBER, 63 | 'dxf': pcbnew.PLOT_FORMAT_DXF, 64 | 'svg': pcbnew.PLOT_FORMAT_SVG, 65 | 'pdf': pcbnew.PLOT_FORMAT_PDF 66 | } 67 | 68 | type_s = self._get_required(map_opts, 'type') 69 | 70 | try: 71 | mo.type = TYPES[type_s] 72 | except KeyError: 73 | raise YamlError("Unknown drill map type: {}".format(type_s)) 74 | 75 | return mo 76 | 77 | def _parse_drill_report(self, report_opts): 78 | 79 | opts = PC.DrillReportOptions() 80 | 81 | opts.filename = self._get_required(report_opts, 'filename') 82 | 83 | return opts 84 | 85 | def _perform_config_mapping(self, otype, cfg_options, mapping_list, 86 | target): 87 | """ 88 | Map a config dict onto a target object given a mapping list 89 | """ 90 | 91 | for mapping in mapping_list: 92 | 93 | # if this output type matches the mapping specification: 94 | if otype in mapping['types']: 95 | 96 | key = mapping['key'] 97 | 98 | # set the internal option as needed 99 | if mapping['required'](cfg_options): 100 | 101 | cfg_val = self._get_required(cfg_options, key) 102 | elif key in cfg_options: 103 | # not required but given anyway 104 | cfg_val = cfg_options[key] 105 | else: 106 | continue 107 | 108 | # transform the value if needed 109 | if 'transform' in mapping: 110 | cfg_val = mapping['transform'](cfg_val) 111 | 112 | setattr(target, mapping['to'], cfg_val) 113 | 114 | def _parse_out_opts(self, otype, options): 115 | 116 | # note - type IDs are strings form the _config_, not the internal 117 | # strings used as enums (in plot_config) 118 | ANY_LAYER = ['gerber', 'ps', 'svg', 'hpgl', 'pdf', 'dxf'] 119 | ANY_DRILL = ['excellon', 'gerb_drill'] 120 | 121 | # mappings from YAML keys to type_option keys 122 | MAPPINGS = [ 123 | { 124 | 'key': 'use_aux_axis_as_origin', 125 | 'types': ['gerber', 'dxf'], 126 | 'to': 'use_aux_axis_as_origin', 127 | 'required': lambda opts: True, 128 | }, 129 | { 130 | 'key': 'exclude_edge_layer', 131 | 'types': ANY_LAYER, 132 | 'to': 'exclude_edge_layer', 133 | 'required': lambda opts: True, 134 | }, 135 | { 136 | 'key': 'exclude_pads_from_silkscreen', 137 | 'types': ANY_LAYER, 138 | 'to': 'exclude_pads_from_silkscreen', 139 | 'required': lambda opts: True, 140 | }, 141 | { 142 | 'key': 'plot_sheet_reference', 143 | 'types': ANY_LAYER, 144 | 'to': 'plot_sheet_reference', 145 | 'required': lambda opts: True, 146 | }, 147 | { 148 | 'key': 'plot_footprint_refs', 149 | 'types': ANY_LAYER, 150 | 'to': 'plot_footprint_refs', 151 | 'required': lambda opts: True, 152 | }, 153 | { 154 | 'key': 'plot_footprint_values', 155 | 'types': ANY_LAYER, 156 | 'to': 'plot_footprint_values', 157 | 'required': lambda opts: True, 158 | }, 159 | { 160 | 'key': 'force_plot_invisible_refs_vals', 161 | 'types': ANY_LAYER, 162 | 'to': 'force_plot_invisible_refs_vals', 163 | 'required': lambda opts: True, 164 | }, 165 | { 166 | 'key': 'tent_vias', 167 | 'types': ANY_LAYER, 168 | 'to': 'tent_vias', 169 | 'required': lambda opts: True, 170 | }, 171 | { 172 | 'key': 'check_zone_fills', 173 | 'types': ANY_LAYER, 174 | 'to': 'check_zone_fills', 175 | 'required': lambda opts: True, 176 | }, 177 | { 178 | 'key': 'line_width', 179 | 'types': ['gerber', 'ps', 'svg', 'pdf'], 180 | 'to': 'line_width', 181 | 'required': lambda opts: True, 182 | }, 183 | { 184 | 'key': 'subtract_mask_from_silk', 185 | 'types': ['gerber'], 186 | 'to': 'subtract_mask_from_silk', 187 | 'required': lambda opts: True, 188 | }, 189 | { 190 | 'key': 'mirror_plot', 191 | 'types': ['ps', 'svg', 'hpgl', 'pdf'], 192 | 'to': 'mirror_plot', 193 | 'required': lambda opts: True, 194 | }, 195 | { 196 | 'key': 'negative_plot', 197 | 'types': ['ps', 'svg', 'pdf'], 198 | 'to': 'negative_plot', 199 | 'required': lambda opts: True, 200 | }, 201 | { 202 | 'key': 'sketch_plot', 203 | 'types': ['ps', 'hpgl'], 204 | 'to': 'sketch_plot', 205 | 'required': lambda opts: True, 206 | }, 207 | { 208 | 'key': 'scaling', 209 | 'types': ['ps', 'hpgl'], 210 | 'to': 'scaling', 211 | 'required': lambda opts: True, 212 | }, 213 | { 214 | 'key': 'drill_marks', 215 | 'types': ['ps', 'svg', 'dxf', 'hpgl', 'pdf'], 216 | 'to': 'drill_marks', 217 | 'required': lambda opts: True, 218 | }, 219 | { 220 | 'key': 'use_protel_extensions', 221 | 'types': ['gerber'], 222 | 'to': 'use_protel_extensions', 223 | 'required': lambda opts: True, 224 | }, 225 | { 226 | 'key': 'gerber_precision', 227 | 'types': ['gerber'], 228 | 'to': 'gerber_precision', 229 | 'required': lambda opts: True, 230 | }, 231 | { 232 | 'key': 'create_gerber_job_file', 233 | 'types': ['gerber'], 234 | 'to': 'create_gerber_job_file', 235 | 'required': lambda opts: True, 236 | }, 237 | { 238 | 'key': 'use_gerber_x2_attributes', 239 | 'types': ['gerber'], 240 | 'to': 'use_gerber_x2_attributes', 241 | 'required': lambda opts: True, 242 | }, 243 | { 244 | 'key': 'use_gerber_net_attributes', 245 | 'types': ['gerber'], 246 | 'to': 'use_gerber_net_attributes', 247 | 'required': lambda opts: True, 248 | }, 249 | { 250 | 'key': 'scale_adjust_x', 251 | 'types': ['ps'], 252 | 'to': 'scale_adjust_x', 253 | 'required': lambda opts: True, 254 | }, 255 | { 256 | 'key': 'scale_adjust_y', 257 | 'types': ['ps'], 258 | 'to': 'scale_adjust_y', 259 | 'required': lambda opts: True, 260 | }, 261 | { 262 | 'key': 'width_adjust', 263 | 'types': ['ps'], 264 | 'to': 'width_adjust', 265 | 'required': lambda opts: True, 266 | }, 267 | { 268 | 'key': 'a4_output', 269 | 'types': ['ps'], 270 | 'to': 'a4_output', 271 | 'required': lambda opts: True, 272 | }, 273 | { 274 | 'key': 'pen_width', 275 | 'types': ['hpgl'], 276 | 'to': 'pen_width', 277 | 'required': lambda opts: True, 278 | }, 279 | { 280 | 'key': 'polygon_mode', 281 | 'types': ['dxf'], 282 | 'to': 'polygon_mode', 283 | 'required': lambda opts: True, 284 | }, 285 | { 286 | 'key': 'use_aux_axis_as_origin', 287 | 'types': ANY_DRILL, 288 | 'to': 'use_aux_axis_as_origin', 289 | 'required': lambda opts: True, 290 | }, 291 | { 292 | 'key': 'map', 293 | 'types': ANY_DRILL, 294 | 'to': 'map_options', 295 | 'required': lambda opts: False, 296 | 'transform': self._parse_drill_map 297 | }, 298 | { 299 | 'key': 'report', 300 | 'types': ANY_DRILL, 301 | 'to': 'report_options', 302 | 'required': lambda opts: False, 303 | 'transform': self._parse_drill_report 304 | }, 305 | { 306 | 'key': 'metric_units', 307 | 'types': ['excellon'], 308 | 'to': 'metric_units', 309 | 'required': lambda opts: True, 310 | }, 311 | { 312 | 'key': 'pth_and_npth_single_file', 313 | 'types': ['excellon'], 314 | 'to': 'pth_and_npth_single_file', 315 | 'required': lambda opts: True, 316 | }, 317 | { 318 | 'key': 'minimal_header', 319 | 'types': ['excellon'], 320 | 'to': 'minimal_header', 321 | 'required': lambda opts: True, 322 | }, 323 | { 324 | 'key': 'mirror_y_axis', 325 | 'types': ['excellon'], 326 | 'to': 'mirror_y_axis', 327 | 'required': lambda opts: True, 328 | }, 329 | ] 330 | 331 | po = PC.OutputOptions(otype) 332 | 333 | # options that apply to the specific output type 334 | to = po.type_options 335 | 336 | self._perform_config_mapping(otype, options, MAPPINGS, to) 337 | 338 | return po 339 | 340 | def _get_layer_from_str(self, s): 341 | """ 342 | Get the pcbnew layer from a string in the config 343 | """ 344 | 345 | D = { 346 | 'F.Cu': pcbnew.F_Cu, 347 | 'B.Cu': pcbnew.B_Cu, 348 | 'F.Adhes': pcbnew.F_Adhes, 349 | 'B.Adhes': pcbnew.B_Adhes, 350 | 'F.Paste': pcbnew.F_Paste, 351 | 'B.Paste': pcbnew.B_Paste, 352 | 'F.SilkS': pcbnew.F_SilkS, 353 | 'B.SilkS': pcbnew.B_SilkS, 354 | 'F.Mask': pcbnew.F_Mask, 355 | 'B.Mask': pcbnew.B_Mask, 356 | 'Dwgs.User': pcbnew.Dwgs_User, 357 | 'Cmts.User': pcbnew.Cmts_User, 358 | 'Eco1.User': pcbnew.Eco1_User, 359 | 'Eco2.User': pcbnew.Eco2_User, 360 | 'Edge.Cuts': pcbnew.Edge_Cuts, 361 | 'Margin': pcbnew.Margin, 362 | 'F.CrtYd': pcbnew.F_CrtYd, 363 | 'B.CrtYd': pcbnew.B_CrtYd, 364 | 'F.Fab': pcbnew.F_Fab, 365 | 'B.Fab': pcbnew.B_Fab, 366 | } 367 | 368 | layer = None 369 | 370 | if s in D: 371 | layer = PC.LayerInfo(D[s], False) 372 | elif s.startswith("Inner"): 373 | m = re.match(r"^Inner\.([0-9]+)$", s) 374 | 375 | if not m: 376 | raise YamlError("Malformed inner layer name: {}" 377 | .format(s)) 378 | 379 | layer = PC.LayerInfo(int(m.group(1)), True) 380 | else: 381 | raise YamlError("Unknown layer name: {}".format(s)) 382 | 383 | return layer 384 | 385 | def _parse_layer(self, l_obj): 386 | 387 | l_str = self._get_required(l_obj, 'layer') 388 | layer_id = self._get_layer_from_str(l_str) 389 | layer = PC.LayerConfig(layer_id) 390 | 391 | layer.desc = l_obj['description'] if 'description' in l_obj else None 392 | layer.suffix = l_obj['suffix'] if 'suffix' in l_obj else "" 393 | 394 | return layer 395 | 396 | def _parse_output(self, o_obj): 397 | 398 | try: 399 | name = o_obj['name'] 400 | except KeyError: 401 | raise self.YamlError("Output needs a name") 402 | 403 | try: 404 | desc = o_obj['description'] 405 | except KeyError: 406 | desc = None 407 | 408 | try: 409 | otype = o_obj['type'] 410 | except KeyError: 411 | raise YamlError("Output needs a type") 412 | 413 | if otype not in ['gerber', 'ps', 'hpgl', 'dxf', 'pdf', 'svg', 414 | 'gerb_drill', 'excellon']: 415 | raise YamlError("Unknown output type: {}".format(otype)) 416 | 417 | try: 418 | options = o_obj['options'] 419 | except KeyError: 420 | raise YamlError("Output need to have options specified") 421 | 422 | logging.debug("Parsing output options for {} ({})".format(name, otype)) 423 | 424 | outdir = self._get_required(o_obj, 'dir') 425 | 426 | output_opts = self._parse_out_opts(otype, options) 427 | 428 | o_cfg = PC.PlotOutput(name, desc, otype, output_opts) 429 | o_cfg.outdir = outdir 430 | 431 | try: 432 | layers = o_obj['layers'] 433 | except KeyError: 434 | layers = [] 435 | 436 | for l in layers: 437 | o_cfg.layers.append(self._parse_layer(l)) 438 | 439 | return o_cfg 440 | 441 | def _parse_preflight(self, pf, cfg): 442 | 443 | logging.debug("Parsing preflight options: {}".format(pf)) 444 | 445 | if 'check_zone_fills' in pf: 446 | cfg.check_zone_fills = pf['check_zone_fills'] 447 | 448 | if 'run_drc' in pf: 449 | cfg.run_drc = pf['run_drc'] 450 | 451 | def read(self, fstream): 452 | """ 453 | Read a file object into a config object 454 | 455 | :param fstream: file stream of a config YAML file 456 | """ 457 | 458 | try: 459 | data = yaml.load(fstream) 460 | except yaml.YAMLError as e: 461 | raise YamlError("Error loading YAML") 462 | return None 463 | 464 | self._check_version(data) 465 | 466 | cfg = PC.PlotConfig() 467 | 468 | if 'preflight' in data: 469 | self._parse_preflight(data['preflight'], cfg) 470 | 471 | for o in data['outputs']: 472 | 473 | op_cfg = self._parse_output(o) 474 | cfg.add_output(op_cfg) 475 | 476 | return cfg 477 | -------------------------------------------------------------------------------- /src/kiplot/error.py: -------------------------------------------------------------------------------- 1 | """ 2 | KiPlot errors 3 | """ 4 | 5 | 6 | class KiPlotError(Exception): 7 | pass 8 | -------------------------------------------------------------------------------- /src/kiplot/kiplot.py: -------------------------------------------------------------------------------- 1 | """ 2 | Main Kiplot code 3 | """ 4 | 5 | import logging 6 | import os 7 | 8 | from . import plot_config as PCfg 9 | from . import error 10 | 11 | try: 12 | import pcbnew 13 | except ImportError: 14 | logging.error("Failed to import pcbnew Python module." 15 | " Do you need to add it to PYTHONPATH?") 16 | raise 17 | 18 | 19 | class PlotError(error.KiPlotError): 20 | pass 21 | 22 | 23 | class Plotter(object): 24 | """ 25 | Main Plotter class - this is what will perform the plotting 26 | """ 27 | 28 | def __init__(self, cfg): 29 | self.cfg = cfg 30 | 31 | def plot(self, brd_file): 32 | 33 | logging.debug("Starting plot of board {}".format(brd_file)) 34 | 35 | board = pcbnew.LoadBoard(brd_file) 36 | 37 | logging.debug("Board loaded") 38 | 39 | self._preflight_checks(board) 40 | 41 | for op in self.cfg.outputs: 42 | 43 | logging.debug("Processing output: {}".format(op.name)) 44 | 45 | # fresh plot controller 46 | pc = pcbnew.PLOT_CONTROLLER(board) 47 | 48 | self._configure_output_dir(pc, op) 49 | 50 | if self._output_is_layer(op): 51 | self._do_layer_plot(board, pc, op) 52 | elif self._output_is_drill(op): 53 | self._do_drill_plot(board, pc, op) 54 | else: 55 | raise PlotError("Don't know how to plot type {}" 56 | .format(op.options.type)) 57 | 58 | pc.ClosePlot() 59 | 60 | def _preflight_checks(self, board): 61 | 62 | logging.debug("Preflight checks") 63 | 64 | if self.cfg.check_zone_fills: 65 | raise PlotError("Not sure if Python scripts can do zone check!") 66 | 67 | if self.cfg.run_drc: 68 | raise PlotError("Not sure if Python scripts can run DRC!") 69 | 70 | def _output_is_layer(self, output): 71 | 72 | return output.options.type in [ 73 | PCfg.OutputOptions.GERBER, 74 | PCfg.OutputOptions.POSTSCRIPT, 75 | PCfg.OutputOptions.DXF, 76 | PCfg.OutputOptions.SVG, 77 | PCfg.OutputOptions.PDF, 78 | PCfg.OutputOptions.HPGL, 79 | ] 80 | 81 | def _output_is_drill(self, output): 82 | 83 | return output.options.type in [ 84 | PCfg.OutputOptions.EXCELLON, 85 | PCfg.OutputOptions.GERB_DRILL, 86 | ] 87 | 88 | def _get_layer_plot_format(self, output): 89 | """ 90 | Gets the Pcbnew plot format for a given KiPlot output type 91 | """ 92 | 93 | mapping = { 94 | PCfg.OutputOptions.GERBER: pcbnew.PLOT_FORMAT_GERBER, 95 | PCfg.OutputOptions.POSTSCRIPT: pcbnew.PLOT_FORMAT_POST, 96 | PCfg.OutputOptions.HPGL: pcbnew.PLOT_FORMAT_HPGL, 97 | PCfg.OutputOptions.PDF: pcbnew.PLOT_FORMAT_PDF, 98 | PCfg.OutputOptions.DXF: pcbnew.PLOT_FORMAT_DXF, 99 | PCfg.OutputOptions.SVG: pcbnew.PLOT_FORMAT_SVG, 100 | } 101 | 102 | try: 103 | return mapping[output.options.type] 104 | except KeyError: 105 | pass 106 | 107 | raise ValueError("Don't know how to translate plot type: {}" 108 | .format(output.options.type)) 109 | 110 | def _do_layer_plot(self, board, plot_ctrl, output): 111 | 112 | # set up plot options for the whole output 113 | self._configure_plot_ctrl(plot_ctrl, output) 114 | 115 | po = plot_ctrl.GetPlotOptions() 116 | layer_cnt = board.GetCopperLayerCount() 117 | 118 | # plot every layer in the output 119 | for l in output.layers: 120 | 121 | layer = l.layer 122 | suffix = l.suffix 123 | desc = l.desc 124 | 125 | # for inner layers, we can now check if the layer exists 126 | if layer.is_inner: 127 | 128 | if layer.layer < 1 or layer.layer >= layer_cnt - 1: 129 | raise PlotError( 130 | "Inner layer {} is not valid for this board" 131 | .format(layer.layer)) 132 | 133 | # Set current layer 134 | plot_ctrl.SetLayer(layer.layer) 135 | 136 | # Skipping NPTH is controlled by whether or not this is 137 | # a copper layer 138 | is_cu = pcbnew.IsCopperLayer(layer.layer) 139 | po.SetSkipPlotNPTH_Pads(is_cu) 140 | 141 | plot_format = self._get_layer_plot_format(output) 142 | 143 | # Plot single layer to file 144 | logging.debug("Opening plot file for layer {} ({})" 145 | .format(layer.layer, suffix)) 146 | plot_ctrl.OpenPlotfile(suffix, plot_format, desc) 147 | 148 | logging.debug("Plotting layer {} to {}".format( 149 | layer.layer, plot_ctrl.GetPlotFileName())) 150 | plot_ctrl.PlotLayer() 151 | 152 | def _configure_excellon_drill_writer(self, board, offset, options): 153 | 154 | drill_writer = pcbnew.EXCELLON_WRITER(board) 155 | 156 | to = options.type_options 157 | 158 | mirror_y = to.mirror_y_axis 159 | minimal_header = to.minimal_header 160 | 161 | merge_npth = to.pth_and_npth_single_file 162 | zeros_format = pcbnew.EXCELLON_WRITER.DECIMAL_FORMAT 163 | 164 | drill_writer.SetOptions(mirror_y, minimal_header, offset, merge_npth) 165 | drill_writer.SetFormat(to.metric_units, zeros_format) 166 | 167 | return drill_writer 168 | 169 | def _configure_gerber_drill_writer(self, board, offset, options): 170 | 171 | drill_writer = pcbnew.GERBER_WRITER(board) 172 | 173 | # hard coded in UI? 174 | drill_writer.SetFormat(5) 175 | drill_writer.SetOptions(offset) 176 | 177 | return drill_writer 178 | 179 | def _do_drill_plot(self, board, plot_ctrl, output): 180 | 181 | to = output.options.type_options 182 | 183 | outdir = plot_ctrl.GetPlotOptions().GetOutputDirectory() 184 | 185 | # dialog_gendrill.cpp:357 186 | if to.use_aux_axis_as_origin: 187 | offset = board.GetAuxOrigin() 188 | else: 189 | offset = pcbnew.wxPoint(0, 0) 190 | 191 | if output.options.type == PCfg.OutputOptions.EXCELLON: 192 | drill_writer = self._configure_excellon_drill_writer( 193 | board, offset, output.options) 194 | elif output.options.type == PCfg.OutputOptions.GERB_DRILL: 195 | drill_writer = self._configure_gerber_drill_writer( 196 | board, offset, output.options) 197 | else: 198 | raise error.PlotError("Can't make a writer for type {}" 199 | .format(output.options.type)) 200 | 201 | gen_drill = True 202 | gen_map = to.generate_map 203 | gen_report = to.generate_report 204 | 205 | if gen_drill: 206 | logging.debug("Generating drill files in {}" 207 | .format(outdir)) 208 | 209 | if gen_map: 210 | drill_writer.SetMapFileFormat(to.map_options.type) 211 | logging.debug("Generating drill map type {} in {}" 212 | .format(to.map_options.type, outdir)) 213 | 214 | drill_writer.CreateDrillandMapFilesSet(outdir, gen_drill, gen_map) 215 | 216 | if gen_report: 217 | drill_report_file = os.path.join(outdir, 218 | to.report_options.filename) 219 | logging.debug("Generating drill report: {}" 220 | .format(drill_report_file)) 221 | 222 | drill_writer.GenDrillReportFile(drill_report_file) 223 | 224 | def _configure_gerber_opts(self, po, output): 225 | 226 | # true if gerber 227 | po.SetUseGerberAttributes(True) 228 | 229 | assert(output.options.type == PCfg.OutputOptions.GERBER) 230 | gerb_opts = output.options.type_options 231 | 232 | po.SetSubtractMaskFromSilk(gerb_opts.subtract_mask_from_silk) 233 | po.SetUseGerberProtelExtensions(gerb_opts.use_protel_extensions) 234 | po.SetGerberPrecision(gerb_opts.gerber_precision) 235 | po.SetCreateGerberJobFile(gerb_opts.create_gerber_job_file) 236 | 237 | po.SetUseGerberAttributes(gerb_opts.use_gerber_x2_attributes) 238 | po.SetIncludeGerberNetlistInfo(gerb_opts.use_gerber_net_attributes) 239 | 240 | def _configure_hpgl_opts(self, po, output): 241 | 242 | assert(output.options.type == PCfg.OutputOptions.HPGL) 243 | hpgl_opts = output.options.type_options 244 | 245 | po.SetHPGLPenDiameter(hpgl_opts.pen_width) 246 | 247 | def _configure_ps_opts(self, po, output): 248 | 249 | assert(output.options.type == PCfg.OutputOptions.POSTSCRIPT) 250 | ps_opts = output.options.type_options 251 | 252 | po.SetWidthAdjust(ps_opts.width_adjust) 253 | po.SetFineScaleAdjustX(ps_opts.scale_adjust_x) 254 | po.SetFineScaleAdjustX(ps_opts.scale_adjust_y) 255 | po.SetA4Output(ps_opts.a4_output) 256 | 257 | def _configure_dxf_opts(self, po, output): 258 | 259 | assert(output.options.type == PCfg.OutputOptions.DXF) 260 | dxf_opts = output.options.type_options 261 | 262 | po.SetDXFPlotPolygonMode(dxf_opts.polygon_mode) 263 | 264 | def _configure_pdf_opts(self, po, output): 265 | 266 | assert(output.options.type == PCfg.OutputOptions.PDF) 267 | # pdf_opts = output.options.type_options 268 | 269 | def _configure_svg_opts(self, po, output): 270 | 271 | assert(output.options.type == PCfg.OutputOptions.SVG) 272 | # pdf_opts = output.options.type_options 273 | 274 | def _configure_output_dir(self, plot_ctrl, output): 275 | 276 | po = plot_ctrl.GetPlotOptions() 277 | 278 | # outdir is a combination of the config and output 279 | outdir = os.path.join(self.cfg.outdir, output.outdir) 280 | 281 | logging.debug("Output destination: {}".format(outdir)) 282 | 283 | po.SetOutputDirectory(outdir) 284 | 285 | def _configure_plot_ctrl(self, plot_ctrl, output): 286 | 287 | logging.debug("Configuring plot controller for output") 288 | 289 | po = plot_ctrl.GetPlotOptions() 290 | 291 | opts = output.options.type_options 292 | 293 | po.SetLineWidth(opts.line_width) 294 | 295 | po.SetAutoScale(opts.auto_scale) 296 | po.SetScale(opts.scaling) 297 | 298 | po.SetMirror(opts.mirror_plot) 299 | po.SetNegative(opts.negative_plot) 300 | 301 | po.SetPlotFrameRef(opts.plot_sheet_reference) 302 | po.SetPlotReference(opts.plot_footprint_refs) 303 | po.SetPlotValue(opts.plot_footprint_values) 304 | po.SetPlotInvisibleText(opts.force_plot_invisible_refs_vals) 305 | 306 | po.SetExcludeEdgeLayer(opts.exclude_edge_layer) 307 | po.SetPlotPadsOnSilkLayer(not opts.exclude_pads_from_silkscreen) 308 | po.SetUseAuxOrigin(opts.use_aux_axis_as_origin) 309 | 310 | po.SetPlotViaOnMaskLayer(not opts.tent_vias) 311 | 312 | # in general, false, but gerber will set it back later 313 | po.SetUseGerberAttributes(False) 314 | 315 | if output.options.type == PCfg.OutputOptions.GERBER: 316 | self._configure_gerber_opts(po, output) 317 | elif output.options.type == PCfg.OutputOptions.POSTSCRIPT: 318 | self._configure_ps_opts(po, output) 319 | elif output.options.type == PCfg.OutputOptions.DXF: 320 | self._configure_dxf_opts(po, output) 321 | elif output.options.type == PCfg.OutputOptions.SVG: 322 | self._configure_svg_opts(po, output) 323 | elif output.options.type == PCfg.OutputOptions.PDF: 324 | self._configure_pdf_opts(po, output) 325 | elif output.options.type == PCfg.OutputOptions.HPGL: 326 | self._configure_hpgl_opts(po, output) 327 | 328 | po.SetDrillMarksType(opts.drill_marks) 329 | 330 | # We'll come back to this on a per-layer basis 331 | po.SetSkipPlotNPTH_Pads(False) 332 | -------------------------------------------------------------------------------- /src/kiplot/plot_config.py: -------------------------------------------------------------------------------- 1 | 2 | import os 3 | 4 | import pcbnew 5 | 6 | from . import error 7 | 8 | 9 | class KiPlotConfigurationError(error.KiPlotError): 10 | pass 11 | 12 | 13 | class TypeOptions(object): 14 | 15 | def validate(self): 16 | """ 17 | Return list of invalid settings 18 | """ 19 | 20 | return [] 21 | 22 | 23 | class LayerOptions(TypeOptions): 24 | """ 25 | Common options that all layer outputs have 26 | """ 27 | 28 | AUTO_SCALE = 0 29 | 30 | def __init__(self): 31 | 32 | super(LayerOptions, self).__init__() 33 | 34 | self.exclude_edge_layer = False 35 | self.exclude_pads_from_silkscreen = False 36 | self.plot_sheet_reference = False 37 | 38 | self._supports_line_width = False 39 | self._line_width = 0 40 | 41 | self._supports_aux_axis_origin = False 42 | self._use_aux_axis_as_origin = False 43 | 44 | # override for scalable formats 45 | self._supports_scaling = False 46 | 47 | self._auto_scale = False 48 | self._scaling = 1 49 | 50 | self._supports_mirror = False 51 | self._mirror_plot = False 52 | 53 | self._supports_negative = False 54 | self._negative_plot = False 55 | 56 | self._supports_drill_marks = False 57 | self._drill_marks = pcbnew.PCB_PLOT_PARAMS.NO_DRILL_SHAPE 58 | 59 | self._support_sketch_mode = False 60 | self._sketch_mode = False 61 | 62 | @property 63 | def line_width(self): 64 | return self._line_width 65 | 66 | @line_width.setter 67 | def line_width(self, value): 68 | """ 69 | Set the line width, in mm 70 | """ 71 | if self._supports_line_width: 72 | self._line_width = pcbnew.FromMM(value) 73 | else: 74 | raise KiPlotConfigurationError( 75 | "This output doesn't support setting line width") 76 | 77 | @property 78 | def auto_scale(self): 79 | return self._auto_scale 80 | 81 | @property 82 | def scaling(self): 83 | return self._scaling 84 | 85 | @scaling.setter 86 | def scaling(self, val): 87 | """ 88 | Set scaling, if possible. AUTO_SCALE to set auto scaling 89 | """ 90 | 91 | if self._supports_scaling: 92 | 93 | if val == self.AUTO_SCALE: 94 | self._scaling = 1 95 | self._auto_scale = True 96 | else: 97 | self._scaling = val 98 | self._auto_scale = False 99 | else: 100 | raise KiPlotConfigurationError( 101 | "This Layer output does not support scaling") 102 | 103 | @property 104 | def mirror_plot(self): 105 | return self._mirror_plot 106 | 107 | @mirror_plot.setter 108 | def mirror_plot(self, val): 109 | 110 | if self._supports_mirror: 111 | self._mirror_plot = val 112 | else: 113 | raise KiPlotConfigurationError( 114 | "This Layer output does not support mirror plotting") 115 | 116 | @property 117 | def negative_plot(self): 118 | return self._negative_plot 119 | 120 | @negative_plot.setter 121 | def negative_plot(self, val): 122 | 123 | if self._supports_mirror: 124 | self._negative_plot = val 125 | else: 126 | raise KiPlotConfigurationError( 127 | "This Layer output does not support negative plotting") 128 | 129 | @property 130 | def drill_marks(self): 131 | return self._drill_marks 132 | 133 | @drill_marks.setter 134 | def drill_marks(self, val): 135 | 136 | if self._supports_drill_marks: 137 | 138 | try: 139 | drill_mark = { 140 | 'none': pcbnew.PCB_PLOT_PARAMS.NO_DRILL_SHAPE, 141 | 'small': pcbnew.PCB_PLOT_PARAMS.SMALL_DRILL_SHAPE, 142 | 'full': pcbnew.PCB_PLOT_PARAMS.FULL_DRILL_SHAPE, 143 | }[val] 144 | except KeyError: 145 | raise KiPlotConfigurationError( 146 | "Unknown drill mark type: {}".format(val)) 147 | 148 | self._drill_marks = drill_mark 149 | else: 150 | raise KiPlotConfigurationError( 151 | "This Layer output does not support drill marks") 152 | 153 | @property 154 | def use_aux_axis_as_origin(self): 155 | return self._use_aux_axis_as_origin 156 | 157 | @use_aux_axis_as_origin.setter 158 | def use_aux_axis_as_origin(self, val): 159 | 160 | if self._supports_aux_axis_origin: 161 | self._use_aux_axis_as_origin = val 162 | else: 163 | raise KiPlotConfigurationError( 164 | "This Layer output does not support using the auxiliary" 165 | " axis as the origin") 166 | 167 | @property 168 | def sketch_mode(self): 169 | return self._sketch_mode 170 | 171 | @sketch_mode.setter 172 | def sketch_mode(self, val): 173 | 174 | if self._supports_sketch_mode: 175 | self._sketch_mode = val 176 | else: 177 | raise KiPlotConfigurationError( 178 | "This Layer output does not support sketch mode") 179 | 180 | 181 | class GerberOptions(LayerOptions): 182 | 183 | def __init__(self): 184 | 185 | super(GerberOptions, self).__init__() 186 | 187 | self._supports_line_width = True 188 | self._supports_aux_axis_origin = True 189 | 190 | self.subtract_mask_from_silk = False 191 | self.use_protel_extensions = False 192 | self.create_gerber_job_file = False 193 | self.use_gerber_x2_attributes = False 194 | self.use_gerber_net_attributes = False 195 | 196 | # either 5 or 6 197 | self._gerber_precision = None 198 | 199 | def validate(self): 200 | 201 | errs = super(GerberOptions, self).validate() 202 | 203 | if (not self.use_gerber_x2_attributes and 204 | self.use_gerber_net_attributes): 205 | errs.append("Must set Gerber X2 attributes to use net attributes") 206 | 207 | return errs 208 | 209 | @property 210 | def gerber_precision(self): 211 | return self._gerber_precision 212 | 213 | @gerber_precision.setter 214 | def gerber_precision(self, val): 215 | """ 216 | Set gerber precision: either 4.5 or 4.6 217 | """ 218 | if val == 4.5: 219 | self._gerber_precision = 5 220 | elif val == 4.6: 221 | self._gerber_precision = 6 222 | else: 223 | raise KiPlotConfigurationError( 224 | "Bad Gerber precision : {}".format(val)) 225 | 226 | 227 | class HpglOptions(LayerOptions): 228 | 229 | def __init__(self): 230 | 231 | super(HpglOptions, self).__init__() 232 | 233 | self._supports_sketch_mode = True 234 | self._supports_mirror = True 235 | self._supports_scaling = True 236 | self._supports_drill_marks = True 237 | 238 | self._pen_width = None 239 | 240 | @property 241 | def pen_width(self): 242 | return self._pen_width 243 | 244 | @pen_width.setter 245 | def pen_width(self, pw_mm): 246 | self._pen_width = pcbnew.FromMM(pw_mm) 247 | 248 | 249 | class PsOptions(LayerOptions): 250 | 251 | def __init__(self): 252 | 253 | super(PsOptions, self).__init__() 254 | 255 | self._supports_mirror = True 256 | self._supports_negative = True 257 | self._supports_scaling = True 258 | self._supports_drill_marks = True 259 | self._supports_line_width = True 260 | self._supports_sketch_mode = True 261 | 262 | self.scale_adjust_x = 1.0 263 | self.scale_adjust_y = 1.0 264 | 265 | self._width_adjust = 0 266 | 267 | self.a4_output = False 268 | 269 | @property 270 | def width_adjust(self): 271 | return self._width_adjust 272 | 273 | @width_adjust.setter 274 | def width_adjust(self, width_adjust_mm): 275 | self._width_adjust = pcbnew.FromMM(width_adjust_mm) 276 | 277 | 278 | class SvgOptions(LayerOptions): 279 | 280 | def __init__(self): 281 | 282 | super(SvgOptions, self).__init__() 283 | 284 | self._supports_line_width = True 285 | self._supports_mirror = True 286 | self._supports_negative = True 287 | self._supports_drill_marks = True 288 | 289 | 290 | class PdfOptions(LayerOptions): 291 | 292 | def __init__(self): 293 | 294 | super(PdfOptions, self).__init__() 295 | 296 | self._supports_line_width = True 297 | self._supports_mirror = True 298 | self._supports_negative = True 299 | self._supports_drill_marks = True 300 | 301 | 302 | class DxfOptions(LayerOptions): 303 | 304 | def __init__(self): 305 | 306 | super(DxfOptions, self).__init__() 307 | 308 | self._supports_aux_axis_origin = True 309 | self._supports_drill_marks = True 310 | 311 | self.polygon_mode = False 312 | 313 | 314 | class DrillOptions(TypeOptions): 315 | 316 | def __init__(self): 317 | 318 | super(DrillOptions, self).__init__() 319 | 320 | self.use_aux_axis_as_origin = False 321 | 322 | self.map_options = None 323 | self.report_options = None 324 | 325 | @property 326 | def generate_map(self): 327 | return self.map_options is not None 328 | 329 | @property 330 | def generate_report(self): 331 | return self.report_options is not None 332 | 333 | 334 | class ExcellonOptions(DrillOptions): 335 | 336 | def __init__(self): 337 | 338 | super(ExcellonOptions, self).__init__() 339 | 340 | self.metric_units = True 341 | self.minimal_header = False 342 | self.mirror_y_axis = False 343 | 344 | 345 | class GerberDrillOptions(DrillOptions): 346 | 347 | def __init__(self): 348 | 349 | super(GerberDrillOptions, self).__init__() 350 | 351 | 352 | class DrillReportOptions(object): 353 | 354 | def __init__(self): 355 | self.filename = None 356 | 357 | 358 | class DrillMapOptions(object): 359 | 360 | def __init__(self): 361 | self.type = None 362 | 363 | 364 | class OutputOptions(object): 365 | 366 | GERBER = 'gerber' 367 | POSTSCRIPT = 'ps' 368 | HPGL = 'hpgl' 369 | SVG = 'svg' 370 | PDF = 'pdf' 371 | DXF = 'dxf' 372 | 373 | EXCELLON = 'excellon' 374 | GERB_DRILL = 'gerb_drill' 375 | 376 | def __init__(self, otype): 377 | self.type = otype 378 | 379 | if otype == self.GERBER: 380 | self.type_options = GerberOptions() 381 | elif otype == self.POSTSCRIPT: 382 | self.type_options = PsOptions() 383 | elif otype == self.HPGL: 384 | self.type_options = HpglOptions() 385 | elif otype == self.SVG: 386 | self.type_options = SvgOptions() 387 | elif otype == self.DXF: 388 | self.type_options = DxfOptions() 389 | elif otype == self.PDF: 390 | self.type_options = PdfOptions() 391 | elif otype == self.EXCELLON: 392 | self.type_options = ExcellonOptions() 393 | elif otype == self.GERB_DRILL: 394 | self.type_options = GerberDrillOptions() 395 | else: 396 | self.type_options = None 397 | 398 | def validate(self): 399 | 400 | if self.type_options is None: 401 | return ["No type specific options found"] 402 | 403 | return self.type_options.validate() 404 | 405 | 406 | class LayerInfo(object): 407 | 408 | def __init__(self, layer, is_inner): 409 | 410 | self.layer = layer 411 | self.is_inner = is_inner 412 | 413 | 414 | class LayerConfig(object): 415 | 416 | def __init__(self, layer): 417 | 418 | # the Pcbnew layer 419 | self.layer = layer 420 | self.suffix = "" 421 | self.desc = "desc" 422 | 423 | 424 | class PlotOutput(object): 425 | 426 | def __init__(self, name, description, otype, options): 427 | self.name = name 428 | self.description = description 429 | self.outdir = None 430 | self.options = options 431 | 432 | self.layers = [] 433 | 434 | def validate(self): 435 | return self.options.validate() 436 | 437 | 438 | class PlotConfig(object): 439 | 440 | def __init__(self): 441 | 442 | self._outputs = [] 443 | self.outdir = None 444 | 445 | self.check_zone_fills = False 446 | self.run_drc = False 447 | 448 | def add_output(self, new_op): 449 | self._outputs.append(new_op) 450 | 451 | def get_output_by_name(self, output_name): 452 | """ 453 | Gets an output with a given name. 454 | 455 | @param output_name the name of the output to find 456 | """ 457 | for o in self.outputs: 458 | 459 | if o.name == output_name: 460 | return o 461 | 462 | return None 463 | 464 | def resolve_output_dir_for_name(self, output_name): 465 | """ 466 | Get the output dir for a given output name 467 | """ 468 | 469 | o = self.get_output_by_name(output_name) 470 | return os.path.join(self.outdir, o.outdir) if o else None 471 | 472 | def validate(self): 473 | 474 | errs = [] 475 | 476 | for o in self._outputs: 477 | errs += o.validate() 478 | 479 | return errs 480 | 481 | @property 482 | def outputs(self): 483 | return self._outputs 484 | -------------------------------------------------------------------------------- /tests/board_samples/simple_2layer.kicad_pcb: -------------------------------------------------------------------------------- 1 | (kicad_pcb (version 20171130) (host pcbnew "(5.0.0-rc2-76-gb5f63567d)") 2 | 3 | (general 4 | (thickness 1.6) 5 | (drawings 5) 6 | (tracks 4) 7 | (zones 0) 8 | (modules 1) 9 | (nets 1) 10 | ) 11 | 12 | (page A4) 13 | (title_block 14 | (title "Simple Plotting Test") 15 | (date 2018-06-04) 16 | (rev A) 17 | (company "KiPlot - KiCad Plotting Driver") 18 | ) 19 | 20 | (layers 21 | (0 F.Cu signal) 22 | (31 B.Cu signal) 23 | (32 B.Adhes user) 24 | (33 F.Adhes user) 25 | (34 B.Paste user) 26 | (35 F.Paste user) 27 | (36 B.SilkS user) 28 | (37 F.SilkS user) 29 | (38 B.Mask user) 30 | (39 F.Mask user) 31 | (40 Dwgs.User user) 32 | (41 Cmts.User user) 33 | (42 Eco1.User user) 34 | (43 Eco2.User user) 35 | (44 Edge.Cuts user) 36 | (45 Margin user) 37 | (46 B.CrtYd user) 38 | (47 F.CrtYd user) 39 | (48 B.Fab user) 40 | (49 F.Fab user) 41 | ) 42 | 43 | (setup 44 | (last_trace_width 0.25) 45 | (trace_clearance 0.2) 46 | (zone_clearance 0.508) 47 | (zone_45_only no) 48 | (trace_min 0.2) 49 | (segment_width 0.2) 50 | (edge_width 0.15) 51 | (via_size 0.8) 52 | (via_drill 0.4) 53 | (via_min_size 0.4) 54 | (via_min_drill 0.3) 55 | (uvia_size 0.3) 56 | (uvia_drill 0.1) 57 | (uvias_allowed no) 58 | (uvia_min_size 0.2) 59 | (uvia_min_drill 0.1) 60 | (pcb_text_width 0.3) 61 | (pcb_text_size 1.5 1.5) 62 | (mod_edge_width 0.15) 63 | (mod_text_size 1 1) 64 | (mod_text_width 0.15) 65 | (pad_size 1.524 1.524) 66 | (pad_drill 0.762) 67 | (pad_to_mask_clearance 0.2) 68 | (aux_axis_origin 0 0) 69 | (visible_elements FFFFFF7F) 70 | (pcbplotparams 71 | (layerselection 0x010fc_ffffffff) 72 | (usegerberextensions false) 73 | (usegerberattributes false) 74 | (usegerberadvancedattributes false) 75 | (creategerberjobfile false) 76 | (excludeedgelayer true) 77 | (linewidth 0.150000) 78 | (plotframeref false) 79 | (viasonmask false) 80 | (mode 1) 81 | (useauxorigin false) 82 | (hpglpennumber 1) 83 | (hpglpenspeed 20) 84 | (hpglpendiameter 15.000000) 85 | (psnegative false) 86 | (psa4output false) 87 | (plotreference true) 88 | (plotvalue true) 89 | (plotinvisibletext false) 90 | (padsonsilk false) 91 | (subtractmaskfromsilk false) 92 | (outputformat 1) 93 | (mirror false) 94 | (drillshape 1) 95 | (scaleselection 1) 96 | (outputdirectory "")) 97 | ) 98 | 99 | (net 0 "") 100 | 101 | (net_class Default "This is the default net class." 102 | (clearance 0.2) 103 | (trace_width 0.25) 104 | (via_dia 0.8) 105 | (via_drill 0.4) 106 | (uvia_dia 0.3) 107 | (uvia_drill 0.1) 108 | ) 109 | 110 | (module TestPoint:TestPoint_THTPad_2.0x2.0mm_Drill1.0mm (layer F.Cu) (tedit 5B1533F4) (tstamp 5B15541F) 111 | (at 140 100) 112 | (descr "THT rectangular pad as test Point, square 2.0mm_Drill1.0mm side length, hole diameter 1.0mm") 113 | (tags "test point THT pad rectangle square") 114 | (attr virtual) 115 | (fp_text reference TP1 (at 0 -2) (layer F.SilkS) 116 | (effects (font (size 1 1) (thickness 0.15))) 117 | ) 118 | (fp_text value TestPoint2mm (at 0 2.05) (layer F.Fab) 119 | (effects (font (size 1 1) (thickness 0.15))) 120 | ) 121 | (fp_line (start 1.5 1.5) (end -1.5 1.5) (layer F.CrtYd) (width 0.05)) 122 | (fp_line (start 1.5 1.5) (end 1.5 -1.5) (layer F.CrtYd) (width 0.05)) 123 | (fp_line (start -1.5 -1.5) (end -1.5 1.5) (layer F.CrtYd) (width 0.05)) 124 | (fp_line (start -1.5 -1.5) (end 1.5 -1.5) (layer F.CrtYd) (width 0.05)) 125 | (fp_line (start -1.2 1.2) (end -1.2 -1.2) (layer F.SilkS) (width 0.12)) 126 | (fp_line (start 1.2 1.2) (end -1.2 1.2) (layer F.SilkS) (width 0.12)) 127 | (fp_line (start 1.2 -1.2) (end 1.2 1.2) (layer F.SilkS) (width 0.12)) 128 | (fp_line (start -1.2 -1.2) (end 1.2 -1.2) (layer F.SilkS) (width 0.12)) 129 | (fp_text user %R (at 0 -2) (layer F.Fab) 130 | (effects (font (size 1 1) (thickness 0.15))) 131 | ) 132 | (pad 1 thru_hole rect (at 0 0) (size 2 2) (drill 1) (layers *.Cu *.Mask)) 133 | ) 134 | 135 | (gr_arc (start 145 98) (end 148 98) (angle -90) (layer Edge.Cuts) (width 0.2)) 136 | (gr_line (start 145 95) (end 132 95) (layer Edge.Cuts) (width 0.2)) 137 | (gr_line (start 148 106) (end 148 98) (layer Edge.Cuts) (width 0.2)) 138 | (gr_line (start 132 106) (end 148 106) (layer Edge.Cuts) (width 0.2)) 139 | (gr_line (start 132 95) (end 132 106) (layer Edge.Cuts) (width 0.2)) 140 | 141 | (segment (start 140 100) (end 143 100) (width 1) (layer F.Cu) (net 0)) 142 | (segment (start 140 100) (end 142 102) (width 1) (layer B.Cu) (net 0)) 143 | (segment (start 142 102) (end 143 102) (width 1) (layer B.Cu) (net 0)) 144 | (segment (start 134 99) (end 134 101) (width 1) (layer F.Cu) (net 0)) 145 | 146 | ) 147 | -------------------------------------------------------------------------------- /tests/conftest.py: -------------------------------------------------------------------------------- 1 | """ 2 | Test configuration 3 | """ 4 | 5 | 6 | def pytest_addoption(parser): 7 | parser.addoption("--plot_dir", action="store", default=None, 8 | help="the plot dir to use (omit to use a temp dir). " 9 | "If given, plots will _not_ be cleared after testing.") 10 | -------------------------------------------------------------------------------- /tests/test_plot/README.md: -------------------------------------------------------------------------------- 1 | This directory contains tests for full testing of KiCad plots. 2 | This tests: 3 | 4 | * KiPlot's config parsing and driving of KiCad 5 | * KiCad's own plotting code 6 | 7 | Generally, boards are drawn from `../board_samples` and configs from 8 | `yaml_samples`. Sometimes, the YAML samples are modified by the test 9 | runners to avoid having hundreds of them! 10 | 11 | Bug that should be tested for: 12 | 13 | * https://bugs.launchpad.net/kicad/+bug/1775037 -------------------------------------------------------------------------------- /tests/test_plot/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/johnbeard/kiplot/f1c85dbdba44a417a0f42d5a209381e569764b78/tests/test_plot/__init__.py -------------------------------------------------------------------------------- /tests/test_plot/plotting_test_utils.py: -------------------------------------------------------------------------------- 1 | 2 | import os 3 | import shutil 4 | import tempfile 5 | import logging 6 | import pytest 7 | 8 | 9 | from kiplot import kiplot 10 | from kiplot import config_reader 11 | 12 | 13 | KICAD_PCB_EXT = '.kicad_pcb' 14 | 15 | 16 | class KiPlotTestContext(object): 17 | 18 | def __init__(self, test_name): 19 | self.cfg = None 20 | 21 | # The name used for the test output dirs and other logging 22 | self.test_name = test_name 23 | 24 | # The name of the PCB board file (will be interpolated into the plot 25 | # files by pcbnewm so we need to know 26 | self.board_name = None 27 | 28 | # The actual board file that will be loaded 29 | self.board_file = None 30 | 31 | # The directory under which to place plots (None: use a temp dir) 32 | self.plot_dir = pytest.config.getoption('plot_dir') 33 | 34 | # The actual output dir for this plot run 35 | self._output_dir = None 36 | # Clean the output dir afterwards (true for temp dirs) 37 | self._del_dir_after = self.plot_dir is None 38 | 39 | def _get_text_cfg_dir(self): 40 | 41 | this_dir = os.path.dirname(os.path.realpath(__file__)) 42 | 43 | return os.path.join(this_dir, '../yaml_samples') 44 | 45 | def _get_board_cfg_dir(self): 46 | 47 | this_dir = os.path.dirname(os.path.realpath(__file__)) 48 | 49 | return os.path.join(this_dir, '../board_samples') 50 | 51 | def load_yaml_config_file(self, filename): 52 | """ 53 | Reads a config from a YAML file 54 | """ 55 | 56 | cfg_file = os.path.join(self._get_text_cfg_dir(), filename) 57 | 58 | cr = config_reader.CfgYamlReader() 59 | 60 | with open(cfg_file) as cf_file: 61 | cfg = cr.read(cf_file) 62 | 63 | self.cfg = cfg 64 | 65 | def _load_board_file(self, filename=None): 66 | """ 67 | Load the named board. 68 | 69 | @param filename: a filename to load, or None to load the relevant 70 | board name from the board sample dir 71 | """ 72 | 73 | if filename is None: 74 | self.board_file = os.path.join(self._get_board_cfg_dir(), 75 | self.board_name + KICAD_PCB_EXT) 76 | else: 77 | self.board_file = filename 78 | 79 | assert os.path.isfile(self.board_file) 80 | 81 | def _set_up_output_dir(self): 82 | 83 | if not self.plot_dir: 84 | # create a tmp dir 85 | self.output_dir = tempfile.mkdtemp( 86 | prefix='tmp_kiplot_{}'.format(self.test_name)) 87 | 88 | else: 89 | self.output_dir = os.path.join(self.plot_dir, self.test_name) 90 | # just create the dir 91 | if os.path.isdir(self.output_dir): 92 | # exists, that's OK 93 | pass 94 | else: 95 | os.makedirs(self.output_dir) 96 | 97 | self.cfg.outdir = self.output_dir 98 | logging.info(self.output_dir) 99 | 100 | def clean_up(self): 101 | 102 | if self._del_dir_after: 103 | shutil.rmtree(self.output_dir) 104 | 105 | def do_plot(self): 106 | 107 | self.cfg.validate() 108 | 109 | self._load_board_file(self.board_file) 110 | 111 | self._set_up_output_dir() 112 | 113 | plotter = kiplot.Plotter(self.cfg) 114 | plotter.plot(self.board_file) 115 | -------------------------------------------------------------------------------- /tests/test_plot/test_simple_2layer.py: -------------------------------------------------------------------------------- 1 | """ 2 | Tests of simple 2-layer PCBs 3 | """ 4 | 5 | from . import plotting_test_utils 6 | 7 | import os 8 | import mmap 9 | import re 10 | import logging 11 | 12 | 13 | def expect_file_at(filename): 14 | 15 | assert(os.path.isfile(filename)) 16 | 17 | 18 | def get_gerber_filename(board_name, layer_slug, ext='.gbr'): 19 | return board_name + '-' + layer_slug + ext 20 | 21 | 22 | def find_gerber_aperture(s, ap_desc): 23 | 24 | m = re.search(r'%AD(.*)' + ap_desc + r'\*%', s) 25 | 26 | if not m: 27 | return None 28 | 29 | return m.group(1) 30 | 31 | 32 | def expect_gerber_has_apertures(gbr_data, ap_list): 33 | 34 | aps = [] 35 | 36 | for ap in ap_list: 37 | 38 | # find the circular aperture for the outline 39 | ap_no = find_gerber_aperture(gbr_data, ap) 40 | 41 | assert ap_no is not None 42 | 43 | # apertures from D10 to D999 44 | assert len(ap_no) in [2, 3] 45 | 46 | aps.append(ap_no) 47 | 48 | logging.debug("Found apertures {}".format(aps)) 49 | return aps 50 | 51 | 52 | def expect_gerber_flash_at(gbr_data, pos): 53 | """ 54 | Check for a gerber flash at a given point 55 | (it's hard to check that aperture is right without a real gerber parser 56 | """ 57 | 58 | repat = r'^X{x}Y{y}D03\*$'.format( 59 | x=int(pos[0] * 100000), 60 | y=int(pos[1] * 100000) 61 | ) 62 | 63 | m = re.search(repat, gbr_data, re.MULTILINE) 64 | 65 | assert(m) 66 | 67 | logging.debug("Gerber flash found: " + repat) 68 | 69 | 70 | def get_mmapped_data(filename): 71 | 72 | with open(filename) as fo: 73 | return mmap.mmap(fo.fileno(), 0, access=mmap.ACCESS_READ) 74 | 75 | 76 | # content of test_sample.py 77 | def test_2layer(): 78 | 79 | ctx = plotting_test_utils.KiPlotTestContext('simple_2layer') 80 | 81 | ctx.load_yaml_config_file('simple_2layer.kiplot.yaml') 82 | ctx.board_name = 'simple_2layer' 83 | 84 | ctx.do_plot() 85 | 86 | gbr_dir = ctx.cfg.resolve_output_dir_for_name('gerbers') 87 | 88 | f_cu_gbr = os.path.join(gbr_dir, 89 | get_gerber_filename(ctx.board_name, "F_Cu")) 90 | 91 | expect_file_at(f_cu_gbr) 92 | 93 | f_cu_data = get_mmapped_data(f_cu_gbr) 94 | 95 | ap_ids = expect_gerber_has_apertures(f_cu_data, [ 96 | "C,0.200000", 97 | "R,2.000000X2.000000", 98 | "C,1.000000"]) 99 | 100 | # expect a flash for the square pad 101 | expect_gerber_flash_at(f_cu_data, (140, -100)) 102 | 103 | ctx.clean_up() 104 | -------------------------------------------------------------------------------- /tests/test_yaml.py: -------------------------------------------------------------------------------- 1 | """ 2 | Tests for the YAML parser 3 | """ 4 | 5 | 6 | # content of test_sample.py 7 | def test_numbers_3_4(): 8 | assert 12 == 12 9 | -------------------------------------------------------------------------------- /tests/yaml_samples/simple_2layer.kiplot.yaml: -------------------------------------------------------------------------------- 1 | # Example KiPlot config file for a basic 2-layer board 2 | kiplot: 3 | version: 1 4 | 5 | outputs: 6 | 7 | - name: 'gerbers' 8 | comment: "Gerbers for the Gerber god" 9 | type: gerber 10 | dir: gerberdir 11 | options: 12 | # generic layer options 13 | exclude_edge_layer: false 14 | exclude_pads_from_silkscreen: false 15 | use_aux_axis_as_origin: false 16 | plot_sheet_reference: false 17 | plot_footprint_refs: true 18 | plot_footprint_values: true 19 | force_plot_invisible_refs_vals: false 20 | tent_vias: true 21 | check_zone_fills: true 22 | 23 | # gerber options 24 | line_width: 0.15 25 | subtract_mask_from_silk: true 26 | use_protel_extensions: false 27 | gerber_precision: 4.5 28 | create_gerber_job_file: true 29 | use_gerber_x2_attributes: true 30 | use_gerber_net_attributes: false 31 | 32 | layers: 33 | - layer: F.Cu 34 | suffix: F_Cu 35 | - layer: F.SilkS 36 | suffix: F_SilkS --------------------------------------------------------------------------------