using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
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 AcAp = Autodesk.AutoCAD.ApplicationServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using AcEd = Autodesk.AutoCAD.EditorInput;
using AcGe = Autodesk.AutoCAD.Geometry;
using AcRx = Autodesk.AutoCAD.Runtime;
//
using AcUi = Autodesk.AutoCAD.Windows;
namespace kdubTestingCommands
{
public partial class TestCommands
{
//
// // CodeHimBelongaKwb © Mar 2008
//
[CommandMethod("Test_01")]
public void getBlocksTest()
{
DateTime TimeStart = DateTime.Now;
string FileNamePath = @"D:\EvalReport_01.txt";
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
ObjectId[] selectedIDs = SelectBlockRefs03();
if (selectedIDs == null) return;
StreamWriter LogFile_Obj
= new StreamWriter
(FileNamePath,
true,
System.Text.Encoding.ASCII); LogFile_Obj.AutoFlush = true;
LogFile_Obj.WriteLine("Starting eval...");
int countAtts = 0;
Database db = AcDb.HostApplicationServices.WorkingDatabase;
AcDb.TransactionManager tm = AcadApp.DocumentManager.MdiActiveDocument.TransactionManager;
using (Transaction tr = tm.StartTransaction())
{
try
{
foreach (ObjectId entID in selectedIDs)
{
BlockReference blkRef = tr.GetObject(entID, OpenMode.ForRead, false)
as BlockReference;
foreach (ObjectId attId in blkRef.AttributeCollection)
{
AttributeReference attRef = tr.GetObject(attId, OpenMode.ForRead)
as AttributeReference;
if (!(attRef == null))
{
countAtts = countAtts + 1;
LogFile_Obj.WriteLine(countAtts.ToString() + '\t' +
attRef.Tag + '\t' + attRef.TextString);
}
}
}
TimeSpan TimeDuration = (DateTime.Now - TimeStart);
ed.WriteMessage("End eval...Process time: "
+ (TimeDuration.TotalSeconds + " Seconds for "
+ countAtts.ToString() + " records of "
+ db.ApproxNumObjects.ToString()));
LogFile_Obj.WriteLine(("End eval...Process time: "
+ (TimeDuration.TotalSeconds + " Seconds.")));
}
catch
{
ed.WriteMessage("Ooops");
}
finally
{
LogFile_Obj.Close();
}
}
}
//
// // CodeHimBelongaKwb © Mar 2008
//
static public ObjectId[] SelectBlockRefs03()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
TypedValue[] tValues =
{ new TypedValue
((int)DxfCode
.Start,
"INSERT"),
new TypedValue
((int)DxfCode
.LayerName,
"BLOCKS"),
new TypedValue
(410,
"Model"),
new TypedValue
(66,
1) // hasAttributes ? };
SelectionFilter sFilter
= new SelectionFilter
(tValues
);
PromptSelectionResult res = ed.SelectAll(sFilter);
if (res.Status != PromptStatus.OK) return null;
return res.Value.GetObjectIds();
}
//
// Translated from VB code AT http://discussion.autodesk.com/thread.jspa?threadID=650451
//
[CommandMethod("Test_02")]
public void Test_ReadWriteAttData()
{
DateTime TimeStart = DateTime.Now;
string FileNamePath = @"D:\EvalReport_02.txt";
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
StreamWriter LogFile_Obj
= new StreamWriter
(FileNamePath,
true,
System.Text.Encoding.ASCII); LogFile_Obj.AutoFlush = true;
LogFile_Obj.WriteLine("Starting eval...");
int countAtts = 0;
Database db = AcDb.HostApplicationServices.WorkingDatabase;
AcDb.TransactionManager tm = AcadApp.DocumentManager.MdiActiveDocument.TransactionManager;
using (Transaction tr = tm.StartTransaction())
{
try
{
BlockTable table = tm.GetObject
(db.BlockTableId, OpenMode.ForRead, false) as BlockTable;
BlockTableRecord modelSpace = tm.GetObject(table[BlockTableRecord.ModelSpace],
OpenMode.ForWrite, false) as BlockTableRecord;
foreach (ObjectId objId in modelSpace)
{
Entity ent = tr.GetObject(objId, OpenMode.ForRead, false) as Entity;
if (ent.GetType().FullName.Equals("Autodesk.AutoCAD.DatabaseServices.BlockReference"))
{
BlockReference blkRef = ent as BlockReference;
if (blkRef.Layer == "BLOCKS")
{
foreach (ObjectId attId in blkRef.AttributeCollection)
{
AttributeReference attRef = tr.GetObject(attId, OpenMode.ForRead)
as AttributeReference;
if (!(attRef == null))
{
countAtts = countAtts + 1;
LogFile_Obj.WriteLine(countAtts.ToString() + '\t' +
attRef.Tag + '\t' + attRef.TextString);
}
}
}
}
}
TimeSpan TimeDuration = (DateTime.Now - TimeStart); ;
ed.WriteMessage("End eval...Process time: "
+ (TimeDuration.TotalSeconds + " Seconds for "
+ countAtts.ToString() + " records of "
+ db.ApproxNumObjects.ToString()));
LogFile_Obj.WriteLine(("End eval...Process time: "
+ (TimeDuration.TotalSeconds + " Seconds.")));
}
catch
{
ed.WriteMessage("Ooops");
}
finally
{
LogFile_Obj.Close();
}
}
}
}
}