namespace wirings
{
public class MyCommands
{
int counter = 0;
int i = 0;
int j = 0;
// Заполнение таблицы
[CommandMethod("wirings", CommandFlags.Modal)]
public void Wirings()
{
string name = "", label = "", chain = "";
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acDb = acDoc.Database;
Editor ed = acDoc.Editor;
using (Transaction trans = acDb.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)acDb.BlockTableId.GetObject(OpenMode.ForRead);
foreach (ObjectId btrId in bt)
{
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(btrId, OpenMode.ForRead);
BlockTableRecord block = null;
if (btr.HasAttributeDefinitions)
{
foreach (ObjectId brId in btr.GetBlockReferenceIds(true, false))
{
BlockReference br = (BlockReference)trans.GetObject(brId, OpenMode.ForRead);
if (br.IsDynamicBlock)
block = trans.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
else
block = trans.GetObject(br.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
if (block.Name == "розетка")
{
foreach (ObjectId attrRefObjId in br.AttributeCollection)
{
AttributeReference attrRef = (AttributeReference)trans.GetObject(attrRefObjId, OpenMode.ForRead);
if (attrRef.Tag == "ОБОЗНАЧЕНИЕ")
label = attrRef.TextString;
if (attrRef.TextString != "" && attrRef.Tag != "ОБОЗНАЧЕНИЕ")
{
chain = attrRef.TextString;
name = label + ":" + attrRef.Tag;
WorkTable(chain);
WorkTable(name);
WorkTable(FindChain(chain));
ed.WriteMessage("\n");
}
}
}
}
}
}
}
counter = 0;
i = 0;
j = 0;
}
// Поиск второй части записи
public string FindChain(string chain)
{
string name = "", label = "", nameTag = "";
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acDb = acDoc.Database;
Editor ed = acDoc.Editor;
using (Transaction trans = acDb.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)acDb.BlockTableId.GetObject(OpenMode.ForRead);
foreach (ObjectId btrId in bt)
{
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(btrId, OpenMode.ForRead);
BlockTableRecord block = null;
if (btr.HasAttributeDefinitions)
{
foreach (ObjectId brId in btr.GetBlockReferenceIds(true, false))
{
BlockReference br = (BlockReference)trans.GetObject(brId, OpenMode.ForRead);
//определение настоящего имени вставки блока
if (br.IsDynamicBlock)
block = trans.GetObject(br.DynamicBlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
else
block = trans.GetObject(br.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
//*************************************************
foreach (ObjectId attrRefObjId in br.AttributeCollection)
{
AttributeReference attrRef = (AttributeReference)trans.GetObject(attrRefObjId, OpenMode.ForRead);
if (attrRef.Tag == "ОБОЗНАЧЕНИЕ")
label = attrRef.TextString;
if (attrRef.TextString == chain)
{
if (block.Name == "клеммник")
{
foreach (ObjectId attrRefObjIdKlem in br.AttributeCollection)
{
AttributeReference attrRefKlem = (AttributeReference)trans.GetObject(attrRefObjIdKlem, OpenMode.ForRead);
if (attrRef.Tag == "ЦЕПЬ-А" && attrRefKlem.Tag == "№-А")
{
nameTag = attrRefKlem.TextString;
name = label + ":" + nameTag;
return name;
}
else if (attrRef.Tag == "ЦЕПЬ-Б" && attrRefKlem.Tag == "№-Б")
{
nameTag = attrRefKlem.TextString;
name = label + ":" + nameTag;
return name;
}
}
}
else
{
nameTag = attrRef.Tag;
name = label + ":" + nameTag;
return name;
}
}
}
}
}
}
return "";
}
}
// Занесение данных в таблицу
public void WorkTable(string slot)
{
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acDb = acDoc.Database;
Editor ed = acDoc.Editor;
using (Transaction trans = acDb.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)acDb.BlockTableId.GetObject(OpenMode.ForWrite);
foreach (ObjectId btrId in bt)
{
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(btrId, OpenMode.ForWrite);
foreach (ObjectId brId in btr.GetBlockReferenceIds(true, false))
{
BlockReference br = (BlockReference)trans.GetObject(brId, OpenMode.ForWrite);
if (br.ObjectId.ObjectClass.Name == "AcDbTable")
{
Table tb = (Table)trans.GetObject(br.ObjectId, OpenMode.ForWrite);
if (counter == 0)
{
i = 2; j = 1;
}
if (j == 4)
{
i = i + 1;
j = 1;
}
tb.Cells[i, j].TextString = slot;
ed.WriteMessage(i.ToString() + ", " + j.ToString() + ": " + tb.Cells[i, j].TextString + "\t");
tb.GenerateLayout();
j++;
counter++;
}
}
}
trans.Commit();
}
}
}
}