Jump to content

"Line Spacing" in Cura


Recommended Posts

Posted · "Line Spacing" in Cura

Hello together,

 

Within my print, I want to tune the line spacing. The printed lines should overlap by 20% to increase the density of my part. Normally in FDM, this is adjusted by the material extrusion multiplier or the line width, but in this case, I want the flow rate to be constant and fill the voids by overlapping. This code should improve my paste extrusion printer.

 

Because I am not familiar with coding Python scripts for Cura, I used ChatGPT 4o, which first said implementing this function is possible and wrote the code for me.

 

I stored the __init__.py in: "C:\Users\myUserName\AppData\Roaming\cura\5.6\plugins\LineSpacingAdjuster\LineSpacingAdjuster\__init__.py"

 

When starting Cura, I get the message: "The plugin LineSpacingAdjuster could not be loaded. Re-installing the plugin might solve the issue."

 

Can you explain to me why the code is not accepted and what I have to optimize so that the code will work and a function for proportional line spacing is integrated into the software?

 

Thanks a lot! Cheers, Lennard

___

Code from (__init__.py):

import logging
from ..Script import Script
 
class LineSpacingAdjuster(Script):
    def __init__(self):
        super().__init__()
 
    def getSettingDataString(self):
        return """{
            "name": "Line Spacing Adjuster",
            "key": "LineSpacingAdjuster",
            "metadata": {},
            "version": 2,
            "settings":
            {
                "line_spacing_multiplier":
                {
                    "label": "Line Spacing Multiplier",
                    "description": "Multiplier to adjust the line spacing.",
                    "type": "float",
                    "default_value": 1.0,
                    "minimum_value": 0.1,
                    "maximum_value": 2.0,
                    "unit": ""
                }
            }
        }"""
 
    def execute(self, data):
        logging.info("Plugin execution started")
        line_spacing_multiplier = self.getSettingValueByKey("line_spacing_multiplier")
        logging.info(f"Line Spacing Multiplier: {line_spacing_multiplier}")
        new_data = []
 
        for layer in data:
            lines = layer.split("\n")
            new_lines = []
 
            for line in lines:
                if line.startswith("G1") and "X" in line and "Y" in line:
                    parts = line.split(" ")
                    new_parts = []
 
                    for part in parts:
                        if part.startswith("X"):
                            try:
                                x_value = float(part[1:])
                                new_x_value = x_value * line_spacing_multiplier
                                new_parts.append(f"X{new_x_value:.4f}")
                            except ValueError as e:
                                logging.error(f"Error parsing X value: {part} - {e}")
                                new_parts.append(part)
                        elif part.startswith("Y"):
                            try:
                                y_value = float(part[1:])
                                new_y_value = y_value * line_spacing_multiplier
                                new_parts.append(f"Y{new_y_value:.4f}")
                            except ValueError as e:
                                logging.error(f"Error parsing Y value: {part} - {e}")
                                new_parts.append(part)
                        else:
                            new_parts.append(part)
 
                    new_line = " ".join(new_parts)
                    new_lines.append(new_line)
                else:
                    new_lines.append(line)
 
            new_layer = "\n".join(new_lines)
            new_data.append(new_layer)
 
        logging.info("Plugin execution finished")
        return new_data
  • Link to post
    Share on other sites

    Posted · "Line Spacing" in Cura

    The python file you wrote is not a plugin by itself, but it is a script, to be used by the PostProcessing plugin.

     

    I have not checked the code at all, but the file path and  file name should be like this:

    C:\Users\myUserName\AppData\Roaming\cura\5.6\scripts\LineSpacingAdjuster.py

  • Link to post
    Share on other sites

    Posted · "Line Spacing" in Cura

    Having looked at the code a bit, this almost certainly does not do what you want. It will scale the entire layer horizontally. Yes, the lines will be closer to or further from eachother, but your dimensional accuracy will be shot.

     

    5 hours ago, Lennard said:

    I want the flow rate to be constant and fill the voids by overlapping

    So lower the line width (which gets the lines closer to eachother) and increase the flow (which compensates for the decreased flow for the thinner lines). Or check out the "skin overlap" and "skin overlap percentage" settings.

  • Link to post
    Share on other sites

    Posted · "Line Spacing" in Cura
    32 minutes ago, ahoeben said:

    The python file you wrote is not a plugin by itself, but it is a script, to be used by the PostProcessing plugin.

    Thanks a lot for your feedback! If I want a specific infill setting, should I code a script?

     

    As part of my research, I want the nozzle to come closer to the last line to increase pressure, which helps the paste fill the voids. My goal is to avoid using width and flow rate settings because they don't fully meet my validation needs.

  • Link to post
    Share on other sites

    Posted · "Line Spacing" in Cura

    The fact that you managed to get a vaguely complete script out of ChatGPT is impressive. I've had to tell it multiple times it's still making the same mistake when I'm just trying to create a loop. Gemini is better, in that when I tell it it did something wrong, it changes it... usually to something else wrong.

     

    Although someone needs to teach it about Script.getValue() and Script.putValue()

    Also Python's string.splitlines()

    Also that script doesn't scale positions on travel moves so you're gonna get some nice long lines you don't want as it gets further from 0, 0.

     

    I will give it some style points for putting the number formatting in the f-strings though.

     

    Anyway, if I may say something useful:

    Can it be done in a script? Theoretically, but it'd be a major hassle. Especially if you're not just printing lines that go straight along the X/Y axis.

    Is ChatGPT's concept wrong? Entirely.

    How would I go about it?

    • The script would need to get the total area for a section, because it's going to have to add new lines.
      • Figuring out the sections if it's not a contiguous body isn't necessarily easy, I'd probably try judging it by long travel moves within the same feature type, but that's still a crapshoot.
    • Instead of just putting a multiplier on the values, I'd use a variable to keep track of the position of the previous line and then go <intended print separation here> past it until it filled the total area, which would involve it creating new lines.
      • This means that for any diagonal line you'd need to use Pythagorean calculations.
      • Curves would be either extremely difficult, or just very, very difficult if you use @ahoeben's Arc Welder plugin, which would rely on your printer supporting G2 and G3 moves.
    1 hour ago, Lennard said:

    My goal is to avoid using width and flow rate settings because they don't fully meet my validation needs.

    I'm not sure what you mean by "validation needs" but adjusting the line width and material flow will give you exactly the same extrusions as making an overcomplicated script to do the same thing - assuming your script was perfect.

     

    In summary, chatbots still don't know how to write code and you should just adjust the settings like @ahoeben suggests because trying to write a script that can do this is a fool's errand that even I'm not crazy enough to try and take on (and I've taken on some pretty crazy scripts... sometimes they're not even for me, they're for other people on the forum - I've long since learned that asking "why" doesn't really give you any more knowledge than you started with).

  • Link to post
    Share on other sites

    Posted · "Line Spacing" in Cura

    Many thanks for your detailed contribution! 

     

    Using a paste extruder and a simple hand-written G-code with a 20% overlap, it was shown in a paper that the manufacturing process produces a nearly complete density in the part while maintaining a high dimensional accuracy. 

     

    If I reduce the track width or increase the extrusion multiplier, the result is that I produce a wider track, which leads to reduced dimensional accuracy. Before I completely close the gaps between the tracks with the paste, I have to create enough over-extrusion so that the paste builds up higher than the actual layer. But in this case the density of the component and the dimensional precision are both critical criteria.

     

    I wanted to validate this behavior for my paste extruder in a series of tests. However, I hoped that this would be easier to implement, as there is already a hidden function in Cura that influences the path distance and can be manipulated. 

  • Link to post
    Share on other sites

    Posted · "Line Spacing" in Cura
    15 hours ago, Lennard said:

    If I reduce the track width or increase the extrusion multiplier, the result is that I produce a wider track, which leads to reduced dimensional accuracy

    So you also need the Horizontal Expansion setting in the mix.

  • Link to post
    Share on other sites

    Posted · "Line Spacing" in Cura
    18 hours ago, Lennard said:

    If I reduce the track width or increase the extrusion multiplier, the result is that I produce a wider track, which leads to reduced dimensional accuracy.

    Isn't it just a volumetric calculation anyway? i.e. half width + 200% flow = same flow as full width? Unless a paste extruder doesn't have a circular nozzle like your average 3D printer it should just be a matter of how much material you're pumping out. Or is this one of those situations where I don't get the maths and realise I should have stayed in school?

  • Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
    • Our picks

      • UltiMaker Cura 5.8 Stable released 🎉
        In the Cura 5.8 stable release, everyone can now tune their Z seams to look better than ever. Method series users get access to new material profiles, and the base Method model now has a printer profile, meaning the whole Method series is now supported in Cura!
        • 5 replies
      • Introducing the UltiMaker Factor 4
        We are happy to announce the next evolution in the UltiMaker 3D printer lineup: the UltiMaker Factor 4 industrial-grade 3D printer, designed to take manufacturing to new levels of efficiency and reliability. Factor 4 is an end-to-end 3D printing solution for light industrial applications
          • Thanks
          • Like
        • 3 replies
    ×
    ×
    • Create New...