CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

在没有事务管理器 (.NET) 的情况下打开和关闭对象

2023-1-1 15:12| 发布者: admin| 查看: 449| 评论: 0|来自: AutoCAD

事务使打开和处理多个对象变得更加容易,但它们并不是打开和编辑对象的唯一方法。除了使用事务之外,您还可以使用 and 方法打开和关闭对象。您仍然需要获取对象 ID 才能使用该方法。与用于事务的方法一样,您需要指定打开模式,返回值是一个对象。OpenCloseOpenGetObject

如果在使用 methodu 打开对象后对其进行了更改,则可以使用该方法回滚自打开以来所做的所有更改。必须在要回滚的每个对象上调用。在关闭对象后,还必须使用 theMethod 正确处置对象,或者可以使用语句关闭和释放对象。OpenCancelCancelDisposeUsing

注意:对象必须与打开和关闭操作配对。如果使用不带语句的方法,则必须在打开的对象上调用任一 theormethod。未能关闭对象将导致读取访问冲突,并导致 AutoCAD 变得不稳定。OpenUsingCloseCancel

如果需要处理单个对象,与使用事务管理器相比,使用 and 方法可以减少可能必须编写的代码行数。但是,建议使用事务来打开和关闭对象。OpenClose

谨慎:使用事务时不应直接使用 与 方法,因为事务管理器可能无法正确打开或关闭对象,这可能会导致 AutoCAD 变得不稳定。相反,使用方法创建一个包装 theand 方法的对象。OpenCloseStartOpenCloseTransationOpenCloseTransactionOpenClose

查询对象(手动打开和关闭对象)

此示例演示如何在不使用事务和方法的情况下手动打开和关闭对象。GetObject

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("OpenCloseObjectId")> _
Public Sub OpenCloseObjectId()
    ' Get the current document and database
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database

    ' Open the Block table for read
    Dim acBlkTbl As BlockTable = Nothing

    Try
        acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead)

        ' Open the Block table record Model space for read
        Dim acBlkTblRec As BlockTableRecord = Nothing

        Try
            acBlkTblRec = acBlkTbl(BlockTableRecord.ModelSpace).Open(OpenMode.ForRead)

            ' Step through the Block table record
            For Each acObjId As ObjectId In acBlkTblRec
                acDoc.Editor.WriteMessage(vbLf & "DXF name: " & acObjId.ObjectClass().DxfName)
                acDoc.Editor.WriteMessage(vbLf & "ObjectID: " & acObjId.ToString())
                acDoc.Editor.WriteMessage(vbLf & "Handle: " & acObjId.Handle.ToString())
                acDoc.Editor.WriteMessage(vbLf)
            Next
        Catch es As Autodesk.AutoCAD.Runtime.Exception
            MsgBox(es.Message)
        Finally
            ' Close the Block table record
            If Not acBlkTbl.ObjectId.IsNull Then
                acBlkTblRec.Close()
                acBlkTblRec.Dispose()
            End If
        End Try

    Catch es As Autodesk.AutoCAD.Runtime.Exception
        MsgBox(es.Message)
    Finally
        ' Close the Block table
        If Not acBlkTbl.ObjectId.IsNull Then
            acBlkTbl.Close()
            acBlkTbl.Dispose()
        End If
    End Try
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("OpenCloseObjectId")]
public static void OpenCloseObjectId()
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Open the Block table for read
    BlockTable acBlkTbl = null;

    try
    {
        acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead) as BlockTable;

        // Open the Block table record Model space for read
        BlockTableRecord acBlkTblRec = null;

        try
        {
            acBlkTblRec = acBlkTbl[BlockTableRecord.ModelSpace].Open(OpenMode.ForRead) as BlockTableRecord;

            // Step through the Block table record
            foreach (ObjectId acObjId in acBlkTblRec)
            {
                acDoc.Editor.WriteMessage("\nDXF name: " + acObjId.ObjectClass.DxfName);
                acDoc.Editor.WriteMessage("\nObjectID: " + acObjId.ToString());
                acDoc.Editor.WriteMessage("\nHandle: " + acObjId.Handle.ToString());
                acDoc.Editor.WriteMessage("\n");
            }
        }
        catch (Autodesk.AutoCAD.Runtime.Exception es)
        {
            System.Windows.Forms.MessageBox.Show(es.Message);
        }
        finally
        {
            // Close the Block table
            if (!acBlkTblRec.ObjectId.IsNull)
            {
                // Close the Block table record
                acBlkTblRec.Close();
                acBlkTblRec.Dispose();
            }
        }
    }
    catch (Autodesk.AutoCAD.Runtime.Exception es)
    {
        System.Windows.Forms.MessageBox.Show(es.Message);
    }
    finally
    {
        // Close the Block table
        if (!acBlkTbl.ObjectId.IsNull)
        {
            acBlkTbl.Close();
            acBlkTbl.Dispose();
        }
    }
}

Query objects (Using statement)

The following example demonstrates how to open and close objects with the Using statement instead of manually closing and disposing of objects.

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
 
<CommandMethod("OpenCloseObjectIdWithUsing")> _
Public Sub OpenCloseObjectIdWithUsing()
    ' Get the current document and database
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database

    ' Open the Block table for read
    Using acBlkTbl As BlockTable = acCurDb.BlockTableId.Open(OpenMode.ForRead)

        ' Open the Block table record Model space for read
        Using acBlkTblRec As BlockTableRecord = acBlkTbl(BlockTableRecord.ModelSpace).Open(OpenMode.ForRead)

            ' Step through the Block table record
            For Each acObjId As ObjectId In acBlkTblRec
                acDoc.Editor.WriteMessage(vbLf & "DXF name: " & acObjId.ObjectClass().DxfName)
                acDoc.Editor.WriteMessage(vbLf & "ObjectID: " & acObjId.ToString())
                acDoc.Editor.WriteMessage(vbLf & "Handle: " & acObjId.Handle.ToString())
                acDoc.Editor.WriteMessage(vbLf)
            Next

            ' Close the Block table record
        End Using

        ' Close the Block table
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
 
[CommandMethod("OpenCloseObjectIdWithUsing")]
public static void OpenCloseObjectIdWithUsing()
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Open the Block table for read
    using (BlockTable acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead) as BlockTable)
    {
        // Open the Block table record Model space for read
        using (BlockTableRecord acBlkTblRec = acBlkTbl[BlockTableRecord.ModelSpace].Open(OpenMode.ForRead)
                                                as BlockTableRecord)
        {
            // Step through the Block table record
            foreach (ObjectId acObjId in acBlkTblRec)
            {
                acDoc.Editor.WriteMessage("\nDXF name: " + acObjId.ObjectClass.DxfName);
                acDoc.Editor.WriteMessage("\nObjectID: " + acObjId.ToString());
                acDoc.Editor.WriteMessage("\nHandle: " + acObjId.Handle.ToString());
                acDoc.Editor.WriteMessage("\n");
            }

        // Close the Block table record
        }

        // Close the Block table
    }
}

Add a new object to the database

This example demonstrates how to create a new object and append it to Model space without using the Transaction Manager.

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
 
<CommandMethod("AddNewCircleOpenClose")> _
Public Sub AddNewCircleOpenClose()
    ' Get the current document and database
    Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim acCurDb As Database = acDoc.Database

    ' Open the Block table for read
    Using acBlkTbl As BlockTable = = acCurDb.BlockTableId.Open(OpenMode.ForRead)

        ' Open the Block table record Model space for write
        Using acBlkTblRec As BlockTableRecord = acBlkTbl(BlockTableRecord.ModelSpace).Open(OpenMode.ForWrite)

            ' Create a circle with a radius of 3 at 5,5
            Using acCirc As Circle = New Circle()
                acCirc.Center = New Point3d(5, 5, 0)
                acCirc.Radius = 3

                ' Add the new object to Model space and the transaction
                acBlkTblRec.AppendEntity(acCirc)

                ' Close the circle object
            End Using

            ' Close the Block table record
        End Using

        ' Close the Block table
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
 
[CommandMethod("AddNewCircleOpenClose")]
public static void AddNewCircleOpenClose()
{
    // Get the current document and database
    Document acDoc = Application.DocumentManager.MdiActiveDocument;
    Database acCurDb = acDoc.Database;

    // Open the Block table for read
    using (BlockTable acBlkTbl = acCurDb.BlockTableId.Open(OpenMode.ForRead) as BlockTable)
    {
        // Open the Block table record Model space for write
        using (BlockTableRecord acBlkTblRec = acBlkTbl[BlockTableRecord.ModelSpace].Open(OpenMode.ForWrite)
                                                as BlockTableRecord)
        {
            // Create a circle with a radius of 3 at 5,5
            using (Circle acCirc = new Circle())
            {
                acCirc.Center = new Point3d(5, 5, 0);
                acCirc.Radius = 3;

                // Add the new object to Model space and the transaction
                acBlkTblRec.AppendEntity(acCirc);

                // Close and dispose the circle object
            }

            // Close the Block table record
        }

        // Close the Block table
    }
}

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

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

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部