예제 #1
0
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.
}
예제 #2
0
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::PrintTimeMaterialEstimates::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();

        /*
         * handle a message which consists purely of a SettingList
        cura::proto::SettingList* setting_list = dynamic_cast<cura::proto::SettingList*>(message.get());
        if (setting_list)
        {
            handleSettingList(setting_list);
        }
        */

        /*
         * handle a message which consists purely of an ObjectList
        cura::proto::ObjectList* object_list = dynamic_cast<cura::proto::ObjectList*>(message.get());
        if (object_list)
        {
            handleObjectList(object_list);
        }
        */

        // Handle the main Slice message
        cura::proto::Slice* slice = dynamic_cast<cura::proto::Slice*>(message.get()); // See if the message is of the message type Slice; returns nullptr otherwise
        if (slice)
        {
            const cura::proto::SettingList& global_settings = slice->global_settings();
            for (auto setting : global_settings.settings())
            {
                FffProcessor::getInstance()->setSetting(setting.name(), setting.value());
            }
            // Reset object counts
            private_data->object_count = 0;
            for (auto object : slice->object_lists())
            {
                handleObjectList(&object, slice->extruders());
            }
        }

        //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();
            sendPrintTimeMaterialEstimates();
            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();

            //sendPrintTimeMaterialEstimates();
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(250));
    }
    log("Closing connection\n");
    private_data->socket->close();
#endif
}