外部参照をアタッチすると、1 つの図面(参照ファイルまたは外部参照)が現在の図面にリンクされます。図面が外部参照を参照すると、AutoCAD では、ブロック定義とブロックの内容が現在の図面に保存される通常のブロックの場合と違って、外部参照定義のみを図面にアタッチします。AutoCAD は参照図面を読んで現在の図面に何を表示するかを決定します。参照ファイルが見つからなかったり壊れている場合は、そのデータは現在の図面に表示されません。図面を開くたびに、AutoCAD は参照ファイルからすべてのグラフィカル オブジェクトおよび非グラフィカル オブジェクト(画層、線種、文字スタイルなど)をロードします。VISRETAIN システム変数がオンの場合は、AutoCAD は、現在の図面内の更新されたすべての外部参照に従属する画層情報を更新します。
外部参照のコピーを必要な数だけアタッチでき、それぞれに異なる位置、尺度、回転角度を割り当てることができます。外部参照に定義されている従属画層と線種プロパティをコントロールすることもできます。
外部参照をアタッチするには、AttachXref メソッドを使用します。このメソッドは、参照される図面のパスとファイル名、および現在の図面内で使用される外部参照の名前を入力する必要があります。AttachXref メソッドは、新しく作成されたオブジェクトの ObjectId を返します。返される ObjectId オブジェクトは、新しい BlockReference オブジェクトを作成し、図面内での配置を定義するために使用されます。BlockReference オブジェクトが作成された後、回転角度と尺度を調整できます。
外部参照のアタッチの詳細については、製品のヘルプ システムで「概要 - 参照図面(外部参照)をアタッチ、アタッチ解除する」を参照してください。
次の例では、外部参照の追加前および追加後の現在の図面内のすべてのブロックを示しています。この例では、Sample フォルダにある Exterior Elevations.dwg ファイルを使用します。このファイルが存在しない場合や、別のフォルダにある場合は、有効なパスおよびファイル名を指定してください。
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
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
}
}
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