1

I'm quite new at programming with python. For the use of programming a machine I need to work with g-code. Therefore I use an Python program to read out the gcode from a data. Now I have to combine the code like following:

Code1:

  • line1 (a, b, c)
  • line2 (d, e, f)

Code2:

  • line1 (g, h, i)
  • line2 (j, k, l)

the result should look like

  • line1 (a,b,c, g, h, i)
  • line2 (d, e, f, j, k, l)

They code I have by now basically only creates the code for both. This does not merge it. I know I have to work with "numpy" somehow but I stuck.

import dxfgrabber
import numpy as np

left = dxfgrabber.readfile('data1')
right = dxfgrabber.readfile('data2')


def createcode(code):
    for i in code.entities:
        for p in i.points:
            mylist = np.array(p)
            print(mylist)


createcode(left)
createcode(right)
Zissouu
  • 924
  • 2
  • 10
  • 17
Rob-In
  • 19
  • 5

2 Answers2

1

Given

import itertools as it

import numpy as np


left = tuple("abc"), tuple("def")
right = tuple("ghi"), tuple("jkl")

Code

Merging can be accomplished with simple chaining:

[tuple(it.chain.from_iterable(i)) for i in zip(left, right)]
# [('a', 'b', 'c', 'g', 'h', 'i'), ('d', 'e', 'f', 'j', 'k', 'l')]

Here items are zipped together and flattened in a list comprehension.

Extended to a single function that prints numpy arrays (or any desired output), you can try the following:

def merge(*iterables):
    """Print merged iterables."""
    for i in zip(*iterables):
        result = tuple(it.chain.from_iterable(i))
        result = np.array(result)
        print(result)

merge(left, right)
# ['a' 'b' 'c' 'g' 'h' 'i']
# ['d' 'e' 'f' 'j' 'k' 'l']

Here any number of iterables can be zipped together.

Demo

lt = [(10.0, 10.0, 0.0), (90.0, 10.0, 0.0), (90.0, 90.0, 0.0), (10.0, 90.0, 0.0)]
rt = [(20.0, 3.0, 6.0), (16.0, 6.0, 9.0), (5.0, 7.0, 7.0), (9.0, 2.0, 8.0)]

merge("abcd", lt, rt)
# ['a' '10.0' '10.0' '0.0' '20.0' '3.0' '6.0']
# ['b' '90.0' '10.0' '0.0' '16.0' '6.0' '9.0']
# ['c' '90.0' '90.0' '0.0' '5.0' '7.0' '7.0']
# ['d' '10.0' '90.0' '0.0' '9.0' '2.0' '8.0']
pylang
  • 40,867
  • 14
  • 129
  • 121
  • Many thanks it works. Is there an easy way to delete all `0.0` within the output? – Rob-In Sep 26 '17 at 19:21
  • The float outputs come from the float inputs. You can pre-process the inputs as integers via `[tuple(map(int, i)) for i in lt[0]]`. – pylang Sep 26 '17 at 19:27
  • `merge("abcd", lt[0], rt[0])`what are the numbers within the brackets for? If I have a list with more than four rows it appears to somehow doesn't work. – Rob-In Sep 26 '17 at 20:48
  • Those are list indices. It is selecting the first item in the list, which for `lt` happens to be another list. `merge` works for an iterable of iterables, e.g. `[[1,2,3], "abc", (1, "b")]`. If your input is more nested, you will need to pre-process it. Note `lt` is a list of a list of iterables and is thus preprocessed. Should you require different inputs, post an example of your actual data. – pylang Sep 26 '17 at 20:59
  • `lt` and `rt` always consist of 3 numbers in a row as a column. You can see it as x, y, z axis. But the number of rows is variable! This depends on the file we compile with `createcode`. After merging we basically have `x1, y1, z1, x2, y2, z2`. Hope this helps. – Rob-In Sep 26 '17 at 21:13
  • No, doesn't help leaving `[0]`. It just goes back to writing it again under on another instead of side by side. – Rob-In Sep 26 '17 at 21:40
  • The results are shown for the given inputs. There is no `[0]` in the update. Please post exactly the input and expected output you want. – pylang Sep 26 '17 at 21:49
0

What about this?

def createcode(code):
    return [[p for p in i.points] for i in code.entities]

You can apply this funcion for both cases, for example:

a = createdcode(left)
b = createdcode(right)

At this instance you have two list. Then you can combine them:

c = [u + j for u, j in zip(a, b)]

Then you can print them:

for i in c:
    print(i)
Eduardo
  • 687
  • 1
  • 6
  • 24
  • The output which is created by the loop lookes like: `[10.0, 10.0, 0.0] [90.0, 10.0, 0.0] [90.0, 90.0, 0.0] [10.0, 90.0, 0.0]` and I need in each line at the end the code from the "right" data. The right data is in the same format. – Rob-In Sep 26 '17 at 17:34
  • I update the answer based on your function. You need iterate in same function for the right data? If you add the _right data_, every list must contains 6 elements? Ok, I think I understood. Wait for the answer. – Eduardo Sep 26 '17 at 17:51
  • I tried to explain in an additional answer. Maybe this helps you to understand the issue. – Rob-In Sep 26 '17 at 18:03