using System;
using Autodesk.AutoCAD.Runtime;
using acadApp = Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using System.Diagnostics;
namespace Test
{
class WriteDwg
{
[CommandMethod("AddBlockTest")]
static public void AddBlockTest()
{
using (Database db = new Database(false, true))
{
db.ReadDwgFile(@"D:\Test\318_ФСА_ПВУ.dwg",
System.IO.FileShare.ReadWrite, true, "");
ObjectIdCollection ids = new ObjectIdCollection();
using (Transaction tr =
db.TransactionManager.StartTransaction())
{
using (Transaction myT = db.TransactionManager.StartTransaction())
{
// Получаем определение блока "Check".
string blockName = "FSA_Filter_In";
BlockTable bt =
db.BlockTableId.GetObject(OpenMode.ForRead) as BlockTable;
BlockTableRecord blockDef =
bt[blockName].GetObject(OpenMode.ForRead) as BlockTableRecord;
// Открываем пространство модели – мы добавляем наш BlockReference к нему
BlockTableRecord ms =
bt[BlockTableRecord.ModelSpace].GetObject(OpenMode.ForWrite)
as BlockTableRecord;
// Создаём BlockReference и связываем его с определением блока
Point3d point = new Point3d(-2.0, 4.0, 6.0);
using (BlockReference blockRef =
new BlockReference(point, blockDef.ObjectId))
{
// Добавляем вставку блока к пространству модели
ms.AppendEntity(blockRef);
myT.AddNewlyCreatedDBObject(blockRef, true);
// Проходимся по определению блока для поиска всех неконстантных атрибутов
int k = 1;
foreach (ObjectId id in blockDef)
{
DBObject obj = id.GetObject(OpenMode.ForRead);
AttributeDefinition attDef = obj as AttributeDefinition;
if ((attDef != null) && (!attDef.Constant))
{
// Это неконстантный AttributeDefinition
// Создаём новый AttributeReference
using (AttributeReference attRef = new AttributeReference())
{
attRef.SetAttributeFromBlock(attDef, blockRef.BlockTransform);
attRef.TextString = Convert.ToString(k);
// Добавляем AttributeReference к BlockReference
blockRef.AttributeCollection.AppendAttribute(attRef);
myT.AddNewlyCreatedDBObject(attRef, true);
k++;
}
}
}
}
// Работа выполнена!
myT.Commit();
}
}
}
}
}
}