/// <summary> /// Чтение свойств из примитива и запись их в стиль примитива /// </summary> /// <param name="style">Стиль примитива</param> /// <param name="entity">Интеллектуальный примитив</param> /// <param name="blockReference">Вставка блока, представляющая интеллектуальный примитив в AutoCAD</param> public static void GetPropertiesFromEntity(this IntellectualEntityStyle style, IntellectualEntity entity, BlockReference blockReference) { var entityType = entity.GetType(); foreach (var propertyInfo in entityType.GetProperties()) { var attribute = propertyInfo.GetCustomAttribute <EntityPropertyAttribute>(); if (attribute != null && attribute.Name != "Style") { if (attribute.PropertyScope != PropertyScope.PaletteAndStyleEditor) { continue; } if (attribute.Name == "LayerName") { style.Properties.Add(new IntellectualEntityProperty( attribute, entityType, blockReference.Layer, ObjectId.Null)); } else if (attribute.Name == "LineType") { style.Properties.Add(new IntellectualEntityProperty( attribute, entityType, blockReference.Linetype, ObjectId.Null)); } else { style.Properties.Add(new IntellectualEntityProperty( attribute, entityType, propertyInfo.GetValue(entity), ObjectId.Null)); } } } }
private void Property_PropertyChanged(object sender, PropertyChangedEventArgs e) { if (_isModifiedFromAutocad) { return; } Overrule.Overruling = false; var intellectualEntityProperty = (IntellectualEntityProperty)sender; try { using (AcadUtils.Document.LockDocument()) { using (var blockReference = _blkRefObjectId.Open(OpenMode.ForWrite, true, true) as BlockReference) { var entityType = _intellectualEntity.GetType(); var propertyInfo = entityType.GetProperty(intellectualEntityProperty.Name); if (propertyInfo != null) { if (intellectualEntityProperty.Name == "Style") { var style = StyleManager.GetStyleByName(entityType, intellectualEntityProperty.Value.ToString()); if (style != null) { _intellectualEntity.ApplyStyle(style, false); } } else if (intellectualEntityProperty.Name == "LayerName") { if (blockReference != null) { blockReference.Layer = intellectualEntityProperty.Value.ToString(); } } else if (intellectualEntityProperty.Name == "LineType") { if (blockReference != null) { blockReference.Linetype = intellectualEntityProperty.Value.ToString(); } } else { propertyInfo.SetValue(_intellectualEntity, intellectualEntityProperty.Value); } _intellectualEntity.UpdateEntities(); _intellectualEntity.GetBlockTableRecordWithoutTransaction(blockReference); using (var resBuf = _intellectualEntity.GetDataForXData()) { if (blockReference != null) { blockReference.XData = resBuf; } } if (blockReference != null) { blockReference.ResetBlock(); } } } } Autodesk.AutoCAD.Internal.Utils.FlushGraphics(); } catch (System.Exception exception) { if (exception.Message != "eOnLockedLayer") { ExceptionBox.Show(exception); } else { OnLockedLayerEventHandler?.Invoke(this, intellectualEntityProperty); } } Overrule.Overruling = true; }
public void CreateAnalogCommand() { var psr = AcadUtils.Editor.SelectImplied(); if (psr.Value == null || psr.Value.Count != 1) { return; } IntellectualEntity intellectualEntity = null; using (AcadUtils.Document.LockDocument()) { using (var tr = new OpenCloseTransaction()) { foreach (SelectedObject selectedObject in psr.Value) { if (selectedObject.ObjectId == ObjectId.Null) { continue; } var obj = tr.GetObject(selectedObject.ObjectId, OpenMode.ForRead); if (obj is BlockReference blockReference) { intellectualEntity = EntityReaderService.Instance.GetFromEntity(blockReference); } } tr.Commit(); } } if (intellectualEntity == null) { return; } var copyLayer = true; var layerActionOnCreateAnalog = MainSettings.Instance.LayerActionOnCreateAnalog; if (layerActionOnCreateAnalog == LayerActionOnCreateAnalog.NotCopy) { copyLayer = false; } else if (layerActionOnCreateAnalog == LayerActionOnCreateAnalog.Ask) { var promptKeywordOptions = new PromptKeywordOptions($"\n{Language.GetItem(Invariables.LangItem, "msg8")}", "Yes No"); var promptResult = AcadUtils.Editor.GetKeywords(promptKeywordOptions); if (promptResult.Status == PromptStatus.OK) { if (promptResult.StringResult == "No") { copyLayer = false; } } else { copyLayer = false; } } var function = TypeFactory.Instance.GetEntityFunctionTypes().FirstOrDefault(f => { var functionName = $"{intellectualEntity.GetType().Name}Function"; var fName = f.GetType().Name; return(fName == functionName); }); function?.CreateAnalog(intellectualEntity, copyLayer); }
/// <summary> /// Применить к указанному интеллектуальному примитиву свойства из стиля /// </summary> /// <param name="entity">Экземпляр примитива</param> /// <param name="style">Стиль</param> /// <param name="isOnEntityCreation">True - применение стиля происходит при создании примитива. /// False - применение стиля происходит при выборе стиля в палитре</param> public static void ApplyStyle(this IntellectualEntity entity, IntellectualEntityStyle style, bool isOnEntityCreation) { var type = entity.GetType(); foreach (var propertyInfo in type.GetProperties()) { var attribute = propertyInfo.GetCustomAttribute <EntityPropertyAttribute>(); if (attribute != null) { var propertyFromStyle = style.Properties.FirstOrDefault(sp => sp.Name == attribute.Name); if (propertyFromStyle != null) { if (attribute.Name == "Scale") { if (isOnEntityCreation) { if (MainSettings.Instance.UseScaleFromStyle) { propertyInfo.SetValue(entity, propertyFromStyle.Value); } else { entity.Scale = AcadUtils.GetCurrentScale(); } } else { propertyInfo.SetValue(entity, propertyFromStyle.Value); } } else if (attribute.Name == "LayerName") { var layerName = propertyFromStyle.Value.ToString(); if (string.IsNullOrEmpty(layerName)) { layerName = Language.GetItem(Invariables.LangItem, "defl"); } if (isOnEntityCreation) { if (MainSettings.Instance.UseLayerFromStyle) { propertyInfo.SetValue(entity, layerName); AcadUtils.SetLayerByName(entity.BlockId, layerName, style.LayerXmlData); } } else { propertyInfo.SetValue(entity, layerName); AcadUtils.SetLayerByName(entity.BlockId, layerName, style.LayerXmlData); } } else if (attribute.Name == "LineType") { var lineType = propertyFromStyle.Value.ToString(); AcadUtils.SetLineType(entity.BlockId, lineType); } else if (attribute.Name == "TextStyle") { var apply = false; if (isOnEntityCreation) { if (MainSettings.Instance.UseTextStyleFromStyle) { apply = true; } } else { apply = true; } if (apply) { var textStyleName = propertyFromStyle.Value.ToString(); if (TextStyleUtils.HasTextStyle(textStyleName)) { propertyInfo.SetValue(entity, textStyleName); } else { if (MainSettings.Instance.IfNoTextStyle == 1 && TextStyleUtils.CreateTextStyle(style.TextStyleXmlData)) { propertyInfo.SetValue(entity, textStyleName); } } } } else { propertyInfo.SetValue(entity, propertyFromStyle.Value); } } } else { if (propertyInfo.Name == "StyleGuid") { propertyInfo.SetValue(entity, style.Guid); } } } }