CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

每个文档数据 (.NET)

2023-1-1 17:47| 发布者: admin| 查看: 681| 评论: 0|来自: AutoCAD

摘要: 全局数据可以临时存储在文档级别的内存中。

全局数据可以临时存储在文档级别的内存中。

当 AutoCAD 程序加载托管应用程序时,它会查询应用程序的程序集以获取一个或多个自定义属性。如果找到此属性的实例,则会为 AutoCAD 图形环境中打开的每个文档创建每个属性的关联类型的实例。然后,将为此后打开的任何文档创建类型的新实例。PerDocumentClass

与 aattribute 关联的类型必须提供采用文档参数的构造函数或采用文档参数并返回该类型实例的方法。如果该方法存在,则将使用它,否则将使用构造函数。为其创建类型实例的文档将作为 document 参数传递,以便类型实例知道它与哪个文档相关联。PerDocumentClassPublicPublic Static CreateCreate

以下过程介绍如何使用属性。PerDocumentClass

  1. 在程序集上下文中,为要为每个文档实例化的每个类声明 a属性。PerDocumentClass
  2. 将类的类型传递给属性。PerDocumentClass
注意:必须在程序集上下文中声明此属性。

VB.NET

...
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.Runtime

<Assembly: PerDocumentClass(GetType(PerDocData))> 

' Defines a global class for constant values
Public NotInheritable Class CSPerDocConsts
    Private Sub New()
    End Sub

    Public Const dbLocVarName As String = "dbLoc"
End Class

' Defines the main class
Public Class PerDocCommands
    ' Creates a command that returns the current value of the per document data
    <CommandMethod("DatabaseLocation")> _
    Public Sub DatabaseLocation()
        Dim acDoc As Document = Application.DocumentManager.MdiActiveDocument
        Dim pDData As PerDocData = DirectCast(acDoc.UserData(CSPerDocConsts.dbLocVarName), PerDocData)
        If pDData Is Nothing Then
            acDoc.Editor.WriteMessage(vbLf & "No user data assigned.")
        Else
            acDoc.Editor.WriteMessage(vbLf & pDData.DbLocation)
        End If
    End Sub
End Class

' Defines the class that wuill be used to initialize the per document data
Public Class PerDocData
    ' Define the internal/member variables of the class
    Private _stDbLoc As String

    ' Expose a public property that returns the value of the internal variable
    Public ReadOnly Property DbLocation() As String
        Get
            Return _stDbLoc
        End Get
    End Property

    ' Public constructor that requires a Document object
    ' Created by AutoCAD when the application is loaded
    Public Sub New(acDoc As Document)
        _stDbLoc = ""
        acDoc.UserData.Add(CSPerDocConsts.dbLocVarName, Me)
    End Sub

    ' Create method: Required constructor for the class
    Public Shared Function Create(acDoc As Document) As PerDocData
        Dim pDData As New PerDocData(acDoc)
        pDData._stDbLoc = "C:\ABCApp\Data\" & acDoc.Name.Remove(acDoc.Name.Length - 4) & ".db"

        Return pDData
    End Function
End Class

C#

...
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;

[assembly: PerDocumentClass(typeof(CSPerDoc.PerDocData))]

namespace CSPerDoc
{
    // Defines a global class for constant values
    public static class CSPerDocConsts
    {
        public const string dbLocVarName = "dbLoc";
    }

    // Defines the main class
    public class PerDocCommands
    {
        // Creates a command that returns the current value of the per document data
        [CommandMethod("DatabaseLocation")]
        public void DatabaseLocation()
        {
            Document acDoc = Application.DocumentManager.MdiActiveDocument;
            PerDocData pDData = (PerDocData) acDoc.UserData[CSPerDocConsts.dbLocVarName];
            if (pDData == null) {
                acDoc.Editor.WriteMessage("\nNo user data assigned.");
            } else {
                acDoc.Editor.WriteMessage("\n" + pDData.DbLocation);
            }
        }
    }

    // Defines the class that wuill be used to initialize the per document data
    public class PerDocData
    {
        // Define the internal/member variables of the class
        private string _stDbLoc;

        // Expose a public property that returns the value of the internal variable
        public string DbLocation
        {
            get { return _stDbLoc; }
        }

        // Public constructor that requires a Document object
        // Created by AutoCAD when the application is loaded
        public PerDocData(Document acDoc)
        {
            _stDbLoc = @"";
            acDoc.UserData.Add(CSPerDocConsts.dbLocVarName, this);
        }

        // Create method: Required constructor for the class
        public static PerDocData Create(Document acDoc)
        {
            PerDocData pDData = new PerDocData(acDoc);
            pDData._stDbLoc = @"C:\\ABCApp\\ProjectData\\" + acDoc.Name.Remove(acDoc.Name.Length - 4) + ".db";

            return pDData;
        }
    }
}

路过

雷人

握手

鲜花

鸡蛋

最新评论

AutoCAD二次开发.NET源码资料

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

GMT+8, 2024-5-6 21:10

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部