Thanks for the reply, but I am still having no luck. I even added some other formatting that I found in other examples.
Here is the complete method I am using:
// Mostly from T.Willey @ the swamp
private static void InsertBlock(Database mainDb, string loName, string blkName, Point3d insPt, string layer,System.Collections.Generic.Dictionary<string, string> dicAttValues, double rotation = 0)
{
using (Transaction Trans = mainDb.TransactionManager.StartTransaction())
{
DBDictionary LoDict = Trans.GetObject(mainDb.LayoutDictionaryId, OpenMode.ForRead) as DBDictionary;
foreach (DictionaryEntry de in LoDict)
{
if (string.Compare((string)de.Key, loName, true).Equals(0))
{
Layout Lo = Trans.GetObject((ObjectId)de.Value, OpenMode.ForWrite) as Layout;
BlockTable BlkTbl = Trans.GetObject(mainDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord LoRec = Trans.GetObject(Lo.BlockTableRecordId, OpenMode.ForRead) as BlockTableRecord;
ObjectId BlkTblRecId = GetNonErasedTableRecordId(BlkTbl.Id, blkName);
if (BlkTblRecId.IsNull)
{
string BlkPath = HostApplicationServices.Current.FindFile(blkName + ".dwg", mainDb, FindFileHint.Default);
if (string.IsNullOrEmpty(BlkPath))
return;
BlkTbl.UpgradeOpen();
using (Database tempDb = new Database(false, true))
{
tempDb.ReadDwgFile(BlkPath, FileShare.Read, true, null);
mainDb.Insert(blkName, tempDb, false);
}
BlkTblRecId = GetNonErasedTableRecordId(BlkTbl.Id, blkName);
}
LoRec.UpgradeOpen();
BlockReference BlkRef = new BlockReference(insPt, BlkTblRecId);
BlkRef.Rotation = rotation;
LayerTable lt = (LayerTable)Trans.GetObject(mainDb.LayerTableId, OpenMode.ForRead);
if (lt.Has(layer))
{
ObjectId lid = lt[layer];
BlkRef.SetLayerId(lid, false);
}
LoRec.AppendEntity(BlkRef);
Trans.AddNewlyCreatedDBObject(BlkRef, true);
BlockTableRecord BlkTblRec = Trans.GetObject(BlkTblRecId, OpenMode.ForRead) as BlockTableRecord;
if (BlkTblRec.HasAttributeDefinitions)
{
foreach (ObjectId objId in BlkTblRec)
{
AttributeDefinition attDef = Trans.GetObject(objId, OpenMode.ForRead) as AttributeDefinition;
if (attDef != null)
{
AttributeReference attRef = new AttributeReference();
attRef.SetPropertiesFrom(attDef);
attRef.Visible = attDef.Visible;
attRef.SetAttributeFromBlock(attDef, BlkRef.BlockTransform);
attRef.HorizontalMode = attDef.HorizontalMode;
attRef.VerticalMode = attDef.VerticalMode;
attRef.Rotation = attDef.Rotation;
attRef.TextStyleId = attDef.TextStyleId;
attRef.Position = attDef.Position + insPt.GetAsVector();
attRef.Tag = attDef.Tag;
attRef.FieldLength = attDef.FieldLength;
if (dicAttValues.ContainsKey(attDef.Tag))
attRef.TextString = dicAttValues[attDef.Tag].ToString();
else
attRef.TextString = attDef.TextString;
attRef.AdjustAlignment(mainDb);
BlkRef.AttributeCollection.AppendAttribute(attRef);
Trans.AddNewlyCreatedDBObject(attRef, true);
}
}
}
Trans.Commit();
}
}
}
}
If I insert the block manually after running this it inserts just fine so I am pretty sure it's not the block.
This code came from here (The Swamp), I have tweeked it a little to suit my needs.
Can you see where I am going wrong?
Thanks,
Tim