明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2448|回复: 12

使用python操作autocad2007

[复制链接]
发表于 2020-6-18 14:16:26 | 显示全部楼层 |阅读模式
本帖最后由 dogingate 于 2020-6-19 14:07 编辑
  1. <div class="blockcode"><blockquote>from pyAutoCAD import Autocad, APoint

  2. dblSpan = 16000
  3. intBeamsCount = 8
  4. dblBeamDist = 1000
  5. dblSideDist = 300
  6. intElesCount = 16

  7. acad = Autocad(create_if_not_exists=True)
  8. acad.prompt('welcome back!')

  9. # #set the new created layer to the current layer
  10. # acad.ActiveDocument.ActiveLayer=olayer

  11. # 1 read,2 yellow,3 green,4 qing,5 blue,6 yanghong,7 white/black
  12. layers_color = [1, 2, 3]
  13. layers_name = ['zhuliang', 'xuliang', 'zhizuo']
  14. layers_linetype = ['continuous', 'continuous', 'continuous']

  15. for i in range(len(layers_name)):
  16.     olayer = acad.ActiveDocument.Layers.Add(layers_name[i])
  17.     olayer.color = layers_color[i]
  18.     olayer.Linetype = layers_linetype[i]

  19. # 当前文件模型空间中所包含的图层总数
  20. layers_nums = acad.ActiveDocument.Layers.count
  21. # 当前文件模型空间中所包含的所有图层名称
  22. layers_names = [acad.ActiveDocument.Layers.Item(i).Name for i in range(layers_nums)]

  23. # 获取指定图层索引号
  24. index = layers_names.index('zhuliang')
  25. # 将指定图层设定当前
  26. acad.ActiveDocument.ActiveLayer = acad.ActiveDocument.Layers.Item(index)

  27. # add some objects to document
  28. p1 = APoint(0, 0)
  29. p2 = APoint(dblSpan, 0)
  30. for i in range(0, intBeamsCount):
  31.     acad.model.AddLine(p1, p2)
  32.     p1 = APoint(0, p1[1]+dblBeamDist)
  33.     p2 = APoint(dblSpan, p1[1])

  34. # 获取指定图层索引号
  35. index = layers_names.index('xuliang')
  36. # 将指定图层设定当前
  37. acad.ActiveDocument.ActiveLayer = acad.ActiveDocument.Layers.Item(index)

  38. dist = dblSpan/intElesCount
  39. p1 = APoint(dist/2, -dblSideDist)
  40. p2 = APoint(dist/2, (intBeamsCount-1)*dblBeamDist+dblSideDist)
  41. for i in range(0, intElesCount):
  42.     acad.model.AddLine(p1, p2)
  43.     p1 = APoint(p1[0]+dist, p1[1])
  44.     p2 = APoint(p2[0]+dist, p2[1])
复制代码

 楼主| 发表于 2020-6-19 14:08:44 | 显示全部楼层
与上一个帖子同样的功能,用VBA实现
  1. Sub deleteTextAndDimension()
  2.    
  3.     Dim oSS As Object
  4.     On Error Resume Next
  5.     If Not IsNull(ThisDrawing.SelectionSets.Item("Wolf")) Then
  6.         Set oSS = ThisDrawing.SelectionSets.Item("wolf")
  7.         oSS.Delete
  8.     End If
  9.     Set oSS = ThisDrawing.SelectionSets.Add("wolf")

  10.     On Error GoTo catchError
  11.     Dim fType() As Integer
  12.     Dim fData As Variant
  13.       strFilterType = "-4,0,0,-4"
  14.     strFilterData = "<or,text,dimension,or>"
  15.     Call createFilter(fType, fData, strFilterType, strFilterData)

  16.     oSS.SelectOnScreen fType, fData
  17.     oSS.Highlight ture
  18.     oSS.Erase
  19.     oSS.Delete

  20. exitSub:
  21.     Exit Sub
  22. catchError:
  23.     ' add error handling
  24.     If Err Then
  25.         Err.Clear
  26.         MsgBox Err.Description
  27.     End If
  28.    
  29. End Sub

  30. Sub createFilter(fType, fData, strFilterType, strFilterData)
  31.     '// add declarations
  32.     On Error GoTo catchError
  33.     arrFilterType = Split(strFilterType, ",")
  34.     arrFilterData = Split(strFilterData, ",")
  35.     If UBound(arrFilterType) = UBound(arrFilterData) Then
  36.         intFilterCount = UBound(arrFilterType)
  37.         ReDim fType(intFilterCount)
  38.         ReDim fData(intFilterCount)
  39.         For i = 0 To UBound(arrFilterType)
  40.             fType(i) = arrFilterType(i)
  41.             fData(i) = arrFilterData(i)
  42.         Next i
  43.     Else
  44.         GoTo exitFunction
  45.     End If

  46. exitFunction:
  47.     Exit Sub
  48. catchError:
  49.     '// add error handling
  50.     GoTo exitFunction
  51. End Sub
 楼主| 发表于 2020-6-18 14:18:16 | 显示全部楼层
本帖最后由 dogingate 于 2020-6-18 14:19 编辑

以下是VBA代码,实现相同的功能,都在autocad2007里面测试完成,本来是想用vba,但是滚轮插件实现不了,老是有问题,刚好看到有Pyautocad,就试了下,效果还不错,可以对比下
  1. Sub Example_AddLine()
  2.     ' 该示例在模型空间中添加直线。
  3.     Dim oline As AcadLine
  4.     Dim startPoint(0 To 2) As Double
  5.     Dim endPoint(0 To 2) As Double
  6.     Dim dblSpan As Double
  7.     Dim intBeamsCount As Integer
  8.     Dim intElesCount As Integer
  9.        Dim dblBeamDist As Double
  10.     Dim dblSideDist As Double
  11.    
  12.      intElesCount = 20
  13.     dblSideDist = 300
  14.     dblBeamDist = 1000
  15.     intBeamsCount = 12
  16.     dblSpan = 16000
  17.     '定义直线的起点和终点
  18.     startPoint(0) = 0#: startPoint(1) = 0#: startPoint(2) = 0#
  19.     endPoint(0) = dblSpan: endPoint(1) = 0#: endPoint(2) = 0#
  20.     For i = 1 To intBeamsCount
  21.         ' 在模型空间中创建直线
  22.         Set oline = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)
  23.         startPoint(0) = startPoint(0): startPoint(1) = startPoint(1) + dblBeamDist: startPoint(2) = 0#
  24.         endPoint(0) = dblSpan: endPoint(1) = startPoint(1): endPoint(2) = 0#
  25.     Next i
  26.    
  27.     startPoint(0) = dblSpan / intElesCount / 2: startPoint(1) = -dblSideDist: startPoint(2) = 0#
  28.     endPoint(0) = dblSpan / intElesCount / 2: endPoint(1) = (intBeamsCount - 1) * dblBeamDist + dblSideDist: endPoint(2) = 0#
  29.     For i = 1 To intElesCount
  30.          Set oline = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)
  31.          startPoint(0) = startPoint(0) + dblSpan / intElesCount: startPoint(1) = startPoint(1): startPoint(2) = 0#
  32.          endPoint(0) = startPoint(0): endPoint(1) = endPoint(1): endPoint(2) = 0#
  33.     Next i
  34.    
  35.     ZoomAll
  36. End Sub

 楼主| 发表于 2020-6-19 14:08:01 | 显示全部楼层
这段代码用于在屏幕上选中的对象删除其中的文字和标注
  1. from pyautocad import Autocad, APoint

  2. from win32com.client import VARIANT
  3. from win32com.client import Dispatch
  4. import pythoncom

  5. def vtpnt(x, y, z=0):
  6.     """坐标点转化为浮点数"""
  7.     return VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, (x, y, z))

  8. def vtobj(obj):
  9.     """转化为对象数组"""
  10.     return VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_DISPATCH, obj)

  11. def vtFloat(list):
  12.     """列表转化为浮点数"""
  13.     return VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, list)
  14.    
  15. def vtInt(list):
  16.     """列表转化为整数"""
  17.     return VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list)

  18. def vtVariant(list):
  19.     """列表转化为变体"""
  20.     return VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_VARIANT, list)

  21. #AutoCAD2007 ProgId is "AutoCAD.Application.17"
  22. acad = Dispatch("AutoCAD.Application.17")  
  23. doc = acad.ActiveDocument
  24. doc.Utility.Prompt("Hello AutoCAD\n")
  25. mp = doc.ModelSpace

  26. if doc.SelectionSets.count>0:
  27.     try:
  28.         doc.SelectionSets.Item("SS1").Delete()
  29.     except:
  30.         print("Delete selection failed")

  31. slt = doc.SelectionSets.Add("SS1")

  32. filterType = [-4, 0, 0, -4]  # 定义过滤类型
  33. filterData = ["<OR", "TEXT", "DIMENSION", "OR>"]  # 设置过滤参数

  34. filterType = vtInt(filterType)  # 数据类型转化
  35. filterData = vtVariant(filterData)  # 数据类型转化

  36. # object.Select(Mode, Point1, Point2, FilterType, FilterData)
  37. slt.SelectOnScreen(filterType, filterData)  # 实现过滤
  38. obj = slt[0]
  39. slt.Erase()  # 删除符合条件的所有圆
复制代码
发表于 2020-6-19 09:35:47 | 显示全部楼层
在AUTOCAD2007下运行了一下VBA,画出一系列1000x800的网格,是这样的吗。
 楼主| 发表于 2020-6-19 10:51:05 | 显示全部楼层
panliang9 发表于 2020-6-19 09:35
在AUTOCAD2007下运行了一下VBA,画出一系列1000x800的网格,是这样的吗。

是的,你可以调整参数,改一下代码,做类似的事情
 楼主| 发表于 2020-6-19 14:30:34 | 显示全部楼层
几个参考文档

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x
 楼主| 发表于 2020-6-20 11:31:39 | 显示全部楼层
贴一个VB
  1. Sub sortPlineByX(arr)
  2. Dim i&, j&, vSwap, min&
  3. For i = LBound(arr, 1) To UBound(arr, 1)
  4.     min = i
  5.     For j = i + 1 To UBound(arr, 1)
  6.         If arr(min, 0) > arr(j, 0) Then min = j
  7.     Next
  8.     If min <> i Then
  9.         For k = 0 To 4
  10.             vSwap = arr(min, k): arr(min, k) = arr(i, k): arr(i, k) = vSwap
  11.         Next k
  12.     End If
  13. Next i

  14. End Sub
A的排序算法
发表于 2020-6-24 10:03:16 | 显示全部楼层
python的话,直接用非狐的pycad不是更好?不过pycad是用来替换.net的
发表于 2020-6-24 10:04:59 | 显示全部楼层

这些文件,一般在完整版的acad的help目录(如\AutoCAD 2008\Help)下都有。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 04:56 , Processed in 0.164665 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表