【发布时间】:2017-10-31 17:41:18
【问题描述】:
目标:
使用 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 对象的原生方法是 Vector 即 FreeCAD.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)
【问题讨论】:
-
请查看我们的帮助中心。具体来说,如何创建Minimal, Complete and Verifiable Example。您提供的信息太多,所以您的问题还不清楚。
-
在
s = s.replace(',', 'FreeCAD.Vector ')之后,打印的结果应该是(0.0034596999FreeCAD.Vector 0.0034775001FreeCAD.Vector 0.0010091923),不是当前在您的问题末尾附近显示的内容。请edit您的问题并创建一个最小示例来说明您遇到的问题(并且这样做是正确的)。 -
看起来 FreeCAD 可以直接从 CSV 导入。为什么你需要编写代码来做到这一点?或者这不会创建 FreeCAD Vectors? freecadweb.org/wiki/Spreadsheet_CSV
标签: python string list tuples coordinates