# pcb-rnd modularization

## Why bother...

I believe good software should be modular. This is especially important in the context of large software, such as CAD applications. There should be a thin core that can model the world and provide the basic operations defined on it but anything else should go in separate modules.

Fortunately PCB already had a strong infrastructure supporting this idea. It has dynamic loadable plugins and the GUI and exporters are in separate HID modules. While working on pcb-gpmi and later pcb-rnd, I added the gpmi module as a separate plugin.

In version 1.0.8 to 1.1.0 a considerable chunk of core code has been moved into core plugins. A core plugin is just a plugin that is maintained together with the core, in the same repository, still the code is somewhat detached from the core. More importantly, the user can choose, for each plugin, separately:

- to compile it as a buildin (static-link it into the pcb executable)
- to compile it as a plugin (dynamic-link it runtime, if the .so is installed in the plugins/ directory)
- to disable the plugin, so it is not compiled at all

I believe such modularization has benefits on multiple levels:

- it is possible to compile smaller, potentially faster executables by omitting features the specific user would never use anyway
- in a distribution dynamic-link plugins can be distributed as separate packages providing the user with the option to decide what features to install
- such plugins have to have some sort of APIs if they want to reference each other or if the code needs to reference them; such an API may not (and often did not) exist when the code is part of the core

## Progress in charts

### Before-after

All numbers are in SLOC and are acquired running sloccount on the given directory. While lines of code alone is not a true measure of complexity, it's a good estimation. The slices of pie charts are the major components of the pcb-rnd executable.

Before modularization: pcb-rnd version 1.0.7 Note: gpmi was already a plugin |  | After modularization: pcb-rnd version 1.1.3 Note: gpmi is part of the "plugins" slice

### Zooming on to the plugins

total size per class

IO plugins

feature plugins

export plugins

HID plugins

import plugins

library plugins

footprint plugins

(Red means the plugin doesn't really work).

## Progress in numbers

Below is a table with the summary of core plugins.

module | size [sloc] | status | configure default | class | description
--- | --- | --- | --- | --- | ---
acompnet | 75 | WIP | disable | feature | Auto-complete the current network. A very limited autorouter/assistant.
autocrop | 158 | works | buildin | feature | Reduce the board dimensions to just enclose the elements.
autoplace | 618 | works | buildin | feature | Automatically place elements.
autoroute | 4361 | works | buildin | feature | Automatically route selected or all rats. This is the original autorouter.
boardflip | 221 | (doesn't update rtrees) | buildin | feature | All objects on the board are up-down flipped.
dbus | 487 | WIP (needs to install the xml?) | disable | feature | Remote control PCB using DBUS.
diag | 211 | works | disable | feature | Actions for pcb-rnd core diagnostics, intended for developers. These are not in core because end users normally don't need these. As a plugin, due to dynamic loading, it can be dropped on an existing pcb-rnd installation with minimal risk of scaring away a reproducible bug.
distalign | 428 | works | buildin | feature | Introducing Align() and Distribute(), which work much like the similarly named functions in Visio. Given that PCB does not have the concept of "first selected object" to draw on, the reference points can be selected by arguments.
distaligntext | 467 | works | buildin | feature | Same as distalign, operates on text objects.
djopt | 2334 | works | buildin | feature | Various board optimization algorithms.
draw_csect | 537 | WIP | disable | feature | Draw cross section and layer map.
draw_fab | 258 | works | buildin | feature | Draw the fab layer (for various exporters).
draw_fontsel | 188 | WIP | disable | feature | Draw the font selector GUI
export_bboard | 422 | WIP | disable-all | export | Export breadboard
export_bom | 234 | works | buildin | export | Export bom (Bill of Materials)
export_dsn | 455 | works | buildin | export | Export specctra .dsn files
export_dxf | 3985 | WIP | disable-all | export | Export dxf
export_fidocadj | 257 | WIP | disable-all | export | Export to FidoCadJ format (.fcd)
export_gcode | 2476 | works | buildin | export | Export to gcode
export_gerber | 1000 | works | buildin | export | Export to gerber
export_ipcd356 | 472 | Work-in-progress | disable-all | export | IPC-D-356 Netlist export.
export_lpr | 108 | works | buildin | export | Export to lpr (using export_ps to generate postscript)
export_nelma | 674 | works | buildin | export | Export to nelma (Numerical capacitance calculator)
export_openscad | 1385 | WIP | disable-all | export | Export openscad
export_png | 1118 | works | buildin | export | Export to png, gif and jpeg
export_ps | 1645 | works | buildin | export | Export postscript or embedded postscript.
export_stat | 269 | works | buildin | export | Export various board statistics in lihata format
export_svg | 571 | works | buildin | export | Scalable Vector Graphics (SVG) exporter
export_test | 261 | WIP | disable | export | A thin layer of code to dump exporter calls for testing the HID exporter API.
export_xy | 276 | works | buildin | export | Export XY centroid element data for pick & place.
fontmode | 266 | works | buildin | feature | Font editing actions.
fp_fs | 389 | works | buildin | fp | Footprint: file system based implementation. Used to be called Newlib: load footprints from directories. Run external processes for the parametric footprints.
fp_wget | 570 | works | buildin | fp | Footprint: get static (file) footprints from the web, e.g. from http://gedasymbols.org
gpmi | 3218 | works | buildin | feature | Scriptable plugin system with about 10 scripting languages supported and dynamic load/unload of scripts that can manipulate the GUI, the board, can implement exporters, etc.
hid_batch | 319 | works | buildin | hid | HID without GUI: read actions from stdin.
hid_gtk2_gdk | 1203 | works | buildin | hid | GUI: GTK2 HID with GDK software rendering.
hid_gtk2_gl | 1638 | works | buildin | hid | GUI: GTK2 with opengl rendering
hid_gtk3_cairo | 1005 | WIP | disable-all | hid | GUI: the GTK3 HID, using cairo for rendering
hid_lesstif | 6994 | works | buildin | hid | GUI: the lesstif HID.
hid_remote | 1174 | WIP | disable-all | hid | Remote access HID: implement a protocol and use it to relay between a core and a remote HID implementation.
hid_srv_ws | 15 | WIP | disable-all | hid | HID server: websockets using hid_remote.
import_dsn | 238 | works | buildin | import | Import specctra .dsn files
import_edif | 3625 | works | buildin | import | Import plugin for netlists in the EDIF format.
import_ltspice | 505 | works | buildin | import | Import the netlist and footprints from an ltspice .asc and .net pair of files
import_mentor_sch | 493 | works | buildin | import | Import Mentor Graphics Design Capture from flattened .edf netlist, using a parts conversion table.
import_mucs | 114 | works | buildin | import | Import lines and vias from MUCS unixplot .pl files
import_netlist | 137 | works | buildin | import | Import plugin for netlists in the classic pcb netlist format.
import_sch | 306 | works | buildin | import | Imports element and netlist data from the schematics (or some other source).
import_tinycad | 148 | works | buildin | import | Import the netlist and footprints from a tinycad netlist.
io_eagle | 940 | WIP | disable-all | io | Load the design from eagle's xml format.
io_hyp | 3278 | works | buildin | import | Import plugin for hyperlynx geometry (no polygons yet).
io_kicad | 3179 | works | buildin | io | Load and save the design and elements in Kicad's s-expression format - this is the new, currently preferred format in Kicad.
io_kicad_legacy | 945 | works | buildin | io | Export the design and elements in Kicad's legacy format.
io_lihata | 2268 | works | buildin | io | Load and save the design and elements in the lihata board format.
io_pcb | 2349 | works | buildin | io | Load and save the design and elements in the original pcb text format.
io_tedax | 686 | works | buildin | import | Import and export tEDAx netlists and footprints.
jostle | 446 | works | buildin | feature | Pushes lines out of the way.
lib_gensexpr | 10 | works | disable-all | lib | S-expression parser lib
lib_gtk_common | 11405 | works | disable-all | lib | hid_gtk* common code (regardless of gtk version or drawing mechanism: for both gtk2 and gtk3 and for both sw rendering and gl)
lib_gtk_config | 2679 | works | disable-all | lib | hid_gtk* preferences dialog common code (regardless of gtk version)
lib_gtk_hid | 1380 | works | disable-all | lib | Generic gtk HID implementation, independent of GTK version (2 vs. 3) and rendering engine. Mostly a dispatcher that fills in all the glue to connect the parts. An actual gtk HID implementation may use this lib or replace parts of it or the whole with local implementation.
lib_legacy_func | 91 | works | buildin | lib | Random collection of old/obsolete (legacy) functions. 3rd party plugins may depend on them. This module implements C functions and variables and does not register actions or flags.
loghid | 273 | WIP | disable | feature | Sits between a HID (or exporter) and the core and logs all core->plugin calls made through the HID structure.
mincut | 905 | works | buildin | feature | Use the minimal cut algorithm to indicate shorts: instead of highlighting two random pins/pads, try to highlight the least number of objects that connect the two networks.
oldactions | 155 | works | disable | feature | Random collection of old/obsolete actions. Bell(): audible feedback, DumpLibrary(): print footprint library on stdout, a set of debug actions useful for writing pcb scripts: Debug(), DebugXY(), Return(). Old plugin actions to toggle or set settings that are now accessible via the unified config system (vendordrill, djopt)
polycombine | 208 | works | buildin | feature | The selected polygons are combined together according to the ordering of their points.
polystitch | 185 | segfaults | disable-all | feature | The polygon under the cursor (based on closest-corner) is stitched together with the polygon surrounding it on the same layer. Use with pstoedit conversions where there's a "hole" in the shape - select the hole.
propedit | 773 | works | buildin | feature | List and edit properties of a group of objects.
puller | 1889 | works | buildin | feature | Pull traces to minimize their length.
query | 1835 | works | buildin | feature | pcb-rnd query language: execute expressions on objects and rules for the programmed drc.
renumber | 309 | works | buildin | feature | Renumber elements (renaming them) and generate a text file for back annotation.
report | 1021 | works | buildin | feature | Report() and ReportObject() actions - print a report about design objects.
rubberband_orig | 904 | works | buildin | feature | The original rubberband code.
shand_cmd | 212 | works | buildin | feature | vi-like command shorthands (1..3 character long commands)

smartdisperse

173

works

buildin

feature

Improve the initial dispersion of elements by choosing an order based on the netlist, rather than the arbitrary element order. This isn't the same as a global autoplace, it's more of a linear autoplace. It might make some useful local groupings. For example, you should not have to chase all over the board to find the resistor that goes with a given LED.

stroke | 139 | partially works (doesn't work with lesstif, works with the gtk hid, but there's no zoom bindings) | disable | feature | Gesture recognition with libstroke.
teardrops | 233 | works | buildin | feature | Draw teardrops on pins.
vendordrill | 517 | works | buildin | feature | Vendor drill mapping.

### Classes

Each plugin implements a class (rarely a set of classes). Classes are:

name | description
--- | ---
feature | random features directly accessible for the user, usually actions
lib | support code library for other plugins (core doesn't depend on these); functionality not directly accessible for the user but other plugins may depend on it
hid | Human Interface Device: interactive user interface, usually GUI
import | load alien formats into the design space
export | save (parts of) the design space in alien formats
fp | footprint (element) library implementation
io | native file format (save & load) implementation

### Plugin dependency map