Quick way to create HTML report
Create Class library project BlockReport
Add 2 classes: Report.cs and SelectBlocks.cs
//
// code for Report.cs
//
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.Runtime;
using System.Diagnostics;
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.ComponentModel;
[assembly: CommandClass(typeof(BlockReport.Report))]
namespace BlockReport
{
public class Report
{
System.Data.DataTable tbl;
string tblname = "BLOCK INFO";
string filename = @"C:\BLOCKS.html";//<--change path for outputfile here
[CommandMethod("REPORT")]
public void Doit()
{
tbl = SelectBlocks.BlockInfoToTable();
DataView dv = tbl.DefaultView;
dv.Sort = "Name ASC";
tbl = dv.ToTable();
BuildHTMLDoc(tbl, tblname, filename);
Process.Start(filename);
}
public void BuildHTMLDoc(System.Data.DataTable dtb, string tblname, string filename)
{
string bdcol = "#91b6dd";
string fcol1 = "#33066";
string fcol2 = "#3300CC";
string bcol1 = "#e8e8e8";
string bcol2 = "#c8c8c8";
string fcolor = "";
string bcolor = "";
//possible HTML colors (a bit of):
//6799ff royal blue
//d7b941 dirty yellow
//3366cc blue
//ededed light gray 25
//e0e0e0 light gray 50
//ff0000 red
//666699 violet
//cc3333 indian red
//3c5b00 green
//73ad00 light green
//d0d5c6 light khaki
//91b6dd light steel blue
try
{
DateTime dt = DateTime.Now;
string dat = dt.ToString("d");
string textHTML =
SetDocumentStart("Simple Report Example") +
SetBodyStart(bdcol) +
SetParagraphStartLeft(dat, fcol1) +
SetCaption(dat, fcol1) +
SetTitle("2", "Report Title") +
SetTitle("3", tblname) +
SetParagraphStart() +
SetTableStart("50");//<--table width in percents
StringBuilder sb = new StringBuilder();
sb.Append(textHTML);
textHTML = SetRowStart(bcolor);
sb.Append(textHTML);
int cnt = 0;
for (int j = 0; j < dtb.Columns.Count; j++)
{
textHTML = SetCellStart(dtb.Columns[j].ColumnName, "blue");
sb.Append(textHTML);
textHTML = SetCellEnd();
sb.Append(textHTML);
cnt += 1;
}
textHTML = SetRowEnd();
sb.Append(textHTML);
cnt = 0;
for (int i = 0; i < dtb.Rows.Count; i++)
{
if (i % 2 == 0)
{
bcolor = bcol1;
fcolor = fcol1;
}
else
{
bcolor = bcol2;
fcolor = fcol2;
}
textHTML = SetRowStart(bcolor);
sb.Append(textHTML);
DataRow dr = dtb.Rows[i];
object[] line = dr.ItemArray;
for (int j = 0; j < dtb.Columns.Count; j++)
{
string celltext = "";
object cellvalue = line[j];
if (cellvalue == null)
{
celltext = "--";
}
else
{
celltext = cellvalue.ToString();
}
textHTML = SetCellStart(celltext, fcolor);
sb.Append(textHTML);
textHTML = SetCellEnd();
sb.Append(textHTML);
cnt += 1;
}
textHTML = SetRowEnd();
sb.Append(textHTML);
}
textHTML = SetRowStart(bcol1);
sb.Append(textHTML);
for (int j = 0; j < dtb.Columns.Count - 2; j++)
{
string celltext = "";
textHTML = SetCellStart(celltext, fcolor);
sb.Append(textHTML);
textHTML = SetCellEnd();
sb.Append(textHTML);
cnt += 1;
}
textHTML = SetCellStart("Blocks Count: ", fcolor);
sb.Append(textHTML);
textHTML = SetCellEnd();
sb.Append(textHTML);
System.Data.DataColumn count =
new System.Data.DataColumn("Count",
System.Type.GetType("System.Int16"));
count.Expression = "COUNT(Name)";
dtb.Columns.Add(count);
string num = dtb.Rows[0].ItemArray[dtb.Columns.Count-1].ToString();
textHTML = SetCellStart(num, fcolor);
sb.Append(textHTML);
textHTML = SetCellEnd();
sb.Append(textHTML);
textHTML = SetRowEnd();
sb.Append(textHTML);
//
textHTML = SetTableEnd();
sb.Append(textHTML);
textHTML = SetParagraphEnd();
sb.Append(textHTML);
textHTML = SetBodyEnd();
sb.Append(textHTML);
textHTML = SetDocumentEnd();
sb.Append(textHTML);
using (StreamWriter sw = new StreamWriter(filename))
{
sw.Write(sb.ToString());
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
}
}
private string SetDocumentStart(string head)
{
return "<html><head><title><" + head + ">" + "</title></head>";
}
private string SetDocumentEnd()
{
return "</html>";
}
private string SetBodyStart(string color)
{
return "<body><body bgcolor =" + "" + color + "" + " " + Environment.NewLine;
}
private string SetBodyEnd()
{
return "</body bgcolor></body>" + Environment.NewLine;
}
private string SetTitle(string size, string value)
{
return "<h" + size + "><p align=center>" + value + "</p align></h" + size + ">";
}
private string SetParagraphIndent(string value, string color)
{
return "<p><blockquote><font color=" + "" + color + "" + " " + value + "</font><blockquote></p>";
}
private string SetParagraphStart()
{
return "<p><p align=center>";
}
private string SetParagraphStartLeft(string value, string color)
{
return "<p><p align=left><font color=" + "" + color + "" + " " + value + "</font></p>";
}
private string SetParagraphEnd()
{
return "<p><p align=center>" + Environment.NewLine;
}
private string SetTableStart(string width)
{
return "<table width=" + "" + width + "%%" + "" + "><t align=center>";
}
private string SetTableEnd()
{
return "<t align></table>" + Environment.NewLine;
}
private string SetRowStart(string color)
{
return "<tr><tr align=center><tr bgcolor=" + "" + color + "" + ">";
}
private string SetRowEnd()
{
return "</tr align></tr>" + Environment.NewLine;
}
private string SetCellStart(string value, string color)
{
string cell = "<td><td align=center><font color=" + "" + color + "" + ">" + value + "</font></td align></td>";
return cell;
}
private string SetCellEnd()
{
return "</td align></td>" + Environment.NewLine;
}
private string SetCaption(string caption, string color)
{
return "<caption><font color=" + "" + color + "" + ">" + "<b>" + caption + "</b></font></caption>";
}
}
}
//
// code for SelectBlocks.cs
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Threading;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.DatabaseServices;
using AcDb = Autodesk.AutoCAD.DatabaseServices;
using acadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using Autodesk.AutoCAD.ApplicationServices;
using System.Windows.Forms;
[assembly: CommandClass(typeof(BlockReport.SelectBlocks))]
namespace BlockReport
{
public class SelectBlocks
{
/// <summary>
///
/// </summary>
/// <returns></returns>
static public System.Data.DataTable BlockInfoToTable()
{
System.Data.DataTable dt = new System.Data.DataTable();
try
{
Document doc = acadApp.DocumentManager.MdiActiveDocument;
Database db = HostApplicationServices.WorkingDatabase;
Editor ed = doc.Editor;
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
/*blocks just with attributes
// TypedValue[] tvs = new TypedValue[]
//{ new TypedValue((int)DxfCode.Start, "INSERT"),
// new TypedValue((int)DxfCode.HasSubentities, 1)
// };*/
TypedValue[] tvs = new TypedValue[]
{ new TypedValue((int)DxfCode.Start, "INSERT")};
SelectionFilter sf = new SelectionFilter(tvs);
PromptSelectionResult psr = ed.SelectAll(sf);
SelectionSet sset = psr.Value;
MessageBox.Show(sset.Count.ToString());
dt.Columns.AddRange(new System.Data.DataColumn[]{
new System.Data.DataColumn("Name",System.Type.GetType("System.String")),
new System.Data.DataColumn("ObjectID",System.Type.GetType("System.String")),
new System.Data.DataColumn("X_Coordinate",System.Type.GetType("System.Double")),
new System.Data.DataColumn("Y_Coordinate",System.Type.GetType("System.Double")),
new System.Data.DataColumn("Z_Coordinate",System.Type.GetType("System.Double")),
new System.Data.DataColumn("Rotation",System.Type.GetType("System.Double")),
});
foreach (SelectedObject sobj in sset)
{
DBObject dbobj = tr.GetObject(sobj.ObjectId, OpenMode.ForRead);
BlockReference bref = dbobj as BlockReference;
object[] param = new object[6];
if (bref != null)
{
param[0] = EffectiveName(bref);
param[1] = sobj.ObjectId;
param[2] = Math.Round(bref.Position.X, 3);
param[3] = Math.Round(bref.Position.Y, 3);
param[4] = Math.Round(bref.Position.Z, 3);
param[5] = Math.Round(RadianToDegree(bref.Rotation), 3);
DataRow dr = dt.NewRow();
dr.ItemArray = param;
dt.Rows.Add(dr);
}
}
}
}
catch (System.Exception ex)
{
MessageBox.Show(ex.StackTrace);
}
return dt;
}
/// <summary>
///
/// </summary>
/// <param name="blkref"></param>
/// <returns></returns>
static public string EffectiveName(BlockReference blkref)
{
if (blkref.IsDynamicBlock)
{
using (BlockTableRecord obj = (BlockTableRecord)
blkref.DynamicBlockTableRecord.GetObject(OpenMode.ForRead))
return obj.Name;
}
return blkref.Name;
}
/// <summary>
///
/// </summary>
/// <param name="angle"></param>
/// <returns></returns>
public static double RadianToDegree(double angle)
{
return angle * (180.0 / Math.PI);
}
}
}
~'J'~