- 积分
- 1386
- 明经币
- 个
- 注册时间
- 2023-2-2
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
寫了個顏色格式刷的例子,大佬們還有哪些地方需要改?
- private static string GetPointOptionsMessage() => "\n选择图元或 [字典(D)/随层(S)] ";
- private static readonly List<string> ignorePatterName = ["SOLID", "ANSI37", "ANSI31"];
- private static readonly List<string> ignoreBlockName = ["Elevation", "ANSI37", "ANSI31"];
- private static readonly Dictionary<List<int>, (string, short, short)> reflectDic = new(){
- { [3,7], (layers.DimLayer_Name,layers.DimLayer_Color,256) },
- { [1], (layers.DimLayer_Name,layers.DimLayer_Color,4) }
- };
- [CommandMethod("B`")]
- public static void BBest()
- {
- PromptSelectionOptions entOpts = new()
- {
- MessageForAdding = GetPointOptionsMessage()
- };
- PromptSelectionResult r1 = Env.Editor.GetSelection(entOpts);
- if (r1.Status == PromptStatus.OK)
- {
- using DBTrans tr = new();
- List<ObjectId> entWillBeSelected = [];
- var ids = r1.Value.GetObjectIds().ToList();
- for (int i = ids.Count - 1; i >= 0; i--)
- {
- var ent = (Entity)tr.GetObject(ids);
- if (ent is Dimension dim)
- {
- if (dim.Layer == layers.DimLayer_Name && dim.ColorIndex == 256)
- continue;
- using (dim.ForWrite())
- dim.SetProperty("0", layers.DimLayer_Name, layers.DimLayer_Color);
- entWillBeSelected.Add(ent.ObjectId);
- }
- if (ent is Hatch hatch)
- {
- if (!ignorePatterName.Contains(hatch.PatternName))
- continue;
- using (hatch.ForWrite())
- hatch.SetProperty("0", layers.EL_250_Name, layers.EL_250_Color);
- entWillBeSelected.Add(ent.ObjectId);
- }
- if (ent is BlockReference brf)
- {
- #if Debug
- Env.Printl($"块名:{brf.Name} ,图块:{brf.Layer}");
- #endif
- var btrId = brf.BlockTableRecord;
- var btr = (BlockTableRecord)tr.GetObject(btrId);
- if (btr.IsFromExternalReference)
- {
- Env.Printl($"块名:{brf.Name} ,来自外部块!");
- continue;
- }
- foreach (var entId in btr)
- {
- //Env.Printl($"dxfname: {entId.ObjectClass.DxfName}");
- var bent = (Entity)tr.GetObject(entId);
- //using (bent.ForWrite())
- // bent.ColorIndex = 1;
- if (bent is Line or Polyline or Circle)
- {
- if (NewMethod(tr, entWillBeSelected, bent))
- entWillBeSelected.Add(ent.ObjectId);
- entWillBeSelected.Remove(bent.ObjectId);
- }
- }
- tr.Editor?.Regen(); // 更新图纸
- }
- if (ent is Line or Polyline or Circle)
- NewMethod(tr, entWillBeSelected, ent);
- }
- tr.Editor?.SetImpliedSelection(entWillBeSelected.ToArray());
- }
- }
- private static bool NewMethod(DBTrans tr, List<ObjectId> entWillBeSelected, Entity ent, bool isChanged = false)
- {
- if (ent is Line or Polyline or Circle)
- {
- var color = tr.GetEntityColor(ent);
- foreach (var key in reflectDic.Keys)
- if (key.Contains(color))
- {
- #if Debug
- Env.Printl($"类型:{ent.GetType().Name} ,包含:{color}");
- var value = reflectDic[key];
- Env.Printl($"字典:{value.Item1}、{value.Item2}、{value.Item3}");
- #endif
- if (ent.Layer != value.Item1)
- {
- isChanged = true;
- entWillBeSelected.Add(ent.ObjectId);
- using (ent.ForWrite())
- {
- ent.SetProperty("0", value.Item1, value.Item2);
- ent.ColorIndex = value.Item3;
- };
- }
- }
- }
- return isChanged;
- }
- namespace ArrowTools;
- public static class AttributeBrushToos
- {
- /// <summary>
- /// 获取实体的真实颜色值
- /// </summary>
- /// /// <remarks>
- /// 备注:<br/>
- /// 0x01 颜色Bylayer:返回图层颜色值<br/>
- /// 0x02 颜色不随层:返回entity的颜色值<br/>
- /// </remarks>
- /// <param name="tr"></param>
- /// <param name="ent"></param>
- /// <returns>颜色真实值</returns>
- public static int GetEntityColor<T>(this DBTrans tr, T ent) where T : Entity
- {
- int colorindex = ent.ColorIndex;
- if (colorindex == 256)
- {
- var layerId = tr.LayerTable[ent.Layer];
- var layer = (LayerTableRecord)tr.GetObject(layerId);
- colorindex = layer.Color.ColorIndex;
- }
- return colorindex;
- }
- }
|
|