/// <summary>
///
/// </summary>
/// <param name="position">Точка вставки блока</param>
/// <param name="defBlkTabRecId">ObjectId описания блока</param>
/// <param name="ownBlkTabRecId">ObjectId объекта, в который вставляется блок</param>
/// <param name="layerId">ObjectId слоя</param>
/// <param name="scale">Масштаб</param>
/// <param name="dynamicPropsValues">Значения динамических параметров</param>
/// <param name="attributesValues">Значения атрибутов</param>
/// <returns>ObjectId вставки блока или ObjectId.Null при неудаче</returns>
public static ObjectId CreateBlockReference
(Point3d position, ObjectId defBlkTabRecId,
ObjectId ownBlkTabRecId, ObjectId layerId,
double scale = 1.0,
Dictionary<string, object> dynamicPropsValues = null,
Dictionary<string, string> attributesValues = null)
{
ObjectId
blkRefId = ObjectId.Null,
blkTabRecId = defBlkTabRecId;
// Создаем блок, настраиваем основные свойства, вставляем в текущее пространство
using (BlockReference bRef = new BlockReference(position, blkTabRecId))
{
bRef.ScaleFactors = bRef.ScaleFactors.MultiplyBy(scale);
if (layerId.CheckObjectIdFor<LayerTableRecord>())
{
bRef.LayerId = layerId;
}
using (BlockTableRecord owner
= ownBlkTabRecId.SafeOpen<BlockTableRecord>(true))
{
if (owner != null)
{
blkRefId = owner.AppendEntity(bRef);
}
}
if (blkRefId.IsValid)
{
// Задаем динамические параметры
if (bRef.IsDynamicBlock
&& dynamicPropsValues != null
&& dynamicPropsValues.Count > 0)
{
//Задаем значения динамических свойств
foreach (DynamicBlockReferenceProperty dynProp
in bRef.DynamicBlockReferencePropertyCollection)
{
if (!dynProp.ReadOnly
&& dynamicPropsValues.ContainsKey(dynProp.PropertyName))
{
IterisExceptions.TryAction(() => dynProp.Value
= dynamicPropsValues[dynProp.PropertyName]);
}
}
blkTabRecId = bRef.BlockTableRecord;
}
}
}
if (!blkRefId.CheckObjectIdFor<BlockReference>())
return ObjectId.Null;
ObjectId[] attDefIds = null;
using (BlockTableRecord bRec
= blkTabRecId.SafeOpen<BlockTableRecord>())
{
if (bRec != null)
{
attDefIds = bRec.GetAttributes().ToArray();
}
}
if (attDefIds != null && attDefIds.Count() > 0)
{
// Создаем атрибуты
Dictionary<ObjectId, ObjectId>
attDefRefDict = new Dictionary<ObjectId, ObjectId>();
Matrix3d? blkTransform = null;
using (BlockReference bRef
= blkRefId.SafeOpen<BlockReference>(true))
{
if (bRef != null)
{
blkTransform = bRef.BlockTransform;
foreach (ObjectId attDefId in attDefIds)
{
using (AttributeDefinition attDef
= attDefId.SafeOpen<AttributeDefinition>())
{
if (attDef != null)
{
using (AttributeReference attRef = new AttributeReference())
{
attRef.SetAttributeFromBlock(attDef, blkTransform.Value);
ObjectId attRefId = bRef.AttributeCollection.AppendAttribute(attRef);
attDefRefDict[attDefId] = attRefId;
if (!attDef.HasFields
&& attributesValues != null
&& attributesValues.ContainsKey(attDef.Tag))
{
attRef.TextString = attributesValues[attDef.Tag];
}
}
}
}
}
}
}
Database db = blkRefId.Database;
if (blkTransform.HasValue)
{
using (WorkDbTmpSwitcher switcher = new WorkDbTmpSwitcher(db))
{
// Настраиваем атрибуты после вставки
foreach (KeyValuePair<ObjectId, ObjectId> pair in attDefRefDict)
{
using (AttributeDefinition attDef
= pair.Key.SafeOpen<AttributeDefinition>())
using (AttributeReference attRef
= pair.Value.SafeOpen<AttributeReference>(true))
{
if (attDef == null || attRef == null) continue;
if (attDef.HasFields)
{
attRef.SetAttributeFromBlock(attDef, blkTransform.Value);
}
if (attRef.Database.Equals(db))
{
attRef.AdjustAlignment(db);
}
}
}
}
}
}
return blkRefId;
}