Saturday, July 18, 2009

SLP Smooth Bellows Fitment




After rotating it

Tuesday, January 20, 2009

Handheld Green Laser

With all the talk about Green laser in small projectors, it was such a coincidence to have a friend show me his handheld one. These are used in combat for aim. Well, maybe a coincidence it WOULD be hypothetically, IF I had a friend who had one it would look like this.

Friday, November 28, 2008

Eaton Vance

Since I read a lot of Blog posts about stock investing, and certainly have chased my share of dead end investment ideas, I feel a need to mention this one. The Eaton Vance Municipal Bond fund is currently yielding 10% and is tax free. Obviously, the tax free part is compelling, and bumps up the yield quite a bit depending on a person's obligations to the federal government each April. Also worth mentioning, is that the dividends come every month at the same time, and the fund has a history longer can Google can display. I don't get a lot of open ears to this idea from my peer group or even older widows or orphans. Maybe life moves too fast for everybody else to put money away at this rate. Maybe I need to spend more to get more out of my life. But for now, I just can't stay away from this one. I have entrusted real estate with my money and gotten burned like probably countless other Americans now. So here is a link to what might be a winner :

EVN

Friday, October 31, 2008

.Net Pie Chart challenge


My first response to this was very blah. Having seen this stuff done by every product since Access 2.0, made it unexciting to think about. But then my friend added a twist : make the pie chart clickable in a drill-down way. So as easy as it is, to create pie slices from an image of a circle, I was stumped at how to handle a "hotspot", and did not even know there was an ImageMap control available for a .Net webform.

So to hurry up and get something to chart, I created a dataset with one column of numeric values, and a web user control to house the ImageMap control. My control exposes a method called "Draw" and raises an event called "HotSpotClicked" which is declared as follows :


public event ImageMapEventHandler HotSpotClicked;


The graphics to write the circle and pie slices are easy to call in the System.Drawing namespace. Here is Draw method, which calls a separate method to add a percentage column to the DataSet table passed to it. It uses random colors for the pie slices, and selects the colors from the Brushes namespace of System.Drawing using Reflection. It creates a GUID for a temporary file name in which to store the Bitmap. For each pie slice it draws, it saves coordinates for where to put the map hotspot in another column in the dataset. So how to compute X,Y coordinates of the outer edge of a pie slice. My good enough answer is to use a triangle defined by center and two end points of the slice. Those should be easy enough to figure out by conversion from angles and knowing the radius of the bounding circle. But not when you have a large slice, say greater than 100 degrees. For that size and higher the triangle it defines is squeezed thinner and thinner and loses usability as a hot regaion. So in that case I added one more coordinate dividing the "sweep" angle by 2 in my computation.


public void Draw(DataSet ds)
{
// Rectangle of that is the width of the ImageMap control.
Rectangle rect =
new Rectangle(0, 0,
Convert.ToInt32(ImageMap1.Width.Value),
Convert.ToInt32(ImageMap1.Height.Value));

/ Using floats as they are small enough and liked by the Math namespace.
float radius = rect.Width / 2;

string imageName = System.Guid.NewGuid().ToString();
Bitmap bitmap = new Bitmap(rect.Width, rect.Height);

Graphics g = Graphics.FromImage(bitmap);

g.DrawRectangle(Pens.Khaki, rect);
g.FillRectangle(Brushes.LightBlue, rect);

g.DrawEllipse(Pens.Khaki, rect);
g.FillEllipse(Brushes.LightPink, rect);

calcPercents(ds);

// Using reflection to get an array of colors from which to choose.
PropertyInfo[] penInfos = typeof(Pens).GetProperties();
PropertyInfo[] brushInfos = typeof(Brushes).GetProperties();

float startAngle = 0;

// For selecting colors.
Random rand = new System.Random();

// Add a column to the dataset to record color used.
ds.Tables[0].Columns.Add("ChartColor");

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow row = ds.Tables[0].Rows[i];

decimal fSweepAngle = 360 * (Convert.ToDecimal(row[1]) / 100);
float sweepAngle = (float)Math.Truncate(fSweepAngle);

if (i == ds.Tables[0].Rows.Count - 1)
{
sweepAngle += 360 - (startAngle + sweepAngle);
}

int colorIndex = rand.Next(brushInfos.Length - 1);

Brush myBrush = (Brush)brushInfos[colorIndex].GetValue(null, null);

g.DrawPie((Pen)penInfos[colorIndex].GetValue(null, null),
rect,
startAngle,
sweepAngle);

g.FillPie(myBrush,
rect,
startAngle,
sweepAngle);


// Calculate the Hot Spot polygon

// First line from center of the pie out to the edge.
string coords = radius.ToString() + "," +
radius.ToString() + ",";

coords += getCoordinates(startAngle, radius);

if (sweepAngle > 90)
{
//TODO: add more coordinates to get better hotspot coverage.
float intermediateAngle = startAngle + (sweepAngle / 2);

coords += getCoordinates(intermediateAngle, radius);
}

// move the needle up to where we just stopped for the next draw operation.
startAngle += sweepAngle;

coords += getCoordinates(startAngle, radius);

row[2] = coords;
row[3] = ((System.Drawing.SolidBrush)myBrush).Color.Name;
}

setHotSpots(ds);

ImageMap1.ImageUrl = "images/" + imageName + ".bmp";

bitmap.Save(Server.MapPath("images/" + imageName + ".bmp"),
System.Drawing.Imaging.ImageFormat.Bmp);
}



Setting hotspots in the map is easy once the coordinates have been solved :


private void setHotSpots(DataSet ds)
{
ImageMap1.HotSpots.Clear();

foreach (DataRow row in ds.Tables[0].Rows)
{
PolygonHotSpot phs = new PolygonHotSpot();
phs.Coordinates = row[2].ToString();
phs.AlternateText = row[0].ToString() + " "
+ row[1].ToString() + " "
+ row[3].ToString();
phs.PostBackValue = row[0].ToString();
phs.HotSpotMode = HotSpotMode.PostBack;
phs.NavigateUrl = "";
ImageMap1.HotSpots.Add(phs);
}
}


Code for calculating the percentage value of each data point provided in the dataset is very trivial ,but I list it here for future reference :


private void calcPercents(DataSet ds)
{
DataColumn PercentColumn = ds.Tables[0].Columns.Add("DataPercentage");
decimal total = 0;

foreach (DataRow row in ds.Tables[0].Rows)
{
total += Convert.ToDecimal(row[0]);
}

decimal totalPercent = 0;
foreach (DataRow row in ds.Tables[0].Rows)
{

decimal percentage = Convert.ToDecimal(row[0]) / total;
row[1] = Math.Round(percentage * 100, 1);
totalPercent += Convert.ToDecimal(row[1]);
}


if (totalPercent < 100)
{
DataRow lastRow = ds.Tables[0].Rows[ds.Tables[0].Rows.Count - 1];
decimal lastRowPercent = Convert.ToDecimal(lastRow[1]) + (100 - totalPercent);
lastRow[1] = lastRowPercent;
}

// add a column for coordinate set.
DataColumn coordinatesColumn = ds.Tables[0].Columns.Add("MapAreaCoordinates");

ds.AcceptChanges();
}


The real brain candy of the project was how to get the X.Y coordinates for an ImageMap knowing the size of the circle in the drawing and the "Sweep Angle" or angle of the pie slice. After some trial and error and merely remembering where to look from high school math, I wrote some calculations using SIN and COSINE. The fatal trap from hell, is that MATH.SIN does not accept Degrees, it accepts Radians. Intellisense doesn't tell us that. So here are the computations :


float Sin90 = (float)Math.Sin(ToRadian(90));

private string getCoordinates(float startAngle,
float radius)
{
float rise = 0;
float run = radius;
string coords = string.Empty;

rise = (radius * SinOfDegree(startAngle)) / Sin90;
run = (radius * CosOfDegree(startAngle)) / Sin90;

coords += Math.Round(radius + run, 0).ToString() + "," +
Math.Round(radius + rise, 0).ToString() + ",";

return coords;
}

private float SinOfDegree(float Degrees)
{
float radianOfDegree = ToRadian(Degrees);
return (float)Math.Sin(radianOfDegree);
}


private float CosOfDegree(float Degrees)
{
float radianOfDegree = ToRadian(Degrees);
return (float)Math.Cos(radianOfDegree);
}

private static float ToRadian(float Degrees)
{
return Degrees * (float)Math.PI / 180;
}

Sunday, October 26, 2008

Update to Config Builder

It has been requested that my tool create configuration elements that look more like old .Net Framework configuration elements, without the "add, remove, clear" syntax.

The new syntax looks like the following :

<BaseballConfig>
<WorldSeriess>
<add Year="2008">
<Teams>
<add Name="Rays" />
</Teams>
</add>
</WorldSeriess>
</BaseballConfig>


While the older, more traditional format looks like :

<BaseballConfig>
<WorldSeriess>
<WorldSeries Year="2008">
<Teams>
<Team Name="Phillies" />
<Team Name="Rays" />
</Teams>
</WorldSeries>
</WorldSeriess>
</BaseballConfig>


In order to get the xml elements to match the configuration class names, an override can be added to the ElementCollection class as follows :


public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}



So to handle this update (or not) I have created the following function for my custom configuration tool:


private static void addConfigurationElementCollectionTypeProperty(CodeTypeDeclaration configElements)
{
CodeTypeReferenceExpression ctre = new CodeTypeReferenceExpression(typeof(ConfigurationElementCollectionType));
CodeTypeReference ctr =
new CodeTypeReference(typeof(ConfigurationElementCollectionType));
CodeMemberProperty propCollectionType = new CodeMemberProperty();
propCollectionType.Name = "CollectionType";
propCollectionType.Attributes = MemberAttributes.Override | MemberAttributes.Public;
propCollectionType.HasSet = false;
propCollectionType.HasGet = true;
propCollectionType.Type = ctr;
propCollectionType.GetStatements.Add(
new CodeMethodReturnStatement(
new CodePropertyReferenceExpression(ctre, "BasicMap")));
configElements.Members.Add(propCollectionType);
}

Thursday, August 21, 2008

CodeDom NHibernate Mapping File Builder

I am currently watching the 'Summer of NHibernate' videos and have found myself wanting a tool for generating the HBM Mapping file. This was a painful place to be looking at a white screen to hand-fill with XML, as if anybody wants to state again all of the columns and data types already typed into a class and a "Create Table" script.


I have code already in all my other tools for getting database schema into my "columns" typed dataset. I ran XSD.EXE over the HBM file which I built by hand while watching 'Summer of NHibernate' to get a Class for easily authoring the XML document. Populating this class and then Serializing it was a nice way to stay in C# for the whole operation.

So here is what I am starting out of the gate with.



public static string GetMappingFile(SqlConnection Connection, string TableName)
{
string fileName = TableName + "hbm.xml";
hibernatemapping newMapping = new hibernatemapping();
newMapping.@namespace = "Acme.DataLayer";
newMapping.@class = new hibernatemappingClass();

hibernatemappingClass newClass = newMapping.@class;
newClass.name = TableName;
newClass.table = TableName;

Columns cols = PopulateColumns(Connection, TableName, CommandType.TableDirect);
newClass.property = new hibernatemappingClassProperty[cols.Column.Count];

int index = 0;

foreach (Columns.ColumnRow cRow in cols.Column)
{
newClass.property[index] = new hibernatemappingClassProperty();
hibernatemappingClassProperty prop = newClass.property[index];
prop.column = cRow.Column_Name;
prop.name = cRow.Column_Name;
prop.type = cRow.Data_Type;

switch (cRow.Data_Type.ToUpper())
{
case "DECIMAL":
break;
case "INT":
prop.type = "Int32";
break;
case "SMALLINT":
prop.type = "Int32";
break;
case "BIT":
prop.type = "Int32";
break;
case "VARCHAR":
prop.type = "string";
break;
default:
prop.type = "string";
break;
}
index++;
}

System.Xml.Serialization.XmlSerializer xmlSerializer =
new System.Xml.Serialization.XmlSerializer(typeof(hibernatemapping));

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);

xmlSerializer.Serialize(sw, newMapping);

return sb.ToString();
}

Saturday, August 09, 2008

CodeDom LWDO Builder

Seeing alot of classes that are "Lightweight Data Objects", simple representations of table entries that can be manipulated locally and then sent to a database, or passed around in application code. So I built a small generator using the CodeDom Namespace. Not intended to compete with MyGenerate or any other tool, just wanted to have this quick one handy.
This is for SQL Server database tables. The main function below :


public static string GetClientClass(SqlConnection Connection, string TableName)
{
CodeTypeReference codeTypeRef;
CodeMemberField memberField;
CodeMemberProperty memberPropPublic;

Columns cols = PopulateColumns(Connection, TableName, CommandType.TableDirect);

CodeNamespace codeNS = new CodeNamespace("ACME.Data");

CodeTypeDeclaration ct = new CodeTypeDeclaration(TableName);
ct.IsClass = true;
codeNS.Types.Add(ct);

CodeExpression markDirty = new CodeMethodInvokeExpression(
new CodeThisReferenceExpression(),
"MarkDirty",
new CodeExpression[] { });

foreach (Columns.ColumnRow cRow in cols.Column)
{
switch (cRow.Data_Type.ToUpper())
{
case "DECIMAL":
codeTypeRef = new CodeTypeReference("System.Decimal");
break;
case "INT":
codeTypeRef = new CodeTypeReference("System.Int32");
break;
case "SMALLINT":
codeTypeRef = new CodeTypeReference("System.Int32");
break;
case "BIT":
codeTypeRef = new CodeTypeReference("System.Int32");
break;
case "VARCHAR":
codeTypeRef = new CodeTypeReference("System.String");
break;
default:
codeTypeRef = new CodeTypeReference("System.String");
break;
}

memberField = new CodeMemberField(
codeTypeRef,
"_" + cRow.Column_Name.ToLower());

memberField.Attributes = MemberAttributes.Private;
ct.Members.Add(memberField);
memberPropPublic = new CodeMemberProperty();
memberPropPublic.Name = MakeFieldName(cRow.Column_Name);
memberPropPublic.Attributes = MemberAttributes.Public;
memberPropPublic.Type = codeTypeRef;
ct.Members.Add(memberPropPublic);

// Get Statement
CodeVariableReferenceExpression cvrexp =
new CodeVariableReferenceExpression("_" + cRow.Column_Name.ToLower());
CodeMethodReturnStatement cmrstmnt = new CodeMethodReturnStatement(cvrexp);
memberPropPublic.GetStatements.Add(cmrstmnt);

// Set Statement
memberPropPublic.SetStatements.Add(
new CodeAssignStatement(cvrexp,
new CodePropertySetValueReferenceExpression()));

memberPropPublic.SetStatements.Add(markDirty);
}

CSharpCodeProvider cSharpProvider = new CSharpCodeProvider();
StringBuilder sb = new StringBuilder();
System.IO.TextWriter tw = new System.IO.StringWriter(sb);
cSharpProvider.GenerateCodeFromNamespace(codeNS,
tw,
new System.CodeDom.Compiler.CodeGeneratorOptions());
return sb.ToString();
}



My code uses a DataSet Class I call "Columns.xsd" just for being able to iterate over something that represents the database table, without making a bunch of DataReader calls.



private static Columns PopulateColumns(SqlConnection Connection,
string TableName,
CommandType TableOrSproc)
{
Columns cols = new Columns();

Connection.Open();
SqlCommand sqlCmd = new SqlCommand();

if(TableOrSproc == CommandType.StoredProcedure)
{
// Get Parameters for Command.
sqlCmd = new SqlCommand(
"Select Parameter_Name as Column_Name,Data_Type, Character_maximum_length" +
" from Information_Schema.Parameters Where Specific_Name = '"
+ TableName +
"'",
Connection);
}
else
{
// Get Columns for the Table.
sqlCmd = new SqlCommand(
"Select Column_Name,Data_Type,Character_maximum_length" +
" from Information_Schema.Columns Where Table_Name = '"
+ TableName +
"'",
Connection);
}

sqlCmd.CommandType = CommandType.Text;

SqlDataReader dr = sqlCmd.ExecuteReader();
while (dr.Read())
{
Columns.ColumnRow row = cols.Column.NewColumnRow();
row.Column_Name = dr["Column_Name"].ToString();
row.Data_Type = dr["Data_Type"].ToString();
row.Character_maximum_length = dr["Character_maximum_length"].ToString();
cols.Column.AddColumnRow(row);
}
dr.Close();
Connection.Close();

return cols;
}