Translate imported gcode?

Here's the place for discussion related to CAM/CNC and the development of the Path module.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
etrombly
Posts: 144
Joined: Thu Dec 05, 2019 6:50 pm

Re: Translate imported gcode?

Post by etrombly »

So I've added an option to use the operation placement, can still change the name later. Need to figure out two issues now though. Once I modify the path to apply the placement the view is updated to the new path plus the operation placement. The path is correct but the view is off now. Also once I move the path I'm getting some moves that look wrong, need to figure out why.

Image

Image

Code: Select all

    def execute(self, obj):
        if obj.Gcode:
            s = ""
            for l in obj.Gcode:
                s += str(l)
            if s:
                path = Path.Path(s)
                if obj.OperationPlacement:
                    base = obj.Placement.Base
                    for x in range(len(path.Commands)):
                        if path.Commands[x].Name in movecommands:
                            new = path.Commands[x]
                            new.Placement.translate(base)
                            path.deleteCommand(x)
                            path.insertCommand(new, x)
                obj.Path = path
I couldn't modify any of the commands in place, so had to delete then reinsert the new command.

edit: figured out the extra moves are because the translate is adding that direction even if the original command didn't have it. Working on a fix now.
User avatar
sliptonic
Veteran
Posts: 3459
Joined: Tue Oct 25, 2011 10:46 pm
Location: Columbia, Missouri
Contact:

Re: Translate imported gcode?

Post by sliptonic »

Russ4262 wrote: Fri Mar 13, 2020 4:01 am @Sliptonic, is the current FC post-processing work flow already setup to apply this placement across the board to all post-processors? Will we only need to insert code into the post-processing work flow to apply the translation before exporting data to a specific post module? Or, will we have to update each post individually to apply the translation?
Shouldn't have to do anything downstream in the post processing part. All the translation should be handled, either in the preprocessor or the Custom.
etrombly
Posts: 144
Joined: Thu Dec 05, 2019 6:50 pm

Re: Translate imported gcode?

Post by etrombly »

It works with this code, but is a little slow (just one time when you change use operation placement to true), maybe someone more familiar with the code base could suggest a better way.

Code: Select all

    def execute(self, obj):
        if obj.Gcode:
            s = ""
            for l in obj.Gcode:
                s += str(l)
            if s:
                path = Path.Path(s)
                if obj.OperationPlacement:
                    for x in range(len(path.Commands)):
                        if path.Commands[x].Name in movecommands:
                            base = copy(obj.Placement.Base)
                            new = path.Commands[x]
                            if 'X' not in path.Commands[x].Parameters:
                                base[0] = 0.0
                            if 'Y' not in path.Commands[x].Parameters:
                                base[1] = 0.0
                            if 'Z' not in path.Commands[x].Parameters:
                                base[2] = 0.0
                            new.Placement.translate(base)
                            path.deleteCommand(x)
                            path.insertCommand(new, x)
                obj.Path = path
Still have the issue with the viewport applying the placement twice now. But other than that I think it looks good.

Image
User avatar
sliptonic
Veteran
Posts: 3459
Joined: Tue Oct 25, 2011 10:46 pm
Location: Columbia, Missouri
Contact:

Re: Translate imported gcode?

Post by sliptonic »

Do you have your code in a git repo yet? It'll be easier to test and offer suggestions if it is.
etrombly
Posts: 144
Joined: Thu Dec 05, 2019 6:50 pm

Re: Translate imported gcode?

Post by etrombly »

I left my laptop at work and didn't commit it yet. I'll post the repo on Monday.
etrombly
Posts: 144
Joined: Thu Dec 05, 2019 6:50 pm

Re: Translate imported gcode?

Post by etrombly »

code is up at https://github.com/etrombly/FreeCAD/tree/gcode . The only files I've modified so far are /Mod/Path/PathScripts/PathCustom.py and /Mod/Path/PathScripts/post/gcode_pre.py (example_pre.py changed to import as PathCustom).
User avatar
sliptonic
Veteran
Posts: 3459
Joined: Tue Oct 25, 2011 10:46 pm
Location: Columbia, Missouri
Contact:

Re: Translate imported gcode?

Post by sliptonic »

The duplicated path is caused because the Job is showing its own version.

Your preprocessor was bringing all the gcode into a list that contained a single line. I made it return a list of lines and corrected a little whitespace problem after the line numbers were removed.
The Custom code looks fine but I tried a slightly more pythonic way to consider.

Thanks for working on this!

https://github.com/sliptonic/FreeCAD/tree/improvecustom
etrombly
Posts: 144
Joined: Thu Dec 05, 2019 6:50 pm

Re: Translate imported gcode?

Post by etrombly »

Thanks for the help. The preprocessor was just copied from the example, so all those changes should go back into example_pre too.

Your version looks a lot better, I had it stuck in my head to modify the existing commands, but they are protected, so I ended up with what you saw. Does Path.addCommands clear out the existing commands? Also would it be ok to add a setCommand(index, newCommand) function to allow you to modify a command rather than then having to clear the old command and add a new one?

looks like there might be a copy paste error:

Code: Select all

obj.addProperty("App::PropertyBool", "OperationPlacement", "Plen(self.toolTableView.selectedIndexes()) > 0len(self.toolTableView.selectedIndexes()) > 0ath", "Use operation placement")
not sure if the "len(self.toolTableView.selectedIndexes()) > 0len(self.toolTableView.selectedIndexes()) > 0" was supposed to go somewhere else.

I'm still not doing anything with the M6 commands. Anything else you think would be good to add before I do a pull request?
User avatar
sliptonic
Veteran
Posts: 3459
Joined: Tue Oct 25, 2011 10:46 pm
Location: Columbia, Missouri
Contact:

Re: Translate imported gcode?

Post by sliptonic »

etrombly wrote: Mon Mar 16, 2020 10:40 pm Thanks for the help. The preprocessor was just copied from the example, so all those changes should go back into example_pre too.
ok. Will you make the changes to example_pre as well and include them in your PR?
Your version looks a lot better, I had it stuck in my head to modify the existing commands, but they are protected, so I ended up with what you saw. Does Path.addCommands clear out the existing commands? Also would it be ok to add a setCommand(index, newCommand) function to allow you to modify a command rather than then having to clear the old command and add a new one?
Good catch. Should just create a new Path and user Path.insertCommand(newcommand) instead.
looks like there might be a copy paste error:

Code: Select all

obj.addProperty("App::PropertyBool", "OperationPlacement", "Plen(self.toolTableView.selectedIndexes()) > 0len(self.toolTableView.selectedIndexes()) > 0ath", "Use operation placement")
not sure if the "len(self.toolTableView.selectedIndexes()) > 0len(self.toolTableView.selectedIndexes()) > 0" was supposed to go somewhere else.
<stupid fat fingers>
I'm still not doing anything with the M6 commands. Anything else you think would be good to add before I do a pull request?
On second thought, I think we should avoid using the actual placement thing and just create a new offset placement property. and then add it to all the move commands. It's simpler. The user can't use the transform drag tool to move the gcode but it avoids the duplicate path problem and simplifies the code. I've pushed my flavor using this to my branch.

The only thing I can think is to modify the gcode_pre to create multple customs, one for each M6. If you don't need it yourself, feel free to send PR as is.
etrombly
Posts: 144
Joined: Thu Dec 05, 2019 6:50 pm

Re: Translate imported gcode?

Post by etrombly »

Been a little busy, but still thinking on how to do this. The visual feedback was the main benefit from doing this in freecad, so I'd like to figure out a way to use the placement still. You could modify the placement to get visual feedback, then set the offset based on those values, but then you'd be inputting it in two different places. If that ends up being the easiest way to do it that's fine.
Do you know why the placement isn't used anywhere in the path workbench(well other than updating the viewport)? It seems a little inconsistent with the way the other workbenches operate.

edit: this is a little off topic, but I've noticed that the gcode parsing is done in several different places. Would it make sense while I'm working on this to consolidate it? I was thinking of making a gcode object and having a fromStr, toStr, toPath function, etc. That way it should be a little more consistent.
Post Reply