一个文件夹下大量图纸(几百甚至几千个文件)需要改图层颜色时,可采用插件实现,效果如下:
转换前:
转换后:
使用方式如下:netload加载此dll插件,输入xx运行。
附部分代码如下:
public void ProcessDwgFile(string filePath, string savePath, string layerName, short colorIndex){Database db = new Database(false, true); // 无文档模式try{// 读取DWG文件db.ReadDwgFile(filePath, FileOpenMode.OpenForReadAndAllShare, false, null);using (Transaction tr = db.TransactionManager.StartTransaction()){LayerTable lt = (LayerTable)tr.GetObject(db.LayerTableId, OpenMode.ForRead);// 用于存储找到的图层IDObjectId layerId = ObjectId.Null;// 遍历所有图层名,进行不区分大小写的比较foreach (ObjectId id in lt){LayerTableRecord ltr = (LayerTableRecord)tr.GetObject(id, OpenMode.ForRead);if (ltr.Name.Equals(layerName, StringComparison.OrdinalIgnoreCase)){layerId = id;break;}}// 如果未找到图层,跳过该文件if (layerId.IsNull){tr.Commit();return;}LayerTableRecord ltrToModify = (LayerTableRecord)tr.GetObject(layerId, OpenMode.ForWrite);// 设置图层颜色(如果选择的不是ByLayer则设置图层颜色)if (colorIndex != 256){ltrToModify.Color = Color.FromColorIndex(ColorMethod.ByAci, colorIndex);}// 修改所有图元的颜色属性BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead);BlockTableRecord btr = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead);foreach (ObjectId entId in btr){*****省略部分代码tr.Commit();}// 保存文件string fileName = Path.GetFileName(filePath);string saveFile = Path.Combine(savePath, fileName);db.SaveAs(saveFile, DwgVersion.Current);}catch (Exception ex){MessageBox.Show($"处理文件 {filePath} 时出错: {ex.Message}");}finally{db.Dispose();}}
插件联系↓↓↓