1%module{Slic3r::XS};
2
3%{
4#include <xsinit.h>
5#include "libslic3r/PrintConfig.hpp"
6%}
7
8%name{Slic3r::Config} class DynamicPrintConfig {
9    DynamicPrintConfig();
10    ~DynamicPrintConfig();
11    static DynamicPrintConfig* new_from_defaults()
12        %code{% RETVAL = DynamicPrintConfig::new_from_defaults_keys(FullPrintConfig::defaults().keys()); %};
13    static DynamicPrintConfig* new_from_defaults_keys(std::vector<std::string> keys);
14    DynamicPrintConfig* clone() %code{% RETVAL = new DynamicPrintConfig(*THIS); %};
15    DynamicPrintConfig* clone_only(std::vector<std::string> keys)
16        %code{% RETVAL = new DynamicPrintConfig(); RETVAL->apply_only(*THIS, keys, true); %};
17    bool has(t_config_option_key opt_key);
18    SV* as_hash()
19        %code{% RETVAL = ConfigBase__as_hash(THIS); %};
20    SV* get(t_config_option_key opt_key)
21        %code{% RETVAL = ConfigBase__get(THIS, opt_key); %};
22    SV* get_at(t_config_option_key opt_key, int i)
23        %code{% RETVAL = ConfigBase__get_at(THIS, opt_key, i); %};
24    SV* get_value(t_config_option_key opt_key)
25        %code{%
26            const ConfigOptionDef *def = THIS->def()->get(opt_key);
27            RETVAL = (def != nullptr && ! def->ratio_over.empty()) ?
28                newSVnv(THIS->get_abs_value(opt_key)) :
29                ConfigBase__get(THIS, opt_key);
30        %};
31    bool set(t_config_option_key opt_key, SV* value)
32        %code{% RETVAL = ConfigBase__set(THIS, opt_key, value); %};
33    bool set_deserialize(t_config_option_key opt_key, SV* str)
34        %code{% RETVAL = ConfigBase__set_deserialize(THIS, opt_key, str); %};
35    void set_ifndef(t_config_option_key opt_key, SV* value, bool deserialize = false)
36        %code{% ConfigBase__set_ifndef(THIS, opt_key, value, deserialize); %};
37    std::string opt_serialize(t_config_option_key opt_key);
38    double get_abs_value(t_config_option_key opt_key);
39    %name{get_abs_value_over}
40        double get_abs_value(t_config_option_key opt_key, double ratio_over);
41    void apply(DynamicPrintConfig* other)
42        %code{% THIS->apply(*other, true); %};
43    std::vector<std::string> diff(DynamicPrintConfig* other)
44        %code{% RETVAL = THIS->diff(*other); %};
45    bool equals(DynamicPrintConfig* other)
46        %code{% RETVAL = THIS->equals(*other); %};
47    void apply_static(StaticPrintConfig* other)
48        %code{% THIS->apply(*other, true); %};
49    %name{get_keys} std::vector<std::string> keys();
50    void erase(t_config_option_key opt_key);
51    void normalize_fdm();
52    %name{setenv} void setenv_();
53    double min_object_distance() %code{% RETVAL = Slic3r::min_object_distance(*THIS); %};
54    static DynamicPrintConfig* load(char *path)
55        %code%{
56            auto config = new DynamicPrintConfig();
57            try {
58                config->load(path, ForwardCompatibilitySubstitutionRule::Disable);
59                RETVAL = config;
60            } catch (std::exception& e) {
61                delete config;
62                croak("Error extracting configuration from %s:\n%s\n", path, e.what());
63            }
64        %};
65    void save(std::string file);
66    int validate() %code%{
67            std::string err = THIS->validate();
68            if (! err.empty())
69                croak("Configuration is not valid: %s\n", err.c_str());
70            RETVAL = 1;
71        %};
72};
73
74%name{Slic3r::Config::Static} class StaticPrintConfig {
75    static StaticPrintConfig* new_GCodeConfig()
76        %code{% RETVAL = new GCodeConfig(); %};
77    static StaticPrintConfig* new_PrintConfig()
78        %code{% RETVAL = static_cast<GCodeConfig*>(new PrintConfig()); %};
79    static StaticPrintConfig* new_PrintObjectConfig()
80        %code{% RETVAL = new PrintObjectConfig(); %};
81    static StaticPrintConfig* new_PrintRegionConfig()
82        %code{% RETVAL = new PrintRegionConfig(); %};
83    static StaticPrintConfig* new_FullPrintConfig()
84        %code{% RETVAL = static_cast<GCodeConfig*>(new FullPrintConfig()); %};
85    ~StaticPrintConfig();
86    bool has(t_config_option_key opt_key);
87    SV* as_hash()
88        %code{% RETVAL = ConfigBase__as_hash(THIS); %};
89    SV* get(t_config_option_key opt_key)
90        %code{% RETVAL = ConfigBase__get(THIS, opt_key); %};
91    SV* get_at(t_config_option_key opt_key, int i)
92        %code{% RETVAL = ConfigBase__get_at(THIS, opt_key, i); %};
93    bool set(t_config_option_key opt_key, SV* value)
94        %code{% RETVAL = StaticConfig__set(THIS, opt_key, value); %};
95    bool set_deserialize(t_config_option_key opt_key, SV* str)
96        %code{% RETVAL = ConfigBase__set_deserialize(THIS, opt_key, str); %};
97    void set_ifndef(t_config_option_key opt_key, SV* value, bool deserialize = false)
98        %code{% ConfigBase__set_ifndef(THIS, opt_key, value, deserialize); %};
99    std::string opt_serialize(t_config_option_key opt_key);
100    double get_abs_value(t_config_option_key opt_key);
101    %name{get_abs_value_over}
102        double get_abs_value(t_config_option_key opt_key, double ratio_over);
103    void apply_static(StaticPrintConfig* other)
104        %code{% THIS->apply(*other, true); %};
105    void apply_dynamic(DynamicPrintConfig* other)
106        %code{% THIS->apply(*other, true); %};
107    %name{get_keys} std::vector<std::string> keys();
108    std::string get_extrusion_axis()
109        %code{%
110            if (GCodeConfig* config = dynamic_cast<GCodeConfig*>(THIS)) {
111                RETVAL = config->get_extrusion_axis();
112            } else {
113                CONFESS("This StaticConfig object does not provide get_extrusion_axis()");
114            }
115        %};
116    %name{setenv} void setenv_();
117    double min_object_distance() %code{% RETVAL = Slic3r::min_object_distance(*THIS); %};
118    static StaticPrintConfig* load(char *path)
119        %code%{
120            auto config = new FullPrintConfig();
121            try {
122                config->load(path, ForwardCompatibilitySubstitutionRule::Disable);
123                RETVAL = static_cast<GCodeConfig*>(config);
124            } catch (std::exception& e) {
125                delete config;
126                croak("Error extracting configuration from %s:\n%s\n", path, e.what());
127            }
128        %};
129
130    void save(std::string file);
131};
132
133%package{Slic3r::Config};
134
135%{
136PROTOTYPES: DISABLE
137
138SV*
139print_config_def()
140    CODE:
141        t_optiondef_map &def = *const_cast<t_optiondef_map*>(&Slic3r::print_config_def.options);
142
143        HV* options_hv = newHV();
144        for (t_optiondef_map::iterator oit = def.begin(); oit != def.end(); ++oit) {
145            HV* hv = newHV();
146
147            t_config_option_key opt_key = oit->first;
148            ConfigOptionDef* optdef     = &oit->second;
149
150            const char* opt_type;
151            if (optdef->type == coFloat || optdef->type == coFloats || optdef->type == coFloatOrPercent) {
152                opt_type = "f";
153            } else if (optdef->type == coPercent || optdef->type == coPercents) {
154                opt_type = "percent";
155            } else if (optdef->type == coInt || optdef->type == coInts) {
156                opt_type = "i";
157            } else if (optdef->type == coString) {
158                opt_type = "s";
159            } else if (optdef->type == coStrings) {
160                opt_type = "s@";
161            } else if (optdef->type == coPoint || optdef->type == coPoints) {
162                opt_type = "point";
163            } else if (optdef->type == coPoint3) {
164                opt_type = "point3";
165            } else if (optdef->type == coBool || optdef->type == coBools) {
166                opt_type = "bool";
167            } else if (optdef->type == coEnum) {
168                opt_type = "select";
169            } else {
170                throw "Unknown option type";
171            }
172            (void)hv_stores( hv, "type",        newSVpv(opt_type, 0) );
173            (void)hv_stores( hv, "gui_type",    newSVpvn(optdef->gui_type.c_str(), optdef->gui_type.length()) );
174            (void)hv_stores( hv, "gui_flags",   newSVpvn(optdef->gui_flags.c_str(), optdef->gui_flags.length()) );
175            (void)hv_stores( hv, "label",       newSVpvn_utf8(optdef->label.c_str(), optdef->label.length(), true) );
176            if (!optdef->full_label.empty())
177                (void)hv_stores( hv, "full_label",  newSVpvn_utf8(optdef->full_label.c_str(), optdef->full_label.length(), true) );
178            (void)hv_stores( hv, "category",    newSVpvn_utf8(optdef->category.c_str(), optdef->category.length(), true) );
179            (void)hv_stores( hv, "tooltip",     newSVpvn_utf8(optdef->tooltip.c_str(), optdef->tooltip.length(), true) );
180            (void)hv_stores( hv, "sidetext",    newSVpvn_utf8(optdef->sidetext.c_str(), optdef->sidetext.length(), true) );
181            (void)hv_stores( hv, "cli",         newSVpvn(optdef->cli.c_str(), optdef->cli.length()) );
182            (void)hv_stores( hv, "ratio_over",  newSVpvn(optdef->ratio_over.c_str(), optdef->ratio_over.length()) );
183            (void)hv_stores( hv, "multiline",   newSViv(optdef->multiline ? 1 : 0) );
184            (void)hv_stores( hv, "full_width",  newSViv(optdef->full_width ? 1 : 0) );
185            (void)hv_stores( hv, "readonly",    newSViv(optdef->readonly ? 1 : 0) );
186            (void)hv_stores( hv, "height",      newSViv(optdef->height) );
187            (void)hv_stores( hv, "width",       newSViv(optdef->width) );
188            (void)hv_stores( hv, "min",         newSViv(optdef->min) );
189            (void)hv_stores( hv, "max",         newSViv(optdef->max) );
190
191            // aliases
192            if (!optdef->aliases.empty()) {
193                AV* av = newAV();
194                av_fill(av, optdef->aliases.size()-1);
195                for (std::vector<t_config_option_key>::iterator it = optdef->aliases.begin(); it != optdef->aliases.end(); ++it)
196                    av_store(av, it - optdef->aliases.begin(), newSVpvn(it->c_str(), it->length()));
197                (void)hv_stores( hv, "aliases", newRV_noinc((SV*)av) );
198            }
199
200            // shortcut
201            if (!optdef->shortcut.empty()) {
202                AV* av = newAV();
203                av_fill(av, optdef->shortcut.size()-1);
204                for (std::vector<t_config_option_key>::iterator it = optdef->shortcut.begin(); it != optdef->shortcut.end(); ++it)
205                    av_store(av, it - optdef->shortcut.begin(), newSVpvn(it->c_str(), it->length()));
206                (void)hv_stores( hv, "shortcut", newRV_noinc((SV*)av) );
207            }
208
209            // enum_values
210            if (!optdef->enum_values.empty()) {
211                AV* av = newAV();
212                av_fill(av, optdef->enum_values.size()-1);
213                for (std::vector<std::string>::iterator it = optdef->enum_values.begin(); it != optdef->enum_values.end(); ++it)
214                    av_store(av, it - optdef->enum_values.begin(), newSVpvn(it->c_str(), it->length()));
215                (void)hv_stores( hv, "values", newRV_noinc((SV*)av) );
216            }
217
218            // enum_labels
219            if (!optdef->enum_labels.empty()) {
220                AV* av = newAV();
221                av_fill(av, optdef->enum_labels.size()-1);
222                for (std::vector<std::string>::iterator it = optdef->enum_labels.begin(); it != optdef->enum_labels.end(); ++it)
223                    av_store(av, it - optdef->enum_labels.begin(), newSVpvn_utf8(it->c_str(), it->length(), true));
224                (void)hv_stores( hv, "labels", newRV_noinc((SV*)av) );
225            }
226
227            if (optdef->default_value)
228                (void)hv_stores( hv, "default", ConfigOption_to_SV(*optdef->default_value.get(), *optdef) );
229            (void)hv_store( options_hv, opt_key.c_str(), opt_key.length(), newRV_noinc((SV*)hv), 0 );
230        }
231
232        RETVAL = newRV_noinc((SV*)options_hv);
233    OUTPUT:
234        RETVAL
235%}
236