Post Process from a script

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!
User avatar
dubstar-04
Posts: 698
Joined: Mon Mar 04, 2013 8:41 pm
Location: Chester, UK
Contact:

Re: Post Process from a script

Post by dubstar-04 »

Inventthis wrote: Do you know how to add that to the post processor script?
Thanks
By writing a custom script to handle this you are circumventing the regular post methods that handle the tools and WCS, please be careful and check all gcode before running it on your machine.

Tools and tool controllers are stored independently of the operations but operations have a reference to the selected tool. You can access the tool controller and tool data using op.ToolController or a helper: PathUtil.toolControllerForOp(op).

One way to combine the operations and the correct tool controllers would be loop through the operations and check if the tool is the same as the currently selected tool, if its not add the tool to a list to be processed followed by the operation, for example:

Code: Select all


import PathScripts.PathUtil as PathUtil
from PathScripts.post import linuxcnc_post as pp

objsToPost = []
currTool = None

for op in job.Operations.Group:
    tc = PathUtil.toolControllerForOp(op)
    if tc is not None and PathUtil.opProperty(op, 'Active'):
        if tc.ToolNumber != currTool:
            objsToPost.append(tc)
            print("post tool:", tc.Name)
            currTool = tc.ToolNumber
        objsToPost.append(op)
        print("post op:", op.Name)

pp.export(objsToPost, 'C:/Documents/FreeCAD-Gcode/outputFile.cnc', '')

I haven't tested this code but it should point you in the right direction.

Thanks,

Dan
User avatar
Inventthis
Posts: 23
Joined: Tue Mar 17, 2020 8:11 pm
Location: USA

Re: Post Process from a script

Post by Inventthis »

Hello Dan,
Thanks again for your great help. I do understand your concerns about writing a custom script but I am doing this because I am working on generating G-code from parametric shape for simple 2.5D door design. https://forum.freecadweb.org/viewtopic.php?f=15&t=44378
I am moving well with my project with your great help and learning Python as well.
I’ve made a slight change to your code because I was getting an error message for”Job” not defined, but moving along the code I can’t find another attribute to replace ‘opProperty’ which does not exist.
I have included a simple FC drawing of the file I used to make simple G-code of line contour that I will use later.
Thanks

Code: Select all

import PathScripts.PathUtil as PathUtil
from PathScripts.post import linuxcnc_post as pp

objsToPost = []
currTool = None

for op in App.ActiveDocument.Job.Operations.Group:
    tc = PathUtil.toolControllerForOp(op)
    if tc is not None and PathUtil.opProperty(op, 'Active'):
        if tc.ToolNumber != currTool:
            objsToPost.append(tc)
            print("post tool:", tc.Name)
            currTool = tc.ToolNumber
        objsToPost.append(op)
        print("post op:", op.Name)

pp.export(objsToPost, 'C:/Documents/FreeCAD-Gcode/outputFile.cnc', '')
Attachments
Door_example.FCStd
(63.25 KiB) Downloaded 32 times
Script-Post-Process2.jpg
Script-Post-Process2.jpg (42.17 KiB) Viewed 968 times
User avatar
dubstar-04
Posts: 698
Joined: Mon Mar 04, 2013 8:41 pm
Location: Chester, UK
Contact:

Re: Post Process from a script

Post by dubstar-04 »

Inventthis wrote:
Macro attached.

Thanks,

Dan
Attachments
CustomPost.FCMacro
(587 Bytes) Downloaded 42 times
User avatar
Inventthis
Posts: 23
Joined: Tue Mar 17, 2020 8:11 pm
Location: USA

Re: Post Process from a script

Post by Inventthis »

Hello Dan,
Thanks again for your great help.
What version of FreeCAD do you have? on my python console I get the following error message:
Traceback (most recent call last):
File "<input>", line 3, in <module>
AttributeError: module 'PathScripts.PathUtil' has no attribute 'opProperty'
My FreeCAD installation

OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.4 (GitTag)
Build type: Release
Branch: releases/FreeCAD-0-18
Hash: 980bf9060e28555fecd9e3462f68ca74007b70f8
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
User avatar
Inventthis
Posts: 23
Joined: Tue Mar 17, 2020 8:11 pm
Location: USA

Re: Post Process from a script

Post by Inventthis »

Hello Dan
Do not worry it works as planned, thanks a lot.
I downloaded FreeCAD 0.19 and in the PathUtil.py I copied the missing function to my FreeCAD 0.18 installation

Code: Select all

def opProperty(op, prop):
    '''opProperty(op, prop) ... return the value of property prop of the underlying operation (or None if prop does not exist)'''
    if hasattr(op, prop):
        return getattr(op, prop)
    if hasattr(op, 'Base'):
        return opProperty(op.Base, prop)
    return None
THANKS A MILLION :D :D :D
User avatar
dubstar-04
Posts: 698
Joined: Mon Mar 04, 2013 8:41 pm
Location: Chester, UK
Contact:

Re: Post Process from a script

Post by dubstar-04 »

Inventthis wrote: Wed Apr 29, 2020 7:10 pm THANKS A MILLION :D :D :D
You are very welcome.

Sorry for the delay in response.

I don't have 0.18 installed to test i'm afraid.

You can add a python function to the macro so that the pathutils function is no longer required.

This function is checking if the operation being processed (op) is active or not. This is made slightly more difficult because we can use dressups that wrap the operation and dressups don't have an active attribute. The example code below checks if the operation has an active attribute and returns it. If the operation is wrapped in a dressup the operation is accessed through op.Base and then checked to see if it is active. Like so:

Code: Select all

def isActive(op):
	# Check if operation is active
	if hasattr(op, 'Active'): 
		return op.Active
	# Check if operation with a dressup is active
	if hasattr(op, 'Base') and hasattr(op.Base, 'Active'):
		return op.Base.Active
	#return false if no active attribute is found
	return False

Complete Macro attached.

Thanks,

Dan
Attachments
CustomPost.FCMacro
(808 Bytes) Downloaded 34 times
User avatar
Inventthis
Posts: 23
Joined: Tue Mar 17, 2020 8:11 pm
Location: USA

Re: Post Process from a script

Post by Inventthis »

Got it Dan and used the macro. G Code output is perfect as planned.

Your help is greatly appreciated me first and all the FreeCAD community.

Thumbs Up
Post Reply