CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

使用命名视图 (.NET)

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

您可以命名和保存要重用的视图。当您不再需要该视图时,可以将其删除。

命名视图存储在视图表中,视图表是图形数据库中的命名符号表之一。使用将新视图添加到视图表的方法创建命名视图。将新的命名视图添加到“视图”表时,将创建默认模型空间视图。Add

您可以在创建视图时为其命名。视图名称最多可包含 255 个字符,包含字母、数字和特殊字符美元符号 ($)、连字符 (-) 和下划线 (_)。

只需使用要删除的对象的方法,即可从 View 表中删除命名视图。EraseViewTableRecord

添加命名视图并将其设置为当前视图

下面的示例向图形添加一个命名视图,并将其设置为当前视图。

VB.NET

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

    '' Start a transaction
    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

        '' Open the View table for read
        Dim acViewTbl As ViewTable
        acViewTbl = acTrans.GetObject(acCurDb.ViewTableId, OpenMode.ForRead)

        '' Check to see if the named view 'View1' exists
        If (acViewTbl.Has("View1") = False) Then
            '' Open the View Table for write
            acTrans.GetObject(acCurDb.ViewTableId, OpenMode.ForWrite)

            '' Create a new View table record and name the view "View1"
            Using acViewTblRec As ViewTableRecord = New ViewTableRecord()
                acViewTblRec.Name = "View1"

                '' Add the new View table record to the View table and the transaction
                acViewTbl.Add(acViewTblRec)
                acTrans.AddNewlyCreatedDBObject(acViewTblRec, True)

                '' Set 'View1' current
                acDoc.Editor.SetCurrentView(acViewTblRec)
            End Using

            '' Commit the changes
            acTrans.Commit()
        End If

        '' Dispose of the transaction
    End Using
End Sub

C#

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

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the View table for read
        ViewTable acViewTbl;
        acViewTbl = acTrans.GetObject(acCurDb.ViewTableId,
                                        OpenMode.ForRead) as ViewTable;

        // Check to see if the named view 'View1' exists
        if (acViewTbl.Has("View1") == false)
        {
            // Open the View table for write
            acTrans.GetObject(acCurDb.ViewTableId, OpenMode.ForWrite);

            // Create a new View table record and name the view 'View1'
            using (ViewTableRecord acViewTblRec = new ViewTableRecord())
            {
                acViewTblRec.Name = "View1";

                // Add the new View table record to the View table and the transaction
                acViewTbl.Add(acViewTblRec);
                acTrans.AddNewlyCreatedDBObject(acViewTblRec, true);

                // Set 'View1' current
                acDoc.Editor.SetCurrentView(acViewTblRec);
            }

            // Commit the changes
            acTrans.Commit();
        }

        // Dispose of the transaction
    }
}

VBA/ActiveX Code Reference

Sub CreateNamedView()
    ' Add a named view to the views collection
    Dim viewObj As AcadView
    Set viewObj = ThisDrawing.Views.Add("View1")
 
    ThisDrawing.ActiveViewport.SetView viewObj
End Sub

Erase a named view

The following example erases a named view from the drawing.

VB.NET

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

    '' Start a transaction
    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()

        '' Open the View table for read
        Dim acViewTbl As ViewTable
        acViewTbl = acTrans.GetObject(acCurDb.ViewTableId, OpenMode.ForRead)

        '' Check to see if the named view 'View1' exists
        If (acViewTbl.Has("View1") = True) Then
            '' Open the View table for write
            acTrans.GetObject(acCurDb.ViewTableId, OpenMode.ForWrite)

            '' Get the named view
            Dim acViewTblRec As ViewTableRecord
            acViewTblRec = acTrans.GetObject(acViewTbl("View1"), OpenMode.ForWrite)

            '' Remove the named view from the View table
            acViewTblRec.Erase()

            '' Commit the changes
            acTrans.Commit()
        End If

        '' Dispose of the transaction
    End Using
End Sub

C#

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

    // Start a transaction
    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Open the View table for read
        ViewTable acViewTbl;
        acViewTbl = acTrans.GetObject(acCurDb.ViewTableId,
                                        OpenMode.ForRead) as ViewTable;

        // Check to see if the named view 'View1' exists
        if (acViewTbl.Has("View1") == true)
        {
            // Open the View table for write
            acTrans.GetObject(acCurDb.ViewTableId, OpenMode.ForWrite);

            // Get the named view
            ViewTableRecord acViewTblRec;
            acViewTblRec = acTrans.GetObject(acViewTbl["View1"],
                                                OpenMode.ForWrite) as ViewTableRecord;

            // Remove the named view from the View table
            acViewTblRec.Erase();

            // Commit the changes
            acTrans.Commit();
        }

        // Dispose of the transaction
    }
}

VBA/ActiveX 代码参考

Sub EraseNamedView()
    On Error Resume Next
    Dim viewObj As AcadView
    Set viewObj = ThisDrawing.Views("View1")
 
    If Err = 0 Then
      ' Delete the view
      viewObj.Delete
    End If
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

AutoCAD二次开发.NET源码资料

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

GMT+8, 2024-5-7 06:10

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部