グリッドが距離を計測するための目に見えるガイドラインであるのに対し、スナップ モードはカーソルの動きを制限するために使用されます。グリッドおよびスナップ モードの間隔を設定するだけでなく、使用するスナップの回転と種類を調整できます。
特定の位置や角度に沿って作図する必要がある場合、スナップ角度を回転できます。スナップ角度回転の中心点は、スナップ基点です。
スナップとグリッドは AutoCAD .NET API を使用して指定された点には影響しませんが、ユーザが作図領域で指定した、GetPoint や GetEntity などのメソッドを使用して入力するように要求された場合の点には影響します。
以下の例は、スナップ基点を(1,1)、スナップ回転角度を 30 度に変更します。グリッドがオンにされ、変更が表示されるように間隔が調整されます。
Imports Autodesk.AutoCAD.ApplicationServices Imports Autodesk.AutoCAD.DatabaseServices Imports Autodesk.AutoCAD.Runtime Imports Autodesk.AutoCAD.Geometry <CommandMethod("ChangeGridAndSnap")> _ Public Sub ChangeGridAndSnap() '' 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 active viewport Dim acVportTblRec As ViewportTableRecord acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, _ OpenMode.ForWrite) '' Turn on the grid for the active viewport acVportTblRec.GridEnabled = True '' Adjust the spacing of the grid to 1, 1 acVportTblRec.GridIncrements = New Point2d(1, 1) '' Turn on the snap mode for the active viewport acVportTblRec.SnapEnabled = True '' Adjust the snap spacing to 0.5, 0.5 acVportTblRec.SnapIncrements = New Point2d(0.5, 0.5) '' Change the snap base point to 1, 1 acVportTblRec.SnapBase = New Point2d(1, 1) '' Change the snap rotation angle to 30 degrees (0.524 radians) acVportTblRec.SnapAngle = 0.524 '' Update the display of the tiled viewport acDoc.Editor.UpdateTiledViewportsFromDatabase() '' Commit the changes and dispose of the transaction acTrans.Commit() End Using End Sub
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.Geometry; [CommandMethod("ChangeGridAndSnap")] public static void ChangeGridAndSnap() { // Get the current database Document acDoc = Application.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // Start a transaction using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // Open the active viewport ViewportTableRecord acVportTblRec; acVportTblRec = acTrans.GetObject(acDoc.Editor.ActiveViewportId, OpenMode.ForWrite) as ViewportTableRecord; // Turn on the grid for the active viewport acVportTblRec.GridEnabled = true; // Adjust the spacing of the grid to 1, 1 acVportTblRec.GridIncrements = new Point2d(1, 1); // Turn on the snap mode for the active viewport acVportTblRec.SnapEnabled = true; // Adjust the snap spacing to 0.5, 0.5 acVportTblRec.SnapIncrements = new Point2d(0.5, 0.5); // Change the snap base point to 1, 1 acVportTblRec.SnapBase = new Point2d(1, 1); // Change the snap rotation angle to 30 degrees (0.524 radians) acVportTblRec.SnapAngle = 0.524; // Update the display of the tiled viewport acDoc.Editor.UpdateTiledViewportsFromDatabase(); // Commit the changes and dispose of the transaction acTrans.Commit(); } }
Sub ChangeGridAndSnap() ' Turn on the grid for the active viewport ThisDrawing.ActiveViewport.GridOn = True ' Adjust the spacing of the grid to 1, 1 ThisDrawing.ActiveViewport.SetGridSpacing 1, 1 ' Turn on the snap mode for the active viewport ThisDrawing.ActiveViewport.SnapOn = True ' Adjust the snap spacing to 0.5, 0.5 ThisDrawing.ActiveViewport.SetSnapSpacing 0.5, 0.5 ' Change the snap base point to 1, 1 Dim newBasePoint(0 To 1) As Double newBasePoint(0) = 1: newBasePoint(1) = 1 ThisDrawing.ActiveViewport.SnapBasePoint = newBasePoint ' Change the snap rotation angle to 30 degrees (0.524 radians) Dim rotationAngle As Double rotationAngle = 0.524 ThisDrawing.ActiveViewport.SnapRotationAngle = rotationAngle ' Reset the viewport ThisDrawing.ActiveViewport = ThisDrawing.ActiveViewport End Sub