如何遍历包含 class 个对象的元组?

How to iterate through a tuple containing class objects?

我目前正在编写一段代码,它将自动执行我每天使用的工程软件 (Orcaflex) 的流程。我正在使用 python 与 Orcaflex 接口以自动进行 运行 悬链线计算。下面是我的代码以及输出以供参考。

我的想法是获取 Orcaflex 模型中的所有对象并遍历该元组对象以提取线名称,以将该数据输入 Orcaflex 的线向导工具。对象类型是一个元组,但是当我对该元组进行索引时,我得到了一种类型 (class 'OrcFxAPI.OrcaFlexObject')。

我的问题是如何遍历这个包含对象的元组以便获得行名字符串?任何帮助将不胜感激。

更新:我能够将对象转换为字符串并执行基本的字符串操作来获取行名。请在下面查看我更新的代码。但是,在我的第二个 post 中可以看到一种更有效的从模型中获取线名的方法,使用点符号来获取线名和类型。

我更新的代码:

# Created by: Brian Weber
# Created on: 09/15/2015

# This script will load a base file and then calculate for
# different tensions using the line setup wizard.

# Note that buoys are modeled as clump attachments on the line with a global offset for
# pennant wire.

import OrcFxAPI
import numpy as np

def convert_MT_to_kN(value_in_MT):
    g = 9.80665002864
    value_in_kN = value_in_MT * g
    return value_in_kN

g = 9.80665002864

# Load file name
filename = 'CX15-Anchor Plan.dat'
model = OrcFxAPI.Model(filename)

# Pipe tensions to be solved for; units are in MT then converted to kN for Orcaflex
min_tension = float(raw_input("\n Please enter the minimum tension to calculate catenary in MT: "))
max_tension = float(raw_input("\n Please enter the maximum tension to calculate catenary in MT: "))
tension_increment = float(raw_input("\n Please enter the tension increment to calculate catenary in MT: "))
pipe_tensions = (np.arange(min_tension, max_tension, tension_increment) * g).tolist()

# Grab lines in model
lines = []
objects = model.objects # returns a tuple
# Convert objects to strings
for o in objects:
    o_string = repr(o)
    if "Line:" in o_string:
        string_split = o_string.split("'")
        lines.append(string_split[1])

# Solve all lines in model for all line tensions
print('\nSolving line tensions...')
for tension in pipe_tensions:
    print('\nLine tension being solved for is {:.2f} kN.\n').format(tension)
    for line in lines:
        model.general.LineSetupCalculationMode = 'Calculate Line Lengths'
        model[line].LineSetupIncluded = 'Yes'
        # model.general.LineSetupMinDamping = 5
        # model.general.LineSetupMaxDamping = 20
        # model.general.LineSetupMaxIterations = 50
        model[line].LineSetupTargetVariable = 'Tension'
        model[line].LineSetupTargetValue = tension
        model[line].LineSetupLineEnd = 'End A'
        print('Line {} set.').format(line)
    print('\nInvoking Line Setup Wizard...')
    model.InvokeLineSetupWizard()   
    print('\nLine Setup Wizard done.')
    # Calculate static position of mooring lines and buoys
    model.CalculateStatics()
    # Save static simulation and load into new object
    model.SaveSimulation(('{} - {:.2f} kN.sim').format(filename, tension))

print('\nLine Wizard has completed!')

你得到的元组中对象的内容是什么?你知道这个对象在元组中的位置吗?这些对象有你想要的 属性 吗?例如,如果对象是元组中的第二个元素,您可以这样做:

objects[1].<<func_name_you_are_interested_in>>()
or 
objects[1].<<property_name_you_are_interested_in>>

能否给出示例中对象变量的示例内容? (通过打印)

我实际上找到了另一种更有效的方法来从 Orcaflex 模型中获取线名。由于对象 returns 是 class 对象的元组,您可以使用点符号来获取类型(line.type 其中 returns 是一个整数) 和名称(line.name 其中 returns 一个字符串)通过使用以下代码:

import OrcFxAPI
import numpy as np

g = 9.80665002864

# Load file name
filename = 'CX15-Anchor Plan.dat'
model = OrcFxAPI.Model(filename)

# Pipe tensions to be solved for; units are in MT then converted to kN for Orcaflex
min_tension = float(raw_input("\n Please enter the minimum tension to calculate catenary in MT: "))
max_tension = float(raw_input("\n Please enter the maximum tension to calculate catenary in MT: "))
tension_increment = float(raw_input("\n Please enter the tension increment to calculate catenary in MT: "))
pipe_tensions = (np.arange(min_tension, max_tension, tension_increment) * g).tolist()

# Grab lines in model
objects = model.objects # returns a tuple
lines = [obj for obj in objects if obj.type == OrcFxAPI.otLine] # All line objects in model

# Solve all lines in model for all line tensions
print('\nSolving line tensions...')
for tension in pipe_tensions:
    print('\nLine tension being solved for is {:.2f} kN.\n').format(tension)
    for line in lines:
        model.general.LineSetupCalculationMode = 'Calculate Line Lengths'
        model[line.name].LineSetupIncluded = 'Yes'
        model[line.name].LineSetupTargetVariable = 'Tension'
        model[line.name].LineSetupTargetValue = tension
        model[line.name].LineSetupLineEnd = 'End A'
        print('Line {} set.').format(line.name)
    print('\nInvoking Line Setup Wizard...')
    model.InvokeLineSetupWizard()   
    print('\nLine Setup Wizard done.')
    # Calculate static position of mooring lines and buoys
    model.CalculateStatics()
    # Save static simulation and load into new object
    model.SaveSimulation(('{} - {:.2f} kN.sim').format(filename, tension))

print('\nLine Wizard has completed!')