当前位置: 首页 > article >正文

【Rhino】【Python】Create a series of Blocks according to Value of object Property

文章目录

    • 1. Complete Code Display
    • 2. Detailed Code Analysis
      • 2.1 Import and Setup
      • 2.2 Function Structure and Initial Setup
      • 2.3 Object Collection and Filtering
      • 2.4 Story Management System
      • 2.5 Locating Point Processing
      • 2.6 Object Organization by Story
      • 2.7 Block Creation System
      • 2.8 Block Assembly and Placement
    • 3. Key Technical Concepts
      • 3.1 Data Organization
      • 3.2 Error Handling
      • 3.3 Automation Features
    • 4. Best Practices Demonstrated
    • 5. Practical Applications
      • 5.1 BIM Integration
      • 5.2 Drawing Management
    • 6. Further Development Possibilities
    • 7. Conclusion

在这里插入图片描述
在这里插入图片描述

1. Complete Code Display

#coding=utf-8

import rhinoscriptsyntax as rs
import scriptcontext as sc
import datetime

def create_blocks_by_story():
    timestamp = datetime.datetime.now().strftime("%Y%m%d")
    parent_layer = "01 STR. LINE LAYOUT(FOR FEM)"
    
    if not rs.IsLayer(parent_layer):
        rs.AddLayer(parent_layer)
    
    # Get all objects
    all_objects = rs.AllObjects()
    etabs_objects = []
    locating_points = []
    
    # Filter Etabs objects
    for obj in all_objects:
        obj_name = rs.ObjectName(obj)
        if obj_name and obj_name.startswith("Etabs"):
            if obj_name == "Etabs locating":
                locating_points.append(obj)
            else:
                etabs_objects.append(obj)
    
    if not etabs_objects:
        print("No objects with names starting with 'Etabs' found")
        return
    
    story_dict = {}
    story_base_points = {}
    story_locating_points = {}
    
    # Process locating points
    if locating_points:
        for point in locating_points:
            if rs.ObjectType(point) == 1:
                story_value = rs.GetUserText(point, "Story")
                if story_value:
                    point_coord = rs.PointCoordinates(point)
                    story_base_points[story_value] = (0, 0, 0)
                    story_locating_points[story_value] = point
    
    # Sort objects by story
    for obj in etabs_objects:
        story_value = rs.GetUserText(obj, "Story")
        if story_value:
            if story_value not in story_dict:
                story_dict[story_value] = []
            story_dict[story_value].append(obj)
    
    # Create blocks for each story
    for story, objs in story_dict.items():
        if len(objs) > 0:
            if story not in story_base_points:
                print("Warning: No base point found for Story {}".format(story))
                continue
                
            block_name = "Story_" + str(story)
            layer_name = "{}::{}_{}_{}".format(parent_layer, block_name, timestamp, "block")
            
            if not rs.IsLayer(layer_name):
                rs.AddLayer(layer_name)
            
            copied_objs = [rs.CopyObject(obj) for obj in objs]
            if story in story_locating_points:
                copied_objs.append(rs.CopyObject(story_locating_points[story]))
            
            base_point = story_base_points[story]
            
            block_ref = rs.AddBlock(copied_objs, base_point, block_name, True)
            instance = rs.InsertBlock(block_ref, base_point)
            rs.ObjectLayer(instance, layer_name)
            
            print("Created block '{}' with {} objects on layer '{}'".format(block_name, len(copied_objs), layer_name))
    
    if etabs_objects:
        rs.DeleteObjects(etabs_objects)
        print("All original objects have been deleted")

create_blocks_by_story()

2. Detailed Code Analysis

2.1 Import and Setup

#coding=utf-8
import rhinoscriptsyntax as rs
import scriptcontext as sc
import datetime
  • UTF-8 encoding for international character support
  • Essential Rhino scripting tools
  • Datetime for timestamp generation

2.2 Function Structure and Initial Setup

def create_blocks_by_story():
    timestamp = datetime.datetime.now().strftime("%Y%m%d")
    parent_layer = "01 STR. LINE LAYOUT(FOR FEM)"

Purpose:

  • Creates unique timestamps for versioning
  • Establishes parent layer structure

2.3 Object Collection and Filtering

all_objects = rs.AllObjects()
etabs_objects = []
locating_points = []

for obj in all_objects:
    obj_name = rs.ObjectName(obj)
    if obj_name and obj_name.startswith("Etabs"):
        if obj_name == "Etabs locating":
            locating_points.append(obj)
        else:
            etabs_objects.append(obj)

Features:

  • Collects all Rhino objects
  • Separates locating points from other Etabs objects
  • Implements smart filtering system

2.4 Story Management System

story_dict = {}
story_base_points = {}
story_locating_points = {}

Data Structure:

  • story_dict: Maps stories to their objects
  • story_base_points: Stores reference points
  • story_locating_points: Manages locating points per story

2.5 Locating Point Processing

if locating_points:
    for point in locating_points:
        if rs.ObjectType(point) == 1:  # Point object check
            story_value = rs.GetUserText(point, "Story")
            if story_value:
                point_coord = rs.PointCoordinates(point)
                story_base_points[story_value] = (0, 0, 0)
                story_locating_points[story_value] = point

Key Features:

  • Validates point objects
  • Extracts story information
  • Sets up reference coordinate system

2.6 Object Organization by Story

for obj in etabs_objects:
    story_value = rs.GetUserText(obj, "Story")
    if story_value:
        if story_value not in story_dict:
            story_dict[story_value] = []
        story_dict[story_value].append(obj)

Process:

  • Reads story metadata
  • Groups objects by story level
  • Creates organized data structure

2.7 Block Creation System

for story, objs in story_dict.items():
    if len(objs) > 0:
        block_name = "Story_" + str(story)
        layer_name = "{}::{}_{}_{}".format(parent_layer, block_name, timestamp, "block")

Features:

  • Dynamic block naming
  • Hierarchical layer structure
  • Timestamp integration

2.8 Block Assembly and Placement

copied_objs = [rs.CopyObject(obj) for obj in objs]
if story in story_locating_points:
    copied_objs.append(rs.CopyObject(story_locating_points[story]))

base_point = story_base_points[story]
block_ref = rs.AddBlock(copied_objs, base_point, block_name, True)
instance = rs.InsertBlock(block_ref, base_point)

Process:

  • Object duplication
  • Locating point integration
  • Block creation and insertion

3. Key Technical Concepts

3.1 Data Organization

- Hierarchical data structures
- Story-based object grouping
- Reference point management

3.2 Error Handling

- Object existence verification
- Layer validation
- Story assignment checking

3.3 Automation Features

- Automatic layer creation
- Dynamic block naming
- Timestamp-based versioning

4. Best Practices Demonstrated

  1. Code Organization:
- Clear function structure
- Logical data flow
- Modular design
  1. Error Prevention:
- Input validation
- Existence checks
- Clear error messages
  1. Performance Optimization:
- Batch processing
- Efficient data structures
- Minimal object manipulation

5. Practical Applications

5.1 BIM Integration

- Etabs model organization
- Story-based structural analysis
- Reference point management

5.2 Drawing Management

- Automated block creation
- Layer organization
- Version control

6. Further Development Possibilities

  1. Enhanced Error Handling:
try:
    # Add more specific error checking
    if not validate_story_data(story_value):
        raise ValueError("Invalid story data")
except Exception as e:
    print(f"Error processing story: {e}")
  1. Advanced Filtering:
def filter_objects_by_criteria(objects, criteria):
    return [obj for obj in objects if meets_criteria(obj, criteria)]
  1. Progress Reporting:
def report_progress(current, total):
    print(f"Processing: {current}/{total} complete")

7. Conclusion

This code represents a sophisticated approach to:

  • Building Information Modeling (BIM) automation
  • Structural engineering workflows
  • Drawing management systems
  • Version control implementation

Understanding these concepts helps in developing more advanced architectural and engineering automation tools.


http://www.kler.cn/a/408252.html

相关文章:

  • 数仓工具—Hive语法之窗口函数中的 case when
  • Ubuntu下的Doxygen+VScode实现C/C++接口文档自动生成
  • 内网渗透横向移动1
  • 【SKFramework框架】一、框架介绍
  • 力扣2388. 将表中的空值更改为前一个值
  • LeetCode题解:28.找出字符串中第一个匹配项的下标【Python题解超详细,滑动窗口法、内置 find 函数、KMP算法】,知识拓展, KMP算法
  • 【042C】基于51RFID门禁系统(LCD12864显示)【Proteus仿真+Keil程序+报告+原理图】
  • Java基础:日期时间相关类
  • python基础导包
  • springmvc-04-Controller及RestFul
  • cocos creator 3.8 3D模型、天空盒、雾 6
  • 基于SpringBoot的京东绿谷旅游信息服务平台设计与实现(源码+定制+开发)
  • 第六届国际科技创新学术交流大会暨新能源科学与电力工程国际学术会议(NESEE 2024)
  • [高阶数据结构三] B-树详解
  • 【Linux】线程的互斥和同步
  • AirScreen 安卓平板作为MacOS副屏
  • 【淘汰9成NLP面试者的高频面题】LSTM中的tanh和sigmoid分别用在什么地方?为什么?
  • postgresql按照年月日统计历史数据
  • VMware16安装macOS12【详细教程】
  • docker 相关命令
  • 力扣 最大数组和-53
  • SQL99版全外连接和交叉连接和总结
  • 4-使用您自己的输出 --github_com_fatih_color测试
  • 2024年Android面试总结
  • Android开发实战班 -网络编程 - Retrofit 网络请求 + OkHttp 使用详解
  • Spring 小案例体验创建对象的快感(Java EE 学习笔记05)