Hi mzakiralam,
thank your for your prompt reply my code is a bit complicated. I have tried to simplify the logic as below:
public void AddBlocksToDrawing()
{
try
{
Document doc = acad_App.DocumentManager.MdiActiveDocument;
Database db = doc.Database;
DocumentBroker broker = DocumentBroker.GetInstance();
LinkedList<BlockReference> legendLines = broker.GetLegendLines(doc);
using (DocumentLock docLock = doc.LockDocument())
{
using (Transaction trans = db.TransactionManager.StartTransaction())
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
ExplodeNestedBlocks(db, trans);
ClearOldBlocks(trans, btr);
try
{
UpdateSymbolIndex(updateSymbols, btr, trans);
Point3d pos = new Point3d(0.0, 0.0, 0.0);
foreach (string category in this.categories)
{
List<string> subCategories = new List<string>();
subCategories = GetSubCategories(category, btr, trans);
foreach (string subCategory in subCategories)
{
if (doc == null || db == null)
{
MessageDialog.Show("Error", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
List<BlockReference> allSymbols = GetAllSubCatSymbols(category, btr, trans);
List<BlockReference> symbols = GetAllSubCatSymbols(category, btr, trans);
pos = AddNewBlock(pos,"block1", trans);
foreach (BlockReference symbol in this.bars)
{
string count = CountSymbol(symbol, allSymbols);
pos = AddBlockLine(symbol, pos, trans);
}
foreach (BlockReference symbol in this.symbols)
{
string count = CountSymbol(symbol, allSymbols);
pos = AddBlockLine(symbol, pos, trans,);
}
pos = AddNewBlock(pos, ,"block2", trans);
}
pos = AddNewBlock(pos, ,"block3", trans);
}
AddNewBlock(pos,,"block4", trans);
UpdateAttributes(btr, trans);
}
catch (System.Exception ex)
{
MessageDialog.Show("Error"+ ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
btr.Close();
bt.Close();
trans.Commit();
}
}
}
catch (System.Exception ex)
{
MessageDialog.Show(ex.Message, "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
public void ExplodeNestedBlocks(Database db, Transaction trans)
{
BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);
foreach (ObjectId id in btr)
{
BlockReference itemBlock = trans.GetObject(id, OpenMode.ForWrite) as BlockReference;
if (itemBlock != null && bla..)
{
BlockTableRecord nestedBtr = itemBlock.BlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId objId in nestedBtr)
{
BlockReference nestedItemBlock = trans.GetObject(objId, OpenMode.ForRead) as BlockReference;
if (nestedItemBlock != null)
{
try
{
itemBlock.ExplodeToOwnerSpace();
itemBlock.Erase(true);
}
catch (System.Exception ex)
{
MessageDialog.Show("Error", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
}
}
}
}
}
}
private void ClearOldBlocks(Transaction trans, BlockTableRecord btr)
{
foreach (ObjectId id in btr)
{
BlockReference itemBlock = trans.GetObject(id, OpenMode.ForRead) as BlockReference;
if (itemBlock != null)
{
AttributeDefinition symbolType = AttributeUtil.GetAttributeDefinition(itemBlock, Const.TYPE);
if (bla..)
{
trans.GetObject(id, OpenMode.ForWrite).Erase();
}
}
}
}
private void UpdateSymbolIndex(Dictionary<string, int> updateSymbols, BlockTableRecord btr, Transaction trans)
{
foreach (ObjectId id in btr)
{
BlockReference itemBlock = trans.GetObject(id, OpenMode.ForRead) as BlockReference;
if (itemBlock != null)
{
AttributeDefinition attrType = AttributeUtil.GetAttributeDefinition(itemBlock, Const.TYPE);
String symbolPrefix = AttributeUtil.GetConfigVar(Const.SYMBOL_PREFIX);
if (bla..)
{
try
{
string newIndex = "X";
BlockTableRecord srcBtr = trans.GetObject(itemBlock.BlockTableRecord, OpenMode.ForRead) as BlockTableRecord;
foreach (ObjectId sID in srcBtr)
{
MText mtext = trans.GetObject(sID, OpenMode.ForWrite) as MText;
if (mtext != null)
{
string content = mtext.Contents;
mtext.Contents = content + newIndex;
mtext.Close();
break;
}
}
srcBtr.Close();
}
catch (System.Exception ex)
{
MessageDialog.Show("Ein Fehler ist aufgetreten:" + Const.NEW_LINE + ex.ToString(), "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
}
}
}
}
private Point3d AddNewBlock(Point3d pos, String blockname, Transaction trans)
{
try
{
BlockReference newBlock = InsertBlockReference("Legend_"+blockname, blockname, new Point3d(0.0, 0.0, 0.0), trans);
try
{
double[] posData = GetProjDataPos();
newBlock.Position = new Point3d(posData[0], posData[1], 0.0);
Extents3d extents = newBlock.GeometricExtents;
double height = Math.Abs(extents.MaxPoint.Y - extents.MinPoint.Y);
pos = new Point3d(newBlock.Position.X, newBlock.Position.Y + height, 0.0);
}
catch (System.Exception ex)
{
MessageDialog.Show("Position konnte nicht ermittelt werden. " + ex.ToString(), "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
return pos;
}
}
}
catch (System.Exception ex)
{
MessageDialog.Show("Ein Fehler ist aufgetreten:" + Const.NEW_LINE + ex.ToString(), "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
return pos;
}
return pos;
}
private BlockReference InsertBlockReference(string blockName, string templateName, Point3d position, Transaction trans)
{
string inputFile = string.Empty;
try
{
inputFile = Path.Combine(Const.TEMPLATE_PATH, templateName + ".dwg");
}
catch (System.Exception)
{
throw new System.Exception("Der angegebene Pfad wurde nicht gefunden.");
}
Document currentFileDoc = acad_App.DocumentManager.MdiActiveDocument;
Database currentFileDB = currentFileDoc.Database;
Database inputFileDB = new Database(false, true);
try
{
inputFileDB.ReadDwgFile(inputFile, FileOpenMode.OpenForReadAndReadShare, false, null);
}
catch (System.Exception)
{
throw new System.Exception("Die Eingabedatei existiert nicht oder ist nicht verfügbar: " + inputFile);
}
BlockTable bt = (BlockTable)trans.GetObject(currentFileDB.BlockTableId, OpenMode.ForRead);
BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
ObjectId objId = currentFileDB.Insert(blockName, inputFileDB, true);
BlockReference newBlock = new BlockReference(position, objId);
btr.AppendEntity(newBlock);
trans.AddNewlyCreatedDBObject(newBlock, true);
return newBlock;
}
private List<string> GetSubCategories(string category, BlockTableRecord btr, Transaction trans)
{
List<string> subCategories = new List<string>();
foreach (ObjectId id in btr)
{
BlockReference itemBlock = trans.GetObject(id, OpenMode.ForRead) as BlockReference;
if (itemBlock != null)
{
if (bla..)
{
try
{
Dictionary<string, string> attrValues = AttributeUtil.GetAllAttributes(itemBlock);
if (bla..)
{
subCategories.Add(subCategory);
}
}
catch (System.Exception ex)
{
MessageDialog.Show("Ein Fehler ist aufgetreten:" + Const.NEW_LINE + ex.ToString(), "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
return null;
}
}
}
}
return subCategories;
}
private List<BlockReference> GetAllSubCatSymbols(string category, string subCatetory, BlockTableRecord btr, Transaction trans)
{
List<BlockReference> symbols = new List<BlockReference>();
foreach (ObjectId id in btr)
{
BlockReference itemBlock = trans.GetObject(id, OpenMode.ForRead) as BlockReference;
if (itemBlock != null)
{
try
{
if (bla...)
{
symbols.Add(itemBlock);
}
}
catch (System.Exception ex)
{
MessageDialog.Show("Ein Fehler ist aufgetreten:" + Const.NEW_LINE + ex.ToString(), "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
return null;
}
}
}
return symbols;
}
private void AddBlockLine(BlockReferencwe symbol, Point3d position, Transaction trans))
{
.
.
.
.
BlockReference newBlock = InsertBlockReference("LegendLine", "template2",position, trans);
CopySymbol(symbol,newBlock, trans);
}
private void UpdateAttributes(BlockTableRecord btr, Transaction trans)
{
foreach (ObjectId id in btr)
{
BlockReference itemBlock = trans.GetObject(id, OpenMode.ForRead) as BlockReference;
if (itemBlock != null)
{
if (bla..)
{
AttributeUtil.SetAttribute(itemBlock, "ATT1", "0", trans);
AttributeUtil.SetAttribute(itemBlock, "ATT2", "0", trans);
AttributeUtil.SetAttribute(itemBlock, "ATT3", "0", trans);
AttributeUtil.SetAttribute(itemBlock, "ATT4", "0", trans);
}
}
}
}
public static void SetAttribute(BlockReference block, string attrTag, string attrValue, Transaction trans)
{
if (!string.IsNullOrEmpty(attrTag) && !string.IsNullOrEmpty(attrValue))
{
try
{
block.UpgradeOpen();
}
catch { }
AttributeDefinition attrDe = AttributeUtil.GetAttributeDefinition(block, attrTag);
if (attrDe != null)
{
if (!attrDe.Constant)
{
AttributeReference attrRe = AttributeUtil.GetAttribute(block, attrTag);
if (attrRe != null)
attrRe.TextString = attrValue;
else
{
using (attrRe = new AttributeReference())
{
attrRe.SetAttributeFromBlock(attrDe, block.BlockTransform);
attrRe.Visible = attrDe.Visible;
Point3d basePoint = attrDe.Position;
basePoint += block.Position.GetAsVector();
attrRe.Position = basePoint;
attrRe.Height = attrDe.Height;
attrRe.Rotation = attrDe.Rotation;
attrRe.FieldLength = attrDe.FieldLength;
if (attrDe.IsMTextAttributeDefinition)
{
attrRe.IsMTextAttribute = true;
attrRe.TextString = attrValue;
attrRe.UpdateMTextAttribute();
}
else
attrRe.TextString = attrValue;
block.AttributeCollection.AppendAttribute(attrRe);
trans.AddNewlyCreatedDBObject(attrRe, true);
}
}
}
}
}
}
private void CopySymbol(BlockReference symbol, BlockReference dstBlock, Transaction trans)
{
BlockTableRecord dstBtr = trans.GetObject(dstBlock.BlockTableRecord, OpenMode.ForWrite) as BlockTableRecord;
BlockReference copy = (BlockReference)symbol.Clone();
AttributeDefinition attrSymbolPos = AttributeUtil.GetAttributeDefinition(dstBlock, Const.SYMBOLPOS);
if (attrSymbolPos == null)
{
throw new System.Exception("Fehler: Symbolposition ist nicht definiert.");
}
copy.Position = attrSymbolPos.Position;
dstBtr.AppendEntity(copy);
trans.AddNewlyCreatedDBObject(copy, true);
}
Thank you in advance !!!
itmtha01.