File size: 5,094 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# SPDX-License-Identifier: LGPL-2.1-or-later

# ***************************************************************************
# *   Copyright (c) 2020 sliptonic <shopinghewoods@gmail.com>               *
# *                                                                         *
# *   This file is part of the FreeCAD CAx development system.              *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   FreeCAD is distributed in the hope that it will be useful,            *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU Lesser General Public License for more details.                   *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with FreeCAD; if not, write to the Free Software        *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************/

import FreeCAD
import Part
import Path
import PathScripts.PathUtils as PathUtils
import datetime
import importDXF
from builtins import open as pyopen

TOOLTIP = """
This is a postprocessor file for the Path workbench. It is used to
take a pseudo-G-code fragment outputted by a Path object, and output
a dxf file.
Operations are output to layers.
vertical moves are ignore
All path moves are flattened to z=0

Does NOT remove redundant lines.  If you have multiple step-downs in your
operation, you'll get multiple redundant lines in your dxf.

import dxf_post
"""

TOOLTIP_ARGS = """
Arguments for dxf:
"""
now = datetime.datetime.now()

# # These globals set common customization preferences
OUTPUT_HEADER = True

if False:
    Path.Log.setLevel(Path.Log.Level.DEBUG, Path.Log.thisModule())
    Path.Log.trackModule(Path.Log.thisModule())
else:
    Path.Log.setLevel(Path.Log.Level.INFO, Path.Log.thisModule())
Path.Log.setLevel(Path.Log.Level.INFO, Path.Log.thisModule())


def processArguments(argstring):
    pass
    # global OUTPUT_HEADER


def export(objectslist, filename, argstring):
    doc = FreeCAD.ActiveDocument
    print("postprocessing...")
    layers = []
    processArguments(argstring)
    for i in objectslist:
        result = parse(i)
        if len(result) > 0:
            layername = i.Name
            grp = doc.addObject("App::DocumentObjectGroup", layername)
            for o in result:
                o.adjustRelativeLinks(grp)
                grp.addObject(o)
            layers.append(grp)

    dxfWrite(layers, filename)


def dxfWrite(objlist, filename):
    importDXF.export(objlist, filename)


def parse(pathobj):
    """accepts a Path object.  Returns a list of wires"""

    feedcommands = Path.Geom.CmdMove
    rapidcommands = Path.Geom.CmdMoveRapid

    edges = []
    objlist = []

    # Gotta start somewhere.  Assume 0,0,0
    curPoint = FreeCAD.Vector(0, 0, 0)
    for c in PathUtils.getPathWithPlacement(pathobj).Commands:
        Path.Log.debug("{} -> {}".format(curPoint, c))
        if "Z" in c.Parameters:
            newparams = c.Parameters
            newparams.pop("Z", None)
            flatcommand = Path.Command(c.Name, newparams)
            c.Parameters = newparams
        else:
            flatcommand = c

        # ignore gcode that isn't moving
        if flatcommand.Name not in feedcommands + rapidcommands:
            Path.Log.debug("non move")
            continue

        # ignore pure vertical feed and rapid
        if (
            flatcommand.Parameters.get("X", curPoint.x) == curPoint.x
            and flatcommand.Parameters.get("Y", curPoint.y) == curPoint.y
        ):
            Path.Log.debug("vertical")
            continue

        # feeding move.  Build an edge
        if flatcommand.Name in feedcommands:
            edges.append(Path.Geom.edgeForCmd(flatcommand, curPoint))
            Path.Log.debug("feeding move")

        # update the curpoint
        curPoint.x = flatcommand.Parameters.get("X", curPoint.x)
        curPoint.y = flatcommand.Parameters.get("Y", curPoint.y)

    if len(edges) > 0:
        candidates = Part.sortEdges(edges)
        for c in candidates:
            obj = FreeCAD.ActiveDocument.addObject("Part::Feature", "Wire")
            obj.Shape = Part.Wire(c)
            objlist.append(obj)

    return objlist