void SettingRegistry::_addSettingToContainer(SettingContainer* parent, rapidjson::Value::ConstMemberIterator& json_object_it, bool warn_duplicates, bool add_to_settings) { const rapidjson::Value& data = json_object_it->value; if (data.HasMember("type") && data["type"].IsString() && (data["type"].GetString() == std::string("polygon") || data["type"].GetString() == std::string("polygons"))) { logWarning("Loading polygon setting %s not implemented...\n", json_object_it->name.GetString()); /// When this setting has children, add those children to the parent setting. if (data.HasMember("children") && data["children"].IsObject()) { const rapidjson::Value& json_object_container = data["children"]; for (rapidjson::Value::ConstMemberIterator setting_iterator = json_object_container.MemberBegin(); setting_iterator != json_object_container.MemberEnd(); ++setting_iterator) { _addSettingToContainer(parent, setting_iterator, warn_duplicates, add_to_settings); } } return; } std::string label; if (!json_object_it->value.HasMember("label") || !data["label"].IsString()) { label = "N/A"; } else { label = data["label"].GetString(); } /// Create the new setting config object. SettingConfig* config = parent->addChild(json_object_it->name.GetString(), label); _loadSettingValues(config, json_object_it, warn_duplicates, add_to_settings); }
void SettingRegistry::handleSetting(const rapidjson::Value::ConstMemberIterator& json_setting_it, std::list<std::string>& path, SettingsBase* settings_base, bool warn_duplicates) { const rapidjson::Value& json_setting = json_setting_it->value; if (!json_setting.IsObject()) { logError("ERROR: json setting is not an object!\n"); return; } std::string name = json_setting_it->name.GetString(); if (json_setting.HasMember("type") && json_setting["type"].IsString() && json_setting["type"].GetString() == std::string("category")) { // skip category objects return; } if (settingIsUsedByEngine(json_setting)) { if (!json_setting.HasMember("label") || !json_setting["label"].IsString()) { logError("ERROR: json setting \"%s\" has no label!\n", name.c_str()); return; } std::string label = json_setting["label"].GetString(); SettingConfig* setting = getSettingConfig(name); if (warn_duplicates && setting) { cura::logError("Duplicate definition of setting: %s a.k.a. \"%s\" was already claimed by \"%s\"\n", name.c_str(), label.c_str(), getSettingConfig(name)->getLabel().c_str()); } if (!setting) { setting = &addSetting(name, label); } _loadSettingValues(setting, json_setting_it, settings_base); } }
int SettingRegistry::loadJSONsettingsFromDoc(rapidjson::Document& json_document, SettingsBase* settings_base, bool warn_duplicates) { if (!json_document.IsObject()) { cura::logError("JSON file is not an object.\n"); return 3; } { // handle machine name std::string machine_name = "Unknown"; if (json_document.HasMember("name")) { const rapidjson::Value& machine_name_field = json_document["name"]; if (machine_name_field.IsString()) { machine_name = machine_name_field.GetString(); } } SettingConfig& machine_name_setting = addSetting("machine_name", "Machine Name"); machine_name_setting.setDefault(machine_name); machine_name_setting.setType("string"); } if (json_document.HasMember("settings")) { std::list<std::string> path; handleChildren(json_document["settings"], path, settings_base, warn_duplicates); } if (json_document.HasMember("overrides")) { const rapidjson::Value& json_object_container = json_document["overrides"]; for (rapidjson::Value::ConstMemberIterator override_iterator = json_object_container.MemberBegin(); override_iterator != json_object_container.MemberEnd(); ++override_iterator) { std::string setting = override_iterator->name.GetString(); SettingConfig* conf = getSettingConfig(setting); if (!conf) //Setting could not be found. { logWarning("Trying to override unknown setting %s.\n", setting.c_str()); continue; } _loadSettingValues(conf, override_iterator, settings_base); } } return 0; }
int SettingRegistry::loadJSONsettingsFromDoc(rapidjson::Document& json_document, bool warn_duplicates) { if (!json_document.IsObject()) { cura::logError("JSON file is not an object.\n"); return 3; } if (json_document.HasMember("machine_extruder_trains")) { categories.emplace_back("machine_extruder_trains", "Extruder Trains Settings Objects"); SettingContainer* category_trains = &categories.back(); const rapidjson::Value& trains = json_document["machine_extruder_trains"]; if (trains.IsArray()) { if (trains.Size() > 0 && trains[0].IsObject()) { unsigned int idx = 0; for (auto it = trains.Begin(); it != trains.End(); ++it) { SettingConfig* child = category_trains->addChild(std::to_string(idx), std::to_string(idx)); for (rapidjson::Value::ConstMemberIterator setting_iterator = it->MemberBegin(); setting_iterator != it->MemberEnd(); ++setting_iterator) { _addSettingToContainer(child, setting_iterator, warn_duplicates, false); } idx++; } } } else { logError("Error: JSON machine_extruder_trains is not an array!\n"); } } if (json_document.HasMember("machine_settings")) { categories.emplace_back("machine_settings", "Machine Settings"); SettingContainer* category_machine_settings = &categories.back(); const rapidjson::Value& json_object_container = json_document["machine_settings"]; for (rapidjson::Value::ConstMemberIterator setting_iterator = json_object_container.MemberBegin(); setting_iterator != json_object_container.MemberEnd(); ++setting_iterator) { _addSettingToContainer(category_machine_settings, setting_iterator, warn_duplicates); } } if (json_document.HasMember("categories")) { for (rapidjson::Value::ConstMemberIterator category_iterator = json_document["categories"].MemberBegin(); category_iterator != json_document["categories"].MemberEnd(); ++category_iterator) { if (!category_iterator->value.IsObject()) { continue; } if (!category_iterator->value.HasMember("settings") || !category_iterator->value["settings"].IsObject()) { continue; } std::string cat_name = category_iterator->name.GetString(); std::list<SettingContainer>::iterator category_found = std::find_if(categories.begin(), categories.end(), [&cat_name](SettingContainer& cat) { return cat.getKey().compare(cat_name) == 0; }); if (category_found != categories.end()) { // category is already present; add settings to category SettingContainer* category = &*category_found; const rapidjson::Value& json_object_container = category_iterator->value["settings"]; for (rapidjson::Value::ConstMemberIterator setting_iterator = json_object_container.MemberBegin(); setting_iterator != json_object_container.MemberEnd(); ++setting_iterator) { _addSettingToContainer(category, setting_iterator, warn_duplicates); } } else { if (!category_iterator->value.HasMember("label") || !category_iterator->value["label"].IsString()) { continue; } categories.emplace_back(cat_name, category_iterator->value["label"].GetString()); SettingContainer* category = &categories.back(); const rapidjson::Value& json_object_container = category_iterator->value["settings"]; for (rapidjson::Value::ConstMemberIterator setting_iterator = json_object_container.MemberBegin(); setting_iterator != json_object_container.MemberEnd(); ++setting_iterator) { _addSettingToContainer(category, setting_iterator, warn_duplicates); } } } } if (json_document.HasMember("overrides")) { const rapidjson::Value& json_object_container = json_document["overrides"]; for (rapidjson::Value::ConstMemberIterator override_iterator = json_object_container.MemberBegin(); override_iterator != json_object_container.MemberEnd(); ++override_iterator) { std::string setting = override_iterator->name.GetString(); SettingConfig* conf = getSettingConfig(setting); _loadSettingValues(conf, override_iterator, false); } } return 0; }