CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

ObjectARX 开发指南

附加外部参照 (.NET)

2023-1-1 10:13| 发布者: admin| 查看: 1001| 评论: 0|来自: AutoCAD

摘要:

附着外部参照会将一个图形(参照文件或外部参照)链接到当前图形。当图形参照外部参照时,AutoCAD 仅将外部参照定义附着到图形,这与常规块不同,常规块的定义和块的内容与当前图形一起存储。AutoCAD 读取参照图形以确定要在当前图形中显示的内容。如果参照文件丢失或损坏,则其数据不会显示在当前图形中。每次打开图形时,AutoCAD 都会从参照文件中加载所有图形和非图形(如图层、线型和文本样式)对象。如果 VISRETAIN 系统变量处于打开状态,AutoCAD 会将任何更新的外部参照相关图层信息存储在当前图形中。

您可以根据需要附着任意数量的外部参照副本,并且每个副本可以具有不同的位置、比例和旋转。还可以控制外部参照中定义的从属图层和线型特性。

要附着外部参照,请使用该方法。此方法要求您输入要参照的图形的路径和文件名,以及当前图形中使用的外部参照的名称。该方法返回新创建的对象。返回用于创建新对象并定义其在图形中的位置。创建对象后,您可以调整其旋转和缩放。AttachXrefAttachXrefObjectIdObjectIdBlockReferenceBlockReference

有关附着外部参照的详细信息,请参见产品帮助系统中的“关于附着和分离参照图形(外部参照)”。

将外部参照附着到图形

本示例显示添加外部参照之前和之后当前绘图中的所有块。此示例使用在示例目录中找到的外部立面.dwg文件。如果此文件不可用,或者它位于其他目录中,请插入有效的路径和文件名。

VB.NET

Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry

<CommandMethod("AttachingExternalReference")> _
Public Sub AttachingExternalReference()
    ' Get the current database and start a transaction
    Dim acCurDb As Autodesk.AutoCAD.DatabaseServices.Database
    acCurDb = Application.DocumentManager.MdiActiveDocument.Database

    Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
        ' Create a reference to a DWG file
        Dim PathName As String = "C:\AutoCAD\Sample\Sheet Sets\Architectural\Res\Exterior Elevations.dwg"
        Dim acXrefId As ObjectId = acCurDb.AttachXref(PathName, "Exterior Elevations")

        ' If a valid reference is created then continue
        If Not acXrefId.IsNull Then
            ' Attach the DWG reference to the current space
            Dim insPt As New Point3d(1, 1, 0)
            Using acBlkRef As New BlockReference(insPt, acXrefId)

                Dim acBlkTblRec As BlockTableRecord
                acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite)

                acBlkTblRec.AppendEntity(acBlkRef)
                acTrans.AddNewlyCreatedDBObject(acBlkRef, True)
            End Using
        End If

        ' Save the new objects to the database
        acTrans.Commit()

        ' Dispose of the transaction
    End Using
End Sub

C#

using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

[CommandMethod("AttachingExternalReference")]
public void AttachingExternalReference()
{
    // Get the current database and start a transaction
    Database acCurDb;
    acCurDb = Application.DocumentManager.MdiActiveDocument.Database;

    using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
    {
        // Create a reference to a DWG file
        string PathName = "C:\\AutoCAD\\Sample\\Sheet Sets\\Architectural\\Res\\Exterior Elevations.dwg";
        ObjectId acXrefId = acCurDb.AttachXref(PathName, "Exterior Elevations");

        // If a valid reference is created then continue
        if (!acXrefId.IsNull)
        {
            // Attach the DWG reference to the current space
            Point3d insPt = new Point3d(1, 1, 0);
            using (BlockReference acBlkRef = new BlockReference(insPt, acXrefId))
            {
                BlockTableRecord acBlkTblRec;
                acBlkTblRec = acTrans.GetObject(acCurDb.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;

                acBlkTblRec.AppendEntity(acBlkRef);
                acTrans.AddNewlyCreatedDBObject(acBlkRef, true);
            }
        }

        // Save the new objects to the database
        acTrans.Commit();

        // Dispose of the transaction
    }
}

VBA/ActiveX 代码参考

Sub AttachingExternalReference()
    Dim InsertPoint(0 To 2) As Double
    Dim insertedBlock As AcadExternalReference
    Dim PathName As String
 
    ' Define external reference to be inserted
    InsertPoint(0) = 1
    InsertPoint(1) = 1
    InsertPoint(2) = 0
    PathName = "C:\AutoCAD\Sample\Sheet Sets\Architectural\Res\Exterior Elevations.dwg"
 
    ' Add the external reference to the drawing
    Set insertedBlock = ThisDrawing.ActiveLayout.Block. _
    AttachExternalReference(PathName, "Exterior Elevations", InsertPoint, 1, 1, 1, 0, False)
End Sub

路过

雷人

握手

鲜花

鸡蛋

最新评论

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

GMT+8, 2024-5-19 15:01

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部