乐筑天下

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

通过VBA,从XLS的VBA编辑器,通过XLS生成DWG

[复制链接]

32

主题

430

帖子

423

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
150
发表于 2017-3-25 14:07:39 | 显示全部楼层 |阅读模式
我有一个:第1列的xls,块名为att1,标记到attn,标记可以是大约15个att。列,在同一行,具有att值。最后一列设置订单号或为空,以插入块。同一行中的所有数据都是dwt模板,模型空间为空。Adoc中的块集合是用户的所有功能。第三行中的块填充模型SAPCE,因为宽有纸空间,并且有一条线将每个块与子提示块连接起来。这是一种统一的电路,没有分支。第四行另存为phat/project。dwg希望它是清晰的,第5次将dwg打印为pdf文件,第6次打开pdf文件,用户将永远不会操作dwg,这是一个商业人士,没有ACAD技能
希望很清楚,或者至少我需要从xls看到xls VBA到dwg,而不是acad VBA到dwg
我精通VLISP,我想开始这项新任务
提前感谢是我心中的一个项目,在XLS和DWT中都没有实现,我只是想知道这是否可能,以及XLS VBA到ACAD的第一步。当然,用户站持有XLS,它是ACAD VBA的补充,ACAD,提前感谢

回复

使用道具 举报

11

主题

40

帖子

3

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
83
发表于 2017-4-18 10:22:09 | 显示全部楼层
我使用一些旧代码从Excel工作簿中包含的数据在ACAD中创建图形
  1. Sub Draw_Lines()
  2. Dim selRng As Range
  3. Dim lineData As Variant
  4. Dim DataStart, DataEnd, DataRange
  5. Dim WorkSheetInput As String, DataInput As String, ChartInput As String
  6. Dim i As Integer, j As Integer, NumRows As Integer
  7. Dim numcols As Integer, StartRow As Integer, EndRow As Integer, StartCol As Integer, EndCol As Integer
  8. Dim MinY As Double, MaxY As Double, MinX As Double, MaxX As Double, MaxWidth As Double
  9. ' Switch to AutoCAD
  10. Dim acad As AutoCAD.AcadApplication
  11. Dim adoc As AutoCAD.AcadDocument
  12. Dim aspace As AcadBlock
  13. Dim appNum As String
  14. On Error GoTo ErrorHandler
  15. appNum = acadVerNum
  16. If appNum = "" Then
  17.     Exit Sub
  18. End If
  19. On Error Resume Next
  20. Set acad = GetObject(, "Autocad.Application." & appNum)
  21. If Err.Number = 429 Then
  22. Err.Clear
  23. On Error GoTo 0
  24. Set acad = CreateObject("Autocad.Application." & appNum)
  25. If Err Then
  26. Exit Sub
  27. End If
  28. End If
  29. acad.WindowState = acMax
  30. Set adoc = acad.ActiveDocument
  31. Set aspace = adoc.ActiveLayout.Block
  32. ' Get the chart data from Excel
  33. Application.ScreenUpdating = False
  34. ' Need to selection the chart data from the worksheet
  35. DataInput = "INSULATOR_SWING"
  36. ChartInput = "CHART_DATA"
  37. Worksheets(ChartInput).Activate
  38. StartRow = 5 ' Need to have method to verify start location with other program
  39. StartCol = 1
  40. EndRow = LastRow(Worksheets(ChartInput))
  41. EndCol = LastCol(Worksheets(ChartInput))
  42. 'EndRow = ActiveSheet.UsedRange.Row - 1 + ActiveSheet.UsedRange.Rows.Count
  43. 'EndCol = ActiveSheet.UsedRange.Column - 1 + ActiveSheet.UsedRange.Columns.Count
  44. DataStart = Cells(StartRow, StartCol).Address(False, False)
  45. DataEnd = Cells(EndRow, EndCol).Address(False, False)
  46. DataRange = DataStart + ":" + DataEnd
  47. Set selRng = Worksheets(ChartInput).Range(DataRange)
  48. 'Set selRng = Selection
  49. lineData = selRng.Value2
  50. MaxX = Worksheets(DataInput).Cells(8, 11)
  51. MinX = Worksheets(DataInput).Cells(7, 11)
  52. NumRows = UBound(lineData, 1) ' Number of Rows
  53. numcols = UBound(lineData, 2) ' Number of Columns
  54. MaxY = 0 ' Reset MaxY Value
  55. For j = 2 To numcols
  56.     If lineData(NumRows, j) > MaxY Then
  57.         MaxY = lineData(NumRows, j)
  58.     End If
  59. Next j
  60. adoc.SetVariable "LTSCALE", (MaxX / 40)
  61. Dim LineTypeObject As AcadLineType
  62. Dim oSpline As AcadSpline
  63. Dim oLWPline As AcadPolyline
  64. Dim GraphLineHor, GraphLineVer, GridLineHor, GridLineVer, TickLine, ChartLine As AcadLine
  65. Dim TickLabel As AcadText
  66. Dim AxixLabel, TempText As AcadText
  67. Dim TextString As String
  68. Dim LineType As String
  69. Dim LayerName As String
  70. Dim TxtLayName As String
  71. Dim BrdrLayName As String
  72. Dim GridLayName As String
  73. Dim Xscale As Double
  74. Dim ticklength As Double
  75. Xscale = 1#  ' 2.5  need to add as an input ?  On the excel sheet?
  76. Dim TextPoint(0 To 2) As Double
  77. Dim GridStart(0 To 2) As Double
  78. Dim GridEnd(0 To 2) As Double
  79. Dim GraphStartPt(0 To 2) As Double
  80. Dim GraphXEndPt(0 To 2) As Double
  81. Dim GraphYEndPt(0 To 2) As Double
  82. Dim TickPtStart(0 To 2) As Double
  83. Dim TickPtEnd(0 To 2) As Double
  84. ' Setup Layers for text, grids, and borders
  85. TxtLayName = "Text"
  86. BrdrLayName = "Border"
  87. GridLayName = "Grid"
  88. MakeSetLayer (TxtLayName)
  89. MakeSetLayer (BrdrLayName)
  90. MakeSetLayer (GridLayName)
  91. 'Set LineTypeObject = AcadLineType.Load(LineType, LineTypeFileName)
  92. On Error Resume Next
  93. adoc.Linetypes.Load "DOT", "acad.lin"
  94. '--------------------------------------------------------------------
  95. ' TO DO.....
  96. ' Draw the Grid lines and axis lines
  97. ' Need to: Create layers for the various items
  98. '           1. Grid lines with appropriate color
  99. '           2. Graph borders ""
  100. '           3. Text ""
  101. '           4. Graphed data ""
  102. '
  103. ' Need to give the user the option for scaling the
  104. ' horizontal data?
  105. '
  106. ' Label the chart data series
  107. ' Draw a border around the graph
  108. ' Added the Title information at the top of the graph
  109. '
  110. '--------------------------------------------------------------------
  111. ' Round up to the nearest 100 to obtain the
  112. ' extents of the chart border
  113. MaxX = (100 * Int(MaxX / 100)) * Xscale
  114. MaxY = (100 * Int(MaxY / 100)) + 100
  115. ' Size the 'tick' and text
  116. ticklength = 0.02 * (MaxX / Xscale)
  117. If ticklength > 10 Then ticklength = 10
  118. ' Set the polyline width
  119. MaxWidth = (MaxX / 200) / Xscale
  120. ' Set the layer for the border and ticks
  121. 'MakeSetLayer (BrdrLayName)
  122. aspace.ActiveLayer = BrdrLayName
  123. ' Define the border of the chart
  124. GraphStartPt(0) = 0#: GraphStartPt(1) = 0#: GraphStartPt(2) = 0#
  125. GraphXEndPt(0) = MaxX: GraphXEndPt(1) = 0#: GraphXEndPt(2) = 0#
  126. GraphYEndPt(0) = 0#: GraphYEndPt(1) = MaxY: GraphYEndPt(2) = 0#
  127. ' Horizontal Axis Line
  128. Set GraphLineHor = aspace.AddLine(GraphStartPt, GraphXEndPt) ' need to change the color
  129. With GraphLineHor
  130.     .Color = 1
  131.     .LineType = "BYLAYER"
  132. End With
  133. 'Vertical Axis Line
  134. Set GraphLineVer = aspace.AddLine(GraphStartPt, GraphYEndPt)
  135. With GraphLineVer
  136.     .Color = 1
  137.     .LineType = "BYLAYER"
  138. End With
  139. ' Set the current layer to "Text" for the labels
  140. MakeSetLayer (TxtLayName)
  141. ' Label Horizontal Axis
  142. TextPoint(0) = MaxX / 2
  143. TextPoint(1) = -2.1 * ticklength * Xscale
  144. TextPoint(2) = 0#
  145. TextString = "HORIZONTAL SPAN"
  146. Set TempText = aspace.AddText(TextString, TextPoint, ticklength * 1.5 * Xscale)
  147. With TempText
  148.     .Alignment = acAlignmentTopCenter
  149.     .TextAlignmentPoint = TextPoint
  150.     .Color = 2#
  151. End With
  152. ' Label Vertical Axis
  153. TextPoint(1) = MaxY / 2
  154. TextPoint(0) = -2.1 * ticklength * Xscale
  155. TextPoint(2) = 0#
  156. TextString = "VERTICAL SPAN"
  157. Set TempText = aspace.AddText(TextString, TextPoint, ticklength * 1.5 * Xscale)
  158. With TempText
  159.     .Rotation = pi() / 2
  160.     .Alignment = acAlignmentBottomCenter
  161.     .TextAlignmentPoint = TextPoint
  162.     .Color = 2#
  163. End With
  164. ' Place and label the tick marks and grid lines
  165. ' Draw ticks and lable for the horizontal axis
  166. For i = 1 To MaxX / (100 * Xscale)
  167.     MakeSetLayer (BrdrLayName)
  168.     TickPtStart(0) = CDbl(i * Xscale * 100): TickPtStart(1) = 0#: TickPtStart(2) = 0#
  169.     TickPtEnd(0) = CDbl(i * Xscale * 100): TickPtEnd(1) = (-1 * ticklength): TickPtEnd(2) = 0#
  170.     Set TickLine = aspace.AddLine(TickPtStart, TickPtEnd)
  171.    
  172.     With TickLine
  173.         .Color = 1#
  174.         .LineType = "BYLAYER"
  175.     End With
  176.         
  177.     ' Label the ticks
  178.     MakeSetLayer (TxtLayName)
  179.     TextString = CStr(i * 100)
  180.     TickPtEnd(1) = (-1.1 * ticklength)
  181.     Set TickLabel = aspace.AddText(TextString, TickPtEnd, ticklength * 0.8 * Xscale)
  182.      
  183.     With TickLabel
  184.         .Alignment = acAlignmentTopCenter
  185.         .TextAlignmentPoint = TickPtEnd
  186.         .Color = 2#
  187.     End With
  188.    
  189.     GridStart(0) = TickPtStart(0): GridStart(1) = TickPtStart(1): GridStart(2) = TickPtStart(2)
  190.     GridEnd(0) = CDbl(i * Xscale * 100): GridEnd(1) = MaxY: GridEnd(2) = 0#
  191.     Set GridLineVer = aspace.AddLine(GridStart, GridEnd)
  192.     With GridLineVer
  193.         .Color = 1#
  194.         .LineType = "DOT"
  195.     End With
  196.          
  197. Next i
  198. ' Draw the ticks for the vertical axis
  199. For i = 1 To MaxY / 100
  200.     MakeSetLayer (BrdrLayName)
  201.     TickPtStart(1) = CDbl(i * 100): TickPtStart(0) = 0#: TickPtStart(2) = 0#
  202.     TickPtEnd(1) = CDbl(i * 100): TickPtEnd(0) = (-1 * ticklength): TickPtEnd(2) = 0#
  203.     Set TickLine = aspace.AddLine(TickPtStart, TickPtEnd)
  204.    
  205.     With TickLine
  206.         .Color = 1#
  207.         .LineType = "BYLAYER"
  208.     End With
  209.         
  210.     ' Label the ticks
  211.     MakeSetLayer (TxtLayName)
  212.     TextString = CStr(i * 100)
  213.     Set TickLabel = aspace.AddText(TextString, TickPtEnd, ticklength * 0.8 * Xscale)
  214.      With TickLabel
  215.         .Rotation = pi() / 2
  216.         .Alignment = acAlignmentBottomCenter
  217.         .TextAlignmentPoint = TickPtEnd
  218.         .Color = 2#
  219.     End With
  220.    
  221.     GridStart(0) = 0#: GridStart(1) = TickPtStart(1): GridStart(2) = 0#
  222.     GridEnd(0) = MaxX: GridEnd(1) = CDbl(i * 100): GridEnd(2) = 0#
  223.     Set GridLineHor = aspace.AddLine(GridStart, GridEnd)
  224.     With GridLineHor
  225.         .Color = 1#
  226.         .LineType = "DOT"
  227.     End With
  228. Next i
  229. ' Draw the insulator swing graph
  230. '=================================
  231. ' example of of creating a spline
  232. 'ReDim ptarr(0 To (UBound(lineData, 1) * 3) - 1) As Double
  233. 'Dim n
  234. 'For i = 1 To UBound(lineData, 1)
  235. '    ptarr(n) = CDbl(lineData(i, 1)): ptarr(n + 1) = CDbl(lineData(i, 2)): ptarr(n + 2) = 0#
  236. '    n = n + 3
  237. 'Next i
  238. Dim startPt(0 To 2) As Double
  239. Dim endPt(0 To 2) As Double
  240. 'startPt(0) = 0.5: startPt(1) = 0.5: startPt(2) = 0#
  241. 'endPt(0) = 0.5: endPt(1) = 0.5: endPt(2) = 0#
  242. 'Set oSpline = aspace.AddSpline(ptarr, startPt, endPt)
  243. '=================================
  244. endPt(2) = 0# ' Z coordinate is always zero
  245. startPt(2) = 0#
  246. Dim N
  247. For j = 2 To numcols
  248.    
  249.     ' Create layer for each graph line
  250.    
  251.     ReDim PTARR(0 To (NumRows * 3) - 1) As Double
  252.     PTARR(N) = CDbl(lineData(1, 1) * Xscale)
  253.     PTARR(N + 1) = CDbl(lineData(1, j))
  254.     PTARR(N + 2) = 0#
  255.     N = N + 3
  256.     'startPt(0) = lineData(1, 1): startPt(1) = lineData(1, j)
  257.    
  258.     For i = 1 To NumRows - 1
  259.         endPt(0) = lineData(i + 1, 1) * Xscale: endPt(1) = lineData(i + 1, j)
  260.         PTARR(N) = endPt(0)
  261.         PTARR(N + 1) = endPt(1)
  262.         PTARR(N + 2) = endPt(2)
  263.         
  264.         ' Draw as multiple line segments
  265.         'If startPt(1) >= 0 Then
  266.         '    Set ChartLine = aspace.AddLine(startPt, endPt)
  267.         '    With ChartLine
  268.         '        .Color = 3 + j
  269.         '        .LineType = "BYLAYER"
  270.         '    End With
  271.         'End If
  272.         'startPt(0) = endPt(0): startPt(1) = endPt(1)
  273.         
  274.         N = N + 3
  275.         
  276.     Next i
  277.    
  278.     LayerName = CStr(j - 1) + "-series"
  279.    
  280.     MakeSetLayer (LayerName)
  281.    
  282.     Set oLWPline = aspace.AddPolyline(PTARR)
  283.     With oLWPline
  284.         .Color = 3 + j
  285.         .LineType = "BYLAYER"
  286.         .ConstantWidth = MaxWidth
  287.                
  288.     End With
  289.    
  290.     N = 0
  291. Next j
  292. '' if you need to draw the closed spline then add this line:
  293. '' oSpline.Closed=True
  294. ZoomExtents
  295. Set aspace = Nothing
  296. Set adoc = Nothing
  297. Set acad = Nothing
  298. Application.ScreenUpdating = True
  299. Worksheets(DataInput).Activate ' return to main screen
  300. ErrorHandler:
  301.     If Err.Number  0 Then
  302.         'MsgBox Err.Description
  303.     End If
  304.    
  305. End Sub

回复

使用道具 举报

32

主题

430

帖子

423

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
150
发表于 2017-4-18 15:38:10 | 显示全部楼层
嗨,Yosso。好像阳光照在我的眼睛上。这是我第一次看到这么好的东西
如果需要的话,我会试着问你。再次感谢
回复

使用道具 举报

106

主题

1万

帖子

101

银币

顶梁支柱

Rank: 50Rank: 50

铜币
1299
发表于 2017-4-20 23:21:07 | 显示全部楼层
使用lisp读取单个单元格的值可以在excel中获得其他两种方法,请查看getexel。lsp,另一种替代方法是生成csv文件,然后再次使用lisp创建dwg。对我来说,使用lisp更舒服。
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-4-20 00:59 , Processed in 3.362796 second(s), 62 queries .

© 2020-2025 乐筑天下

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