乐筑天下

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

ACAD 2018和ObjectDBX难题?

[复制链接]

11

主题

40

帖子

3

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
83
发表于 2018-4-4 08:24:10 | 显示全部楼层 |阅读模式
使用一些古老的代码尝试从Excel工作表在ACAD 2018中创建表
一切顺利进行,直到我尝试创建一行并显示错误消息;无效的过程调用或参数
oDBxDoc.PaperSpace。AddLine sp,ep创建oDbxDoc,并在监视窗口中显示它是有效的AxDbDocument
在剥离AutoCAD 2017 my PC之前,代码(我认为)是有效的……但这个项目已经进行了几个月,我的内存越来越差
我的测试模块:
  1. Sub ACADCreateDwg()
  2. On Error GoTo ErrorHandler
  3. ' Testing Variables
  4. Dim oDBx As cObjDbx
  5. Dim oDBxDoc As AxDbDocument ' Object ' Late binding!
  6. Dim OpenName As String
  7. Dim SaveName As String
  8. Dim sp(0 To 2) As Single
  9. Dim ep(0 To 2) As Single
  10. Set oDBx = New cObjDbx
  11. sp(0) = 0#: sp(1) = 0#: sp(2) = 0#
  12. ep(0) = 34#: ep(1) = 23#: ep(2) = 0#
  13. OpenName = "D:\Dropbox\AMCE Work Folders\Substation Spreadsheets\Sag-Tension Workbook\XML OUTPUT FROM PLS-CADD\Sag_Tables.dwt"
  14. SaveName = "TEST"
  15. ' Open the template file
  16. oDBx.xOpen OpenName
  17. ' Cast the Doc property object as an AxDbDocument Ojbect
  18. Set oDBxDoc = oDBx.Doc
  19. ' Add a line
  20. oDBxDoc.PaperSpace.AddLine sp, ep
  21. oDBx.xSaveAs oDBx.OrigPath, "Copy of ", "1"
  22. oDBx.xClose
  23. 'There 's no New method on an AxDbDocument.
  24. '
  25. 'If you want to create a new document that's derived
  26. 'from a template, just open the template (DWT) file
  27. 'in the AxDbDocument, and when you've done what you
  28. 'need, save it as a .DWG file.
  29. Exit Sub
  30. ErrorHandler:
  31. Debug.Print Err.Description
  32. Stop
  33. oDBx.xClose

来自Ed Jobe的课程模块(大约2006年)
  1. Option Explicit
  2. '=================================================================================================
  3. ' Purpose   : Create an object class for access Autocad via the ObjectDBX
  4. '
  5. ' Date      : 01/19/18
  6. ' Updated   :
  7. '=================================================================================================
  8. ' Need code to differentiate the various versions of ACAD?
  9. ' 2018-01-19
  10. ' Replaced AXDBLib with AXDBLib
  11. '=================================================================================================
  12. ' References Requires
  13. ' AutoCAD    C:\Program Files\Common Files\Autodesk Shared\acax22enu.tlb
  14. ' AXDBLib    C:\Program Files\Common Files\Autodesk Shared\axdb22enu.tlb
  15. ' Scripting  C:\Windows\SysWOW64\scrrun.dll
  16. '=================================================================================================
  17. 'copyright 2006, Ed Jobe
  18. ' Note: This requires ObjectDBX to be registered on each
  19. ' user's machine. This is done automatically on 2004 and up.
  20. ' In your AutoCAD folder, locate the file AxDb15.dll.
  21. ' This only needs to be done once at each machine. If you are using vb instead
  22. ' of vba, you will also need to set a reference to "ObjectDBX 1.0 Type library"
  23. ' and "Microsoft Scripting Runtime".
  24. ' This class auto-registers the dll, but here is the normal procedure---
  25. ' From the dos command line, type:
  26. ' C:\> cd Autodesk\Map200i
  27. ' C:\Autodesk\Map2000i> RegSvr32.exe AxDb15.dll
  28. 'This class unfortunately doesn't follow standards for objects due to the fact that
  29. 'I was unable to Implement the properties/methods of ObjectDbx. Normally, the Open,
  30. 'Close, Save and SaveAs methods would belong in the doc class. But since you can't use
  31. 'the Implements statement on ObjectDbx, I had to move them to this class. This class
  32. 'uses late binding to be able to handle versioning. In order to access all the
  33. 'methods/properties of an AxDbDocument in your project, you must Dim a variable as
  34. ' type AxDbDocument and then the object returned by the Doc property to cast it as
  35. ' an AxDbDocument object. Therefore, this class does not reference AxDb15.dll, so
  36. 'your project needs to.
  37. 'Last error number used: 1003
  38. 'Variable declarations for Properties
  39. Private oDoc As AxDbDocument ' Object
  40. Private strOrigPath As String
  41. Private strTempPath As String
  42. Private strExt As String
  43. Private bReadOnly As Boolean
  44. 'Property Declarations
  45. '*********************
  46. Public Property Get Doc() As Object
  47.     Set Doc = oDoc
  48. End Property
  49. Public Property Get Ext() As String
  50.     Ext = strExt
  51. End Property
  52. Public Property Get OrigPath() As String
  53.     OrigPath = strOrigPath
  54. End Property
  55. Public Property Get TempPath() As String
  56.     TempPath = strTempPath
  57. End Property
  58. Public Property Get ReadOnly() As Boolean
  59.     ReadOnly = bReadOnly
  60. End Property
  61. 'Class Methods
  62. '*************
  63. Public Sub xClose()
  64.     'required for proper cleanup of temp files
  65.     Set oDoc = Nothing
  66. End Sub
  67. Private Function acadVerNum() As String
  68.     Dim verNum As String
  69.     verNum = "HKEY_CLASSES_ROOT\AutoCAD.Drawing\CurVer"
  70.    
  71.     Dim wsh As Object
  72.     ' Error trapping
  73.     On Error GoTo ErrorHandler
  74.    
  75.     'access Windows scripting
  76.     Set wsh = CreateObject("WScript.Shell")
  77.    
  78.     'read key from registry
  79.     Dim resKey As String
  80.     resKey = wsh.RegRead(verNum)
  81.       
  82.     acadVerNum = Right(resKey, 2)
  83.    
  84.     Set wsh = Nothing
  85.     Exit Function
  86.    
  87. ErrorHandler:
  88.     acadVerNum = ""
  89.     Set wsh = Nothing
  90.    
  91. End Function
  92. Public Sub xOpen(FilePath As String)
  93.     'Sets the Doc property to an ObjectDBX Document
  94.     'late binding is used to avoid setting a reference.
  95.     '
  96.     On Error GoTo ErrHandler
  97.    
  98.     Dim dbxdoc As AxDbDocument 'Object
  99.     Dim strTempName As String
  100.     Dim fso As Scripting.FileSystemObject
  101.     Dim fsoFile As Scripting.File
  102.     Dim colPCs As AcadPlotConfigurations
  103.     Dim objPC As AcadPlotConfiguration
  104.     Dim varList As Variant
  105.     Dim i As Integer
  106.     Dim cnt As Integer
  107.     Dim iActiveLayout As Integer
  108.     Dim strObjDbxPath As String
  109.     Dim ACAD As Object
  110.         
  111.     bReadOnly = False
  112.     strOrigPath = FilePath
  113.    
  114.     Set fso = CreateObject("Scripting.FileSystemObject")
  115.    
  116. Cleanup:
  117.     'if this is not the first time a doc was opened, then
  118.     'there may be a temp file left. Clean it up!
  119.     If fso.FileExists(strTempPath) Then
  120.         fso.DeleteFile (strTempPath)
  121.         strTempPath = ""
  122.     End If
  123.     'check for dwt, ObjectDbx can only open dwg's
  124.     strExt = fso.GetExtensionName(FilePath)
  125.     'Calling sub can check the Ext property. If it equals "dwt", then
  126.     'a temp file was created at the location stored in the TempPath property.
  127. SetDbxDoc:
  128.     ' MSJ - added a function to obtain the acad version number
  129.     ' 4/4/18
  130.     ' Trying a "trick" get dbxdoc set correct.
  131.     ' https://www.theswamp.org/index.php?topic=15028.0
  132.     ' Set dbxdoc = New AxDbDocument
  133.    
  134.     Select Case acadVerNum ' Left(ThisDrawing.GetVariable("ACADVER"), 2)
  135.     Case Is = "22"
  136.         Set dbxdoc = AcadApplication.GetInterfaceObject("ObjectDBX.AxDbDocument.22")
  137.     Case Is = "21"
  138.         Set dbxdoc = AcadApplication.GetInterfaceObject("ObjectDBX.AxDbDocument.21")
  139.     Case Else
  140.         ' Unable to find ACAD version
  141.          MsgBox "Unable to extract AutoCAD version!!!", vbCritical, "ERROR"
  142.         GoTo ErrHandler
  143.         'Set dbxdoc = AcadApplication.GetInterfaceObject("ObjectDBX.AxDbDocument.19")
  144.     End Select
  145.     If strExt = "dwt" Then
  146.         Err.Raise vbObjectError + 1001
  147.     End If
  148.     dbxdoc.Open FilePath
  149.     Set oDoc = dbxdoc
  150.    
  151.     Set dbxdoc = Nothing
  152.     Set fso = Nothing
  153.     Exit Sub
  154.    
  155. ErrHandler:
  156.     Select Case Err.Number
  157.     Case Is = -2147221005
  158.         'register dll
  159.         'STOP
  160.         strObjDbxPath = "regsvr32 " & AcadApplication.Path & "\axdb22.dll"
  161.         Shell (strObjDbxPath)
  162.         Err.Clear
  163.         GoTo SetDbxDoc
  164.     Case Is = vbObjectError + 1001, 70, -2147467259
  165.         'vbObjectError + 1001 = filetype is *.dwt
  166.         '70 = file access permission denied
  167.         '-2147467259 = Method 'Open' of object 'IAxDbDocument' failed
  168.         'Plan for occasion where the file is already open by
  169.         'another user. This is necessary because ObjectDBX
  170.         'does not support a ReadOnly argument for the Open method.
  171.         'The calling sub can check the ReadOnly property. If True, then
  172.         'then you can clean up by deleting the temp file when done. ObjectDBX also
  173.         'does not open dwt files. If the Ext property equals "dwt", you may need to clean up.
  174.         'I try do do it for you at CleanUp: and Class.Terminate.
  175.         'If there are no errors, the TempPath property will = "", vbNullString.
  176.         
  177.         ' TODO - 2018-01-22 - ADD TEST TO SEE IF ACAD IS OPEN!
  178.                
  179.         ' 4/4/18 = ACAD does not work with AutoCAD 2018
  180.         ' strTempName = ACAD.Application.Preferences.Files.TempFilePath & fso.GetBaseName(FilePath) & ".dwg"
  181.          strTempName = autocad.Application.Preferences.Files.TempFilePath & fso.GetBaseName(FilePath) & ".dwg"
  182.         
  183.         fso.CopyFile FilePath, strTempName, True 'overwrite without prompting
  184.         SetAttr strTempName, vbNormal
  185.         FilePath = strTempName
  186.         dbxdoc.Open FilePath
  187.         strTempPath = strTempName
  188.         bReadOnly = True
  189.         Set oDoc = dbxdoc
  190.         Set dbxdoc = Nothing
  191.         Set fso = Nothing
  192.     Case Else
  193.         MsgBox Err.Number & ": " & Err.Description, vbCritical, "ObjDbx.xOpen"
  194.     End Select
  195. End Sub
  196. Public Sub xSave()
  197.     'Save the currently open oDoc
  198.     'Replaces the AxDbDocument's Save method
  199.     On Error GoTo ErrHandler
  200.    
  201.     If oDoc Is Nothing Then
  202.         Err.Raise vbObjectError + 1002, , "Method failed. There is no document to save."
  203.     End If
  204.    
  205.     'Use the SaveAs method, since Save "doesn't work"
  206.     oDoc.SaveAs strOrigPath
  207.     Exit Sub
  208.    
  209. ErrHandler:
  210.     Select Case Err.Number
  211.     Case Is = vbObjectError + 1002
  212.         MsgBox Err.Number & ": " & Err.Description, vbCritical, "ObjDbx.xSave"
  213.     Case Else
  214.         MsgBox Err.Number & ": " & Err.Description, vbCritical, "ObjDbx.xSave"
  215.     End Select
  216.    
  217. End Sub
  218. Public Sub xSaveAs(FilePath As String, Optional Prefix As String, Optional Suffix As String)
  219.     'SaveAs the currently open oDoc
  220.     'For flexibility only, since oDoc inherits the AxDbDocument's methods.
  221.     'Also, I added some optional arguments.
  222.     On Error GoTo ErrHandler
  223.    
  224.     Dim fso As Scripting.FileSystemObject
  225.     Dim strFile As String
  226.    
  227.     If oDoc Is Nothing Then
  228.         Err.Raise vbObjectError + 1003, , "Method failed. There is no document to save."
  229.     End If
  230.     Set fso = CreateObject("Scripting.FileSystemObject")
  231.     strFile = fso.GetParentFolderName(FilePath) & "" & Prefix & _
  232.               fso.GetBaseName(FilePath) & Suffix & "." & fso.GetExtensionName(FilePath)
  233.     oDoc.SaveAs strFile
  234.     Set fso = Nothing
  235.     Exit Sub
  236.    
  237. ErrHandler:
  238.     Select Case Err.Number
  239.     Case Is = vbObjectError + 1002
  240.         MsgBox Err.Number & ": " & Err.Description, vbCritical, "ObjDbx.xSaveAs"
  241.     Case Else
  242.         MsgBox Err.Number & ": " & Err.Description, vbCritical, "ObjDbx.xSaveAs"
  243.     End Select
  244.    
  245. End Sub
  246. Private Sub Class_Terminate()
  247.     Dim fso As Scripting.FileSystemObject
  248.    
  249.     'Make sure that the doc object doesn't
  250.     'have a hold on the temp file.
  251.     'Close the doc without saving changes.
  252.     Set oDoc = Nothing
  253.     'Cleanup temp file if it exists.
  254.     Set fso = CreateObject("Scripting.FileSystemObject")
  255.     If fso.FileExists(strTempPath) Then fso.DeleteFile (strTempPath)
  256.     Set fso = Nothing
  257. End Sub

回复

使用道具 举报

4

主题

219

帖子

4

银币

后起之秀

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

铜币
238
发表于 2018-4-4 09:56:54 | 显示全部楼层
我相信你'我需要在新安装中加载VBA enabler:https://knowledge.autodesk.com/support/autocad/downloads/caas/downloads/content/download-the-microsoft-visual-basic-for-applications-module-vba.html
回复

使用道具 举报

11

主题

40

帖子

3

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
83
发表于 2018-4-4 10:03:36 | 显示全部楼层
好的,它对我来说适用于以下代码(AutoCAD2018,实际上是AutoCAD Civil3D 2018):
  1. Option Explicit
  2. Public Sub DbxTest()
  3.     Dim fileName As String
  4.     fileName = "D:\Temp\SideDbTest.dwg"
  5.    
  6.     Dim doc As AXDBLib.AxDbDocument
  7.    
  8.     Dim sp(0 To 2) As Double
  9.     Dim ep(0 To 2) As Double
  10.    
  11.     sp(0) = 0#: sp(1) = 0#: sp(2) = 0#
  12.     ep(0) = 100#: ep(1) = 100#: ep(2) = 0#
  13.    
  14.     Set doc = ThisDrawing.Application.GetInterfaceObject("ObjectDBX.AxDbDocument.22")
  15.     doc.Open fileName
  16.    
  17.     On Error Resume Next
  18.    
  19.     doc.PaperSpace.AddLine sp, ep
  20.    
  21.     If Err.Number  0 Then
  22.         MsgBox "Updated failed: " & Err.Description
  23.     Else
  24.         MsgBox "Updated"
  25.     End If
  26.    
  27.     doc.SaveAs fileName
  28.    
  29. End Sub
然而,我'd避免使用ObjectDBX将实体添加到图纸空间,因为如果图形具有多个布局(很可能),则在打开AxDbDocument时,您不确定哪个布局是图纸空间。通常,在AutoCAD中打开并保存图形时,它是最后一个激活的布局,但在使用ObjectDBX处理图形时,您无法知道这一点
因此,如果使用ObjectDBX文档,而不是使用AxDbDocument。PaperSpace,I'd使用AxDbDocument。布局([特定布局索引])。块来添加实体,以便始终确保将实体添加到特定布局
不过,我不知道为什么会出现这种错误,因为每个图形应该至少有一个布局。您可以试试看图形是否至少有一个布局:如果是doc.Layouts。计数(&gt);然后为0 文档.纸张空间.添加行…
&nbsp'' 或&nbsp'' 文档布局(0)。块。添加行 Msgbox“;图纸中没有布局。无法将行添加到图纸空间&引用
如果结束
回复

使用道具 举报

0

主题

3

帖子

3

银币

初来乍到

Rank: 1

铜币
3
发表于 2018-4-4 10:16:51 | 显示全部楼层
@Deegeeses_V.2.0 I'我已经安装了VBA enabler…我想…让我检查一下。是的,它'已安装。
@n.yuan 似乎我的类代码工作不正常,最好保持简单&nbsp
谢谢大家的反馈和建议
回复

使用道具 举报

11

主题

40

帖子

3

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
83
发表于 2018-9-11 05:24:37 | 显示全部楼层
我很高兴成为这里的一员。以便我完全访问数据
回复

使用道具 举报

发表回复

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

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

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

GMT+8, 2025-3-13 03:38 , Processed in 0.533645 second(s), 62 queries .

© 2020-2025 乐筑天下

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