乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 523|回复: 15

[编程交流] 从ACAD中绘制矩形

[复制链接]

1

主题

5

帖子

4

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 22:14:27 | 显示全部楼层 |阅读模式
朋友们好,我正在使用AutoCAD 2012。我在Excel工作表(inputList.xlsx)中有一个矩形尺寸列表,我需要在autocad中创建矩形,并将其以dxf格式保存在一个文件夹中,而不实际打开autocad。有人能帮忙吗???我有vba在excel方面的知识。。。
回复

使用道具 举报

5

主题

1074

帖子

1088

银币

初来乍到

Rank: 1

铜币
9
发表于 2022-7-6 22:21:51 | 显示全部楼层
欢迎来到CADTutor。
 
对不起,我不能帮你,但如果有人帮你,希望他们能为我写一个,将我的日常工作
没有我的眼睛。
对不起,我忍不住,请耐心等待,可能有人会帮你施展魔法。
回复

使用道具 举报

35

主题

2471

帖子

2447

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
174
发表于 2022-7-6 22:22:48 | 显示全部楼层
您可以通过COM技术访问AutoCAD应用程序:
  1. Set appAutoCAD = CreateObject("AutoCAD.Application")

但是,您可以选择隐藏其界面,但AutoCAD应处于打开状态才能编辑图形。
  1. myDrawing.Application.Visible = False
回复

使用道具 举报

35

主题

2471

帖子

2447

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
174
发表于 2022-7-6 22:30:31 | 显示全部楼层
顺便说一句,最简单的解决方案是从这些坐标系中创建脚本,方法是在Excel中格式化数据或使用VBA自动化,然后在AutoCAD中调用它。
  1. _RECTANGLE 0.0,0.0 10.0,10.0
  2. ;end of script
回复

使用道具 举报

10

主题

598

帖子

594

银币

初来乍到

Rank: 1

铜币
48
发表于 2022-7-6 22:31:45 | 显示全部楼层
如果在excel中格式化数据,使矩形坐标存储在前4列中。
A列包含第一个x坐标
B列包含第一个y坐标
C列包含第二个x坐标
D列包含第二个y坐标
 
然后,以下代码将创建一个脚本文件,您可以将其拖动到打开的autocad窗口中
 
  1. Sub WriteToTextFile1()
  2.    Dim iFileNumber                        As Long
  3.    Dim strFileName                        As String
  4.    iFileNumber = FreeFile()
  5.    strFileName = "C:\scr\test.scr"         'name and location of the script file
  6.    Open strFileName For Output As #iFileNumber
  7.    For r = 1 To 2                          'row number of data
  8.    Print #iFileNumber, "rectangle"
  9.    x1 = Cells(r, 1).Value
  10.    y1 = Cells(r, 2).Value
  11.    x2 = Cells(r, 3).Value
  12.    y2 = Cells(r, 4).Value
  13.    Print #iFileNumber, x1 & "," & y1  'first point of rectangel
  14.    Print #iFileNumber, x2 & "," & y2  'second point of rectangle
  15.    Next
  16.    Close #iFileNumber
  17. End Sub

您可以根据需要更改文件名,然后需要创建文件夹,将“r”的值更改为您拥有的数据行数。最后一件事是确保关闭所有OSNAP,即使在运行脚本时,如果某个点接近您的坐标,autocad也会捕捉到该点。首先在空白图形文件上测试这一点,并始终在运行脚本之前备份任何图形。
这是使用Excel VBA创建脚本的基础,您可以添加到其中以创建DXF文件,只需计算出命令序列并将其添加到脚本中即可。
回复

使用道具 举报

1

主题

5

帖子

4

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 22:36:32 | 显示全部楼层
这段代码是我在VBA(AutoCAD)中用来创建矩形的
 
  1. Private Sub CommandButton1_Click()
  2. Pi = 3.14159265358979
  3. UserForm1.Hide
  4. pt1 = ThisDrawing.Utility.GetPoint(, "Pick Lower Left Corner:")
  5. pt2 = ThisDrawing.Utility.PolarPoint(pt1, 0, Val(TextBox1.Text))
  6. pt3 = ThisDrawing.Utility.PolarPoint(pt2, Pi / 2, Val(TextBox2.Text))
  7. pt4 = ThisDrawing.Utility.PolarPoint(pt1, Pi / 2, Val(TextBox2.Text))
  8. ThisDrawing.ModelSpace.AddLine pt1, pt2
  9. ThisDrawing.ModelSpace.AddLine pt2, pt3
  10. ThisDrawing.ModelSpace.AddLine pt3, pt4
  11. ThisDrawing.ModelSpace.AddLine pt4, pt1
  12. ThisDrawing.ModelSpace.AddCircle pt1, Val(TextBox2.Text) / 2
  13. End Sub

 
但它使用一个用户表单,从用户那里获取数据,数据来自AutoCAD
 
我需要从Excel和Excel中输入。。。
回复

使用道具 举报

4

主题

2143

帖子

2197

银币

限制会员

铜币
-24
发表于 2022-7-6 22:40:53 | 显示全部楼层
请阅读代码发布指南并使用代码标签。
回复

使用道具 举报

1

主题

1069

帖子

1050

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
69
发表于 2022-7-6 22:42:34 | 显示全部楼层
请附上测试用Excel文件的截图,
你的数据是如何填写的还不够清楚
假设你的坐标在每偶数行8列中,
试试这个例子
  1. Option Explicit
  2. '---------------------------------------------------------------'
  3. ' Notes:
  4. ' 1. requires settings: Tools -> Options -> General tab -> Error
  5. trapping field -> check "Break on Unhandled errors"
  6. ' 2. requires reference to Microsoft Excel XX.0 Object Library
  7. '---------------------------------------------------------------'
  8. ' written by Sccadmember
  9. ' Date: Aug/04/06
  10. ' [url]http://discussion.autodesk.com/thread.jspa?threadID=489202[/url]
  11. ' Just thought I would post this because I have been looking for a working
  12. VBA file open dialog box
  13. ' solution for awhile. 'I'm an old autolisped making the jump to VBA and I
  14. have seen and read
  15. ' various solutons for the equivalent getfiled 'autolisp function but I never
  16. had much luck with them.
  17. ' This one worked for me it uses the Win API to do the job.
  18. '---------------------------------------------------------------'
  19. Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
  20. "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
  21. Private Type OPENFILENAME
  22. lStructSize As Long
  23. hwndOwner As Long
  24. hInstance As Long
  25. lpstrFilter As String
  26. lpstrCustomFilter As String
  27. nMaxCustFilter As Long
  28. nFilterIndex As Long
  29. lpstrFile As String
  30. nMaxFile As Long
  31. lpstrFileTitle As String
  32. nMaxFileTitle As Long
  33. lpstrInitialDir As String
  34. lpstrTitle As String
  35. flags As Long
  36. nFileOffset As Integer
  37. nFileExtension As Integer
  38. lpstrDefExt As String
  39. lCustData As Long
  40. lpfnHook As Long
  41. lpTemplateName As String
  42. End Type
  43. '---------------------------------------------------------------'
  44. Public Function ShowOpen(Filter As String, _
  45. InitialDir As String, _
  46. DialogTitle As String) As String
  47. Dim OFName As OPENFILENAME
  48. 'Set the structure size
  49. OFName.lStructSize = Len(OFName)
  50. 'Set the owner window
  51. OFName.hwndOwner = 0
  52. 'Set the filter
  53. OFName.lpstrFilter = Filter
  54. 'Set the maximum number of chars
  55. OFName.nMaxFile = 255
  56. 'Create a buffer
  57. OFName.lpstrFile = Space(254)
  58. 'Create a buffer
  59. OFName.lpstrFileTitle = Space$(254)
  60. 'Set the maximum number of chars
  61. OFName.nMaxFileTitle = 255
  62. 'Set the initial directory
  63. OFName.lpstrInitialDir = InitialDir
  64. 'Set the dialog title
  65. OFName.lpstrTitle = DialogTitle
  66. 'no extra flags
  67. OFName.flags = 0
  68. 'Show the 'Open File' dialog
  69. If GetOpenFileName(OFName) Then
  70. ShowOpen = Trim(OFName.lpstrFile)
  71. Else
  72. ShowOpen = ""
  73. End If
  74. End Function
  75. '---------------------------------------------------------------'
  76. Function IsExcelRunning() As Boolean
  77. Dim xlApp As Excel.Application
  78. On Error Resume Next
  79. Set xlApp = GetObject(, "Excel.Application")
  80. IsExcelRunning = (Err.Number = 0)
  81. Set xlApp = Nothing
  82. Err.Clear
  83. End Function
  84. Sub CreateDxfDocuments()
  85. ' To read data from specific Excel range
  86. Dim xlApp As Excel.Application
  87. Dim blnIsOK As Boolean
  88. Dim xlBook As Excel.Workbook
  89. Dim xlSheet As Excel.Worksheet
  90. blnIsOK = IsExcelRunning()
  91. If blnIsOK Then
  92. Set xlApp = GetObject(, "Excel.Application")
  93. Else
  94. Set xlApp = CreateObject("Excel.Application")
  95. xlApp.Visible = True
  96. xlApp.UserControl = True
  97. End If
  98. Dim xlFileName As String
  99. Dim Filter As String
  100. Dim InitialDir As String
  101. Dim DialogTitle As String
  102. Filter = "Excel Files (*.xlsx)" + Chr$(0) + "*.xlsx" + Chr$(0) + _
  103. "All Files (*.*)" + Chr$(0) + "*.*" + Chr$(0)
  104. InitialDir = ThisDrawing.GetVariable("dwgprefix")
  105. DialogTitle = "Open an Excel file"
  106. xlFileName = ShowOpen(Filter, InitialDir, DialogTitle)
  107. ' to avoid using code above try hard coded file path,
  108. ' set full path of your Excel file here:
  109. ' xlFileName = "C:\Users\User\myxlFile.xlsx"
  110. xlApp.Application.ScreenUpdating = False
  111. ' open file for read
  112. Set xlBook = xlApp.Workbooks.Open(xlFileName)
  113. Set xlSheet = xlBook.Worksheets("Sheet1") ' desired sheet name , same if use
  114. xlBook.Worksheets(1)
  115. xlSheet.Activate
  116. Dim xlrange As Excel.Range
  117. Set xlrange = xlSheet.Range("A1:H4") ' range address we interested in
  118. xlrange.Select
  119.    Dim cols As Long
  120.    Dim rows As Long
  121.    cols = xlrange.Columns.Count
  122.    rows = xlrange.rows.Count
  123. Dim rangeValue As Variant
  124. rangeValue = xlrange.Value2
  125. ' clean up memory
  126.    xlBook.Close False
  127. Set xlBook = Nothing
  128.    xlApp.Quit
  129. Set xlApp = Nothing
  130. 'DoEvents
  131. ' end of work with Excel,
  132. ' go to Autocad then
  133. MsgBox "Back to AutoCAD"
  134. ''--------------------------------------------------------
  135. Dim dxfname As String
  136. dxfname = "-Sample.dxf"
  137. Dim ptCoordinates As New Collection
  138.   Dim i, j
  139.   For i = LBound(rangeValue, 1) To UBound(rangeValue, 1)
  140.   ReDim ptarray(LBound(rangeValue, 2) To UBound(rangeValue, 2)) As
  141. Double
  142.   For j = LBound(rangeValue, 2) To UBound(rangeValue, 2)
  143.   ptarray(j) = rangeValue(i, j)
  144.   Next j
  145.   ptCoordinates.Add ptarray, CStr(i) & dxfname
  146.   Next i
  147.   Dim docMgr As AcadDocuments
  148.   Set docMgr = Application.Documents
  149. Dim item As Variant
  150. Dim num As Integer
  151. num = 1
  152. For Each item In ptCoordinates
  153. Dim acDoc As AcadDocument
  154. ReDim dblPoints(LBound(item) To UBound(item)) As Double
  155. For i = LBound(item) To UBound(item)
  156. dblPoints(i) = CDbl(item(i))
  157. Next i
  158. Set acDoc = docMgr.Add()
  159. Dim lwPoly As AcadLWPolyline
  160. Set lwPoly = acDoc.ModelSpace.AddLightWeightPolyline(dblPoints)
  161. lwPoly.Closed = True
  162. acDoc.Application.ZoomExtents
  163. acDoc.SaveAs ThisDrawing.GetVariable("dwgprefix") & CStr(num) &
  164. dxfname, ac2007_dxf
  165. acDoc.Close
  166. num = num + 1
  167. Next item
  168. MsgBox "Done"
  169. End Sub
  170. '---------------------------------------------------------------'
回复

使用道具 举报

1

主题

5

帖子

4

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 22:48:48 | 显示全部楼层
这是文件的屏幕截图。
231432vlxzxh0zexey1i7r.png
 
 
回复

使用道具 举报

1

主题

5

帖子

4

银币

初来乍到

Rank: 1

铜币
5
发表于 2022-7-6 22:52:06 | 显示全部楼层
令人惊异的人你太棒了!谢谢你的回复。它解决了这个问题。
在连续的8列中给出坐标,它工作得完美无缺
 
我想补充两件事,
 
1) 如何选择保存文件的最终目录。
2) 如果我在另一列中有相应的文件名,我如何使用它?
(我将使用它一次生成大约500个dxf。为了减少冗余,我需要这一步)
 
 
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-4 11:22 , Processed in 1.087142 second(s), 74 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表