【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 objectsstory_base_points
: Stores reference pointsstory_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
- Code Organization:
- Clear function structure
- Logical data flow
- Modular design
- Error Prevention:
- Input validation
- Existence checks
- Clear error messages
- 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
- 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}")
- Advanced Filtering:
def filter_objects_by_criteria(objects, criteria):
return [obj for obj in objects if meets_criteria(obj, criteria)]
- 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.