背景介绍:.需要绘制的图是半径为0.0075-0.010的随机圆,大概需要30亿个圆
出现的问题:绘制相同规格的圆,使用批处理绘制,比一块一块绘制的时间长的多
绘制代码:
import os
import math
import Rhino
import scriptcontext as sc
import csv
# import rand_v4 # 确保 rand_v4 模块在同级目录下并可用
import time
# 定义画布大小、半径偏移量
canvas_size = (20, 20)
r_offset = 0.005
# r_offset = 0
# 计算最终行数和列数
# final_rows = math.ceil(rand_v4.final_canvas_size[1] / rand_v4.canvas_size[1])
# final_cols = math.ceil(rand_v4.final_canvas_size[0] / rand_v4.canvas_size[0])
def draw_circles_in_rhino(points):
for point in points:
# 提取点和半径
x, y, radius = point
# 在指定的点处绘制一个指定半径的圆
center_point = Rhino.Geometry.Point3d(x, y, 0)
circle = Rhino.Geometry.Circle(center_point, radius)
sc.doc.Objects.AddCircle(circle)
def draw_line(start_point, end_point):
line = Rhino.Geometry.Line(start_point, end_point)
sc.doc.Objects.AddLine(line)
def save_as_dwg(file_path):
save_script = f"-_Export \"{file_path}\" _Enter"
Rhino.RhinoApp.RunScript(save_script, True)
def main():
time_start = time.time()
# 获取脚本所在目录
script_directory = os.path.dirname(__file__)
# 定义数据目录
save_directory = os.path.join(script_directory, 'data')
# 确保目录存在
if not os.path.exists(save_directory):
print("数据目录不存在")
return
# 绘制圆点
for row in range(2):
for col in range(2):
# 指定 CSV 文件的文件名
file_points_all = 'points_all%dx%d.csv' % (row, col)
# 从 CSV 文件读取points_all数据
file_path = os.path.join(save_directory, file_points_all)
if not os.path.exists(file_path):
print(f"文件 {file_path} 不存在")
continue
with open(file_path, mode='r') as file:
reader = csv.reader(file)
next(reader) # 跳过标题行(如果有)
points_all = [[float(x), float(y), float(r)] for x, y, r in reader]
draw_circles_in_rhino(points_all)
# 绘制画布边界
draw_line(Rhino.Geometry.Point3d(0, 0, 0), Rhino.Geometry.Point3d(canvas_size[0] * (1 + col), 0, 0))
draw_line(Rhino.Geometry.Point3d(0, 0, 0), Rhino.Geometry.Point3d(0, canvas_size[1] * (1 + row), 0))
draw_line(Rhino.Geometry.Point3d(canvas_size[0] * (1 + col), 0, 0), Rhino.Geometry.Point3d(canvas_size[0] * (1 + col), canvas_size[1] * (1 + row), 0))
draw_line(Rhino.Geometry.Point3d(0, canvas_size[1] * (1 + row), 0), Rhino.Geometry.Point3d(canvas_size[0] * (1 + col), canvas_size[1] * (1 + row), 0))
sc.doc.Views.Redraw() # 重绘视图
# 保存为 DWG 文件
dwg_file_path = os.path.join(script_directory, '500x200.dwg')
save_as_dwg(dwg_file_path)
time_end = time.time()
print(f"绘制时间为:({time_end - time_start})")
if __name__ == "__main__":
main()