使用Python实现AutoCAD中等高线自动删除

本文介绍如何使用Python中的pyautocad库对AutoCAD中的等高线进行批量处理,包括获取等高线的最大最小高程,并按指定等高距筛选保留等高线。

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

  作为苦逼的测绘工作者,免不了和AutoCAD及其众多二次开发版打交道,在这类软件中,有时需要进行一些工作量巨大的操作,手工做是不可能手工做的,Lisp又不会写,怎么办呢?(众所周知,AutoCAD提供有AutoLisp来实现编程,但是并不简单易学,反正本人是不会2333),现在我们使用Python中提供的pyautocad包就能够简单地操作CAD啦。

背景:

  前段时间博主遇到一个很蛋疼的问题,拿到一幅地形图,里面的等高线密密麻麻(由于地形图涉密,就不给大家看了),然而并不需要这么多条等高线,因为非常不利于后续导入其他软件里面处理,会造成很大性能开销,甚至导致软件崩溃,到这里有同学可能会问:这个用南方CASS处理一下不就好了吗?是的,CASS的确提供处理等高线功能,但是这幅图并不是CASS画出来的,也就是说要在CASS中当做等高线处理,必须先刷属性,可是经博主尝试,可能因为数据量太大,一刷属性整个软件就崩溃掉了,所以这个办法显然行不通(在这里不得不吐槽南方CASS性能有点堪忧),而原生CAD里又没有专门提供等高线的处理功能,所以我们还是得自己动手,丰衣足食。

环境:

Python 3.6.1

AutoCAD 2010

走你!

第一步当然是安装pyautocad这个库,推荐用pip方式安装,如下:

pip install pyautocad

回车,稍等片刻后搞定

然后根据pyautocad的官方文档,需要用如下代码来初始化:

from pyautocad import Autocad
acad = Autocad(create_if_not_exists=True)
acad.prompt("Hello, Autocad from Python\n")#这句话作用是在CAD控制台里输出Hello, Autocad from Python,用于测试对CAD的控制是否成功

然后我们可以打印当前文档名称:

print (acad.doc.Name)

我们的目的是对等高线进行过滤,保留指定等高距的等高线,所以必须知道图面上等高线最大高程和最小高程

temp_height = list()
for dgx in acad.iter_objects('Spline'):#Spline表示样条曲线
    temp_height.append(dgx.ControlPoints[2])#ControlPoints是曲线上的锚点,这句话作用是把曲线上所有锚点的Z坐标提取出来
maximum = max(temp_height)
minimum = min(temp_height)

接下来可以根据想要的等高距,确定处于哪些高程的等高线需要保留

height_arr = list()
for height in range(int(minimum),int(maximum)+1,250):#这里的等高距设置的250米,可以根据自己需要修改
    height_arr.append(height)
然后遍历所有等高线,检查每一条等高线是否需要保留,将不需要保留的删除(在这个过程中,可能会由于谜之原因抛出异常,初步猜测是数据量过大,所以搞个异常处理,然后递归即可)

def delete():
    try:
        for dgx in acad.iter_objects('Spline'):
            if(int(dgx.ControlPoints[2]) not in height_arr):
                dgx.Delete()
    except:
        delete()

细心的同学会发现,我直接调用了dgx的Delete方法,没错,pyautocad已经提供了很多方法和属性,我们可以通过

print(dir(dgx))

来把这些属性或者方法打印出来查看,例如对于样条曲线,有如下方法\属性:

AddFitPoint
AddRef
Application
Area
ArrayPolar
ArrayRectangular
Closed
ControlPoints
Copy
Database
Degree
Delete
DeleteFitPoint
Document
ElevateOrder
EndTangent
EntityName
EntityType
Erase
FitPoints
FitTolerance
GetBoundingBox
GetControlPoint
GetExtensionDictionary
GetFitPoint
GetIDsOfNames
GetTypeInfo
GetTypeInfoCount
GetWeight
GetXData
Handle
HasExtensionDictionary
Highlight
Hyperlinks
IntersectWith
Invoke
IsPeriodic
IsPlanar
IsRational
Knots
Layer
Linetype
LinetypeScale
Lineweight
Material
Mirror
Mirror3D
Move
NumberOfControlPoints
NumberOfFitPoints
ObjectID
ObjectID32
ObjectName
Offset
OwnerID
OwnerID32
PlotStyleName
PurgeFitData
QueryInterface
Release
Reverse
Rotate
Rotate3D
ScaleEntity
SetControlPoint
SetFitPoint
SetWeight
SetXData
StartTangent
TransformBy
TrueColor
Update
Visible
Weights
_AddRef
_GetIDsOfNames
_GetTypeInfo
_IAcadEntity__com_ArrayPolar
_IAcadEntity__com_ArrayRectangular
_IAcadEntity__com_Copy
_IAcadEntity__com_GetBoundingBox
_IAcadEntity__com_Highlight
_IAcadEntity__com_IntersectWith
_IAcadEntity__com_Mirror
_IAcadEntity__com_Mirror3D
_IAcadEntity__com_Move
_IAcadEntity__com_Rotate
_IAcadEntity__com_Rotate3D
_IAcadEntity__com_ScaleEntity
_IAcadEntity__com_TransformBy
_IAcadEntity__com_Update
_IAcadEntity__com__get_EntityName
_IAcadEntity__com__get_EntityType
_IAcadEntity__com__get_Hyperlinks
_IAcadEntity__com__get_Layer
_IAcadEntity__com__get_Linetype
_IAcadEntity__com__get_LinetypeScale
_IAcadEntity__com__get_Lineweight
_IAcadEntity__com__get_Material
_IAcadEntity__com__get_PlotStyleName
_IAcadEntity__com__get_TrueColor
_IAcadEntity__com__get_Visible
_IAcadEntity__com__get_color
_IAcadEntity__com__set_Layer
_IAcadEntity__com__set_Linetype
_IAcadEntity__com__set_LinetypeScale
_IAcadEntity__com__set_Lineweight
_IAcadEntity__com__set_Material
_IAcadEntity__com__set_PlotStyleName
_IAcadEntity__com__set_TrueColor
_IAcadEntity__com__set_Visible
_IAcadEntity__com__set_color
_IAcadObject__com_Delete
_IAcadObject__com_Erase
_IAcadObject__com_GetExtensionDictionary
_IAcadObject__com_GetXData
_IAcadObject__com_SetXData
_IAcadObject__com__get_Application
_IAcadObject__com__get_Database
_IAcadObject__com__get_Document
_IAcadObject__com__get_Handle
_IAcadObject__com__get_HasExtensionDictionary
_IAcadObject__com__get_ObjectID
_IAcadObject__com__get_ObjectID32
_IAcadObject__com__get_ObjectName
_IAcadObject__com__get_OwnerID
_IAcadObject__com__get_OwnerID32
_IAcadSpline__com_AddFitPoint
_IAcadSpline__com_DeleteFitPoint
_IAcadSpline__com_ElevateOrder
_IAcadSpline__com_GetControlPoint
_IAcadSpline__com_GetFitPoint
_IAcadSpline__com_GetWeight
_IAcadSpline__com_Offset
_IAcadSpline__com_PurgeFitData
_IAcadSpline__com_Reverse
_IAcadSpline__com_SetControlPoint
_IAcadSpline__com_SetFitPoint
_IAcadSpline__com_SetWeight
_IAcadSpline__com__get_Area
_IAcadSpline__com__get_Closed
_IAcadSpline__com__get_ControlPoints
_IAcadSpline__com__get_Degree
_IAcadSpline__com__get_EndTangent
_IAcadSpline__com__get_FitPoints
_IAcadSpline__com__get_FitTolerance
_IAcadSpline__com__get_IsPeriodic
_IAcadSpline__com__get_IsPlanar
_IAcadSpline__com__get_IsRational
_IAcadSpline__com__get_Knots
_IAcadSpline__com__get_NumberOfControlPoints
_IAcadSpline__com__get_NumberOfFitPoints
_IAcadSpline__com__get_StartTangent
_IAcadSpline__com__get_Weights
_IAcadSpline__com__set_ControlPoints
_IAcadSpline__com__set_EndTangent
_IAcadSpline__com__set_FitPoints
_IAcadSpline__com__set_FitTolerance
_IAcadSpline__com__set_Knots
_IAcadSpline__com__set_StartTangent
_IAcadSpline__com__set_Weights
_IDispatch__com_GetIDsOfNames
_IDispatch__com_GetTypeInfo
_IDispatch__com_GetTypeInfoCount
_IDispatch__com_Invoke
_IUnknown__com_AddRef
_IUnknown__com_QueryInterface
_IUnknown__com_Release
_Invoke
_QueryInterface
_Release
__bool__
__class__
__cmp__
__com_interface__
__ctypes_from_outparam__
__del__
__delattr__
__dict__
__dir__
__doc__
__eq__
__format__
__ge__
__getattr__
__getattribute__
__gt__
__hash__
__init__
__init_subclass__
__le__
__lt__
__map_case__
__module__
__ne__
__new__
__reduce__
__reduce_ex__
__repr__
__setattr__
__setstate__
__sizeof__
__str__
__subclasshook__
__weakref__
_b_base_
_b_needsfree_
_case_insensitive_
_compointer_base__get_value
_idlflags_
_iid_
_invoke
_methods_
_needs_com_addref_
_objects
_type_
color
from_param
value

至于以上属性\方法的各自作用,就留给大家自己去研究、测试啦。关于pyautocad的其他用法,可以访问pyautocad的官网http://pyautocad.readthedocs.io/en/latest/翻阅文档(虽然官网的文档也很少2333,但也聊胜于无吧),最后附上源代码:

from pyautocad import Autocad
acad = Autocad(create_if_not_exists=True)
acad.prompt("Hello, Autocad from Python\n")
print (acad.doc.Name)
temp_height = list()
for dgx in acad.iter_objects('Spline'):
    temp_height.append(dgx.ControlPoints[2])
maximum = max(temp_height)
minimum = min(temp_height)
height_arr = list()
for height in range(int(minimum),int(maximum)+1,250):
    height_arr.append(height)
print('开始删除')
def delete():
    try:
        for dgx in acad.iter_objects('Spline'):
            if(int(dgx.ControlPoints[2]) not in height_arr):
                dgx.Delete()
    except:
        delete()
delete()
print('OK')



评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值