void ArcusCommunication::sliceNext() { const Arcus::MessagePtr message = private_data->socket->takeNextMessage(); //Handle the main Slice message. const cura::proto::Slice* slice_message = dynamic_cast<cura::proto::Slice*>(message.get()); //See if the message is of the message type Slice. Returns nullptr otherwise. if (!slice_message) { return; } logDebug("Received a Slice message.\n"); Slice slice(slice_message->object_lists().size()); Application::getInstance().current_slice = &slice; private_data->readGlobalSettingsMessage(slice_message->global_settings()); private_data->readExtruderSettingsMessage(slice_message->extruders()); const size_t extruder_count = slice.scene.extruders.size(); //For each setting, register what extruder it should be obtained from (if this is limited to an extruder). for (const cura::proto::SettingExtruder& setting_extruder : slice_message->limit_to_extruder()) { const int32_t extruder_nr = setting_extruder.extruder(); //Cast from proto::int to int32_t! if (extruder_nr < 0 || extruder_nr > static_cast<int32_t>(extruder_count)) { //If it's -1 it should be ignored as per the spec. Let's also ignore it if it's beyond range. continue; } ExtruderTrain& extruder = slice.scene.extruders[setting_extruder.extruder()]; slice.scene.limit_to_extruder.emplace(setting_extruder.name(), &extruder); } //Load all mesh groups, meshes and their settings. private_data->object_count = 0; for (const cura::proto::ObjectList& mesh_group_message : slice_message->object_lists()) { private_data->readMeshGroupMessage(mesh_group_message); } logDebug("Done reading Slice message.\n"); if (!slice.scene.mesh_groups.empty()) { slice.compute(); FffProcessor::getInstance()->finalize(); flushGCode(); sendPrintTimeMaterialEstimates(); sendFinishedSlicing(); slice.reset(); private_data->slice_count++; } std::this_thread::sleep_for(std::chrono::milliseconds(250)); //Pause before checking again for a slice message. }
void CommandSocket::connect(const std::string& ip, int port) { #ifdef ARCUS private_data->socket = new Arcus::Socket(); private_data->socket->addListener(new Listener()); //private_data->socket->registerMessageType(1, &Cura::ObjectList::default_instance()); private_data->socket->registerMessageType(&cura::proto::Slice::default_instance()); private_data->socket->registerMessageType(&cura::proto::Layer::default_instance()); private_data->socket->registerMessageType(&cura::proto::Progress::default_instance()); private_data->socket->registerMessageType(&cura::proto::GCodeLayer::default_instance()); private_data->socket->registerMessageType(&cura::proto::ObjectPrintTime::default_instance()); private_data->socket->registerMessageType(&cura::proto::SettingList::default_instance()); private_data->socket->registerMessageType(&cura::proto::GCodePrefix::default_instance()); private_data->socket->registerMessageType(&cura::proto::SlicingFinished::default_instance()); private_data->socket->connect(ip, port); log("Connecting to %s:%i\n", ip.c_str(), port); while(private_data->socket->getState() != Arcus::SocketState::Connected && private_data->socket->getState() != Arcus::SocketState::Error) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); } log("Connected to %s:%i\n", ip.c_str(), port); bool slice_another_time = true; // Start & continue listening as long as socket is not closed and there is no error. while(private_data->socket->getState() != Arcus::SocketState::Closed && private_data->socket->getState() != Arcus::SocketState::Error && slice_another_time) { // Actually start handling messages. Arcus::MessagePtr message = private_data->socket->takeNextMessage(); cura::proto::SettingList* setting_list = dynamic_cast<cura::proto::SettingList*>(message.get()); if (setting_list) { handleSettingList(setting_list); } /*cura::proto::ObjectList* object_list = dynamic_cast<cura::proto::ObjectList*>(message.get()); if (object_list) { handleObjectList(object_list); }*/ cura::proto::Slice* slice = dynamic_cast<cura::proto::Slice*>(message.get()); if (slice) { // Reset object counts private_data->object_count = 0; for (auto object : slice->object_lists()) { handleObjectList(&object); } } //If there is an object to slice, do so. if (private_data->objects_to_slice.size()) { FffProcessor::getInstance()->resetMeshGroupNumber(); for (auto object : private_data->objects_to_slice) { if (!FffProcessor::getInstance()->processMeshGroup(object.get())) { logError("Slicing mesh group failed!"); } } private_data->objects_to_slice.clear(); FffProcessor::getInstance()->finalize(); flushGcode(); sendPrintTime(); sendFinishedSlicing(); slice_another_time = false; // TODO: remove this when multiple slicing with CuraEngine is safe //TODO: Support all-at-once/one-at-a-time printing //private_data->processor->processModel(private_data->object_to_slice.get()); //private_data->object_to_slice.reset(); //private_data->processor->resetFileNumber(); //sendPrintTime(); } std::this_thread::sleep_for(std::chrono::milliseconds(250)); } log("Closing connection\n"); private_data->socket->close(); #endif }
void CommandSocket::connect(const std::string& ip, int port) { d->socket = new Arcus::Socket(); //d->socket->registerMessageType(1, &Cura::ObjectList::default_instance()); d->socket->registerMessageType(1, &cura::proto::Slice::default_instance()); d->socket->registerMessageType(2, &cura::proto::SlicedObjectList::default_instance()); d->socket->registerMessageType(3, &cura::proto::Progress::default_instance()); d->socket->registerMessageType(4, &cura::proto::GCodeLayer::default_instance()); d->socket->registerMessageType(5, &cura::proto::ObjectPrintTime::default_instance()); d->socket->registerMessageType(6, &cura::proto::SettingList::default_instance()); d->socket->registerMessageType(7, &cura::proto::GCodePrefix::default_instance()); d->socket->connect(ip, port); // Start & continue listening as long as socket is not closed and there is no error. while(d->socket->state() != Arcus::SocketState::Closed && d->socket->state() != Arcus::SocketState::Error) { //If there is an object to slice, do so. if(d->objects_to_slice.size()) { FffProcessor::getInstance()->resetFileNumber(); for(auto object : d->objects_to_slice) { if(!FffProcessor::getInstance()->processMeshGroup(object.get())) { logError("Slicing mesh group failed!"); } } d->objects_to_slice.clear(); FffProcessor::getInstance()->finalize(); sendGCodeLayer(); sendPrintTime(); //TODO: Support all-at-once/one-at-a-time printing //d->processor->processModel(d->object_to_slice.get()); //d->object_to_slice.reset(); //d->processor->resetFileNumber(); //sendPrintTime(); } // Actually start handling messages. Arcus::MessagePtr message = d->socket->takeNextMessage(); cura::proto::SettingList* setting_list = dynamic_cast<cura::proto::SettingList*>(message.get()); if(setting_list) { handleSettingList(setting_list); } /*cura::proto::ObjectList* object_list = dynamic_cast<cura::proto::ObjectList*>(message.get()); if(object_list) { handleObjectList(object_list); }*/ cura::proto::Slice* slice = dynamic_cast<cura::proto::Slice*>(message.get()); if(slice) { // Reset object counts d->object_count = 0; d->object_ids.clear(); for(auto object : slice->object_lists()) { handleObjectList(&object); } } std::this_thread::sleep_for(std::chrono::milliseconds(250)); if(!d->socket->errorString().empty()) { logError("%s\n", d->socket->errorString().data()); d->socket->clearError(); } } }