Root/solidify/solidify.c

Source at commit 01d8e411f34d448b83fdb1cbfa4b63b0ac95fe6b created 8 years 11 months ago.
By Werner Almesberger, Use the project name to disambiguate names in POV-Ray output.
1/*
2 * solidify.c - Merge two opposing faces of a part into a solid
3 *
4 * Written 2010 by Werner Almesberger
5 * Copyright 2010 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#include <stdlib.h>
15#include <stdio.h>
16#include <unistd.h>
17#include <string.h>
18#include <locale.h>
19#include <gtk/gtk.h>
20
21#include "face.h"
22#include "solid.h"
23#include "project.h"
24#include "style.h"
25#include "level.h"
26#include "overlap.h"
27
28
29static struct project *prj;
30static const struct face *active; /* NULL if overlapping */
31static GtkWidget *canvas;
32
33
34static void clicked(GtkButton *button, gpointer user_data)
35{
36    struct face *face = user_data;
37
38    if (active == face)
39        return;
40
41    gtk_widget_destroy(gtk_bin_get_child(GTK_BIN(canvas)));
42
43    if (face)
44        level(canvas, face);
45    else
46        overlap(canvas, &prj->s);
47    active = face;
48
49    gtk_widget_show_all(canvas);
50}
51
52
53static GtkWidget *gui_buttons(void)
54{
55    GtkWidget *vbox, *but;
56
57    vbox = gtk_vbox_new(FALSE, 0);
58
59    but = gtk_button_new_with_label("A");
60    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
61    g_signal_connect(G_OBJECT(but), "clicked",
62        G_CALLBACK(clicked), prj->s.a);
63
64    but = gtk_button_new_with_label("B");
65    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
66    g_signal_connect(G_OBJECT(but), "clicked",
67        G_CALLBACK(clicked), prj->s.b);
68
69    but = gtk_button_new_with_label("A+B");
70    gtk_box_pack_start(GTK_BOX(vbox), but, FALSE, FALSE, 0);
71    g_signal_connect(G_OBJECT(but), "clicked",
72        G_CALLBACK(clicked), NULL);
73
74    return vbox;
75}
76
77
78static gboolean key_press_event(GtkWidget *widget, GdkEventKey *event,
79    gpointer data)
80{
81    if (event->keyval == 'q')
82        gtk_main_quit();
83    return TRUE;
84}
85
86
87static void gui(void)
88{
89    GtkWidget *root, *hbox, *buttons;
90
91    root = gtk_window_new(GTK_WINDOW_TOPLEVEL);
92    gtk_window_set_position(GTK_WINDOW(root), GTK_WIN_POS_CENTER);
93
94    hbox = gtk_hbox_new(FALSE, 0);
95    gtk_container_add(GTK_CONTAINER(root), hbox);
96
97    canvas = gtk_event_box_new();
98    gtk_box_pack_start(GTK_BOX(hbox), canvas, FALSE, FALSE, 0);
99
100    /* initialize root->window */
101    gtk_widget_show_all(root);
102
103    buttons = gui_buttons();
104    gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0);
105
106    level(canvas, prj->s.a);
107    active = prj->s.a;
108
109    init_style(root->window);
110
111    gtk_widget_show_all(root);
112
113    g_signal_connect(G_OBJECT(root), "key-press-event",
114        G_CALLBACK(key_press_event), NULL);
115    g_signal_connect(G_OBJECT(root), "destroy",
116        G_CALLBACK(gtk_main_quit), NULL);
117
118    gtk_main();
119}
120
121
122static void usage(const char *name)
123{
124    fprintf(stderr, "usage: %s project [top bottom dist]\n", name);
125    exit(1);
126}
127
128
129int main(int argc, char **argv)
130{
131    double dist;
132
133    gtk_init(&argc, &argv);
134    setlocale(LC_ALL, "C"); /* damage control */
135
136    switch (argc) {
137    case 2:
138        prj = load_project(argv[1]);
139        break;
140    case 5:
141        dist = atof(argv[4]);
142        prj = new_project(argv[1], argv[2], argv[3], dist);
143        break;
144    default:
145        usage(*argv);
146    }
147
148    gui();
149
150    save_project(prj);
151
152    if (!isatty(1)) {
153        const char *slash = strrchr(prj->name, '/');
154        char tmp[1000]; /* @@@ enough */
155
156        strcpy(tmp, slash ? slash+1 : prj->name);
157        if (strchr(tmp, '.'))
158            *strchr(tmp, '.') = 0;
159        povray(tmp, &prj->s);
160    }
161
162    return 0;
163}
164

Archive Download this file

Branches:
master



interactive