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>,
    pub board_zone: Option<Point>,
    pub zones: Vec<SheetZone>,
}

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.

§board_zone: Option<Point>

A wiring diagram kept beside a board’s schematic only: where, in this diagram’s coordinates, the board’s schematic origin sits when the two are drawn as one sheet (sheet). Absent until the diagram is first written from a sheet, so every older diagram reads and writes as it did.

§zones: Vec<SheetZone>

The boards of their own this assembly places (sub-board zones, sheet). A wiring diagram stores where each one’s schematic is drawn on the sheet, by occurrence, once it has been written from a sheet; a sheet holds each one with its parts and nets, read-only. Never on a schematic.

Implementations§

Source§

impl Document

Source

pub fn set_pin_function( &mut self, id: Uuid, number: &str, function: Option<&str>, ) -> Result<bool, String>

Choose which function component id’s pin number goes by: one of the pin’s Pin::alternates, or its main name (None, or the main name itself), which clears the choice. Documentary only; see Pin::alternates. Returns whether anything changed.

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 is_harness(&self, wire: &Wire) -> bool

Whether wire is a harness wire, by this document’s kind.

Source

pub fn on_board(&self, component: &Component) -> bool

Whether component is on the board, by this document’s kind.

Source

pub fn net_reaches(&self, terminal: &Terminal) -> Result<(), String>

Whether a net may reach terminal: on a sheet, only a pin of a part on the board. The refusal names the part.

Source

pub fn harness_reaches(&self, terminal: &Terminal) -> Result<(), String>

Whether a harness wire may end on terminal: anywhere but a pin inside a sub-board the board declares no point for. The refusal names the pin and says what to do.

Source

pub fn zone_member(&self, id: Uuid) -> Option<&SheetZone>

The sub-board zone a part of the sheet is drawn from.

Source

pub fn zone_of_net(&self, id: Uuid) -> Option<&SheetZone>

The sub-board zone a net of the sheet is drawn from.

Source

pub fn without_zones(&self) -> Document

This sheet with its sub-board zones left out: its own board’s parts, nets and markers, and the parts off any board. What its nets and its check read.

Source

pub fn sub_zone_extent(&self, occurrence: &str) -> Option<(Point, Point)>

The box round a sub-board zone’s parts: None for one with none.

Source

pub fn move_zone(&mut self, occurrence: &str, by: Point) -> Result<(), String>

Move a sub-board zone, its parts and nets with it, and the harness wire ends on them.

Source

pub fn zone_edit_refusal(&self, before: &Document) -> Option<String>

Why the edit from before to self is refused: it changed a sub-board’s parts or nets other than by moving the whole zone. They are the board’s own document, read-only on this sheet until the zone is entered (step 3).

Source

pub fn board_zone_extent(&self) -> Option<(Point, Point)>

The parts on the board, as the zone drawn round them spans the sheet: None when none is.

Source

pub fn set_off_board(&mut self, id: Uuid, off_board: bool) -> Result<(), String>

Put a part of a sheet on the board (off_board == false) or take it off.

Taking it off lets go of everything of the board’s that holds its pins (net wire ends, net flags, no-connect and power-flag markers); its harness wires stay. A part placed from an assembly part that has no pads is refused entry, with the reason, because the board could not seat it.

Source

pub fn merge_sheet( pcb: Option<Document>, diagram: Option<Document>, offset: Option<Point>, ) -> Result<(Document, SheetSource), String>

Read a board’s schematic and a wiring diagram into one sheet. offset is where the host last drew the diagram this session (the schematic origin, in diagram coordinates); the diagram’s own Document::board_zone wins over it, and with neither the diagram is put to the left of the board.

Source

pub fn merge_sheet_with( pcb: Option<Document>, diagram: Option<Document>, offset: Option<Point>, boards: Vec<SubBoard>, ) -> Result<(Document, SheetSource), String>

Document::merge_sheet for an assembly that places boards of its own: each is a zone of the sheet, drawn from its schematic where the diagram’s Document::zones say, else beside everything else.

Source

pub fn split_sheet(&self, source: &SheetSource) -> Result<SplitSheet, String>

Write a sheet back as a board’s schematic and a wiring diagram, each one EQUAL to the one SheetSource merged where nothing of it was edited.

  • The schematic holds the parts on the board, the nets and the layout; it is None when there was none and the sheet has nothing of the board’s.
  • The diagram holds the parts off the board, the harness wires, and a twin of each part on the board that a harness wire reaches (or that had one).
Source§

impl Document

Source

pub fn new(kind: DocumentKind) -> Self

An empty document of the given kind.

Source

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

Put a symbol on the sheet with NO part and NO assembly occurrence behind it.

A BREP sheet is one view of an assembly: every valued component on it is an assembly component, and the one way to put one down is Document::place_assembly_component, after the part has been added to the assembly. This primitive is left public for the two things that are NOT assembly components — a power symbol, which names a net (Document::place_power_symbol), and an editor’s preview sheet — and for tests that build a sheet shape directly. No editor door reaches it for a valued part: a saved component with a part and no occurrence is the LEGACY shape, which the host binds to an assembly component on open.

Source

pub fn place_power_symbol( &mut self, symbol: Symbol, at: Point, rotation: u8, ) -> Result<Uuid, String>

Put a power symbol on the sheet: a #PWR symbol that names a net (Symbol::power_net) and is no part — it has no footprint, no BOM line and no assembly occurrence. Refused for any other symbol: a valued part goes on a sheet through Document::place_assembly_component alone.

Source

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

Place an ASSEMBLY COMPONENT on the sheet. occurrence is the assembly occurrence the component IS (ACOMP3, or ACOMP7:ACOMP1 for a device inside a sub-assembly): the symbol and pads of its part are copied into the document, and the component records the occurrence with the part’s key and version (part.key, part.signature; part.instance is overwritten with occurrence). reference is the label the host gives it — its reference designator — or the next free one when it has none.

This is the ONE way a valued component reaches a sheet. It takes the occurrence as a required argument so that a sheet component without an assembly component behind it cannot be written: a board is an assembly, and its footprints are the assembly’s components seen from above. An occurrence already on the sheet is refused — one component per occurrence.

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_assembly_component 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 net_terminal_at(&self, at: Point) -> Option<Terminal>

Document::terminal_at, for a net: never a pin of a sheet’s part off the board.

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_targets_as( &mut self, start: ConnectionTarget, end: ConnectionTarget, harness: bool, ) -> Option<Uuid>

Document::connect_targets drawing a harness wire (harness) or a net. Only a DocumentKind::Sheet holds both; the other kinds draw their own.

Source

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

Source

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

Document::connect drawing a harness wire (harness) or a net. A schematic draws only nets and a wiring diagram only harness wires, whatever is asked.

Source

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

The HARNESS wire with an end attached to this pin, if any: the connection a harness pin takes one of. A net on the same pin is not one.

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.