top of page

使用Python節點取得梁柱連續編號

  • 作家相片: Yimin Chen
    Yimin Chen
  • 2022年8月5日
  • 讀畢需時 0 分鐘

ree

說明

在許多圖面中,常常會用連續編號的方式來大量標註對應的梁柱尺寸,本文將示範在GH中使用Python節點來讀取所有相對應的編號,相同的邏輯亦可應用到Dynamo的Python節點中。

程式流程

  1. 依",”分開字串

  2. 判別文字是否包含"~”

    1. 若無,則直接回傳字串

    2. 若有,則進入【步驟3】進行連續號處理

  3. 依"~”分開字串,取得【前字串】與【後字串】,例:B51與B59

  4. 利用regex(正規表示式)讀取字串中的數字,分別得到【前字串數字】與【後字串數字】,例51與59

  5. 利用【步驟4】取得的數字作為迴圈的起始值與結束值,回傳【前置字串】+【數值】+【後置字串】

程式碼


#encoding:utf-8
import rhinoscriptsyntax as rs
import scriptcontext as sc

import re

def GetSequenceSizes(txt):
    
    # 搜尋目標:多個數字
    pattern1 = "\d+"
    
    # 步驟2-判斷字串中是否含有"~"
    if not "~" in txt:
        return txt
    else:
        
        # 步驟3-依"~”分開字串,取得【前字串(start_string)】與【後字串(end_string)】,例:B51與B59
        splited_txts = txt.split("~")
        start_string = splited_txts[0]
        end_string = splited_txts[1]

        # 步驟4-利用regex(正規表示式)讀取字串中的數字,分別得到【前字串數字(start_number)】與【後字串數字(end_number)】,例51與59
        start_index = re.search(pattern1, start_string).start()
        end_index = re.search(pattern1, start_string).end()
        start_number = int(start_string[start_index:end_index])

        start_index = re.search(pattern1, end_string).start()
        end_index = re.search(pattern1, end_string).end()
        end_number = int(end_string[start_index:end_index])

        # 步驟5-利用【步驟4】取得的數字作為迴圈的起始值與結束值,回傳【前置字串(start_string[0:start_index])】+【數值(x)】+【後置字串(start_string[end_index:])】
        return [start_string[0:start_index]+ str(x) + start_string[end_index:] for x in range(start_number, end_number+1)]


input_text = input

# 步驟1-依",”分開字串
splited_texts = input_text.split(",")
new_list = []

for t in splited_texts:
    
    # 處理字串
    result = GetSequenceSizes(t)
    
    # 若結果回傳list,則使用extend,反之則使用append
    if isinstance(result ,list ):
        new_list.extend(result)
    else:
        new_list.append(result)

a = new_list




Comments


0920520711

新北市永和區成功路一段93巷20弄17號3樓

  • Facebook

Copyright © 2020 by 鉤逸科技有限公司

bottom of page