Skip to main content

Document

Struct Document 

Source
pub struct Document {
    pub version: u32,
    pub kind: DocumentKind,
    pub title: String,
    pub components: Vec<Component>,
    pub wires: Vec<Wire>,
    pub junctions: BTreeSet<Point>,
    pub labels: Vec<Label>,
    pub board: Board,
    pub no_connects: Vec<Terminal>,
    pub power_flags: Vec<Terminal>,
}

Fields§

§version: u32§kind: DocumentKind§title: String§components: Vec<Component>§wires: Vec<Wire>§junctions: BTreeSet<Point>§labels: Vec<Label>§board: Board

Physical layout. Absent in version 1-3 documents.

§no_connects: Vec<Terminal>

Pins the user marked as meant to be left open: the electrical rule check (Document::erc) does not ask for them to be connected. Omitted when empty, so a document without markers reads as it did before they existed.

§power_flags: Vec<Terminal>

Pins carrying a power flag, KiCad’s PWR_FLAG: the net they are on is driven from off the sheet, so its power inputs are not reported undriven.

Implementations§

Source§

impl Document

Source

pub fn autoroute(&mut self) -> RouteOutcome

Route every unrouted connection and add the copper to the board.

Source§

impl Document

Source

pub fn fill_zones(&mut self) -> Vec<ZoneReport>

Fill every zone of the board against the schematic’s nets.

Source§

impl Document

Source

pub fn sync_board(&mut self) -> BoardSync

Place each new component with its part’s pads and drop the placements of removed components. Existing placements are kept. A component with no pads is left off the board and listed in BoardSync::without_pads: pads come with the part, from a KiCad import or the pads editor, never from a generator.

Source

pub fn place_by_nets(&mut self)

Lay every part out afresh by its nets: the part with the most pins in the middle of the board, then each part, in Self::placement_order, in the free room nearest the parts already laid out that it shares a small net with. Each keeps its side and its rotation. Where the room runs out the rest are packed in rows below, as Self::auto_place packs them, and the outline grows.

Self::auto_place packs the same order into rows, so a part’s neighbours were the ones beside it in the row and the next row began at the left edge however far that was from them (the eCAD re-audit’s B10).

Source

pub fn auto_place(&mut self)

Pack all placements in rows inside the outline, keeping connected parts together.

Source

pub fn fit_board_outline(&mut self, margin: i32)

Resize a rectangular outline around all courtyards with a margin.

Source

pub fn track_nets(&self) -> BTreeMap<Uuid, String>

The schematic net each drawn track carries, for the tracks whose copper island carries exactly one. A track joined to nothing, or bridging two nets, has none.

Source

pub fn apply_net_widths(&mut self) -> usize

Give every drawn track the width its net asks for, or the default width where its net has no rule of its own. Tracks whose width was set by hand keep it. Returns how many tracks changed. Widening copper can break clearances, so run the design rule check afterwards.

Source

pub fn set_track_width(&mut self, id: Uuid, width: i32) -> Result<(), String>

Set one track’s width, whatever its net asks for. Document::apply_net_widths then leaves this track alone until Document::follow_net_width gives it back.

Source

pub fn follow_net_width(&mut self, id: Uuid)

Hand one track back to its net’s width rule.

Source

pub fn board_drc(&self) -> Vec<Violation>

Source§

impl Document

Source

pub fn erc(&self) -> Vec<ErcFinding>

Run the electrical rule check. Errors first, then warnings, each in sheet order. A wiring diagram has no nets and so no findings.

Source

pub fn apply_erc_fix(&mut self, fix: &ErcFix)

Make the edit a finding offers.

Source§

impl Document

Source

pub fn fabrication(&self, name: &str) -> Result<Fabrication, String>

Gerber and drill files for the board. name prefixes every file name.

Source

pub fn pick_and_place_csv(&self) -> String

The pick-and-place (centroid) file, one row per placed part, in the column convention of JLCPCB’s CPL template, which KiCad’s own position export and most assembly houses also accept: Designator,Val,Package,Mid X,Mid Y,Rotation,Layer.

  • Mid X / Mid Y are the footprint’s anchor (its origin, where KiCad puts a part’s position), in millimetres with a mm suffix, from the Gerbers’ own origin: the outline’s lower-left corner, Y up.
  • Rotation is in degrees, counter-clockwise as seen from the top, 0 to 270. A bottom part is reported the same way: the angle of its mirrored footprint seen from the top. No assembler-specific bottom-side correction is applied.
  • Layer is Top or Bottom.

Rows are in reference order (R2 before R10). Power symbols have no part to place and do not appear.

Source

pub fn electronics_bom_csv(&self) -> String

The electronics bill of materials: one line per value and footprint, with every reference that uses it, in the column convention of JLCPCB’s BOM template: Comment,Designator,Footprint,Quantity. Comment is the part’s value, as JLCPCB names it; Designator lists the references in order (R1,R2,R10), quoted because it holds commas.

Every component on the schematic is listed, placed on the board or not: a part not yet on the board still has to be bought. Its footprint is the one it would be placed with. Power symbols are not parts and are left out. Lines are in the order of their first reference.

Source§

impl Document

Source

pub fn kicad_netlist(&self, source: &str) -> Result<String, String>

The KiCad netlist (.net) of this schematic. source names the design in the file’s header, as KiCad names its schematic file there.

Source§

impl Document

Source

pub fn new(kind: DocumentKind) -> Self

An empty document of the given kind.

Source

pub fn place(&mut self, symbol: Symbol, at: Point, rotation: u8) -> Uuid

Source

pub fn place_part( &mut self, part: PartSource, symbol: Symbol, pads: Option<Footprint>, reference: Option<String>, at: Point, rotation: u8, ) -> Result<Uuid, String>

A free wire. Wiring diagrams have none; their wires come from Document::connect. Place a part: its symbol and pads are copied into the document, and the component remembers which part and which version they came from. reference is the label the host gives it, or the next free one when it has none.

Source

pub fn set_reference(&mut self, id: Uuid, reference: &str) -> Result<(), String>

Give a placed part the label a host keeps for it, such as its reference designator. Labels are nonempty and unique in the document.

Source

pub fn set_value(&mut self, ids: &[Uuid], value: &str) -> usize

Give each of ids the value value (10k), as one edit: the value the sheet draws under the reference, and the one the BOM groups by and the netlist and pick-and-place write. Taken as given, as the Inspector’s field types it (trimming there would eat the space of 10 k as it is typed); an empty value is allowed, as KiCad allows it. Returns how many changed.

Source

pub fn unvalued_siblings(&self, id: Uuid) -> Vec<Uuid>

The components that still carry the value their symbol gave them when they were placed (Symbol::default_value) and are placed from the same symbol as id, other than id itself, in reference order: the parts a value typed for id is offered to as well.

Source

pub fn parts(&self) -> Vec<PartSource>

Every part the document has components from, with the version each was copied from. A host compares these against its own parts to find what to refresh.

Source

pub fn refresh_part( &mut self, key: &str, symbol: &Symbol, pads: Option<&Footprint>, signature: &str, renames: &BTreeMap<String, String>, ) -> Result<usize, String>

Take a new version of one part into every component placed from it: its symbol and pads are copied again, and each component records signature. renames maps a pin label of the old version to its label in the new one; connections on a renamed pin move to its new label, connections on the pins whose labels remain stay, and the rest go. Returns how many components were refreshed. A refusal leaves the document as it was.

Source

pub fn refresh_component( &mut self, component: Uuid, symbol: &Symbol, pads: Option<&Footprint>, signature: &str, renames: &BTreeMap<String, String>, ) -> Result<(), String>

Take a new version of a part into ONE placed component — the per-component half of Document::refresh_part, for a host that decides component by component which version each carries.

A host whose parts nest cannot name one key and mean them all: the same key spells a different part at every level of an assembly tree, and one occurrence’s version lives in its OWN parent’s library. Such a host resolves each component’s device itself and hands the result here. renames and the atomicity are Document::refresh_part’s; only the PartSource’s signature is re-stamped, so a component keeps the key it was placed from — Document::relink is the call that moves that.

Bind a placed component to a DIFFERENT device — the resolving half of the UNLINKED lane.

A sheet component names its device by OCCURRENCE (PartSource::instance), and neither half of that is stable on its own: an occurrence chain changes whenever the device is moved in the assembly tree, and the parts-library key is not stable across a part’s version history. When the binding cannot be resolved the component goes UNLINKED and the USER picks its device; this is what that pick does. Nothing is guessed at here, and nothing but the binding moves: the component keeps where it sits, what it is called, and every wire landing on a pin it still has.

A device already placed on this sheet is refused, as Document::place_part refuses one.

Source

pub fn add_wire(&mut self, a: Point, b: Point)

Source

pub fn set_net_flag( &mut self, terminal: Terminal, name: &str, ) -> Result<Uuid, String>

Create or rename the flag attached to this pin. Equal names share a net.

Source

pub fn transform_label(&mut self, id: Uuid, at: Point, rotation: u8)

Move the tip; free wire endpoints follow, and direct pin contact is recomputed at the new location.

Source

pub fn terminal_position(&self, terminal: &Terminal) -> Option<Point>

Source

pub fn terminal_at(&self, at: Point) -> Option<Terminal>

Source

pub fn connection_path( &self, start: &Terminal, end: &Terminal, ) -> Option<Vec<Point>>

Short orthogonal route with outward terminal leads. Avoid crossing other terminal positions; this is deliberately not a full obstacle router.

Source

pub fn target_position(&self, target: &ConnectionTarget) -> Option<Point>

Source

pub fn target_path( &self, start: &ConnectionTarget, end: &ConnectionTarget, ) -> Option<Vec<Point>>

Source

pub fn connect_targets( &mut self, start: ConnectionTarget, end: ConnectionTarget, ) -> Option<Uuid>

Source

pub fn connect(&mut self, start: Terminal, end: Terminal) -> Option<Uuid>

Source

pub fn attached_wire(&self, terminal: &Terminal) -> Option<&Wire>

The wire with an end attached to this pin, if any.

Source

pub fn terminal_name(&self, terminal: &Terminal) -> String

A pin as reference and pin number, such as R1.2.

Source

pub fn connections(&self) -> Vec<Connection>

A wiring diagram’s point-to-point connections, ordered by connection ID. Each runs from the pin its wire was drawn from; a port is a pin number.

Source

pub fn connections_csv(&self) -> Result<String, String>

The connection list as CSV: a header row, then one row per connection.

Source

pub fn migrate_legacy_units(&mut self) -> Vec<String>

Bring every component whose symbol the KiCad import wrote before gates existed up to the gate shape (Symbol::migrate_legacy_units), each gate placed exactly where the symbol drawn whole put it, so no pin, wire or flag moves. Returns the references of the components it changed.

A host calls this where it OPENS a stored sheet, and adopts the result as part of the document rather than as an edit: nothing the user drew changes, and a sheet not saved again is migrated the same way the next time it is opened.

Source

pub fn replace_component_symbol( &mut self, id: Uuid, symbol: Symbol, ) -> Result<(), String>

Replace one instance’s definition, retaining connectivity by pin number.

Source

pub fn transform_component(&mut self, id: Uuid, at: Point, rotation: u8)

Infer attachments on legacy/free wires before moving, then keep them attached. A component whose gates are placed apart moves as one: every gate goes with it, turned about at by the same quarter turns.

Source

pub fn transform_gate(&mut self, id: Uuid, gate: u32, at: Point, rotation: u8)

Move and turn ONE gate of a component whose gates are placed apart (Component::gates); its wires and flags follow as they follow a component. Nothing happens for a gate the component does not place.

Source

pub fn open_spot(&self, symbol: &Symbol) -> Point

Where a new component drawn with symbol can go without covering one already on the sheet: the first free cell, row by row and eight to a row, of a grid that starts at the sheet origin. A cell is the symbol’s own box plus a gap: 10.16 mm across, which is room for the reference and value an editor writes to the right of a symbol (two lines of text, some 80 points wide at the default zoom), and 5.08 mm down. The origin itself when it is free, so the first part lands where it always did.

A host that seats parts on its own, with no pointer to say where, asks this rather than stacking every part at the origin: stacked parts publish the same rect, and a click or a drag reaches only the one drawn last (the eCAD workflow audit, issue 6).

Source

pub fn covers_another(&self, id: Uuid) -> bool

Whether component id covers part of another component, each taken as the box its symbol spans grown by half a sheet grid step (635 µm) on every side — so a symbol that is only a row of pins, whose box has no height, still covers one drawn on top of it, and parts a whole grid step apart do not.

Source

pub fn delete_component(&mut self, id: Uuid)

Source

pub fn from_json(text: &str) -> Result<Self, String>

Source

pub fn to_json(&self) -> Result<String, String>

Source

pub fn from_value(value: Value) -> Result<Self, String>

Read a document kept inside a host’s own JSON file, with the same migration and checks as Document::from_json.

Source

pub fn to_value(&self) -> Result<Value, String>

The document as a JSON value for a host’s own file, checked as Document::to_json checks it.

Source

pub fn validate(&self) -> Result<(), String>

Source

pub fn netlist(&self) -> Netlist

Rebuild electrical equivalence classes. Interior crossings do not connect unless a junction, pin, or label is explicitly present there.

Trait Implementations§

Source§

impl Clone for Document

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Document

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Document

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Document

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Document

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for Document

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Document

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.