Root/eeshow/kicad/sch-render.c

1/*
2 * kicad/sch-render.c - Render schematics
3 *
4 * Written 2016 by Werner Almesberger
5 * Copyright 2016 by Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#define _GNU_SOURCE /* for asprintf */
15#include <stdio.h>
16#include <assert.h>
17
18#include "gfx/misc.h"
19#include "gfx/style.h"
20#include "gfx/gfx.h"
21#include "kicad/dwg.h"
22#include "kicad/lib.h"
23#include "kicad/sch.h"
24
25
26/* ----- Rendering --------------------------------------------------------- */
27
28
29static void dump_field(const struct comp_field *field, const int m[6])
30{
31    struct text txt = field->txt;
32    int dx, dy;
33
34    dx = txt.x - m[0];
35    dy = txt.y - m[3];
36    txt.x = mx(dx, dy, m);
37    txt.y = my(dx, dy, m);
38
39    text_rot(&txt, matrix_to_angle(m));
40
41    switch (txt.rot) {
42    case 180:
43        text_rot(&txt, 180);
44        txt.hor = text_flip(txt.hor);
45        txt.vert = text_flip(txt.vert);
46        break;
47    case 270:
48        text_rot(&txt, 180);
49        txt.vert = text_flip(txt.vert);
50        txt.hor = text_flip(txt.hor);
51        break;
52    default:
53        break;
54    }
55
56    if (matrix_is_mirrored(m)) {
57        if ((txt.rot % 180) == 0)
58            txt.hor = text_flip(txt.hor);
59        else
60            txt.vert = text_flip(txt.vert);
61    }
62
63    text_fig(&txt, COLOR_FIELD, LAYER_FIELD);
64}
65
66
67static void do_hsheet_text(const struct sch_obj *obj,
68    const struct sch_sheet *sheet)
69{
70    char *s;
71
72    assert(sheet->name && sheet->file);
73
74    struct text sheet_txt = {
75        .size = sheet->name_dim,
76        .x = obj->x,
77        .y = obj->y,
78        .rot = 0,
79        .hor = text_min,
80        .vert = text_min,
81    };
82    if (asprintf(&s, "Sheet: %s", sheet->name)) {}
83    sheet_txt.s = s; /* work around "const" mismatch */
84
85    struct text file_txt = {
86        .size = sheet->file_dim,
87        .x = obj->x,
88        .y = obj->y,
89        .rot = 0,
90        .hor = text_min,
91        .vert = text_max,
92    };
93    if (asprintf(&s, "File: %s", sheet->file)) {}
94    file_txt.s = s; /* work around "const" mismatch */
95
96    if (sheet->rotated) {
97        sheet_txt.rot = file_txt.rot = 90;
98        sheet_txt.x -= HSHEET_FIELD_OFFSET;
99        sheet_txt.y += sheet->h;
100        file_txt.x += sheet->w + HSHEET_FIELD_OFFSET;
101        file_txt.y += sheet->h;
102    } else {
103        sheet_txt.y -= HSHEET_FIELD_OFFSET;
104        file_txt.y += sheet->h + HSHEET_FIELD_OFFSET;
105    }
106
107    text_fig(&sheet_txt, COLOR_HSHEET_SHEET, LAYER_HSHEET_FIELD);
108    text_fig(&file_txt, COLOR_HSHEET_FILE, LAYER_HSHEET_FIELD);
109
110// free((void *) ctx->sheet);
111// free((void *) ctx->file);
112}
113
114
115static void render_sheet(const struct sch_obj *obj,
116    const struct sch_sheet *sheet)
117{
118    const struct sheet_field *field;
119
120    gfx_rect(obj->x, obj->y, obj->x + sheet->w, obj->y + sheet->h,
121        COLOR_HSHEET_BOX, sheet->error ? COLOR_MISSING_BG : COLOR_NONE,
122        LAYER_HSHEET_BOX);
123    do_hsheet_text(obj, sheet);
124
125    for (field = sheet->fields; field; field = field->next)
126        dwg_hlabel(field->x, field->y, field->s,
127            field->side, field->dim,
128            field->shape, NULL);
129    // free(field->s)
130}
131
132
133void sch_render(const struct sheet *sheet)
134{
135    struct sch_obj *obj;
136
137    for (obj = sheet->objs; obj; obj = obj->next)
138        switch (obj->type) {
139        case sch_obj_wire:
140            {
141                const struct sch_wire *wire = &obj->u.wire;
142
143                wire->fn(obj->x, obj->y, wire->ex, wire->ey);
144            }
145            break;
146        case sch_obj_junction:
147            dwg_junction(obj->x, obj->y);
148            break;
149        case sch_obj_noconn:
150            dwg_noconn(obj->x, obj->y);
151            break;
152        case sch_obj_glabel:
153        case sch_obj_text:
154            {
155                struct sch_text *text = &obj->u.text;
156
157                text->fn(obj->x, obj->y, text->s, text->dir,
158                    text->dim, text->shape, &text->bbox);
159            }
160            break;
161        case sch_obj_comp:
162            {
163                const struct sch_comp *comp = &obj->u.comp;
164                const struct comp_field *field;
165
166                lib_render(comp->comp, comp->unit,
167                    comp->convert, comp->m);
168                for (field = comp->fields; field;
169                    field = field->next)
170                    dump_field(field, comp->m);
171            }
172            break;
173        case sch_obj_sheet:
174            render_sheet(obj, &obj->u.sheet);
175            break;
176        }
177}
178

Archive Download this file

Branches:
master



interactive