乐筑天下

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

[编程交流] 从e另存为autocad图形

[复制链接]

42

主题

173

帖子

132

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
220
发表于 2022-7-6 21:53:18 | 显示全部楼层 |阅读模式
大家好
 
我使用的是Christos Samaras先生编写的excell的vba
 
此vba绘图块位于激活的autocad图形中
 
我希望每次运行vba宏时都将图形另存为单元格A1内容
 
e、 g.如果单元格A1为(section1),则vba将图形另存为(section1.dwg)
 
任何帮助都将不胜感激
提前感谢
 
  1. Option Explicit
  2. 'A custom type that holds the scale factors of the block.
  3. Private Type ScaleFactor
  4.    X As Double
  5.    Y As Double
  6.    Z As Double
  7. End Type
  8. Sub InsertBlocks()
  9.    '--------------------------------------------------------------------------------------------------------------------------
  10.    'Inserts blocks in AutoCAD using data - insertion point, block name/full path, scale factors, rotation angle - from Excel.
  11.    'Note that the block name or the block path must already exists, otherwise nothing will be inserted.
  12.    'The code uses late binding, so no reference to external AutoCAD (type) library is required.
  13.    'It goes without saying that AutoCAD must be installed at your computer before running this code.
  14.    
  15.    'Written by:    Christos Samaras
  16.    'Date:          21/04/2014
  17.    'e-mail:        xristos.samaras@gmail.com
  18.    'site:          http://www.myengineeringworld.net
  19.    '--------------------------------------------------------------------------------------------------------------------------
  20.       
  21.    'Declaring the necessary variables.
  22.    Dim acadApp                 As Object
  23.    Dim acadDoc                 As Object
  24.    Dim acadBlock               As Object
  25.    Dim LastRow                 As Long
  26.    Dim i                       As Long
  27.    Dim InsertionPoint(0 To 2)  As Double
  28.    Dim BlockName               As String
  29.    Dim BlockScale              As ScaleFactor
  30.    Dim RotationAngle           As Double
  31.    
  32.    'Activate the coordinates sheet and find the last row.
  33.    With Sheets("ADD SECTION")
  34.        .Activate
  35.        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  36.    End With
  37.       
  38.    'Check if there are coordinates for at least one circle.
  39.    If LastRow < 2 Then
  40.        MsgBox "There are no coordinates for the insertion point!", vbCritical, "Insertion Point Error"
  41.        Exit Sub
  42.    End If
  43.    
  44.    'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
  45.    On Error Resume Next
  46.    Set acadApp = GetObject(, "AutoCAD.Application")
  47.    If acadApp Is Nothing Then
  48.        Set acadApp = CreateObject("AutoCAD.Application")
  49.        acadApp.Visible = True
  50.    End If
  51.    
  52.    'Check (again) if there is an AutoCAD object.
  53.    If acadApp Is Nothing Then
  54.        MsgBox "Sorry, it was impossible to start AutoCAD!", vbCritical, "AutoCAD Error"
  55.        Exit Sub
  56.    End If
  57.    On Error GoTo 0
  58.    
  59.    'If there is no active drawing create a new one.
  60.    On Error Resume Next
  61.    Set acadDoc = acadApp.ActiveDocument
  62.    If acadDoc Is Nothing Then
  63.        Set acadDoc = acadApp.Documents.Add
  64.    End If
  65.    On Error GoTo 0
  66.    'Check if the active space is paper space and change it to model space.
  67.    If acadDoc.ActiveSpace = 0 Then '0 = acPaperSpace in early binding
  68.        acadDoc.ActiveSpace = 1     '1 = acModelSpace in early binding
  69.    End If
  70.    
  71.    On Error Resume Next
  72.    'Loop through all the rows and add the corresponding blocks in AutoCAD.
  73.    With Sheets("ADD SECTION")
  74.        For i = 2 To LastRow
  75.            'Set the block name.
  76.            BlockName = .Range("D" & i).Value
  77.            'If the block name is not empty, insert the block.
  78.            If BlockName <> vbNullString Then
  79.                'Set the insertion point.
  80.                InsertionPoint(0) = .Range("A" & i).Value
  81.                InsertionPoint(1) = .Range("B" & i).Value
  82.                InsertionPoint(2) = .Range("C" & i).Value
  83.                'Initialize the optional parameters.
  84.                BlockScale.X = 1
  85.                BlockScale.Y = 1
  86.                BlockScale.Z = 1
  87.                RotationAngle = 0
  88.                'Set the optional parameters (if there are values on the corresponding ranges).
  89.                If .Range("E" & i).Value <> vbNullString Then BlockScale.X = .Range("E" & i).Value
  90.                If .Range("F" & i).Value <> vbNullString Then BlockScale.Y = .Range("F" & i).Value
  91.                If .Range("G" & i).Value <> vbNullString Then BlockScale.Z = .Range("G" & i).Value
  92.                If .Range("H" & i).Value <> vbNullString Then RotationAngle = .Range("H" & i).Value
  93.                'Add the block using the sheet data (insertion point, block name, scale factors and rotation angle).
  94.                'The 0.0174532925 is to convert degrees into radians.
  95.                Set acadBlock = acadDoc.ModelSpace.InsertBlock(InsertionPoint, BlockName, _
  96.                                BlockScale.X, BlockScale.Y, BlockScale.Z, RotationAngle * 0.0174532925)
  97.            End If
  98.        Next i
  99.    End With
  100.    
  101.    'Zoom in to the drawing area.
  102.    acadApp.ZoomExtents
  103.    'Release the objects.
  104.    Set acadBlock = Nothing
  105.    Set acadDoc = Nothing
  106.    Set acadApp = Nothing
  107.    
  108.    'Inform the user about the process.
  109.    MsgBox "The blocks were successfully inserted in AutoCAD!", vbInformation, "Finished"
  110. End Sub
  111. Sub ClearAll()
  112.    
  113.    Dim LastRow As Long
  114.    
  115.    'Find the last row and clear all the input data..
  116.    With Sheets("ADD SECTION")
  117.        .Activate
  118.        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
  119.        .Range("A2:H" & LastRow).ClearContents
  120.        .Range("A2").Select
  121.    End With
  122.    
  123. End Sub
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-5-26 01:27 , Processed in 0.341067 second(s), 54 queries .

© 2020-2025 乐筑天下

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