diff --git a/NFox/.editorconfig b/NFox/.editorconfig new file mode 100644 index 0000000000000000000000000000000000000000..939b35feadd191b8ce11573626791b58b21088b7 --- /dev/null +++ b/NFox/.editorconfig @@ -0,0 +1,10 @@ +[*.cs] + +# CA1305: 指定 IFormatProvider +dotnet_diagnostic.CA1305.severity = none + +# CA1304: 指定 CultureInfo +dotnet_diagnostic.CA1304.severity = none + +# CA1307: 指定 StringComparison +dotnet_diagnostic.CA1307.severity = none diff --git a/NFox/Basal/WPF/RelayCommand.cs b/NFox/Basal/WPF/RelayCommand.cs index 60cbbdecc3a81ba72b0f87bbbaa85f180b947aec..831f479c74fbd9c600b4ae6219c1a30bc2df8c8f 100644 --- a/NFox/Basal/WPF/RelayCommand.cs +++ b/NFox/Basal/WPF/RelayCommand.cs @@ -28,7 +28,7 @@ namespace NFox.WPF /// execute public RelayCommand(Action execute,Func canExecute) { - _execute = execute ?? throw new ArgumentNullException("execute"); + _execute = execute ?? throw new ArgumentNullException(nameof(execute)); _canExecute = canExecute; } diff --git a/NFox/Cad/ExtendMethods/CollectionEx.cs b/NFox/Cad/ExtendMethods/CollectionEx.cs index cf98fd34fc68b936d163d15805ce1bfc45659f46..49d41f600726cbaf4dcaaec1e08505e8789c11b7 100644 --- a/NFox/Cad/ExtendMethods/CollectionEx.cs +++ b/NFox/Cad/ExtendMethods/CollectionEx.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; @@ -17,6 +18,10 @@ namespace NFox.Cad /// 对象id集合 public static ObjectIdCollection ToCollection(this IEnumerable ids) { + if (ids == null) + { + throw new ArgumentNullException(nameof(ids)); + } ObjectIdCollection idCol = new ObjectIdCollection(); foreach (ObjectId id in ids) idCol.Add(id); @@ -31,6 +36,10 @@ namespace NFox.Cad /// 实体集合 public static DBObjectCollection ToCollection(this IEnumerable objs) where T : DBObject { + if (objs == null) + { + throw new ArgumentNullException(nameof(objs)); + } DBObjectCollection objCol = new DBObjectCollection(); foreach (T obj in objs) objCol.Add(obj); diff --git a/NFox/Cad/ExtendMethods/SymbolTableEx.cs b/NFox/Cad/ExtendMethods/SymbolTableEx.cs index 4e1d4534423d386ed6fba1bab5929a4b0966c841..fbfb6d59f224ab869150b4178d9bdad3869a1c59 100644 --- a/NFox/Cad/ExtendMethods/SymbolTableEx.cs +++ b/NFox/Cad/ExtendMethods/SymbolTableEx.cs @@ -19,7 +19,7 @@ namespace NFox.Cad /// 块定义名 /// 图元实体集合 /// 块定义Id - public static ObjectId Add(this SymbolTable table, string blockName, IEnumerable ents) + public static ObjectId Add(this SymbolTableCollection table, string blockName, IEnumerable ents) { return table.Add(blockName, btr => table.Trans.AddEntity(btr, ents)); } @@ -31,7 +31,7 @@ namespace NFox.Cad /// 图元集合 /// 属性定义集合 /// 块定义Id - public static ObjectId Add(this SymbolTable table, string blockName, IEnumerable ents, IEnumerable attdef) + public static ObjectId Add(this SymbolTableCollection table, string blockName, IEnumerable ents, IEnumerable attdef) { return table.Add(blockName, btr => { @@ -50,7 +50,7 @@ namespace NFox.Cad /// 块定义名 /// 是否覆盖 /// 块定义Id - public static ObjectId GetBlockFrom(this SymbolTable table, string fileName, string blockName, bool over) + public static ObjectId GetBlockFrom(this SymbolTableCollection table, string fileName, string blockName, bool over) { return table.GetRecordFrom( @@ -67,7 +67,7 @@ namespace NFox.Cad /// 文件名 /// 是否覆盖 /// 块定义Id - public static ObjectId GetBlockFrom(this SymbolTable table, string fileName, bool over) + public static ObjectId GetBlockFrom(this SymbolTableCollection table, string fileName, bool over) { FileInfo fi = new FileInfo(fileName); string blkdefname = fi.Name; @@ -101,7 +101,7 @@ namespace NFox.Cad /// 线型id /// 线宽 /// 图层id - public static ObjectId Add(this SymbolTable table, string layerName, Color color, ObjectId LinetypeId, LineWeight lineweight) + public static ObjectId Add(this SymbolTableCollection table, string layerName, Color color, ObjectId LinetypeId, LineWeight lineweight) { return table.Add( @@ -123,7 +123,7 @@ namespace NFox.Cad /// 图层名 /// 是否覆盖 /// 图层Id - public static ObjectId GetLayerFrom(this SymbolTable table, string fileName, string layerName, bool over) + public static ObjectId GetLayerFrom(this SymbolTableCollection table, string fileName, string layerName, bool over) { return table.GetRecordFrom( @@ -146,7 +146,7 @@ namespace NFox.Cad /// 大字体名 /// 宽度比例 /// 文字样式Id - public static ObjectId Add(this SymbolTable table, string textStyleName, string smallfont, string bigfont, double xscale) + public static ObjectId Add(this SymbolTableCollection table, string textStyleName, string smallfont, string bigfont, double xscale) { return table.Add( @@ -168,7 +168,7 @@ namespace NFox.Cad /// 字体名 /// 宽度比例 /// 文字样式Id - public static ObjectId Add(this SymbolTable table, string textStyleName, string font, double xscale) + public static ObjectId Add(this SymbolTableCollection table, string textStyleName, string font, double xscale) { return table.Add( @@ -189,7 +189,7 @@ namespace NFox.Cad /// 字体样式名 /// 是否覆盖 /// 文字样式Id - public static ObjectId GetTextStyleFrom(this SymbolTable table, string fileName, string textStyleName, bool over) + public static ObjectId GetTextStyleFrom(this SymbolTableCollection table, string fileName, string textStyleName, bool over) { return table.GetRecordFrom( @@ -203,7 +203,7 @@ namespace NFox.Cad #region DimStyleTable - private static void SetDimstyleData(this SymbolTable table, ObjectId id) + private static void SetDimstyleData(this SymbolTableCollection table, ObjectId id) { var dstr = table.GetRecord(id); table.Database.SetDimstyleData(dstr); @@ -217,7 +217,7 @@ namespace NFox.Cad /// 标注样式名 /// 是否覆盖 /// 注样式记录id - public static ObjectId GetDimStyleFrom(this SymbolTable target, SymbolTable source, string dimStyleName, bool over) + public static ObjectId GetDimStyleFrom(this SymbolTableCollection target, SymbolTableCollection source, string dimStyleName, bool over) { ObjectId id = target.GetRecordFrom(source, dimStyleName, over); @@ -234,7 +234,7 @@ namespace NFox.Cad /// 标注样式名 /// 是否覆盖 /// 注样式记录id - public static ObjectId GetDimStyleFrom(this SymbolTable table, string fileName, string dimStyleName, bool over) + public static ObjectId GetDimStyleFrom(this SymbolTableCollection table, string fileName, string dimStyleName, bool over) { ObjectId id = table.GetRecordFrom( @@ -259,7 +259,7 @@ namespace NFox.Cad /// 线型长度 /// 笔画长度数组 /// 线型id - public static ObjectId Add(this SymbolTable table, string name, string description, double length, double[] dash) + public static ObjectId Add(this SymbolTableCollection table, string name, string description, double length, double[] dash) { return table.Add( name, @@ -287,7 +287,7 @@ namespace NFox.Cad /// 线型名 /// 是否覆盖 /// 线型Id - public static ObjectId GetLinetypeFrom(this SymbolTable table, string fileName, string linetypeName, bool over) + public static ObjectId GetLinetypeFrom(this SymbolTableCollection table, string fileName, string linetypeName, bool over) { return table.GetRecordFrom( diff --git a/NFox/Cad/ResultData/LispList.cs b/NFox/Cad/ResultData/LispList.cs index cc757746a82521b2633dd4c1e379c28da0a4b62b..97fbbb6bc0fa0ecec96cd7269f095ebf7da602bb 100644 --- a/NFox/Cad/ResultData/LispList.cs +++ b/NFox/Cad/ResultData/LispList.cs @@ -13,7 +13,7 @@ namespace NFox.Cad /// /// lisp列表的类 /// - public class LispList : LispData, IEnumerable + public class LispListCollection : LispData, IEnumerable { /// /// LispData 列表 @@ -23,7 +23,7 @@ namespace NFox.Cad /// /// LispList 的父对象 /// - protected LispList _parent; + protected LispListCollection _parent; /// /// 列表的结尾 /// @@ -41,7 +41,7 @@ namespace NFox.Cad /// /// 构造函数 /// - public LispList() + public LispListCollection() : base(LispDataType.ListBegin) { } /// @@ -69,9 +69,13 @@ namespace NFox.Cad /// LispData 对象 public void Add(LispData value) { + if (value == null) + { + throw new ArgumentNullException(nameof(value)); + } _lst.Add(value); if (value.IsList) - ((LispList)value)._parent = this; + ((LispListCollection)value)._parent = this; } private void Add(TypedValue value) @@ -257,18 +261,18 @@ namespace NFox.Cad /// /// ResultBuffer对象 /// LispList对象 - public static LispList FromBuffer(ResultBuffer rb) + public static LispListCollection FromBuffer(ResultBuffer rb) { - LispList lst = new LispList(); + LispListCollection lst = new LispListCollection(); if (rb != null) { - LispList clst = lst; + LispListCollection clst = lst; foreach (TypedValue value in rb) { switch ((LispDataType)value.TypeCode) { case LispDataType.ListBegin: - var slst = new LispList(); + var slst = new LispListCollection(); clst.Add(slst); clst = slst; break; @@ -280,7 +284,7 @@ namespace NFox.Cad case LispDataType.DottedPair: var plst = clst._parent; plst[plst.IndexOf(clst)] = - new LispDottedPair + new LispDottedPairCollection { _lst = clst._lst, _parent = clst._parent @@ -303,7 +307,7 @@ namespace NFox.Cad /// /// lisp 点对表 /// - public class LispDottedPair : LispList + public class LispDottedPairCollection : LispListCollection { /// /// 列表结尾 diff --git a/NFox/Cad/Runtime/AcadVersion.cs b/NFox/Cad/Runtime/AcadVersion.cs index bf60079c49ae2b965e71f1f69b4f8a0125fd47b2..7159717d3bca791ff19ea7a63c75ca89eb0e58a0 100644 --- a/NFox/Cad/Runtime/AcadVersion.cs +++ b/NFox/Cad/Runtime/AcadVersion.cs @@ -1,4 +1,5 @@ using Microsoft.Win32; +using System; using System.Collections.Generic; using System.Reflection; using System.Text.RegularExpressions; @@ -25,10 +26,7 @@ namespace NFox.Cad /// /// 版本号 /// - public double ProgId - { - get { return double.Parse($"{Major}.{Minor}"); } - } + public double ProgId => double.Parse($"{Major}.{Minor}"); /// /// 注册表名称 @@ -93,6 +91,11 @@ namespace NFox.Cad /// cad版本号对象 public static AcadVersion FromApp(object app) { + if (app == null) + { + throw new ArgumentNullException(nameof(app)); + } + string acver = app.GetType() .InvokeMember( diff --git a/NFox/Cad/Runtime/AssemInfo.cs b/NFox/Cad/Runtime/AssemInfo.cs index 496754db9e8cbcca8a67033ebcf4f62640521d2b..8f243481a1a5d377319061ea171d658ad6cabf30 100644 --- a/NFox/Cad/Runtime/AssemInfo.cs +++ b/NFox/Cad/Runtime/AssemInfo.cs @@ -11,26 +11,26 @@ namespace NFox.Cad /// /// 注册名 /// - public string Name; + public string Name { get; set; } /// /// 程序集全名 /// - public string Fullname; + public string Fullname { get; set; } /// /// 程序集路径 /// - public string Loader; + public string Loader { get; set; } /// /// 加载方式 /// - public AssemLoadType LoadType; + public AssemLoadType LoadType { get; set; } /// /// 程序集说明 /// - public string Description; + public string Description { get; set; } } } \ No newline at end of file diff --git a/NFox/Cad/Runtime/AutoRegAssem.cs b/NFox/Cad/Runtime/AutoRegAssem.cs index 2340ecd03a2daf19d3dc8f9b59f02ee98d78121f..6b0118543ec7217c3d2bdb253a8b77b8cd3cd7d0 100644 --- a/NFox/Cad/Runtime/AutoRegAssem.cs +++ b/NFox/Cad/Runtime/AutoRegAssem.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Reflection; using Autodesk.AutoCAD.DatabaseServices; using CadRuntime = Autodesk.AutoCAD.Runtime; - +using System; namespace NFox.Cad { @@ -37,26 +37,24 @@ namespace NFox.Cad /// /// 程序集的路径 /// - public FileInfo Location - { - get { return new FileInfo(Assembly.GetCallingAssembly().Location); } - } + public static FileInfo Location => new FileInfo(Assembly.GetCallingAssembly().Location); /// /// 程序集的目录 /// - public DirectoryInfo CurrDirectory - { - get { return Location.Directory; } - } + public static DirectoryInfo CurrDirectory => Location.Directory; /// /// 获取程序集的目录 /// /// 程序集 /// 路径对象 - public DirectoryInfo GetDirectory(Assembly assem) + public static DirectoryInfo GetDirectory(Assembly assem) { + if (assem == null) + { + throw new ArgumentNullException(nameof(assem)); + } return new FileInfo(assem.Location).Directory; } /// @@ -78,7 +76,7 @@ namespace NFox.Cad #region RegApp - private RegistryKey GetAcAppKey() + private static RegistryKey GetAcAppKey() { string key = HostApplicationServices.Current.MachineRegistryProductRootKey; diff --git a/NFox/Cad/Runtime/CommandInfo.cs b/NFox/Cad/Runtime/CommandInfo.cs index 82cc16cb0f03a045ecd8e270a563fc4be178f87e..80c62ee10f83b59312a7d403f6bd4428fcb14807 100644 --- a/NFox/Cad/Runtime/CommandInfo.cs +++ b/NFox/Cad/Runtime/CommandInfo.cs @@ -11,27 +11,27 @@ namespace NFox.Cad /// /// 全局名称 /// - public string GlobalName; + public string GlobalName { get; set; } /// /// 本地化名称 /// - public string LocalizedName; + public string LocalizedName { get; set; } /// /// 组名 /// - public string GroupName; + public string GroupName { get; set; } /// /// 类型名 /// - public string TypeName; + public string TypeName { get; set; } /// /// 方法名 /// - public string MethodName; + public string MethodName { get; set; } /// /// 说明 /// - public string Description; + public string Description { get; set; } /// /// 默认初始化 /// @@ -46,10 +46,23 @@ namespace NFox.Cad { GlobalName = globalName; } - - int IComparable.CompareTo(CommandInfo other) + /// + /// 将当前实例与同一类型的另一个对象进行比较,并返回一个整数,该整数指示当前实例在排序顺序中的位置是位于另一个对象之前、之后还是与其位置相同。 + /// + /// 与此实例进行比较的对象。 + /// + /// 一个值,指示要比较的对象的相对顺序。 返回值的含义如下: + /// 值 含义 小于零 此实例在排序顺序中位于 之前。 零 此实例在排序顺序中出现在与 的相同位置。 大于零 此实例在排序顺序中位于 之后。 + /// + public int CompareTo(CommandInfo other) { - return this.GlobalName.CompareTo(other.GlobalName); + if (other == null) + { + throw new ArgumentNullException(nameof(other)); + } + return GlobalName.CompareTo(other.GlobalName); } + + } } \ No newline at end of file diff --git a/NFox/Cad/Runtime/DBTransaction.cs b/NFox/Cad/Runtime/DBTransaction.cs index bc3b3cd7aad85d659dd9061be7746d203bc2ec9f..ba5d407970fcd6c635fa5c7900c958f1e94d1c4a 100644 --- a/NFox/Cad/Runtime/DBTransaction.cs +++ b/NFox/Cad/Runtime/DBTransaction.cs @@ -197,6 +197,10 @@ namespace NFox.Cad /// 实体对象 public void Flush(Entity entity) { + if (entity == null) + { + throw new ArgumentNullException(nameof(entity)); + } entity.RecordGraphicsModified(true); Transaction.TransactionManager.QueueForGraphicsFlush(); } @@ -216,18 +220,27 @@ namespace NFox.Cad /// 事务管理器 public static implicit operator Transaction(DBTransaction tr) { - return tr.Transaction; + return tr ?? tr.Transaction; + } + /// + /// 转到到事务管理器 + /// + /// 事务管理器 + public Transaction ToTransaction() + { + return Transaction; } -#endregion Trans + #endregion Trans //对象获取 -#region GetObject + #region GetObject private T GetObject(T obj, ObjectId id) where T : DBObject { - return obj ?? (obj = GetObject(id)); + //return obj ?? (_ = GetObject(id)); + return obj ?? GetObject(id); } /// @@ -331,23 +344,23 @@ namespace NFox.Cad /// 符号表的引用 /// 符号表的id /// 符号表 - private SymbolTable GetSymbolTable(ref SymbolTable table, ObjectId tableId) + private SymbolTableCollection GetSymbolTable(ref SymbolTableCollection table, ObjectId tableId) where TTable : SymbolTable where TRecord : SymbolTableRecord, new() { - return table ?? (table = new SymbolTable(this, tableId)); + return table ?? (table = new SymbolTableCollection(this, tableId)); } //块表 #region BlockTable - private SymbolTable _bt = null; + private SymbolTableCollection _bt = null; /// /// 块表 /// - public SymbolTable BlockTable + public SymbolTableCollection BlockTable { get { return GetSymbolTable(ref _bt, Database.BlockTableId); } } @@ -358,12 +371,12 @@ namespace NFox.Cad #region LayerTable - private SymbolTable _lt = null; + private SymbolTableCollection _lt = null; /// /// 层表 /// - public SymbolTable LayerTable + public SymbolTableCollection LayerTable { get { return GetSymbolTable(ref _lt, Database.LayerTableId); } } @@ -374,12 +387,12 @@ namespace NFox.Cad #region TextStyleTable - private SymbolTable _tst = null; + private SymbolTableCollection _tst = null; /// /// 文字样式表 /// - public SymbolTable TextStyleTable + public SymbolTableCollection TextStyleTable { get { return GetSymbolTable(ref _tst, Database.TextStyleTableId); } } @@ -390,11 +403,11 @@ namespace NFox.Cad #region RegAppTable - private SymbolTable _rat = null; + private SymbolTableCollection _rat = null; /// /// 注册应用程序表 /// - public SymbolTable RegAppTable + public SymbolTableCollection RegAppTable { get { return GetSymbolTable(ref _rat, Database.RegAppTableId); } } @@ -405,12 +418,12 @@ namespace NFox.Cad #region DimStyleTable - private SymbolTable _dst = null; + private SymbolTableCollection _dst = null; /// /// 标注样式表 /// - public SymbolTable DimStyleTable + public SymbolTableCollection DimStyleTable { get { return GetSymbolTable(ref _dst, Database.DimStyleTableId); } } @@ -421,12 +434,12 @@ namespace NFox.Cad #region LinetypeTable - private SymbolTable _ltt = null; + private SymbolTableCollection _ltt = null; /// /// 线型表 /// - public SymbolTable LinetypeTable + public SymbolTableCollection LinetypeTable { get { return GetSymbolTable(ref _ltt, Database.LinetypeTableId); } } @@ -470,11 +483,11 @@ namespace NFox.Cad #region UcsTable - private SymbolTable _ut = null; + private SymbolTableCollection _ut = null; /// /// 用户坐标系表 /// - public SymbolTable UcsTable + public SymbolTableCollection UcsTable { get { return GetSymbolTable(ref _ut, Database.UcsTableId); } } @@ -485,11 +498,11 @@ namespace NFox.Cad #region ViewTable - private SymbolTable _vt = null; + private SymbolTableCollection _vt = null; /// /// 视图表 /// - public SymbolTable ViewTable + public SymbolTableCollection ViewTable { get { return GetSymbolTable(ref _vt, Database.ViewTableId); } } @@ -500,11 +513,11 @@ namespace NFox.Cad #region ViewportTable - private SymbolTable _vpt = null; + private SymbolTableCollection _vpt = null; /// /// 视口表 /// - public SymbolTable ViewportTable + public SymbolTableCollection ViewportTable { get { return GetSymbolTable(ref _vpt, Database.ViewportTableId); } } @@ -541,6 +554,10 @@ namespace NFox.Cad /// 键值 public void SetToDictionary(DBDictionary dict, string key, DBObject value) { + if (dict == null) + { + throw new ArgumentNullException(nameof(dict)); + } using (dict.UpgradeOpenAndRun()) { if (dict.Contains(key)) @@ -594,13 +611,13 @@ namespace NFox.Cad /// /// 获取子字典 /// - /// 对象 + /// 对象 /// 是否创建子字典 /// 字典名称数组的可变参数 /// 字典对象 - public DBDictionary GetSubDictionary(DBObject obj, bool createSubDictionary, params string[] dictNames) + public DBDictionary GetSubDictionary(DBObject DBobj, bool createSubDictionary, params string[] dictNames) { - return obj.GetSubDictionary(Transaction, createSubDictionary, dictNames); + return DBobj.GetSubDictionary(Transaction, createSubDictionary, dictNames); } #endregion Value @@ -617,7 +634,11 @@ namespace NFox.Cad /// 键值 public void SetXRecord(DBDictionary dict, string key, ResultBuffer rb) { - SetToDictionary(dict, key, new Xrecord { Data = rb }); + using (var data = new Xrecord { Data = rb }) + { + SetToDictionary(dict, key, data); + } + } /// @@ -683,6 +704,10 @@ namespace NFox.Cad /// 编组Id public ObjectId AddGroup(string name, IEnumerable ids) { + if (ids == null) + { + throw new ArgumentNullException(nameof(ids)); + } if (GroupDictionary.Contains(name)) { return ObjectId.Null; @@ -722,6 +747,10 @@ namespace NFox.Cad /// 编组集合 public IEnumerable GetGroups(Entity ent) { + if (ent == null) + { + throw new ArgumentNullException(nameof(ent)); + } return ent.GetPersistentReactorIds() .Cast() @@ -1061,7 +1090,7 @@ namespace NFox.Cad /// ObjectId对象列表 public List AddEntity(BlockTableRecord btr, params Entity [] ents) { - return btr.AddEntity(Transaction, ents.ToCollection()); + return btr.AddEntity(Transaction, ents.Cast()); } #endregion Add Entity @@ -1076,18 +1105,23 @@ namespace NFox.Cad /// 成功返回true,失败返回false public bool Erase(ObjectIdCollection ids) { + if (ids == null) + { + throw new ArgumentNullException(nameof(ids)); + } try { foreach (ObjectId id in ids) { - DBObject obj; - obj = GetObject(id, OpenMode.ForWrite); + DBObject obj = GetObject(id, OpenMode.ForWrite); obj.Erase(true); } return true; } - catch - { } + catch (ObjectDisposedException e) + { + Editor.WriteMessage(e.Message); + } return false; } @@ -1098,18 +1132,23 @@ namespace NFox.Cad /// 成功返回true,失败返回false public bool Erase(IEnumerable ids) { + if (ids == null) + { + throw new ArgumentNullException(nameof(ids)); + } try { foreach (ObjectId id in ids) { - DBObject obj; - obj = GetObject(id, OpenMode.ForWrite); + DBObject obj = GetObject(id, OpenMode.ForWrite); obj.Erase(true); } return true; } - catch - { } + catch (ObjectDisposedException e) + { + Editor.WriteMessage(e.Message); + } return false; } @@ -1123,6 +1162,10 @@ namespace NFox.Cad /// public bool Erase(IEnumerable ents) where T : DBObject { + if (ents == null) + { + throw new ArgumentNullException(nameof(ents)); + } try { foreach (T ent in ents) @@ -1134,8 +1177,10 @@ namespace NFox.Cad } return true; } - catch - { } + catch (ObjectDisposedException e) + { + Editor.WriteMessage(e.Message); + } return false; } @@ -1145,6 +1190,10 @@ namespace NFox.Cad /// 块表记录 public void Clear(BlockTableRecord btr) { + if (btr == null) + { + throw new ArgumentNullException(nameof(btr)); + } foreach (ObjectId id in btr) { DBObject obj = GetObject(id, OpenMode.ForWrite); @@ -1256,39 +1305,43 @@ namespace NFox.Cad public ObjectId InsertBlock(Point3d position, ObjectId blockId, Scale3d scale = default, double range = default, Dictionary atts = default) { - var blockref = new BlockReference(position, blockId) + using (var blockref = new BlockReference(position, blockId) { ScaleFactors = scale, Rotation = range - }; - var objid = AddEntity(OpenCurrentSpace(), blockref); - if (atts != default) + }) { - var btr = GetObject(blockref.BlockTableRecord); - if (btr.HasAttributeDefinitions) + var objid = AddEntity(OpenCurrentSpace(), blockref); + if (atts != default) { - var attdefs = - btr.GetEntities(Transaction) - .Where(attdef => !(attdef.Constant || attdef.Invisible)); - foreach (var attdef in attdefs) + var btr = GetObject(blockref.BlockTableRecord); + if (btr.HasAttributeDefinitions) { - AttributeReference attref = new AttributeReference(); - attref.SetAttributeFromBlock(attdef, blockref.BlockTransform); - attref.Position = attdef.Position; - attref.AdjustAlignment(Database); - if (atts.ContainsKey(attdef.Tag)) + var attdefs = + btr.GetEntities(Transaction) + .Where(attdef => !(attdef.Constant || attdef.Invisible)); + foreach (var attdef in attdefs) { - attref.TextString = atts[attdef.Tag]; + using (AttributeReference attref = new AttributeReference()) + { + attref.SetAttributeFromBlock(attdef, blockref.BlockTransform); + attref.Position = attdef.Position; + attref.AdjustAlignment(Database); + if (atts.ContainsKey(attdef.Tag)) + { + attref.TextString = atts[attdef.Tag]; + } + + blockref.AttributeCollection.AppendAttribute(attref); + Transaction.AddNewlyCreatedDBObject(attref, true); + } + } - - blockref.AttributeCollection.AppendAttribute(attref); - Transaction.AddNewlyCreatedDBObject(attref, true); - } } + //return AddEntity(OpenCurrentSpace(), blockref); + return objid; } - //return AddEntity(OpenCurrentSpace(), blockref); - return objid; } /// /// 插入块参照 @@ -1336,6 +1389,14 @@ namespace NFox.Cad public Dictionary AppendAttribToBlock(BlockReference blkref, List atts) { + if (blkref == null) + { + throw new ArgumentNullException(nameof(blkref)); + } + if (atts == null) + { + throw new ArgumentNullException(nameof(atts)); + } var blkdef = GetObject(blkref.BlockTableRecord); int i = 0; @@ -1388,16 +1449,26 @@ namespace NFox.Cad public Dictionary AppendAttribToBlock(BlockReference blkref, List atts) { + if (blkref == null) + { + throw new ArgumentNullException(nameof(blkref)); + } + if (atts == null) + { + throw new ArgumentNullException(nameof(atts)); + } var attribs = new Dictionary(); for (int i = 0; i < atts.Count; i++) { AttributeDefinition attdef = atts[i]; - AttributeReference attref = new AttributeReference(); - attref.SetAttributeFromBlock(attdef, blkref.BlockTransform); - attref.TextString = attdef.TextString; - blkref.AttributeCollection.AppendAttribute(attref); - Transaction.AddNewlyCreatedDBObject(attref, true); + using (AttributeReference attref = new AttributeReference()) + { + attref.SetAttributeFromBlock(attdef, blkref.BlockTransform); + attref.TextString = attdef.TextString; + blkref.AttributeCollection.AppendAttribute(attref); + Transaction.AddNewlyCreatedDBObject(attref, true); + } } return attribs; } @@ -1446,9 +1517,11 @@ namespace NFox.Cad public ObjectId InsertBlockRef(ObjectId bdefid, List atts) { BlockTableRecord btr = OpenCurrentSpace(); - BlockReference blkref = new BlockReference(Point3d.Origin, bdefid); - ObjectId id = AddEntity(btr, blkref); - return DragBlockRef(blkref, atts) ? id : ObjectId.Null; + using (BlockReference blkref = new BlockReference(Point3d.Origin, bdefid)) + { + ObjectId id = AddEntity(btr, blkref); + return DragBlockRef(blkref, atts) ? id : ObjectId.Null; + } } /// @@ -1498,6 +1571,14 @@ namespace NFox.Cad /// 裁剪多边形点表 public void ClipBlockRef(BlockReference bref, IEnumerable pt3ds) { + if (bref == null) + { + throw new ArgumentNullException(nameof(bref)); + } + if (pt3ds == null) + { + throw new ArgumentNullException(nameof(pt3ds)); + } Matrix3d mat = bref.BlockTransform.Inverse(); var pts = pt3ds @@ -1506,9 +1587,11 @@ namespace NFox.Cad .ToCollection(); SpatialFilterDefinition sfd = new SpatialFilterDefinition(pts, Vector3d.ZAxis, 0.0, 0.0, 0.0, true); - SpatialFilter sf = new SpatialFilter { Definition = sfd }; - var dict = GetSubDictionary(bref, true, filterDictName); - SetToDictionary(dict, spatialName, sf); + using (SpatialFilter sf = new SpatialFilter { Definition = sfd }) + { + var dict = GetSubDictionary(bref, true, filterDictName); + SetToDictionary(dict, spatialName, sf); + } } /// @@ -1519,6 +1602,10 @@ namespace NFox.Cad /// 第二角点 public void ClipBlockRef(BlockReference bref, Point3d pt1, Point3d pt2) { + if (bref == null) + { + throw new ArgumentNullException(nameof(bref)); + } Matrix3d mat = bref.BlockTransform.Inverse(); pt1 = pt1.TransformBy(mat); pt2 = pt2.TransformBy(mat); @@ -1530,13 +1617,17 @@ namespace NFox.Cad }; SpatialFilterDefinition sfd = new SpatialFilterDefinition(pts, Vector3d.ZAxis, 0.0, 0.0, 0.0, true); - SpatialFilter sf = new SpatialFilter { Definition = sfd }; - var dict = GetSubDictionary(bref, true, filterDictName); - SetToDictionary(dict, spatialName, sf); + using (SpatialFilter sf = new SpatialFilter { Definition = sfd }) + { + var dict = GetSubDictionary(bref, true, filterDictName); + SetToDictionary(dict, spatialName, sf); + } } -#endregion ClipBlockRef -#endregion BlockReference + + #endregion ClipBlockRef + + #endregion BlockReference } } \ No newline at end of file diff --git a/NFox/Cad/Runtime/SymbolTable.cs b/NFox/Cad/Runtime/SymbolTable.cs index dd140646c858628f864010baa0fd97324190551b..f3d8ef67d19912403ce05f875c0eed0883ad8af0 100644 --- a/NFox/Cad/Runtime/SymbolTable.cs +++ b/NFox/Cad/Runtime/SymbolTable.cs @@ -10,7 +10,7 @@ namespace NFox.Cad /// /// 符号表 /// 符号表记录 - public class SymbolTable : IEnumerable + public class SymbolTableCollection : IEnumerable where TTable : SymbolTable where TRecord : SymbolTableRecord, new() { @@ -41,7 +41,7 @@ namespace NFox.Cad /// /// 事务管理器 /// 符号表的对象id - internal SymbolTable(DBTransaction tr, ObjectId tableId) + internal SymbolTableCollection(DBTransaction tr, ObjectId tableId) { Trans = tr; AcTable = Trans.GetObject(tableId, OpenMode.ForRead) as TTable; @@ -219,24 +219,26 @@ namespace NFox.Cad /// 符号表记录名 /// 是否覆盖, 为覆盖, 为不覆盖 /// 对象id - public ObjectId GetRecordFrom(SymbolTable table, string name, bool over) + public ObjectId GetRecordFrom(SymbolTableCollection table, string name, bool over) { + if (table == null) + { + throw new ArgumentNullException(nameof(table)); + } ObjectId rid = this[name]; bool has = rid != ObjectId.Null; if ((has && over) || !has) { ObjectId id = table[name]; - IdMapping idm = new IdMapping(); - table.Database.WblockCloneObjects - ( - new ObjectIdCollection { id }, - AcTable.Id, - idm, - DuplicateRecordCloning.Replace, - false - ); - rid = idm[id].Value; + using (IdMapping idm = new IdMapping()) + { + using (ObjectIdCollection ids = new ObjectIdCollection { id }) + { + table.Database.WblockCloneObjects(ids, AcTable.Id, idm, DuplicateRecordCloning.Replace, false); + } + rid = idm[id].Value; + } } return rid; } @@ -249,7 +251,7 @@ namespace NFox.Cad /// 符号表记录名 /// 是否覆盖, 为覆盖, 为不覆盖 /// 对象id - internal ObjectId GetRecordFrom(Func> tableSelector, string fileName, string name, bool over) + internal ObjectId GetRecordFrom(Func> tableSelector, string fileName, string name, bool over) { using (var tr = new DBTransaction(fileName)) { diff --git a/NFox/Cad/Runtime/SystemManager.cs b/NFox/Cad/Runtime/SystemManager.cs index 0801e4265f2e047a0f46af8f98ca9d3c0ebbe993..89fc86597b9edae3f63bd1f691ddce4737551571 100644 --- a/NFox/Cad/Runtime/SystemManager.cs +++ b/NFox/Cad/Runtime/SystemManager.cs @@ -82,9 +82,8 @@ namespace NFox.Cad /// 获取对话框配置的数据 /// /// 对话框对象 - /// 数据名 /// 配置项 - public static IConfigurationSection GetDialogSection(object dialog, string propertyName) + public static IConfigurationSection GetDialogSection(object dialog) { UserConfigurationManager ucm = Application.UserConfigurationManager; IConfigurationSection ds = ucm.OpenDialogSection(dialog); @@ -239,7 +238,7 @@ namespace NFox.Cad get { string s = (string)Application.GetSystemVariable("dimblk"); - if (s == "" || s == null) + if (string.IsNullOrEmpty(s)) { return DimblkType.Defult; } diff --git a/NFox/NFox.csproj b/NFox/NFox.csproj index 55f9470391fcea1cddfb29c76bb1aa5da33bb360..b1f33c18a38b1047a1da15706e1c60c3a22fcd74 100644 --- a/NFox/NFox.csproj +++ b/NFox/NFox.csproj @@ -38,12 +38,12 @@ - + diff --git a/NFox/test.cs b/NFox/test.cs index 6bd82a0307ed8cadbfe6602ac59d89cb5623f387..2779a4464adb62a580307bc28443b0283299ab27 100644 --- a/NFox/test.cs +++ b/NFox/test.cs @@ -18,6 +18,9 @@ using NFox.Cad; namespace NFox.Cad.Test { + + + /// /// /// diff --git a/doc/WPF.md b/doc/WPF.md index 8ad6779e34a2b706aedccfc8d0b98e34a3d9566a..5a1998ca5aa6cb7c23ffa91a6274e6e2eddcab61 100644 --- a/doc/WPF.md +++ b/doc/WPF.md @@ -1,3 +1,5 @@ + + # WPF支持 ## 一、只要在项目文件里添加如下代码即可支持wpf @@ -20,32 +22,67 @@ ## 二、在项目中创建wpf窗体 -由于在类库项目内,只能添加wpf的自定义控件项目,因此为了将wpf窗体添加到类库项目里,需要采用一些奇淫巧技。 +由于在类库项目内,只能添加wpf的自定义控件项目。如下图,你是不能添加wpf窗体到类库项目的。 -1. 首先打开vs创建一个wpf窗体项目。紧接着创建一个名为MyWindow的Window。创建完之后什么,可以自定义一些内容,比如添加一些属性啊什么的,本示例保持默认。 -2. 然后菜单栏-项目-导出模版,打开导出模版向导。 -3. 选择项模版,下一步 -4. 只选择MyWindow.xaml文件,下一步 -5. 选择项引用,直接下一步。 -6. 在模版选项里填写模版名称、说明,还可以添加图标,然后点击完成。 +![img](./png/wpf-9.png) -![img](./png/1.png) +因此为了将wpf窗体添加到类库项目里,需要采用一些奇淫巧技。 -7. 现在新建个standard类库项目,然后将第一条里的内容添加到项目文件里。然后保存。 +1. 首先打开vs创建一个wpf应用项目。 -![img](png/2.png) +![img](png/wpf-1.png) -8. 然后项目右键-添加-新建项,选择MyWindow,点击添加 +2. 创建完之后什么,可以自定义一些内容,比如添加一些属性啊什么的,本示例保持默认。 -![img](png/3.png) +![img](png/wpf-2.png) -9. 然后就会在类库里添加了window类型的wpf窗体了。 +3. 然后菜单栏-项目-导出模版,打开导出模版向导。 -![img](png/4.png) +![img](png/wpf-3.png) + +4. 选择项模版,下一步 + +![wpf-4](png/wpf-4.png) + +5. 只选择MainWindow.xaml文件,下一步 + +![wpf-5](png/wpf-5.png) + +6. 选择项引用,什么都不选,直接下一步。 + +![wpf-6](png/wpf-6.png) + +7. 在模版选项里填写模版名称、说明,还可以添加图标,然后点击完成。 + +![wpf-7](png/wpf-7.png) +现在可以在类库里添加的模版就创建好了,下面是怎么使用这个模版在类库里插入wpf窗体。首先以frameworks类库项目为例: +1. 新建个类库项目,然后右键添加-新建项。 +![wpf-8](png/wpf-8.png) +2. 然后选择刚刚新建的模版,点击添加。 + +![img](png/wpf-10.png) + +3. 然后记得添加 PresentationCore、PresentationFramework、System.Xaml、WindowsBase这四个引用。 + +![img](png/wpf-11.png) + +下面是standard类库项目: + +1. 现在新建个standard类库项目,然后将第一条里的内容添加到项目文件里。然后保存。 + +![img](png/2.png) + +2. 然后项目右键-添加-新建项,选择MyWindow,点击添加 + +![img](png/3.png) + +3. 然后就会在类库里添加了window类型的wpf窗体了。 + +![img](png/4.png) # mvvm模式支持 diff --git a/doc/png/wpf-1.png b/doc/png/wpf-1.png new file mode 100644 index 0000000000000000000000000000000000000000..e93fc7496d98ce18ff82662d3921961099fc028a Binary files /dev/null and b/doc/png/wpf-1.png differ diff --git a/doc/png/wpf-10.png b/doc/png/wpf-10.png new file mode 100644 index 0000000000000000000000000000000000000000..5dce533cb571382149acc8be983091279956decd Binary files /dev/null and b/doc/png/wpf-10.png differ diff --git a/doc/png/wpf-11.png b/doc/png/wpf-11.png new file mode 100644 index 0000000000000000000000000000000000000000..e98a5f881ac0e7e1b62bd11b04c3c2630576877c Binary files /dev/null and b/doc/png/wpf-11.png differ diff --git a/doc/png/wpf-2.png b/doc/png/wpf-2.png new file mode 100644 index 0000000000000000000000000000000000000000..cc316a872fff64922a904fd8a76cd97e462eb1d3 Binary files /dev/null and b/doc/png/wpf-2.png differ diff --git a/doc/png/wpf-3.png b/doc/png/wpf-3.png new file mode 100644 index 0000000000000000000000000000000000000000..90d4a35a85cb6148dbe1b6c9165214248da70b62 Binary files /dev/null and b/doc/png/wpf-3.png differ diff --git a/doc/png/wpf-4.png b/doc/png/wpf-4.png new file mode 100644 index 0000000000000000000000000000000000000000..4e42284ae1d1fd4a398739beb6e413070321fd6a Binary files /dev/null and b/doc/png/wpf-4.png differ diff --git a/doc/png/wpf-5.png b/doc/png/wpf-5.png new file mode 100644 index 0000000000000000000000000000000000000000..3cc73d658f2ecc94d29c2586d6c7b459b319f54c Binary files /dev/null and b/doc/png/wpf-5.png differ diff --git a/doc/png/wpf-6.png b/doc/png/wpf-6.png new file mode 100644 index 0000000000000000000000000000000000000000..0f67e713c7d9bee0fec373ccbc92a25d79f0db8c Binary files /dev/null and b/doc/png/wpf-6.png differ diff --git a/doc/png/wpf-7.png b/doc/png/wpf-7.png new file mode 100644 index 0000000000000000000000000000000000000000..e00b6f30fa6346e77af7fe30e271d870e5f649f9 Binary files /dev/null and b/doc/png/wpf-7.png differ diff --git a/doc/png/wpf-8.png b/doc/png/wpf-8.png new file mode 100644 index 0000000000000000000000000000000000000000..6fffdfc5143e97473f7239e522a0c378b9d16559 Binary files /dev/null and b/doc/png/wpf-8.png differ diff --git a/doc/png/wpf-9.png b/doc/png/wpf-9.png new file mode 100644 index 0000000000000000000000000000000000000000..ed8d00868d733d59b867dd2a47299179839e6649 Binary files /dev/null and b/doc/png/wpf-9.png differ