/// <summary> /// Creates BlockReference from the block with the given name. /// Throws ArgumentException if block with such a name does not exist. /// </summary> /// <param name="blockName"></param> /// <param name="insertionPoint"></param> /// <param name="space">The model space or some of the paper spaces</param> /// <param name="blockTable">The block table of the associated drawing in the helper.</param> /// <returns>The BlockReference of the block</returns> private BlockReference CreateBlockReference(string blockName, UnitsValue sourceBlockMeasurementUnits, Point3d insertionPoint, BlockTableRecord space, BlockTable blockTable) { Matrix3d ucs = _ed.CurrentUserCoordinateSystem; BlockReference newBlockReference; //All open objects opened during a transaction are closed at the end of the transaction. using (Transaction transaction = _db.TransactionManager.StartTransaction()) { blockTable.UpgradeOpen(); // If the DWG already contains this block definition we will create a block reference and not a copy of the same definition if (!blockTable.Has(blockName)) { _logger.Error(MethodBase.GetCurrentMethod().DeclaringType.FullName + "." + MethodBase.GetCurrentMethod().Name + " : Block with name '" + blockName + "' does not exist."); transaction.Abort(); throw new ArgumentException("Block with name '" + blockName + "' does not exist."); } BlockTableRecord sourceBlockTableRecord = (BlockTableRecord)transaction.GetObject(blockTable[blockName], OpenMode.ForRead); newBlockReference = new BlockReference(insertionPoint, sourceBlockTableRecord.ObjectId); var converter = new MeasurementUnitsConverter(); var scaleFactor = converter.GetScaleRatio(sourceBlockMeasurementUnits, blockTable.Database.Insunits); _ed.CurrentUserCoordinateSystem = Matrix3d.Identity; newBlockReference.TransformBy(ucs); _ed.CurrentUserCoordinateSystem = ucs; newBlockReference.ScaleFactors = new Scale3d(scaleFactor); space.UpgradeOpen(); space.AppendEntity(newBlockReference); transaction.AddNewlyCreatedDBObject(newBlockReference, true); AttributeCollection atcoll = newBlockReference.AttributeCollection; foreach (ObjectId subid in sourceBlockTableRecord) { var entity = (Entity)subid.GetObject(OpenMode.ForRead); var attributeDefinition = entity as AttributeDefinition; if (attributeDefinition != null) { var attributeReference = new AttributeReference(); attributeReference.SetPropertiesFrom(attributeDefinition); attributeReference.Visible = attributeDefinition.Visible; attributeReference.SetAttributeFromBlock(attributeDefinition, newBlockReference.BlockTransform); attributeReference.HorizontalMode = attributeDefinition.HorizontalMode; attributeReference.VerticalMode = attributeDefinition.VerticalMode; attributeReference.Rotation = attributeDefinition.Rotation; attributeReference.Position = attributeDefinition.Position + insertionPoint.GetAsVector(); attributeReference.Tag = attributeDefinition.Tag; attributeReference.FieldLength = attributeDefinition.FieldLength; attributeReference.TextString = attributeDefinition.TextString; attributeReference.AdjustAlignment(_db); atcoll.AppendAttribute(attributeReference); transaction.AddNewlyCreatedDBObject(attributeReference, true); } } transaction.Commit(); } _ed.Regen(); return(newBlockReference); }