フォローする

.NET - 拡張データの追加と削除

更新 : 2017-01-17 06:37:06 UTC

拡張データを使用する事で、図面内のオブジェクトに独自の情報を持たせることができます。

添付されているサンプルプロジェクトでは、 .NET API を使用して、
選択したオブジェクトに対しての、拡張データの追加や削除を行っています。

VB.NET

'拡張データを追加する
<CommandMethod("ADDXDATE")>
Public Sub AddXDataSample()

    Dim icDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim icDb As Database = icDoc.Database
    Dim icEd As Editor = icDoc.Editor

    Dim icRes As PromptEntityResult = icEd.GetEntity(vbCrLf + "オブジェクトを選択")
    If icRes.Status <> PromptStatus.OK Then Return

    Dim AppName As String = "SampleXData"
    Dim XDataValue As Object = "SampleXDataValue"

    Using icTrans As Transaction = icDb.TransactionManager.StartTransaction
        Using icEnt As Entity = icTrans.GetObject(icRes.ObjectId, OpenMode.ForRead)

            Dim icRegAppTable As RegAppTable = icTrans.GetObject(icDb.RegAppTableId, OpenMode.ForRead)

            If Not icRegAppTable.Has(AppName) Then
                Using icRegAppRecord As RegAppTableRecord = New RegAppTableRecord()

                    icRegAppRecord.Name = AppName

                    icRegAppTable.UpgradeOpen()
                    icRegAppTable.Add(icRegAppRecord)
                    icTrans.AddNewlyCreatedDBObject(icRegAppRecord, True)

                End Using
            End If

            Using icResBuf As ResultBuffer = New ResultBuffer()

                icResBuf.Add(New TypedValue(DxfCode.ExtendedDataRegAppName, AppName))
                icResBuf.Add(New TypedValue(DxfCode.ExtendedDataAsciiString, XDataValue))

                icEnt.UpgradeOpen()
                icEnt.XData = icResBuf
                icEnt.DowngradeOpen()

            End Using
            icTrans.Commit()
        End Using
    End Using
End Sub

'拡張データを削除する
<CommandMethod("REMOVEXDATE")>
Public Sub RemoveXDataSample()

    Dim icDoc As Document = Application.DocumentManager.MdiActiveDocument
    Dim icDb As Database = icDoc.Database
    Dim icEd As Editor = icDoc.Editor

    Dim icRes As PromptEntityResult = icEd.GetEntity(vbCrLf + "オブジェクトを選択")
    If icRes.Status <> PromptStatus.OK Then Return

    Dim AppName As String = "SampleXData"

    Using icTrans As Transaction = icDb.TransactionManager.StartTransaction
        Using icEnt As Entity = icTrans.GetObject(icRes.ObjectId, OpenMode.ForRead)
            Using icResBuf As ResultBuffer = New ResultBuffer()

                icResBuf.Add(New TypedValue(DxfCode.ExtendedDataRegAppName, AppName))

                icEnt.UpgradeOpen()
                icEnt.XData = icResBuf
                icEnt.DowngradeOpen()

                icTrans.Commit()
            End Using
        End Using
    End Using
End Sub

C#

//拡張データを追加する
[CommandMethod("ADDXDATE")]
public void AddXDataSample()
{
    Document icDoc = Application.DocumentManager.MdiActiveDocument;
    Database icDb = icDoc.Database;
    Editor icEd = icDoc.Editor;

    PromptEntityResult icRes = icEd.GetEntity("\nオブジェクトを選択");
    if (icRes.Status != PromptStatus.OK) return;

    string AppName = "SampleXData";
    object XDataValue = "SampleXDataValue";

    using (Transaction icTrans = icDb.TransactionManager.StartTransaction())
    using (Entity icEnt = icTrans.GetObject(icRes.ObjectId, OpenMode.ForRead) as Entity)
    {
        RegAppTable icRegAppTable = icTrans.GetObject(icDb.RegAppTableId, OpenMode.ForRead) as RegAppTable;

        if (!icRegAppTable.Has(AppName))
        {
            using (RegAppTableRecord icRegAppRecord = new RegAppTableRecord())
            {
                icRegAppRecord.Name = AppName;

                icRegAppTable.UpgradeOpen();
                icRegAppTable.Add(icRegAppRecord);
                icTrans.AddNewlyCreatedDBObject(icRegAppRecord, true);
            }
        }

        using (ResultBuffer icResBuf = new ResultBuffer())
        {
            icResBuf.Add(new TypedValue((Int32)DxfCode.ExtendedDataRegAppName, AppName));
            icResBuf.Add(new TypedValue((Int32)DxfCode.ExtendedDataAsciiString, XDataValue));

            icEnt.UpgradeOpen();
            icEnt.XData = icResBuf;
            icEnt.DowngradeOpen();
        }
        icTrans.Commit();
    }
}        

//拡張データを削除する
[CommandMethod("REMOVEXDATE")]
public void RemoveXDataSample()
{
    Document icDoc = Application.DocumentManager.MdiActiveDocument;
    Database icDb = icDoc.Database;
    Editor icEd = icDoc.Editor;

    PromptEntityResult icRes = icEd.GetEntity("\nオブジェクトを選択");
    if (icRes.Status != PromptStatus.OK) return;

    string AppName = "SampleXData";

    using (Transaction icTrans = icDb.TransactionManager.StartTransaction())
    using (Entity icEnt = icTrans.GetObject(icRes.ObjectId, OpenMode.ForRead) as Entity)
    using (ResultBuffer icResBuf = new ResultBuffer())
    {
        icResBuf.Add(new TypedValue((Int32)DxfCode.ExtendedDataRegAppName, AppName));

        icEnt.UpgradeOpen();
        icEnt.XData = icResBuf;
        icEnt.DowngradeOpen();

        icTrans.Commit();
    }
}
この記事は役に立ちましたか?
0人中0人がこの記事が役に立ったと言っています
他にご質問がございましたら、リクエストを送信してください

コメント