using System.Collections.Generic;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
namespace Multiply_Insert
{
public class Commands
{
[CommandMethod("LMXMULTIPLYINSERT")]
public void lmxInsertBlock()
{
Document lxDocument = Application.DocumentManager.MdiActiveDocument;
Database lxDatabase = Application.DocumentManager.MdiActiveDocument.Database;
Editor lxEditor = Application.DocumentManager.MdiActiveDocument.Editor;
ObjectId lxBlockID;
string FullPath = "D:\\test.dwg";
System.IO.FileInfo lxFile = new System.IO.FileInfo(FullPath);
string BlockName = lxFile.Name;
double lx = 0;
Point3d lxBlockInsertPoint = new Point3d(lx, 0, 0);
List<BlockReference> lxBlockReferenceCollect = new List<BlockReference>();
int i = 0;
while(i < 6)
{
using (Transaction lxTransaction = lxDatabase.TransactionManager.StartTransaction())
{
using (Database lxTempDatabase = new Database(false, true))
{
BlockTable lxBlockTable = lxTransaction.GetObject(lxDatabase.BlockTableId, OpenMode.ForWrite, false) as BlockTable;
if (!lxBlockTable.Has(BlockName))
{
lxTempDatabase.ReadDwgFile(FullPath, System.IO.FileShare.Read, true, null);
lxBlockID = lxDatabase.Insert(BlockName, lxTempDatabase, true);
}
else
{
lxBlockID = lxBlockTable[BlockName];
}
}
lxTransaction.Commit();
}
using (Transaction lxTransaction = lxDatabase.TransactionManager.StartTransaction())
{
BlockTable lxBlockTable = lxTransaction.GetObject(lxDatabase.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord lxBlock = lxBlockTable[BlockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
BlockReference lxBlockReference = new BlockReference(lxBlockInsertPoint, lxBlockID);
BlockTableRecord lxBlockTableRecord = lxTransaction.GetObject(lxBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
lxBlockTableRecord.AppendEntity(lxBlockReference);
lxTransaction.AddNewlyCreatedDBObject(lxBlockReference, true);
foreach (ObjectId lxID in lxBlock)
{
DBObject lxObject = lxID.GetObject(OpenMode.ForRead);
AttributeDefinition lxAttributeDefenition = lxObject as AttributeDefinition;
if ((lxAttributeDefenition != null) && (!lxAttributeDefenition.Constant))
{
using (AttributeReference lxAttributeReference = new AttributeReference())
{
lxAttributeReference.SetAttributeFromBlock(lxAttributeDefenition, lxBlockReference.BlockTransform);
lxBlockReference.AttributeCollection.AppendAttribute(lxAttributeReference);
lxTransaction.AddNewlyCreatedDBObject(lxAttributeReference, true);
}
}
}
lxBlockReferenceCollect.Add(lxBlockReference);
lxTransaction.Commit();
}
lx = lx + 5;
i++;
lxBlockInsertPoint = new Point3d(lx, 0, 0);
}
try
{
lmxDrawJig lxJig = new lmxDrawJig(new Point3d(0, 0, 0));
using (Transaction lxTrasaction = lxDatabase.TransactionManager.StartTransaction())
{
foreach (BlockReference lxObject in lxBlockReferenceCollect)
{
BlockReference lxBlockReference = lxTrasaction.GetObject(lxObject.ObjectId, OpenMode.ForWrite) as BlockReference;
lxJig.AddEntity(lxBlockReference);
}
PromptResult lxPromptResult = Application.DocumentManager.MdiActiveDocument.Editor.Drag(lxJig);
if (lxPromptResult.Status == PromptStatus.OK)
{
lxJig.TransformEntities();
lxTrasaction.Commit();
}
else
lxTrasaction.Abort();
}
}
catch (System.Exception ex)
{
Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage(ex.ToString());
}
}
class lmxDrawJig : DrawJig
{
private Point3d lxOriginPoint;
private Point3d lxNewPoint;
List<BlockReference> lxReferenceCollect;
public lmxDrawJig(Point3d basePt)
{
lxOriginPoint = basePt.TransformBy(UCS);
lxReferenceCollect = new List<BlockReference>();
}
public Point3d Base
{
get { return lxNewPoint; }
set { lxNewPoint = value; }
}
public Point3d Location
{
get { return lxNewPoint; }
set { lxNewPoint = value; }
}
public Matrix3d UCS
{
get
{
return Application.DocumentManager.MdiActiveDocument.Editor.CurrentUserCoordinateSystem;
}
}
public void AddEntity(BlockReference lxRef)
{
lxReferenceCollect.Add(lxRef);
}
public void TransformEntities()
{
Matrix3d lxMatrix = Matrix3d.Displacement(lxOriginPoint.GetVectorTo(lxNewPoint));
foreach (BlockReference lxObject in lxReferenceCollect)
{
lxObject.TransformBy(lxMatrix);
}
}
protected override bool WorldDraw(WorldDraw lxDraw)
{
Matrix3d lxMatrix = Matrix3d.Displacement(lxOriginPoint.GetVectorTo(lxNewPoint));
WorldGeometry lxGeometry = lxDraw.Geometry;
if (lxGeometry != null)
{
lxGeometry.PushModelTransform(lxMatrix);
foreach (BlockReference ent in lxReferenceCollect)
{
ent.Visible = true;
lxGeometry.Draw(ent);
}
lxGeometry.PopModelTransform();
}
return true;
}
protected override SamplerStatus Sampler(JigPrompts lxPrompts)
{
JigPromptPointOptions lxPPO = new JigPromptPointOptions("\nNew location:");
lxPPO.UseBasePoint = false;
PromptPointResult lxPointResult = lxPrompts.AcquirePoint(lxPPO);
if (lxPointResult.Status == PromptStatus.Cancel || lxPointResult.Status == PromptStatus.Error)
return SamplerStatus.Cancel;
if (!lxNewPoint.IsEqualTo(lxPointResult.Value, new Tolerance(10e-10, 10e-10)))
{
lxNewPoint = lxPointResult.Value;
return SamplerStatus.OK;
}
else return SamplerStatus.NoChange;
}
}
}
}