using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#if NC
using Teigha.DatabaseServices;
using Teigha.Runtime;
using Teigha.Geometry;
using HostMgd.ApplicationServices;
using HostMgd.EditorInput;
using Trtm = Teigha.Runtime;
using Platform = HostMgd;
using PlatformDb = Teigha;
using Teigha.Colors;
//using Autodesk.AutoCAD.ApplicationServices;
//using Autodesk.AutoCAD.DatabaseServices;
//using Autodesk.AutoCAD.EditorInput;
//using Autodesk.AutoCAD.Runtime;
//using Autodesk.AutoCAD.Colors;
#else
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Runtime;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Trtm = Autodesk.AutoCAD.Runtime;
using Platform = Autodesk.AutoCAD;
using PlatformDb = Autodesk.AutoCAD;
#endif
namespace drz
/*подготовка подосновы
*
*/
{
public class NormalizedBlock : IExtensionApplication
{
[CommandMethod("drz-BlcNorm", CommandFlags.Modal)]
public void BlockNormalized()
/* Слой примитивов блока в Zero
* Цвет примитивов блока по слою
*/
{
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
int count = 0;
short newColorIndex = 256;
string newLayer = "0";
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);
foreach (ObjectId btrId in bt)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
if (!btr.IsFromExternalReference)
{
if (btr.IsLayout)//проходим по всем Space для обновления вида атрибутов
{
count += BlockNormalizedAttRef(btrId, newColorIndex, newLayer);
}
else //вне чертежа, меняем примитивы в описаниях блоков
{
foreach (ObjectId id in btr)
{
Entity ent = tr.GetObject(id, OpenMode.ForRead) as Entity;//может быстрее будет открывать сразу на запись?
if (ent != null)
{
if (ent.Layer != newLayer)// может быстрее не делать проверку, сразу переписывать?
{
ent.UpgradeOpen();
ent.Layer = newLayer;
ent.DowngradeOpen();
}
if (ent.ColorIndex != newColorIndex)
{
ent.UpgradeOpen();
ent.ColorIndex = newColorIndex;
ent.DowngradeOpen();
}
}
}
}
}
}
tr.Commit();
}
ed.Regen();
ed.WriteMessage(
"\nИзменено {0} атрибут{1} в цвет {2} на слое {3}.",
count,
count == 1 ? "" : "ов",
newColorIndex,
newLayer
);
}
//--------------------------
private int BlockNormalizedAttRef(ObjectId btrId, short colorIndex, string layerName)
{
int changedCount = 0;
Document doc = Application.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
Editor ed = doc.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTableRecord btr = (BlockTableRecord)tr.GetObject(btrId, OpenMode.ForRead);
foreach (ObjectId entId in btr)
{
BlockReference br = tr.GetObject(entId, OpenMode.ForRead) as BlockReference;
if (br != null)
{
//----------------------
foreach (ObjectId attId in br.AttributeCollection)
{
AttributeReference att = tr.GetObject(attId, OpenMode.ForRead, false, true) as AttributeReference;
if (att != null)
{
bool bCount = false;
if (att.ColorIndex != colorIndex)
{
bCount = true;
att.UpgradeOpen();
att.ColorIndex = colorIndex;
att.DowngradeOpen();
}
if (att.Layer != layerName)
{
bCount = true;
att.UpgradeOpen();
att.Layer = layerName;
att.DowngradeOpen();
}
if (bCount) changedCount++;
}
}
//-----------------------
// Рекурсивно для вложенных блоков
changedCount += BlockNormalizedAttRef(br.BlockTableRecord, colorIndex, layerName);
}
}
tr.Commit();
}
return changedCount;
}
//-----------------------------
public void Initialize()
{
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("\"drz\" загружен");
}
public void Terminate()
{
}
}
}