2017-10-31 2 views
-1

Ziel:lesen X, Y, Z aus der Datei, Tupel/Liste String ohne Anführungszeichen durch Liste gefolgt ändern

Python verwenden, erhalten FreeCAD.Vector(223.90,67.99,45.00) aus TXT-Datei.

Comma getrennte Textdateiformat:

1.0,8.2,9.888 
6.345,4.32,2.43 
... 

d.h. X, Y, Z als Schwimmer. Eine spezifische Anwendung "FreeCAD" Python verwendet als Basisskriptsprache (Makros) Die native Methode zu einem nativen FreeCAD Objekt ist VectorFreeCAD.Vector d.h. der Vektor (X, Y, Z), d.h. FreeCAD.Vector(105453.164062,90917.8671875,1274.77026367).

Als weiteres ausführliches Beispiel:

>>> points=[FreeCAD.Vector(-2.50563430786,0.3603053689,0.0),FreeCAD.Vector(-1.67500686646,0.959897279739,0.0),FreeCAD.Vector(-0.53083139658,0.393310427666,0.0),FreeCAD.Vector(0.767367601395,0.932393193245,0.0)] 
>>> line = Draft.makeWire(points,closed=False,face=True,support=None) 
>>> Draft.autogroup(line) 

Hier ist ein Beispiel für die data.txt Datei:

105507.460938 91080.125 1331.37109375 
105509.648438 91077.9375 1326.85534668 
105501.375 91072.890625 1318.00634766 
105487.0 91070.3984375 1318.89746094 

Hinweis: Das Trennzeichen kann Komma oder Leerzeichen sein - es ist nicht wichtig und kann entweder

Das Problem:

eine Variable erstellen, die diesen Wert hat:

[FreeCAD.Vector(-2.50563430786,0.3603053689,0.0),FreeCAD.Vector(-1.67500686646,0.959897279739,0.0),...

oder

Erarbeitung einen Weg, um die Eingabe zu montieren, so dass die FreeCAD die XYZ-Eingang akzeptieren kann.

My-Code so weit:

# -*- coding: utf-8 -*- 
# 10/26/2017 6:54:16 AM 
# For Python, PEP 8 has emerged as the style guide 
# http://docs.python-guide.org/en/latest/intro/learning/ 
# https://www.freecadweb.org/wiki/Code_snippets 
# https://github.com/FreeCAD/FreeCAD 
import FreeCAD,Draft,Arch 
# In Python, all the statements indented by the same number of character spaces 
# after a programming construct are considered to be part of a single block of code. 
# Python uses indentation as its method of grouping statements. 

## @package Makewire 
# \ingroup ARCH 
# \brief Creates DWire object from file of 3D coordinates 
# 
# This program opens a file, reads each space delimited line 
# and draws a dwire object 
__title__ = "DWire Import" 
__author__ = "Greg Robinson" 
__url__ = "http://Lucrosol.com" 
# example that works 
# p1 = FreeCAD.Vector(0,0,1) 
# p2 = FreeCAD.Vector(1,1,2) 
# p3 = FreeCAD.Vector(2,4,3) 
# Draft.makeWire([p1,p2,p3],closed=False) 
# https://www.freecadweb.org/wiki/Draft_API 
# hard coded path & file of coordinates 
# Example: 
#105507.460938 91080.125 1331.37109375 
#105509.648438 91077.9375 1326.85534668 
#105501.375 91072.890625 1318.00634766 
#105487.0 91070.3984375 1318.89746094 
#105482.851562 91068.8203125 1318.02026367 
#105480.5625 91063.5234375 1318.0456543 
#105480.351562 90950.0859375 1318.84057617 
#105475.992188 90940.046875 1319.13378906 
#105473.546875 90933.515625 1318.09472656 
#105473.820312 90897.3359375 1321.03942871 
#105473.820312 90897.3359375 1321.03942871 
#105475.671875 90889.4140625 1276.28381348 
#105454.164062 90909.0078125 1274.7479248 
#105453.164062 90917.8671875 1274.77026367 
# Find & Replace Examples 
# x = [s.replace('a', 'b') for s in x] 
# words = [w.replace('[br]', '<br />') for w in words] 
# 
# function readfile 
def fileread(): 
    myfile = open("C:/Users/Greg/Desktop/txt/pipe_example.txt") 
    # Create empty set 
    global lines 
    lines = [] 
    for l in myfile.readlines(): 
     lines.append(l) 
    myfile.close() 
fileread() 
# End readfile function 

# contents of file are now in list 
# Note 
# Lists are enclosed in square brackets ([ and ]) and tuples in parentheses ((and)). 
numbers = [] 
dwire_list = [] 
# Process list from file into float then get them into vectors 
for line in lines: 
    newline = [float(x) for x in line.split()] 
    dwire_list.append(newline) 
# append the processed items together 

a_list = [] 
coords = [] 
count = len(dwire_list) 
while (count > 0): 
    coord0 = FreeCAD.Vector(dwire_list[count - 1]) 
    coords.append(coord0) 
    count = count - 1 
# 
print "Loop Executed, Dwire should have appeared on file" 
# Creat Dwire 
# Draft.makeWire(coords,closed=False) 
#  "makePipe([baseobj,diameter,length,placement,name]): creates an pipe object from the given base object" 
# Create Dwire 
Draft.makeWire(points,closed=False,face=True,support=None) 
print coords 
line = Draft.makeWire(coords,closed=False,face=True,support=None) 
FreeCAD.ActiveDocument.recompute() 

jedoch:

Im Allgemeinen ist die Frage, wie eine solche Hybrid-String-Liste zu machen, ist mir schwer zu fassen.

ich in diesem Stadium bin oder Arbeits durch Ausgabe:

## Open the file with read only permit 
# f = open('C:/Users/Greg/Desktop/txt/pipe_example_s.txt', "r") 

## use readlines to read all lines in the file 
## The variable "lines" is a list containing all lines 
#lines = f.read().splitlines() 


with open('C:/Users/Greg/Desktop/txt/pipe_example_c.txt') as f: 
    mylist = [tuple(map(float, i.split(','))) for i in f] 

## close the file after reading the lines. 
#f.close() 
print mylist 
print "One" 
print mylist[0] 

p1 = 34.8999 
print "FreeCAD.Vector (%s)" % '34.8999, 2.8997, 3.09665' 

p1 = "FreeCAD.Vector (%s)" % '34.8999, 2.8997, 3.09665' 
print p1 

p2 = 123.456 
p1 = "FreeCAD.Vector (%s)" % (p2) + '34.8999, 2.8997, 3.09665' 
print p1 


p3 = tuple(mylist) 
print p3 

s = '(0.0034596999, 0.0034775001, 0.0010091923)' 
s = s.replace(',', 'FreeCAD.Vector ') 
print(s) # -> [0.0034596999 0.0034775001 0.0010091923] 

print mylist[1] 

Welche Ausgänge:

[(102360.003871, 92614.733022, 1114.159952), (102360.045926, 92613.778689, 1114.097542), (102361.109418, 92613.926808, 1114.123386), (102360.90909, 92614.061128, 1144.246289), (102360.008406, 92614.203715, 1144.217125), (102360.073353, 92615.032739, 1145.531946), (102338.988007, 92623.107091, 1113.028396), (102339.457605, 92623.63571, 1113.615987), (102339.991842, 92624.037633, 1112.722525), (102305.610817, 92666.076043, 1112.399744), (102306.151843, 92666.628226, 1112.942683), (102306.687078, 92666.984192, 1112.139385), (102323.38305, 92643.109377, 1112.511513), (102323.824052, 92643.468957, 1113.20053), (102324.553584, 92643.911892, 1112.594155), (102359.99434, 92614.715556, 1113.832716), (102359.998296, 92613.819679, 1113.790412), (102361.103982, 92613.907801, 1113.974154), (102361.095313, 92614.148855, 1129.857483), (102359.940314, 92614.062477, 1129.823867), (102360.029689, 92614.917234, 1130.076275), (102360.090856, 92614.626343, 1145.496362), (102360.454485, 92613.963864, 1145.244853), (102361.020532, 92614.211705, 1145.234733)] 
One 
(102360.003871, 92614.733022, 1114.159952) 
FreeCAD.Vector (34.8999, 2.8997, 3.09665) 
FreeCAD.Vector (34.8999, 2.8997, 3.09665) 
FreeCAD.Vector (123.456)34.8999, 2.8997, 3.09665 
((102360.003871, 92614.733022, 1114.159952), (102360.045926, 92613.778689, 1114.097542), (102361.109418, 92613.926808, 1114.123386), (102360.90909, 92614.061128, 1144.246289), (102360.008406, 92614.203715, 1144.217125), (102360.073353, 92615.032739, 1145.531946), (102338.988007, 92623.107091, 1113.028396), (102339.457605, 92623.63571, 1113.615987), (102339.991842, 92624.037633, 1112.722525), (102305.610817, 92666.076043, 1112.399744), (102306.151843, 92666.628226, 1112.942683), (102306.687078, 92666.984192, 1112.139385), (102323.38305, 92643.109377, 1112.511513), (102323.824052, 92643.468957, 1113.20053), (102324.553584, 92643.911892, 1112.594155), (102359.99434, 92614.715556, 1113.832716), (102359.998296, 92613.819679, 1113.790412), (102361.103982, 92613.907801, 1113.974154), (102361.095313, 92614.148855, 1129.857483), (102359.940314, 92614.062477, 1129.823867), (102360.029689, 92614.917234, 1130.076275), (102360.090856, 92614.626343, 1145.496362), (102360.454485, 92613.963864, 1145.244853), (102361.020532, 92614.211705, 1145.234733)) 
(0.0034596999FreeCAD.Vector 0.0034775001FreeCAD.Vector 0.0010091923) 
(102360.045926, 92613.778689, 1114.097542) 
+3

Bitte lesen Sie unsere Hilfe. Insbesondere, wie Sie ein [minimales, vollständiges und überprüfbares Beispiel] erstellen (https://stackoverflow.com/help/mcve). Sie haben viel zu viele Informationen gegeben, daher ist Ihre Frage ziemlich unklar. – bendl

+0

Nach dem 's = s.replace (',', 'FreeCAD.Vector')', sollte das Ergebnis gedruckt werden '(0.0034596999FreeCAD.Vector 0.0034775001FreeCAD.Vector 0.0010091923)', ** Not **, was derzeit in der Nähe angezeigt wird das Ende deiner Frage. Bitte [bearbeiten] Sie Ihre Frage und erstellen Sie ein ** minimales ** Beispiel, das das Problem darstellt, dem Sie begegnen (und es richtig macht). – martineau

+0

Es sieht so aus, als ob FreeCAD direkt aus CSV importieren kann. Warum musst du Code schreiben, um es zu tun?Oder erstellt dies nicht FreeCAD-Vektoren? https://www.freecadweb.org/wiki/Spreadsheet_CSV – Wodin

Antwort

0

Es ist nicht ganz klar, was Sie mit Hilfe benötigen, aber vielleicht wird diese geben Ihnen einige Ideen, die Ihnen helfen könnten, das Problem zu lösen:

>>> def vector(x, y, z): 
...  print("vector: x=%f, y=%f, z=%f" % (x, y, z)) 
... 
>>> vector(1, 2, 3) 
vector: x=1.000000, y=2.000000, z=3.000000 
>>> v = [1, 2, 3] 
>>> vector(*v) 
vector: x=1.000000, y=2.000000, z=3.000000 
Verwandte Themen