public static long DrawBlockWithAttributes(string block,
AcadGeometry.Point3d position,
string layer,
double faktor,
double drehung,
Dictionary<String, String> attribute,
bool useStartOpenClose)
{
AcadDbServices.Database dwg = AcadDbServices.HostApplicationServices.WorkingDatabase;
AcadDbServices.Transaction trans = null;
long handle = -1L;
using (trans = useStartOpenClose ? dwg.TransactionManager.StartOpenCloseTransaction() : dwg.TransactionManager.StartTransaction())
{
/*
* blockTable wird nicht in einen using-Block genommen,
* weil unter BricsCAD mit dem Disposen() der Transaktion auch ein darin allokiertes Objekt
* seine Gültigkeit verliert.
* Siehe https://www.bricsys.com/bricscad/help/en_US/CurVer/DevRef/index.html?page=source%2FdotNET_overview.htm
* */
AcadDbServices.BlockTable blockTable = null;
try
{
if (String.IsNullOrEmpty(block))
throw new ArgumentException
("Blockname darf nicht leer sein");
AcadDbServices.ObjectId blockTableId = dwg.BlockTableId;
blockTable = (AcadDbServices.BlockTable)trans.GetObject(blockTableId, AcadDbServices.OpenMode.ForRead, true);
if (!blockTable.Has(block))
/* Inserts the block and calls Commit() on the transaction parameter */
InsertBlock(block, dwg, trans);
if (trans.IsDisposed)
{
trans = useStartOpenClose ? dwg.TransactionManager.StartOpenCloseTransaction() : dwg.TransactionManager.StartTransaction();
#if BRX_APP
blockTable = (AcadDbServices.BlockTable)trans.GetObject(blockTableId, AcadDbServices.OpenMode.ForRead, true);
#endif
}
using (var modelSpace = (AcadDbServices.BlockTableRecord)trans.GetObject(blockTable[AcadDbServices.BlockTableRecord.ModelSpace], AcadDbServices.OpenMode.ForWrite))
using (var blockDefinition = (AcadDbServices.BlockTableRecord)trans.GetObject(blockTable[block], AcadDbServices.OpenMode.ForRead))
{
if (!blockDefinition.HasAttributeDefinitions)
CreateBlockDefinitionWithAttributes(trans, block, attribute.Keys);
using (var blockReference
= new AcadDbServices
.BlockReference(position, blockDefinition
.ObjectId)) {
modelSpace.AppendEntity(blockReference);
trans.AddNewlyCreatedDBObject(blockReference, true);
handle = blockReference.Handle.Value;
blockReference
.ScaleFactors = new AcadGeometry
.Scale3d(faktor
); blockReference.Rotation = drehung;
blockReference.Layer = layer;
foreach (AcadDbServices.ObjectId oid in blockDefinition)
{
var attributeDefinition = trans.GetObject(oid, AcadDbServices.OpenMode.ForRead) as AcadDbServices.AttributeDefinition;
if (attributeDefinition == null || attributeDefinition.Constant)
continue;
using (var attributeReference
= new AcadDbServices
.AttributeReference()) {
blockReference.AttributeCollection.AppendAttribute(attributeReference);
trans.AddNewlyCreatedDBObject(attributeReference, true);
//attributeReference.SetDatabaseDefaults();
attributeReference.SetAttributeFromBlock(attributeDefinition, blockReference.BlockTransform);
attributeReference.TextString = attribute[attributeDefinition.Tag];
}
}
}
trans.Commit();
return handle;
}
}
catch (Exception e)
{
// ... do something
}
finally
{
blockTable.Dispose();
}
}
}