dogingate 发表于 2020-6-18 14:16:26

使用python操作autocad2007

本帖最后由 dogingate 于 2020-6-19 14:07 编辑

<div class="blockcode"><blockquote>from pyautocad import Autocad, APoint

dblSpan = 16000
intBeamsCount = 8
dblBeamDist = 1000
dblSideDist = 300
intElesCount = 16

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

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

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

for i in range(len(layers_name)):
    olayer = acad.ActiveDocument.Layers.Add(layers_name)
    olayer.color = layers_color
    olayer.Linetype = layers_linetype

# 当前文件模型空间中所包含的图层总数
layers_nums = acad.ActiveDocument.Layers.count
# 当前文件模型空间中所包含的所有图层名称
layers_names =

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

# add some objects to document
p1 = APoint(0, 0)
p2 = APoint(dblSpan, 0)
for i in range(0, intBeamsCount):
    acad.model.AddLine(p1, p2)
    p1 = APoint(0, p1+dblBeamDist)
    p2 = APoint(dblSpan, p1)

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

dist = dblSpan/intElesCount
p1 = APoint(dist/2, -dblSideDist)
p2 = APoint(dist/2, (intBeamsCount-1)*dblBeamDist+dblSideDist)
for i in range(0, intElesCount):
    acad.model.AddLine(p1, p2)
    p1 = APoint(p1+dist, p1)
    p2 = APoint(p2+dist, p2)

dogingate 发表于 2020-6-19 14:08:44

与上一个帖子同样的功能,用VBA实现
Sub deleteTextAndDimension()
   
    Dim oSS As Object
    On Error Resume Next
    If Not IsNull(ThisDrawing.SelectionSets.Item("Wolf")) Then
      Set oSS = ThisDrawing.SelectionSets.Item("wolf")
      oSS.Delete
    End If
    Set oSS = ThisDrawing.SelectionSets.Add("wolf")

    On Error GoTo catchError
    Dim fType() As Integer
    Dim fData As Variant
      strFilterType = "-4,0,0,-4"
    strFilterData = "<or,text,dimension,or>"
    Call createFilter(fType, fData, strFilterType, strFilterData)

    oSS.SelectOnScreen fType, fData
    oSS.Highlight ture
    oSS.Erase
    oSS.Delete

exitSub:
    Exit Sub
catchError:
    ' add error handling
    If Err Then
      Err.Clear
      MsgBox Err.Description
    End If
   
End Sub

Sub createFilter(fType, fData, strFilterType, strFilterData)
    '// add declarations
    On Error GoTo catchError
    arrFilterType = Split(strFilterType, ",")
    arrFilterData = Split(strFilterData, ",")
    If UBound(arrFilterType) = UBound(arrFilterData) Then
      intFilterCount = UBound(arrFilterType)
      ReDim fType(intFilterCount)
      ReDim fData(intFilterCount)
      For i = 0 To UBound(arrFilterType)
            fType(i) = arrFilterType(i)
            fData(i) = arrFilterData(i)
      Next i
    Else
      GoTo exitFunction
    End If

exitFunction:
    Exit Sub
catchError:
    '// add error handling
    GoTo exitFunction
End Sub

dogingate 发表于 2020-6-18 14:18:16

本帖最后由 dogingate 于 2020-6-18 14:19 编辑

以下是VBA代码,实现相同的功能,都在autocad2007里面测试完成,本来是想用vba,但是滚轮插件实现不了,老是有问题,刚好看到有Pyautocad,就试了下,效果还不错,可以对比下
Sub Example_AddLine()
    ' 该示例在模型空间中添加直线。
    Dim oline As AcadLine
    Dim startPoint(0 To 2) As Double
    Dim endPoint(0 To 2) As Double
    Dim dblSpan As Double
    Dim intBeamsCount As Integer
    Dim intElesCount As Integer
       Dim dblBeamDist As Double
    Dim dblSideDist As Double
   
   intElesCount = 20
    dblSideDist = 300
    dblBeamDist = 1000
    intBeamsCount = 12
    dblSpan = 16000
    '定义直线的起点和终点
    startPoint(0) = 0#: startPoint(1) = 0#: startPoint(2) = 0#
    endPoint(0) = dblSpan: endPoint(1) = 0#: endPoint(2) = 0#
    For i = 1 To intBeamsCount
      ' 在模型空间中创建直线
      Set oline = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)
      startPoint(0) = startPoint(0): startPoint(1) = startPoint(1) + dblBeamDist: startPoint(2) = 0#
      endPoint(0) = dblSpan: endPoint(1) = startPoint(1): endPoint(2) = 0#
    Next i
   
    startPoint(0) = dblSpan / intElesCount / 2: startPoint(1) = -dblSideDist: startPoint(2) = 0#
    endPoint(0) = dblSpan / intElesCount / 2: endPoint(1) = (intBeamsCount - 1) * dblBeamDist + dblSideDist: endPoint(2) = 0#
    For i = 1 To intElesCount
         Set oline = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)
         startPoint(0) = startPoint(0) + dblSpan / intElesCount: startPoint(1) = startPoint(1): startPoint(2) = 0#
         endPoint(0) = startPoint(0): endPoint(1) = endPoint(1): endPoint(2) = 0#
    Next i
   
    ZoomAll
End Sub

dogingate 发表于 2020-6-19 14:08:01

这段代码用于在屏幕上选中的对象删除其中的文字和标注
from pyautocad import Autocad, APoint

from win32com.client import VARIANT
from win32com.client import Dispatch
import pythoncom

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

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

def vtFloat(list):
    """列表转化为浮点数"""
    return VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_R8, list)
   
def vtInt(list):
    """列表转化为整数"""
    return VARIANT(pythoncom.VT_ARRAY | pythoncom.VT_I2, list)

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

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

if doc.SelectionSets.count>0:
    try:
      doc.SelectionSets.Item("SS1").Delete()
    except:
      print("Delete selection failed")

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

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

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

# object.Select(Mode, Point1, Point2, FilterType, FilterData)
slt.SelectOnScreen(filterType, filterData)# 实现过滤
obj = slt
slt.Erase()# 删除符合条件的所有圆

panliang9 发表于 2020-6-19 09:35:47

在AUTOCAD2007下运行了一下VBA,画出一系列1000x800的网格,是这样的吗。

dogingate 发表于 2020-6-19 10:51:05

panliang9 发表于 2020-6-19 09:35
在AUTOCAD2007下运行了一下VBA,画出一系列1000x800的网格,是这样的吗。

是的,你可以调整参数,改一下代码,做类似的事情

dogingate 发表于 2020-6-19 14:30:34

几个参考文档

dogingate 发表于 2020-6-20 11:31:39

贴一个VBSub sortPlineByX(arr)
Dim i&, j&, vSwap, min&
For i = LBound(arr, 1) To UBound(arr, 1)
    min = i
    For j = i + 1 To UBound(arr, 1)
      If arr(min, 0) > arr(j, 0) Then min = j
    Next
    If min <> i Then
      For k = 0 To 4
            vSwap = arr(min, k): arr(min, k) = arr(i, k): arr(i, k) = vSwap
      Next k
    End If
Next i

End SubA的排序算法

guangdonglbq 发表于 2020-6-24 10:03:16

python的话,直接用非狐的pycad不是更好?不过pycad是用来替换.net的{:1_1:}

guangdonglbq 发表于 2020-6-24 10:04:59

dogingate 发表于 2020-6-19 14:30
几个参考文档

这些文件,一般在完整版的acad的help目录(如\AutoCAD 2008\Help)下都有。
页: [1] 2
查看完整版本: 使用python操作autocad2007