CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

将 VBA 或 VB 中的错误处理程序与 VB.NET (.NET) 进行比较

2023-1-1 08:50| 发布者: admin| 查看: 317| 评论: 0|来自: AutoCAD

VBA 或 VB 中的错误处理是使用语句完成的。虽然语句可以与 VB.NET 一起使用而没有任何问题,但建议改用语句。语句比 theand语句更灵活。On ErrorOn ErrorTryTryOn Error Resume NextOn Error GoTo Label

使用 ofand语句 可以使用语句重写。下面显示了如何使用 重写语句。On Error Resume NextOn Error GoTo LabelTry-CatchOn Error GoTo LabelTry-Catch

出错时 - VBA

Sub ColorEntities()
    On Error GoTo MyErrorHandler
 
    Dim entry As Object
    For Each entry In ThisDrawing.ModelSpace
        entry.color = acRed
    Next entry
 
    ' Important! Exit the subroutine before the error handler
  Exit Sub
 
MyErrorHandler:
    MsgBox entry.EntityName + " is on a locked layer." + _
                              " The handle is: " + entry.Handle
  Resume Next
End Sub

尝试捕获 - VB.NET

<CommandMethod("ColorEntities")> _
Public Sub ColorEntities()
  '' Get the current document and database, and start a transaction
  Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
  Dim acCurDb As Database = acDoc.Database
 
  Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
      '' Open the Block table record for read
      Dim acBlkTbl As BlockTable
      acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, _
                                   OpenMode.ForRead)
 
      '' Open the Block table record Model space for read
      Dim acBlkTblRec As BlockTableRecord
      acBlkTblRec = acTrans.GetObject(acBlkTbl(BlockTableRecord.ModelSpace), _
                                      OpenMode.ForRead)
 
      Dim acObjId As ObjectId
 
      '' Step through each object in Model space
      For Each acObjId In acBlkTblRec
          Try
              Dim acEnt As Entity
              acEnt = acTrans.GetObject(acObjId, _
                                        OpenMode.ForWrite)
 
              acEnt.ColorIndex = 1
          Catch
              Application.ShowAlertDialog(acObjId.ObjectClass.DxfName & _
                                          " is on a locked layer." & _
                                          " The handle is: " & acObjId.Handle.ToString())
          End Try
      Next
 
      acTrans.Commit()
  End Using
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-5-19 13:53

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部