将AutoCAD ActiveSpace设置为模型空间 VB.NET



我试图确保我在模型空间中寻址实体,但我得到一个异常,没有提示问题是什么,因为它是我猜的 COM 对象。有谁知道我可能做错了什么?如果我去掉该行(和缩放范围线(,其余代码工作正常,因此我知道我的文档对象设置正确。

        Dim acDWG As AutoCAD.AcadDocument
        ' open the drawing
        acDWG = acApp.Documents.Open(dgvr.Cells("FullName").Value.ToString)
        ' ensure the drawing has the modelspace tab activated (doesnt work)
        acDWG.ActiveSpace = AutoCAD.AcActiveSpace.acModelSpace
        ' zoom to extents (sometimes works, sometimes not)                ' 
        acApp.ZoomExtents()
        ' build a selectionset of all blocks named 'Solid1' and then delete them all
        Dim ss As AutoCAD.AcadSelectionSet = acDWG.SelectionSets.Add("DELETE")
        Dim gpCode(1) As Int16
        Dim dataValue(1) As Object
        gpCode(0) = 0 : dataValue(0) = "Insert"
        gpCode(1) = 2 : dataValue(1) = "Solid1"
        ss.Select(AutoCAD.AcSelect.acSelectionSetAll,,, gpCode, dataValue)
        ss.Erase()
        ss.Delete()
        ss = Nothing

更新:我发现了为什么出现错误。代码正确,但问题是绘图尚未完成打开。如果我直接在Open行之后放置"等待 5 秒"的代码行,它就可以正常工作。因此,似乎我的问题是如何打开绘图 VB.Net 并等待来自COM对象的信号,表明它"已准备好继续"?(不知道怎么说(

使用 Do...LoopTry...Catch 块的组合来"等待",如下所示:

            Dim acDWG As AutoCAD.AcadDocument
            acDWG = acApp.Documents.Open(dgvr.Cells("FullName").Value.ToString)
            Dim bOpen As Boolean = False
            Do Until bOpen = True
                Try
                    acDWG.ActiveSpace = AutoCAD.AcActiveSpace.acModelSpace
                    bOpen = True
                Catch ex As Exception
                    ' ignore the error and try again until it is open
                End Try
            Loop

最新更新