ok, I am having a problem, which is probably something simple I am overlooking.
[CommandMethod("TEPWireLabel")]
public static void WireLabel()
{
Database db = HostApplicationServices.WorkingDatabase;
Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
PromptKeywordOptions pko = new PromptKeywordOptions("");
pko.Message = "\nJustification and Quanity";
pko.Keywords.Add("L1");
pko.Keywords.Add("L2");
pko.Keywords.Add("L3");
pko.Keywords.Add("L4");
pko.Keywords.Add("L5");
pko.Keywords.Add("L6");
pko.Keywords.Add("R1");
pko.Keywords.Add("R2");
pko.Keywords.Add("R3");
pko.Keywords.Add("R4");
pko.Keywords.Add("R5");
pko.Keywords.Add("R6");
pko.Keywords.Default = "L3";
pko.AllowNone = false;
PromptResult pkr = doc.Editor.GetKeywords(pko);
int intOsnapMode = System.Convert.ToInt32(Application.GetSystemVariable("OSMODE"));
Application.SetSystemVariable("OSMODE", 1);
PromptPointOptions ppo = new PromptPointOptions("\nSelect Point: ");
PromptPointResult ppr = ed.GetPoint(ppo);
if (ppr.Status != PromptStatus.OK)
{
ed.WriteMessage("\nPoint not selected, Error!");
}
string strPath = @"\\autocad\AcadCustomizations\SYMBOLS\";
string strBlock = pkr.StringResult + "DESC";
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;
if (bt.Has(strBlock))
{
ObjectId tmpBlockId = bt[strBlock];
BlockTableRecord blkDefRecord = (BlockTableRecord)tr.GetObject(tmpBlockId, OpenMode.ForRead);
BlockReference br = new BlockReference(ppr.Value, 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 = @"\\autocad\AcadCustomizations\SYMBOLS\L3DESC.dwg";
try
{
using ( Database tempDb = new Database( false, true ))
{
tempDb.ReadDwgFile(sourceFileName, FileShare.Read, true, null);
db.Insert(strBlock, tempDb, false);
}
}
catch (Autodesk.AutoCAD.Runtime.Exception e)
{
ed.WriteMessage(e.Message);
}
}
tr.Commit();
}
Application.SetSystemVariable("OSMODE", intOsnapMode);
}
If I manually insert the block first, everything works as expected.
If I Do Not insert the block first, it goes and gets the block, but doesn't insert it. (it might be there, just not visable) It does define the block in the BT though, because I can manually insert it after running through the code.
On a side note, I thought I saw a thread about direct cast VS. using AS, any preference or situations where one works and the other doesn't?