-
Notifications
You must be signed in to change notification settings - Fork 29
WIP: [kicad-parser] write a KicadBoard #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mkovaxx
wants to merge
2
commits into
bschwind:main
Choose a base branch
from
mkovaxx:kicad-writer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,146 @@ | ||||||
use sexp::{atom_f, atom_s, Sexp}; | ||||||
|
||||||
use crate::{ | ||||||
board::{BoardLayer, Footprint, KicadBoard}, | ||||||
graphics::{GraphicArc, GraphicCircle, GraphicLine, GraphicRect}, | ||||||
}; | ||||||
|
||||||
pub fn write_board<W: std::io::Write>( | ||||||
writer: &mut W, | ||||||
board: &KicadBoard, | ||||||
) -> Result<(), std::io::Error> { | ||||||
let sexp = board_to_sexp(board); | ||||||
write!(writer, "{}", sexp) | ||||||
} | ||||||
|
||||||
fn board_to_sexp(board: &KicadBoard) -> Sexp { | ||||||
let mut items = vec![atom_s("kicad_pcb")]; | ||||||
|
||||||
for line in &board.graphic_lines { | ||||||
items.push(line_to_sexp(line)); | ||||||
} | ||||||
|
||||||
for arc in &board.graphic_arcs { | ||||||
items.push(arc_to_sexp(arc)); | ||||||
} | ||||||
|
||||||
for circle in &board.graphic_circles { | ||||||
items.push(circle_to_sexp(circle)); | ||||||
} | ||||||
|
||||||
for rect in &board.graphic_rects { | ||||||
items.push(rect_to_sexp(rect)); | ||||||
} | ||||||
|
||||||
for footprint in &board.footprints { | ||||||
items.push(footprint_to_sexp(footprint)); | ||||||
} | ||||||
|
||||||
Sexp::List(items) | ||||||
} | ||||||
|
||||||
fn line_to_sexp(line: &GraphicLine) -> Sexp { | ||||||
let items = vec![ | ||||||
atom_s("gr_line"), | ||||||
cons_s("start", point_to_sexp(line.start_point)), | ||||||
cons_s("end", point_to_sexp(line.end_point)), | ||||||
cons_s("layer", layer_to_sexp(&line.layer)), | ||||||
]; | ||||||
Sexp::List(items) | ||||||
} | ||||||
|
||||||
fn arc_to_sexp(arc: &GraphicArc) -> Sexp { | ||||||
let items = vec![ | ||||||
atom_s("gr_arc"), | ||||||
cons_s("start", point_to_sexp(arc.start_point)), | ||||||
cons_s("mid", point_to_sexp(arc.mid_point)), | ||||||
cons_s("end", point_to_sexp(arc.end_point)), | ||||||
cons_s("layer", layer_to_sexp(&arc.layer)), | ||||||
]; | ||||||
Sexp::List(items) | ||||||
} | ||||||
|
||||||
fn circle_to_sexp(circle: &GraphicCircle) -> Sexp { | ||||||
let items = vec![ | ||||||
atom_s("gr_circle"), | ||||||
cons_s("start", point_to_sexp(circle.center_point)), | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
cons_s("end", point_to_sexp(circle.end_point)), | ||||||
cons_s("layer", layer_to_sexp(&circle.layer)), | ||||||
]; | ||||||
Sexp::List(items) | ||||||
} | ||||||
|
||||||
fn rect_to_sexp(rect: &GraphicRect) -> Sexp { | ||||||
let items = vec![ | ||||||
atom_s("gr_rect"), | ||||||
cons_s("start", point_to_sexp(rect.start_point)), | ||||||
cons_s("end", point_to_sexp(rect.end_point)), | ||||||
cons_s("layer", layer_to_sexp(&rect.layer)), | ||||||
]; | ||||||
Sexp::List(items) | ||||||
} | ||||||
|
||||||
fn footprint_to_sexp(footprint: &Footprint) -> Sexp { | ||||||
let mut items = vec![ | ||||||
atom_s("footprint"), | ||||||
//cons_s("layer", layer_to_sexp(&footprint.layer)), | ||||||
cons_s("at", position_to_sexp(footprint.location, footprint.rotation_degrees)), | ||||||
]; | ||||||
|
||||||
for line in &footprint.graphic_lines { | ||||||
items.push(fp_line_to_sexp(line)); | ||||||
} | ||||||
|
||||||
for arc in &footprint.graphic_arcs { | ||||||
items.push(fp_arc_to_sexp(arc)); | ||||||
} | ||||||
|
||||||
Sexp::List(items) | ||||||
} | ||||||
|
||||||
// NOTE: The assumption that FootprintLine and GraphicLine are equivalent may not be correct | ||||||
fn fp_line_to_sexp(line: &GraphicLine) -> Sexp { | ||||||
let items = vec![ | ||||||
atom_s("fp_line"), | ||||||
cons_s("start", point_to_sexp(line.start_point)), | ||||||
cons_s("end", point_to_sexp(line.end_point)), | ||||||
cons_s("layer", layer_to_sexp(&line.layer)), | ||||||
]; | ||||||
Sexp::List(items) | ||||||
} | ||||||
|
||||||
// NOTE: The assumption that FootprintArc and GraphicArc are equivalent may not be correct | ||||||
fn fp_arc_to_sexp(arc: &GraphicArc) -> Sexp { | ||||||
let items = vec![ | ||||||
atom_s("fp_arc"), | ||||||
cons_s("start", point_to_sexp(arc.start_point)), | ||||||
cons_s("mid", point_to_sexp(arc.mid_point)), | ||||||
cons_s("end", point_to_sexp(arc.end_point)), | ||||||
cons_s("layer", layer_to_sexp(&arc.layer)), | ||||||
]; | ||||||
Sexp::List(items) | ||||||
} | ||||||
|
||||||
fn point_to_sexp(point: (f64, f64)) -> Sexp { | ||||||
Sexp::List(vec![atom_f(point.0), atom_f(point.1)]) | ||||||
} | ||||||
|
||||||
fn layer_to_sexp(layer: &BoardLayer) -> Sexp { | ||||||
let layer_str: &str = layer.into(); | ||||||
atom_s(layer_str) | ||||||
} | ||||||
|
||||||
fn position_to_sexp(location: (f64, f64), rotation_degrees: f64) -> Sexp { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might call this |
||||||
Sexp::List(vec![atom_f(location.0), atom_f(location.1), atom_f(rotation_degrees)]) | ||||||
} | ||||||
|
||||||
fn cons_s(head: &str, tail: Sexp) -> Sexp { | ||||||
Sexp::List(match tail { | ||||||
Sexp::List(sexps) => { | ||||||
let mut items = vec![atom_s(head)]; | ||||||
items.extend(sexps); | ||||||
items | ||||||
}, | ||||||
atom => vec![atom_s(head), atom], | ||||||
}) | ||||||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this instead be
KicadPcb
? I think my original plan was forKicadBoard
to hold the actual board data like copper, footprints, silkscreens, etc, whileKicadPcb
would hold aKicadBoard
, as well as extra data like the version and other metadata.