summaryrefslogtreecommitdiff
path: root/kicad/sch.h
blob: 92e6de9a51a962d47f32c2083f4ece67f417b134 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * kicad/sch.h - Parse Eeschema .sch file
 *
 * Written 2016 by Werner Almesberger
 * Copyright 2016 by Werner Almesberger
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */


#ifndef KICAD_SCH_H
#define KICAD_SCH_H

#include <stdbool.h>

#include "kicad/dwg.h"
#include "gfx/text.h"
#include "gfx/gfx.h"
#include "file/file.h"
#include "kicad/lib.h"


enum sch_state {
	sch_basic,	/* basic state */
	sch_descr,	/* prelude and description */
	sch_comp,	/* component */
	sch_sheet,	/* sub-sheet */
	sch_text,	/* text or label */
	sch_wire,	/* wire */
	sch_eof,	/* skipping anything after $EndSCHEMATC */
};

struct sheet;

struct sch_obj {
	enum sch_obj_type {
		sch_obj_wire,
		sch_obj_junction,
		sch_obj_noconn,
		sch_obj_text,
		sch_obj_glabel,
		sch_obj_comp,
		sch_obj_sheet,
	} type;

	int x, y;

	union {
		struct sch_wire {
			void (*fn)(struct gfx *gfx,
			    int sx, int sy, int ex, int ey);
			int ex, ey;
		} wire;
		struct sch_text {
			void (*fn)(struct gfx *gfx,
			    int x, int y, const char *s,
			    int dir, int dim, enum dwg_shape shape,
			    enum text_style style, struct dwg_bbox *bbox);
			const char *s;
			int dir;	/* orientation */
			int dim;	/* dimension */
			enum dwg_shape shape;
			enum text_style style;
			struct dwg_bbox bbox; /* set when rendering; glabel */
		} text;
		struct sch_comp {
			const struct comp *comp; /* current component */
			unsigned unit;	/* unit of current component */
			unsigned convert;/* "De Morgan"  selection */
			struct comp_field {
				struct text txt;
				unsigned n; /* field number */
				bool visible;
				struct comp_field *next;
			} *fields;
			int m[6];
		} comp;
		struct sch_sheet {
			unsigned h, w;
			const char *name;
			unsigned name_dim;
			const char *file;
			unsigned file_dim;
			bool rotated;
			bool error;	/* if set, sheet == NULL */
			const struct sheet *sheet;
				/* pointer to sub-sheet; NULL if absent */

			struct sheet_field {
				char *s;
				int x, y;
				unsigned dim;
				enum dwg_shape shape;
				unsigned side;
				struct sheet_field *next;
			} *fields;
		} sheet;
	} u;

	struct sch_obj *next;
};

struct sheet {
	const char *title;		/* malloced, unless delta */
	const char *file;		/* file name; idem */
	const char *path;		/* schematics path; idem */
	struct sch_obj *objs;
	struct sch_obj **next_obj;
	struct sheet *next;

	/* header items */
	const char *size;		/* paper size, from $Descr */
	int w, h;
	const char *date;
	const char *rev;
	const char *comp;
	const char **comments;
	unsigned n_comments;

	bool has_children;		/* aka sub-sheets */

	/* caching */
	void *oid;
};

struct sch_ctx {
	enum sch_state state;

	bool recurse;

	struct sch_obj obj;

	struct sheet *curr_sheet;
	struct sheet *sheets;
	struct sheet **next_sheet;

	/* for caching */
	const struct sch_ctx *prev;

	const struct lib *lib;
};


void decode_alignment(struct text *txt, char hor, char vert);

void sch_render(const struct sheet *sheet, struct gfx *gfx);
bool sch_parse(struct sch_ctx *ctx, struct file *file, const struct lib *lib,
    const struct sch_ctx *prev);
void sch_init(struct sch_ctx *ctx, bool recurse);
void sch_free(struct sch_ctx *ctx);

#endif /* !KICAD_SCH_H */