Python AutoCAD 块组

本文详述了使用Python进行AutoCAD二次开发的过程,包括块的创建、编辑、属性添加,以及组的创建等核心操作。通过实例代码,展示了如何在AutoCAD中创建和管理块与组,适用于建筑设计、机械设计等领域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Blog Links






一、连接及库导入

#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''
=============================
Author: DalNur
Email: liyang@alu.hit.edu.cn
=============================
'''

from pyautocad import Autocad

acad = Autocad(create_if_not_exists = True)
acad.prompt("Hello! AutoCAD from pyautocad.")
print(acad.doc.Name)

二、块

  创建的是Block。


  To create a block reference, first you must create a block definition, or block, by using the Add method. Once created, you can insert an instance of this block into your drawing using the InsertBlock method.


  To add or delete items from the block reference, you must first use the Explode method to break it into its component objects. Even though a block reference is broken into its component objects, the block definition still remains in the drawings block symbol table.


2.1. 创建新块

grip = APoint(20, 0)
blockObj = acad.ActiveDocument.Blocks.Add(grip, "HIT_Block")
          # 新建块的名称为"HIT_Block";
          # grip为块定位夹点所在位置。

2.2. 添加图元到块

center = APoint(0, 0)
radius = 10
CircleObj = blockObj.AddCircle(center, radius)
center = APoint(40, 10)
majAxis = APoint(10, 0, 0)
EllObj = blockObj.AddEllipse(center, majAxis, 0.5)

2.3. 插入块

  Block插入到图纸空间后为BlockReference。

2.3.1. 从当前文件中插入块

insertionPnt = APoint(0, 0)
RetVal = acad.model.InsertBlock(insertionPnt, "HIT_Block", 1, 1, 1, 0 )
          # acad.model.InsertBlock(InsertionPoint, Name, Xscale, Yscale, ZScale, Rotation);
          # insertionPnt为块的插入点,即块的定位夹点与图纸空间中的该点对齐。

2.3.2. 外部文件作为块插入

insertionPnt = APoint(10, 0)
RetVal = acad.model.InsertBlock(insertionPnt, "D:\AutoCAD\Harbin.dwg", 1, 1, 1, 0 )
          # 外部文件名尽量与当前文件中的各块名称不同;
          # 插入后外部文件名将作为其在当前文件中的块名;
          # 外部文件的坐标原点为其作为块的定位夹点。

2.4. 添加属性到块

2.4.1. 当前文件创建的块属性添加

height = 1  # 字高
mode = 2    # 模式
prompt = "Attribute_Prompt"  # 提示
insertionPoint = APoint(0, 0)
tag = "Attribute_Tag"        # 标记
value = "Attribute_Value"    # 默认
attributeObj = blockObj.AddAttribute(height, mode, prompt, insertionPoint, tag, value)

2.4.2. 外部文件作为插入块的属性添加

blocks_nums = acad.ActiveDocument.Blocks.count
       # 图纸空间中块的总数目
blocks_names = [acad.ActiveDocument.Blocks.Item(i).Name for i in range(blocks_nums)]
       # 图纸空间所有块的名称组成的列表
index = blocks_names.index("Harbin")
       # 外部文件块"Harbin"在图纸空间块中的索引号
external_blockObj = acad.ActiveDocument.Blocks.Item(index)
       # external_blockObj即为外部插入到本地文件中的块
ex_att_Obj = external_blockObj.AddAttribute(height, mode, prompt, insertionPoint, tag, value)

2.5. 已有块属性的读取

# RetVal = object.GetAttributes() 此方法在不借助其他库的情况下如何使用暂时未知;
# 采用把块炸开将属性部分暴露出来,然后提取其相关信息的方式获取属性信息;
# 此方式虽然曲折,但至少可行;
# 为了清晰直观,文本代码暂未考虑效率问题,故代码行数较多。

# 不采用如下方式,直接炸开块会报错。

try:
    RetVal.Explode()  # 炸开块
except:
    RetVal.Delete()   # 删除重复

for obj in acad.iter_objects("AttributeDefinition"):
    # 以下获取块的属性信息
    # 如果想获取某一特定块的属性信息可以用ObjectID识别特定块
    print(obj.ObjectName)
    print(obj.TagString)
    print(obj.TextString)
    print(obj.PromptString)

三、组

3.1. 创建新组

groupObj = acad.ActiveDocument.Groups.Add("Harbin Institute of Technology")
          # 新建组的名称为"Harbin Institute of Technology"

四、尾声

  以上,便是关于 AutoCAD块组 的一些基本代码,因篇幅有限,某些非关键功能未做详细介绍,如有疑问,欢迎邮件来询。

  本文部分功能的实现离不开广大博友的大力帮助,有些功能看似简单,但第一次实现出来却是相当不容易的。

  鉴于,相关示例代码相对较少,特写本文,一方面是为自己的阶段性学习做一个总结,另一方面更是为有需要的人提供多一点参考。

  如果您已实现一些本文未提及的功能,还请在评论区呈现,以便为后续学习者提供更多的帮助。

  胸藏文墨怀若谷,腹有诗书气自华,希望各位都能在知识的pāo子里快乐徜徉。

  因本人野生学习Python,水平确实有限,文中难免有所疏漏,还请各位大神不吝批评指正。

  最后,祝各位攻城狮们,珍爱生命,保护发际线!

  本文部分内容,源于网络!

  欢迎大家点赞、评论及转载,转载请注明出处!

  为我打call,不如为我打款!

  打赏可备注邮箱,本人将赠送本系列博客的全部 Python 源代码。



在这里插入图片描述


评论 114
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hulunbuir

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值