簡體   English   中英

從文件中讀取 X、Y、Z,將元組/列表修改為不帶引號的字符串,后跟列表

[英]Read X,Y,Z from file, modify tuple / list to unquoted string followed by list

客觀的:

使用 Python,從 .txt 文件中獲取FreeCAD.Vector(223.90,67.99,45.00)

逗號分隔的文本文件格式:

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

即 X,Y,Z 作為浮點數。 一個特定的應用程序“FreeCAD”使用 Python 作為基本腳本語言(宏) 原生 FreeCAD 對象的原生方法是VectorFreeCAD.Vector ,向量是 (X,Y,Z),即FreeCAD.Vector(105453.164062,90917.8671875,1274.77026367) .

作為更詳細的例子:

>>> 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)

以下是data.txt文件的示例:

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

注意:分隔符可以是逗號或空格 - 這並不重要,可以是

問題:

創建具有此值的變量:

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

或者

設計一種組裝輸入的方法,以便 FreeCAD 可以接受 XYZ 輸入。

到目前為止我的代碼:

# -*- 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()

然而:

一般來說,如何制作這樣一個混合字符串列表的問題對我來說是難以捉摸的。

我正處於這個階段或正在解決這個問題:

## 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]

哪些輸出:

[(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)

您需要什么幫助並不完全清楚,但也許這會給您一些可能幫助您解決問題的想法:

>>> 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM