Hey LE, yea its further down. I didnt post the whole thing as it takes up a lot of space. Ill post the whole thing.
using System;
//using System.Collections; //Dont need this as we are using Generics
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System.Windows.Forms;
namespace TEPTITLE
{
public class TEPTITLE
{
[CommandMethod("TEPTitle")]
public static void TEPTitleBlock()
{
int cbo1int;
TitleBlock.TepTitleBlock frm = new TitleBlock.TepTitleBlock();
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(frm);
cbo1int = frm.BlockTypeIndex;
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
string strUser = System.Environment.UserName.ToUpper();
DateTime dateToday = DateTime.Today;
DateTime dateOnly = dateToday.Date;
string strDate = dateOnly.ToString("d");
Point3d insPt = new Point3d(0, 0, 0);
string strBlock = null;
#region TitleChoice
switch (cbo1int)
{
case 0:
strBlock = "TEP-INFO";
break;
case 1:
strBlock = "VTEP-INFO";
break;
case 2:
strBlock = "TEP-CIP";
break;
case 3:
strBlock = "VTEP-CIP";
break;
case 4:
strBlock = "UES-INFO";
break;
case 5:
strBlock = "VUES-INFO";
break;
case 6:
strBlock = "UES-CIP";
break;
case 7:
strBlock = "VUES-CIP";
break;
default:
break;
}
#endregion
#region SwitchUser
switch (strUser)
{
case "UA02038":
strUser = "D.HALL";
break;
case "UA00648":
strUser = "D.LIVINGSTONE";
break;
case "UA00071":
strUser = "G.TAYLOR";
break;
case "UA05645":
strUser = "N.MERCHANT";
break;
case "UA00664":
strUser = "P.OLIVAS";
break;
case "UA50151":
strUser = "L.RICHARDSON";
break;
case "UA50270":
strUser = "A.DOMINGUEZ";
break;
case "UA50050":
strUser = "D.LOPEZ";
break;
case "UA50463":
strUser = "C.PADILLA";
break;
}
#endregion
IDictionary<string, string> idAttlist = new Dictionary<string, string>();
//idAttlist.Add("TagName", "StringValue");
idAttlist.Add("TITLE1", "AC SCHEMATIC");
idAttlist.Add("TITLE2", "DC SCHEMATIC");
idAttlist.Add("TECH", strUser);
idAttlist.Add("TECHDATE", strDate);
string strPath = @"S:\TITLEBLOCKS\";
using (Transaction tr = db.TransactionManager.StartTransaction())
{
BlockTableRecord CurrentSpaceBTR = tr.GetObject(db.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord;
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
#region InsertBlockFirstTime
if (bt.Has(strBlock))
{
ObjectId tmpBlockId = bt[strBlock];
BlockTableRecord blkDefRecord = (BlockTableRecord)tr.GetObject(tmpBlockId, OpenMode.ForRead);
//BlockReference br = new BlockReference(ppr.Value, tmpBlockId);
BlockReference br = new BlockReference(insPt, tmpBlockId);
CurrentSpaceBTR.AppendEntity(br);
if (blkDefRecord.HasAttributeDefinitions)
{
foreach (ObjectId idAtt in blkDefRecord)
{
AttributeDefinition attDef = tr.GetObject(idAtt, OpenMode.ForRead) as AttributeDefinition;
if (attDef != null)
{
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
br.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
tr.AddNewlyCreatedDBObject(br, true);
}
}
else
{
string sourceFileName = string.Concat(strPath, strBlock, ".dwg");
try
{
using (Database tempDb = new Database(false, true))
{
tempDb.ReadDwgFile(sourceFileName, FileShare.Read, true, null);
db.Insert(strBlock, tempDb, false);
}
ObjectId tmpBlockId = bt[strBlock];
BlockTableRecord blkDefRecord = (BlockTableRecord)tr.GetObject(tmpBlockId, OpenMode.ForRead);
//BlockReference br = new BlockReference(ppr.Value, tmpBlockId);
BlockReference br = new BlockReference(insPt, tmpBlockId);
CurrentSpaceBTR.AppendEntity(br);
if (blkDefRecord.HasAttributeDefinitions)
{
foreach (ObjectId idAtt in blkDefRecord)
{
AttributeDefinition attDef = tr.GetObject(idAtt, OpenMode.ForRead) as AttributeDefinition;
if (attDef != null)
{
AttributeReference attRef = new AttributeReference();
attRef.SetAttributeFromBlock(attDef, br.BlockTransform);
attRef.SetPropertiesFrom(attDef);
br.AttributeCollection.AppendAttribute(attRef);
tr.AddNewlyCreatedDBObject(attRef, true);
}
}
tr.AddNewlyCreatedDBObject(br, true);
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
ed.WriteMessage(e.Message);
}
}
#endregion
#region EditAttributes
if (bt.Has(strBlock))
{
foreach (ObjectId entId in CurrentSpaceBTR)
{
Entity ent = tr.GetObject(entId, OpenMode.ForRead) as Entity;
if (ent != null)
{
BlockReference br = ent as BlockReference;
if (br != null)
{
BlockTableRecord bd =
(BlockTableRecord)tr.GetObject(
br.BlockTableRecord,
OpenMode.ForRead
);
if (bd.Name.ToUpper() == strBlock)
{
foreach (
ObjectId arId in br.AttributeCollection
)
{
DBObject obj = tr.GetObject(arId, OpenMode.ForRead);
AttributeReference ar = obj as AttributeReference;
if (ar != null)
{
ar.UpgradeOpen();
switch (ar.Tag)
{
case "TITLE1":
ar.TextString = idAttlist["TITLE1"];
break;
case "TITLE2":
ar.TextString = idAttlist["TITLE2"];
break;
case "TECH":
ar.TextString = idAttlist["TECH"];
break;
case "TECHDATE":
ar.TextString = idAttlist["TECHDATE"];
break;
}
ar.DowngradeOpen();
}
}
}
}
}
}
}
#endregion
tr.Commit();
}
}
}
}