Code:
using System;
using System.Collections;
using System.Collections.Generic;using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using RSNNAcadApp.ExtendedCommands;
[assembly: CommandClass(typeof(RSNNAcadApp.ExtendedCommands.EntityDragger))]
namespace RSNNAcadApp.ExtendedCommands
{
public class EntityDragger
{
private Point3d m_basePoint;
[CommandMethod("dragSS")]
public void SsDragger()
{
Editor currentEditor = Application.DocumentManager.MdiActiveDocument.Editor;
//Create a new selection set options object
PromptSelectionOptions ssOptions = new PromptSelectionOptions();
ssOptions.AllowDuplicates = true;
ssOptions.MessageForAdding = "Select objects to copy: ";
//Get a selection from the user
PromptSelectionResult ssResult = currentEditor.GetSelection(ssOptions);
if (ssResult.Status != PromptStatus.OK)
{
return;
}
//Get a base point from the user
PromptPointResult pointResult = currentEditor.GetPoint("\nSelect base point: ");
if (pointResult.Status != PromptStatus.OK)
{
return;
}
m_basePoint = pointResult.Value;
//Initiate the drag callback delegate
DragCallback dragCallbackDelegate = this.FollowCursor;
//Start the drag operation
PromptDragOptions dragOptions = new PromptDragOptions(ssResult.Value, "\nSelect destination point: ", dragCallbackDelegate);
PromptPointResult destinationPointResult = currentEditor.Drag(dragOptions);
if (destinationPointResult.Status != PromptStatus.OK)
{
return;
}
//Determine the final displacement matrix
Matrix3d transMat = Matrix3d.Displacement(m_basePoint.GetVectorTo(destinationPointResult.Value));
//transform the ss to the new location
this.TransformSelectionSet(ssResult.Value, transMat);
}
//DragCallback Delegate
public SamplerStatus FollowCursor(Point3d currentPoint, ref Matrix3d transMat)
{
//transMat = Matrix3d.Scaling(2, currentPoint);
//transMat *= Matrix3d.Displacement(m_basePoint.GetVectorTo(currentPoint));
transMat = Matrix3d.Displacement(m_basePoint.GetVectorTo(currentPoint));
return SamplerStatus.OK;
}
private void TransformSelectionSet(SelectionSet ss, Matrix3d transMat)
{
if (ss.Count == 0)
{
return;
}
Database targetDatabase = ss[0].ObjectId.Database;
using (Transaction trans = targetDatabase.TransactionManager.StartTransaction())
{
BlockTableRecord currentSpace = (BlockTableRecord)trans.GetObject(targetDatabase.CurrentSpaceId, OpenMode.ForWrite);
foreach (SelectedObject selectedObj in ss)
{
Entity selectedEntity = (Entity)trans.GetObject(selectedObj.ObjectId, OpenMode.ForRead);
Entity transformedEntity = selectedEntity.GetTransformedCopy(transMat);
currentSpace.AppendEntity(transformedEntity);
trans.AddNewlyCreatedDBObject(transformedEntity, true);
}
trans.Commit();
}
}
}
}