using System.Collections.Generic;
using System.IO;
using System;
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 lmxMultiplyInsert()
{
List<string> lxCollection = new List<string>();
lxCollection.Add("D:\\Test_1.dwg");
for (int i=0; i<4; i++)
{
lxCollection.Add("D:\\Test_2.dwg");
lxCollection.Add("D:\\Test_3.dwg");
lxCollection.Add("D:\\Test_4.dwg");
lxCollection.Add("D:\\Test_5.dwg");
}
lmxInsertBlock(lxCollection, "A1", "00");
}
private void lmxInsertBlock(List<string> lCollection, string lPosition, string lSlot)
{
Database lxDatabase = Application.DocumentManager.MdiActiveDocument.Database;
Editor lxEditor = Application.DocumentManager.MdiActiveDocument.Editor;
ObjectId lxBlockID;
Point3d lxBlockInsertPoint = new Point3d(0, 0, 0);
List<ObjectId> lxIDCollect = new List<ObjectId>();
double lx = (-8*3)+1.5;
int lxCountChar = lSlot.Length;
foreach (string lxPath in lCollection)
{
string BlockName = Path.GetFileNameWithoutExtension(lxPath);
using (Transaction lxTransaction = lxDatabase.TransactionManager.StartTransaction())
{
BlockTable lxBlockTable = lxTransaction.GetObject(lxDatabase.BlockTableId, OpenMode.ForWrite, false) as BlockTable;
if (!lxBlockTable.Has(BlockName))
{
Database lxTempDatabaseChassis = new Database(false, true);
lxTempDatabaseChassis.ReadDwgFile(lxPath, FileShare.Read, true, null);
lxBlockID = lxDatabase.Insert(BlockName, lxTempDatabaseChassis, true);
}
else { lxBlockID = lxBlockTable[BlockName]; }
lxTransaction.Commit();
}
using (Transaction lxTransaction = lxDatabase.TransactionManager.StartTransaction())
{
BlockTable lxBlockTable = lxTransaction.GetObject(lxDatabase.BlockTableId, OpenMode.ForWrite, false) as BlockTable;
BlockTableRecord lxBlock = lxBlockTable[BlockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
BlockTableRecord lxModelSpace = lxTransaction.GetObject(lxBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
BlockReference lxBlockReference = new BlockReference(lxBlockInsertPoint, lxBlockID);
lxModelSpace.AppendEntity(lxBlockReference);
lxTransaction.AddNewlyCreatedDBObject(lxBlockReference, true);
lxIDCollect.Add(lxBlockReference.ObjectId);
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);
}
}
}
AttributeCollection lxAttributeCollection = lxBlockReference.AttributeCollection;
ObjectId lxAttrRef = lxAttributeCollection[0];
AttributeReference lxAttr = lxAttrRef.GetObject(OpenMode.ForRead) as AttributeReference;
string lxTextString = lxAttr.TextString;
if (lxTextString.Contains("-"))
{
lxAttr = lxAttrRef.GetObject(OpenMode.ForWrite) as AttributeReference;
lxAttr.TextString = $"{lPosition}-{lSlot}";
lSlot = (Convert.ToInt32(lSlot) + 1).ToString($"D{lxCountChar}");
}
else
{
lxAttr = lxAttrRef.GetObject(OpenMode.ForWrite) as AttributeReference;
lxAttr.TextString = $"{lPosition}";
}
lxBlockReference.Visible = false;
lxTransaction.Commit();
}
if(lx == -22.5) { lx = -22.5; }
lxBlockInsertPoint = new Point3d(lx, 16.615, 0);
lx = lx + 3;
}
using (Transaction lxTransaction = lxDatabase.TransactionManager.StartTransaction())
{
lmxDrawJig lxJig = new lmxDrawJig(new Point3d(0, 0, 0));
foreach (ObjectId lxID in lxIDCollect)
{
BlockReference lxBlockReference = lxTransaction.GetObject(lxID, OpenMode.ForWrite) as BlockReference;
lxJig.AddEntity(lxBlockReference.Clone());
}
PromptResult lxPromptResult = Application.DocumentManager.MdiActiveDocument.Editor.Drag(lxJig);
if (lxPromptResult.Status == PromptStatus.OK)
{
foreach (ObjectId lxID in lxIDCollect)
{
BlockReference lxBlockReference = lxTransaction.GetObject(lxID, OpenMode.ForWrite) as BlockReference;
lxBlockReference.Erase();
}
lxJig.TransformEntities();
lxTransaction.Commit();
}
else lxTransaction.Abort();
}
}
class lmxDrawJig : DrawJig
{
private Point3d lxOriginPoint;
private Point3d lxNewPoint;
List<object> lxReferenceCollect;
public lmxDrawJig(Point3d basePt)
{
lxOriginPoint = basePt.TransformBy(UCS);
lxReferenceCollect = new List<object>();
}
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(object lxRef)
{
lxReferenceCollect.Add(lxRef);
}
public void TransformEntities()
{
Matrix3d lxMatrix = Matrix3d.Displacement(lxOriginPoint.GetVectorTo(lxNewPoint));
Database lxDatabase = Application.DocumentManager.MdiActiveDocument.Database;
using (Transaction lxTransaction = lxDatabase.TransactionManager.StartTransaction())
{
BlockTable lxBlockTable = lxTransaction.GetObject(lxDatabase.BlockTableId, OpenMode.ForWrite, false) as BlockTable;
BlockTableRecord lxModelSpace = lxTransaction.GetObject(lxBlockTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
foreach (BlockReference lxObject in lxReferenceCollect)
{
lxModelSpace.AppendEntity(lxObject);
lxObject.TransformBy(lxMatrix);
}
lxTransaction.Commit();
}
}
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 lxObject in lxReferenceCollect)
{
lxObject.Visible = true;
lxGeometry.Draw(lxObject);
}
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;
}
}
}
}