图纸数据遍历(包括获取数据和设置数据)
代码
using System;
using System.Collections.Generic;
using System.Text;
using SWF = System.Windows.Forms;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Windows;
using ArxHlb.UI;
namespace ArxHlb
{
//委托类
public delegate void DelegateSet(Transaction tr, BlockTableRecord btr);
public delegate void DelegateGet(Transaction tr, BlockTableRecord btr, ref object reObj);
//批量处理图纸类
public class CADDwgOps
{
/// <summary>
/// 批量处理图纸 传入文件路径列表的 by houlinbo QQ420021327
/// </summary>
/// <param name="cationOpenDlg">打开窗口的cation</param>
/// <param name="cationWait">等待窗口的cation</param>
/// <param name="DwgOp">处理函数,使用委托</param>
public static void SetDwgsData(string cationOpenDlg, string cationWait, string msgFinish, List<string> files, DelegateSet Op)
{
using (frmWaiting frm = new frmWaiting())
{
frm.Count = files.Count;
frm.Fun = cationWait;
frm.Show();
//打开图形数据库db 多张图纸时使用
int i = 0;
foreach (string fileName in files)
{
frm.Index = ++i;
frm.FileName = fileName;
System.Windows.Forms.Application.DoEvents();
frm.Update();
if (!frm.TopMost)
{
frm.TopMost = true;
}
Database db = ArxHlb.CADEntAdd.Getdb(fileName, System.IO.FileShare.ReadWrite, false);
if (Application.DocumentManager.Count == 0) Application.DocumentManager.Add("acadiso.dwt");
Document doc = Application.DocumentManager.MdiActiveDocument;
DocumentLock lockDoc = doc.LockDocument();
using (lockDoc)
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Op(tr, btr);//委托的函数
tr.Commit();
}
}
db.SaveAs(db.Filename, DwgVersion.Current);
}
SWF.MessageBox.Show(msgFinish);
}
}
/// <summary>
/// 批量处理图纸 自已选择得到文件路径列表的 by houlinbo QQ420021327
/// </summary>
/// <param name="cationOpenDlg">打开窗口的cation</param>
/// <param name="cationWait">等待窗口的cation</param>
/// <param name="DwgOp">处理函数,使用委托</param>
public static void SetDwgsData(string cationOpenDlg, string cationWait, string msgFinish, DelegateSet Op)
{
OpenFileDialog dlg = new OpenFileDialog(cationOpenDlg, "*.dwg", "dwg", "*",
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple);
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) return;
string[] files = dlg.GetFilenames();
if (files == null) return;
using (frmWaiting frm = new frmWaiting())
{
frm.Count = files.Length;
frm.Fun = cationWait;
frm.Show();
//打开图形数据库db 多张图纸时使用
int i = 0;
foreach (string fileName in files)
{
frm.Index = ++i;
frm.FileName = fileName;
System.Windows.Forms.Application.DoEvents();
frm.Update();
if (!frm.TopMost)
{
frm.TopMost = true;
}
Database db = ArxHlb.CADEntAdd.Getdb(fileName, System.IO.FileShare.ReadWrite, false);
if (Application.DocumentManager.Count == 0) Application.DocumentManager.Add("acadiso.dwt");
Document doc = Application.DocumentManager.MdiActiveDocument;
DocumentLock lockDoc = doc.LockDocument();
using (lockDoc)
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Op(tr, btr);//委托的函数
tr.Commit();
}
}
db.SaveAs(db.Filename, DwgVersion.Current);
}
SWF.MessageBox.Show(msgFinish);
}
}
/// <summary>
/// 批量得到图纸数据 传入文件路径列表的 by houlinbo QQ420021327
/// </summary>
/// <param name="cationOpenDlg">打开窗口的cation</param>
/// <param name="cationWait">等待窗口的cation</param>
/// <param name="DwgOp">处理函数,使用委托</param>
public static object GetDwgsData(string cationOpenDlg, string cationWait, string msgFinish, List<string> files, DelegateGet Op)
{
object reObj = null;//返回的值
using (frmWaiting frm = new frmWaiting())
{
frm.Count = files.Count;
frm.Fun = cationWait;
frm.Show();
//打开图形数据库db 多张图纸时使用
int i = 0;
foreach (string fileName in files)
{
frm.Index = ++i;
frm.FileName = fileName;
System.Windows.Forms.Application.DoEvents();
frm.Update();
if (!frm.TopMost)
{
frm.TopMost = true;
}
Database db = ArxHlb.CADEntAdd.Getdb(fileName, System.IO.FileShare.ReadWrite, false);
if (Application.DocumentManager.Count == 0) Application.DocumentManager.Add("acadiso.dwt");
Document doc = Application.DocumentManager.MdiActiveDocument;
DocumentLock lockDoc = doc.LockDocument();
using (lockDoc)
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
Op(tr, btr, ref reObj);//委托的函数
}
}
}
}
return reObj;
}
/// <summary>
/// 批量得到图纸数据 自已选择得到文件路径列表的 by houlinbo QQ420021327
/// </summary>
/// <param name="cationOpenDlg">打开窗口的cation</param>
/// <param name="cationWait">等待窗口的cation</param>
/// <param name="DwgOp">处理函数,使用委托</param>
public static object GetDwgsData(string cationOpenDlg, string cationWait, string msgFinish, DelegateGet Op)
{
object reObj = null;//返回的值
OpenFileDialog dlg = new OpenFileDialog(cationOpenDlg, "*.dwg", "dwg", "*",
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple);
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) return null;
string[] files = dlg.GetFilenames();
if (files == null) return null;
using (frmWaiting frm = new frmWaiting())
{
frm.Count = files.Length;
frm.Fun = cationWait;
frm.Show();
//打开图形数据库db 多张图纸时使用
int i = 0;
foreach (string fileName in files)
{
frm.Index = ++i;
frm.FileName = fileName;
System.Windows.Forms.Application.DoEvents();
frm.Update();
if (!frm.TopMost)
{
frm.TopMost = true;
}
Database db = ArxHlb.CADEntAdd.Getdb(fileName, System.IO.FileShare.ReadWrite, false);
if (Application.DocumentManager.Count == 0) Application.DocumentManager.Add("acadiso.dwt");
Document doc = Application.DocumentManager.MdiActiveDocument;
DocumentLock lockDoc = doc.LockDocument();
using (lockDoc)
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
Op(tr, btr, ref reObj);//委托的函数
}
}
}
}
return reObj;
}
}
}
using System.Collections.Generic;
using System.Text;
using SWF = System.Windows.Forms;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Windows;
using ArxHlb.UI;
namespace ArxHlb
{
//委托类
public delegate void DelegateSet(Transaction tr, BlockTableRecord btr);
public delegate void DelegateGet(Transaction tr, BlockTableRecord btr, ref object reObj);
//批量处理图纸类
public class CADDwgOps
{
/// <summary>
/// 批量处理图纸 传入文件路径列表的 by houlinbo QQ420021327
/// </summary>
/// <param name="cationOpenDlg">打开窗口的cation</param>
/// <param name="cationWait">等待窗口的cation</param>
/// <param name="DwgOp">处理函数,使用委托</param>
public static void SetDwgsData(string cationOpenDlg, string cationWait, string msgFinish, List<string> files, DelegateSet Op)
{
using (frmWaiting frm = new frmWaiting())
{
frm.Count = files.Count;
frm.Fun = cationWait;
frm.Show();
//打开图形数据库db 多张图纸时使用
int i = 0;
foreach (string fileName in files)
{
frm.Index = ++i;
frm.FileName = fileName;
System.Windows.Forms.Application.DoEvents();
frm.Update();
if (!frm.TopMost)
{
frm.TopMost = true;
}
Database db = ArxHlb.CADEntAdd.Getdb(fileName, System.IO.FileShare.ReadWrite, false);
if (Application.DocumentManager.Count == 0) Application.DocumentManager.Add("acadiso.dwt");
Document doc = Application.DocumentManager.MdiActiveDocument;
DocumentLock lockDoc = doc.LockDocument();
using (lockDoc)
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Op(tr, btr);//委托的函数
tr.Commit();
}
}
db.SaveAs(db.Filename, DwgVersion.Current);
}
SWF.MessageBox.Show(msgFinish);
}
}
/// <summary>
/// 批量处理图纸 自已选择得到文件路径列表的 by houlinbo QQ420021327
/// </summary>
/// <param name="cationOpenDlg">打开窗口的cation</param>
/// <param name="cationWait">等待窗口的cation</param>
/// <param name="DwgOp">处理函数,使用委托</param>
public static void SetDwgsData(string cationOpenDlg, string cationWait, string msgFinish, DelegateSet Op)
{
OpenFileDialog dlg = new OpenFileDialog(cationOpenDlg, "*.dwg", "dwg", "*",
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple);
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) return;
string[] files = dlg.GetFilenames();
if (files == null) return;
using (frmWaiting frm = new frmWaiting())
{
frm.Count = files.Length;
frm.Fun = cationWait;
frm.Show();
//打开图形数据库db 多张图纸时使用
int i = 0;
foreach (string fileName in files)
{
frm.Index = ++i;
frm.FileName = fileName;
System.Windows.Forms.Application.DoEvents();
frm.Update();
if (!frm.TopMost)
{
frm.TopMost = true;
}
Database db = ArxHlb.CADEntAdd.Getdb(fileName, System.IO.FileShare.ReadWrite, false);
if (Application.DocumentManager.Count == 0) Application.DocumentManager.Add("acadiso.dwt");
Document doc = Application.DocumentManager.MdiActiveDocument;
DocumentLock lockDoc = doc.LockDocument();
using (lockDoc)
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForWrite) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
Op(tr, btr);//委托的函数
tr.Commit();
}
}
db.SaveAs(db.Filename, DwgVersion.Current);
}
SWF.MessageBox.Show(msgFinish);
}
}
/// <summary>
/// 批量得到图纸数据 传入文件路径列表的 by houlinbo QQ420021327
/// </summary>
/// <param name="cationOpenDlg">打开窗口的cation</param>
/// <param name="cationWait">等待窗口的cation</param>
/// <param name="DwgOp">处理函数,使用委托</param>
public static object GetDwgsData(string cationOpenDlg, string cationWait, string msgFinish, List<string> files, DelegateGet Op)
{
object reObj = null;//返回的值
using (frmWaiting frm = new frmWaiting())
{
frm.Count = files.Count;
frm.Fun = cationWait;
frm.Show();
//打开图形数据库db 多张图纸时使用
int i = 0;
foreach (string fileName in files)
{
frm.Index = ++i;
frm.FileName = fileName;
System.Windows.Forms.Application.DoEvents();
frm.Update();
if (!frm.TopMost)
{
frm.TopMost = true;
}
Database db = ArxHlb.CADEntAdd.Getdb(fileName, System.IO.FileShare.ReadWrite, false);
if (Application.DocumentManager.Count == 0) Application.DocumentManager.Add("acadiso.dwt");
Document doc = Application.DocumentManager.MdiActiveDocument;
DocumentLock lockDoc = doc.LockDocument();
using (lockDoc)
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
Op(tr, btr, ref reObj);//委托的函数
}
}
}
}
return reObj;
}
/// <summary>
/// 批量得到图纸数据 自已选择得到文件路径列表的 by houlinbo QQ420021327
/// </summary>
/// <param name="cationOpenDlg">打开窗口的cation</param>
/// <param name="cationWait">等待窗口的cation</param>
/// <param name="DwgOp">处理函数,使用委托</param>
public static object GetDwgsData(string cationOpenDlg, string cationWait, string msgFinish, DelegateGet Op)
{
object reObj = null;//返回的值
OpenFileDialog dlg = new OpenFileDialog(cationOpenDlg, "*.dwg", "dwg", "*",
Autodesk.AutoCAD.Windows.OpenFileDialog.OpenFileDialogFlags.AllowMultiple);
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) return null;
string[] files = dlg.GetFilenames();
if (files == null) return null;
using (frmWaiting frm = new frmWaiting())
{
frm.Count = files.Length;
frm.Fun = cationWait;
frm.Show();
//打开图形数据库db 多张图纸时使用
int i = 0;
foreach (string fileName in files)
{
frm.Index = ++i;
frm.FileName = fileName;
System.Windows.Forms.Application.DoEvents();
frm.Update();
if (!frm.TopMost)
{
frm.TopMost = true;
}
Database db = ArxHlb.CADEntAdd.Getdb(fileName, System.IO.FileShare.ReadWrite, false);
if (Application.DocumentManager.Count == 0) Application.DocumentManager.Add("acadiso.dwt");
Document doc = Application.DocumentManager.MdiActiveDocument;
DocumentLock lockDoc = doc.LockDocument();
using (lockDoc)
{
Transaction tr = db.TransactionManager.StartTransaction();
using (tr)
{
BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
Op(tr, btr, ref reObj);//委托的函数
}
}
}
}
return reObj;
}
}
}
调用采用下面的方法
代码
using System;
using System.Collections.Generic;
using System.Text;
using SWF = System.Windows.Forms;
using CE.ELD.PID.UI;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Windows;
using System.Data;
//本类用于图纸转换,从1.0-2.0
namespace CE.ELD.PID.BLL
{
public class DwgTrans
{
/// <summary>
/// 批量处理图纸
/// </summary>
/// <param name="cationOpenDlg">打开窗口的cation</param>
/// <param name="cationWait">等待窗口的cation</param>
/// <param name="DwgOp">处理函数,使用委托</param>
public void DwgTransformation()
{
try
{
ArxHlb.CADDwgOps.SetDwgsData("请批量选择需要转换的文件", "正在转换......", "转换图纸完成", SetOp);
}
catch (Exception ex)
{
CE.Common.Utility.ShowExMessage("批量转换图纸错误", ex.Message);
}
}
/// <summary>
/// 操作图纸 Set数据
/// </summary>
/// <param name="db"></param>
private void SetOp(Transaction tr, BlockTableRecord btr)
{
foreach (ObjectId objId in btr)
{
DBObject dbObj = tr.GetObject(objId, OpenMode.ForWrite);
//。。。这里写入对每个对象的操作
}
}
//***********以下是得到数据的调用
/// <summary>
/// 操作图纸 Get数据 返回 DataTable
/// </summary>
/// <param name="db"></param>
private void GetOp1(Transaction tr, BlockTableRecord btr, ref object reObj)
{
System.Data.DataTable dt;
//初始化reObj或为reObj赋值
if (reObj == null)
{
dt = new System.Data.DataTable();
//dt = DBDevLst.GetDataList(" and 1<>1");//取表结构 不可隐掉 只是没有DBDevLst 不能编译
}
else
{
dt = reObj as System.Data.DataTable;
}
//返回
foreach (ObjectId objId in btr)
{
DBObject dbObj = tr.GetObject(objId, OpenMode.ForWrite);
//。。。这里写入对每个对象的操作
DataRow dr = dt.NewRow();
dr["1"] = "";
dr["2"] = "";
dt.Rows.Add(dr.ItemArray);
}
reObj = dt as object; //引用类型还原
}
/// <summary>
/// 操作图纸 Get数据 返回 List<object>
/// </summary>
/// <param name="db"></param>
private void GetOp2(Transaction tr, BlockTableRecord btr, ref object reObj)
{
List<object> lstObj;
//初始化reObj或为reObj赋值
if (reObj == null)
{
lstObj = new List<object>();
}
else
{
lstObj = reObj as List<object>;
}
//返回
foreach (ObjectId objId in btr)
{
DBObject dbObj = tr.GetObject(objId, OpenMode.ForWrite);
//。。。这里写入对每个对象的操作
string type = ""; //或是任何类型
lstObj.Add(type as object );
}
reObj = lstObj as object; //引用类型还原
}
///。。。可是是任何类型
}
}
using System.Collections.Generic;
using System.Text;
using SWF = System.Windows.Forms;
using CE.ELD.PID.UI;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Windows;
using System.Data;
//本类用于图纸转换,从1.0-2.0
namespace CE.ELD.PID.BLL
{
public class DwgTrans
{
/// <summary>
/// 批量处理图纸
/// </summary>
/// <param name="cationOpenDlg">打开窗口的cation</param>
/// <param name="cationWait">等待窗口的cation</param>
/// <param name="DwgOp">处理函数,使用委托</param>
public void DwgTransformation()
{
try
{
ArxHlb.CADDwgOps.SetDwgsData("请批量选择需要转换的文件", "正在转换......", "转换图纸完成", SetOp);
}
catch (Exception ex)
{
CE.Common.Utility.ShowExMessage("批量转换图纸错误", ex.Message);
}
}
/// <summary>
/// 操作图纸 Set数据
/// </summary>
/// <param name="db"></param>
private void SetOp(Transaction tr, BlockTableRecord btr)
{
foreach (ObjectId objId in btr)
{
DBObject dbObj = tr.GetObject(objId, OpenMode.ForWrite);
//。。。这里写入对每个对象的操作
}
}
//***********以下是得到数据的调用
/// <summary>
/// 操作图纸 Get数据 返回 DataTable
/// </summary>
/// <param name="db"></param>
private void GetOp1(Transaction tr, BlockTableRecord btr, ref object reObj)
{
System.Data.DataTable dt;
//初始化reObj或为reObj赋值
if (reObj == null)
{
dt = new System.Data.DataTable();
//dt = DBDevLst.GetDataList(" and 1<>1");//取表结构 不可隐掉 只是没有DBDevLst 不能编译
}
else
{
dt = reObj as System.Data.DataTable;
}
//返回
foreach (ObjectId objId in btr)
{
DBObject dbObj = tr.GetObject(objId, OpenMode.ForWrite);
//。。。这里写入对每个对象的操作
DataRow dr = dt.NewRow();
dr["1"] = "";
dr["2"] = "";
dt.Rows.Add(dr.ItemArray);
}
reObj = dt as object; //引用类型还原
}
/// <summary>
/// 操作图纸 Get数据 返回 List<object>
/// </summary>
/// <param name="db"></param>
private void GetOp2(Transaction tr, BlockTableRecord btr, ref object reObj)
{
List<object> lstObj;
//初始化reObj或为reObj赋值
if (reObj == null)
{
lstObj = new List<object>();
}
else
{
lstObj = reObj as List<object>;
}
//返回
foreach (ObjectId objId in btr)
{
DBObject dbObj = tr.GetObject(objId, OpenMode.ForWrite);
//。。。这里写入对每个对象的操作
string type = ""; //或是任何类型
lstObj.Add(type as object );
}
reObj = lstObj as object; //引用类型还原
}
///。。。可是是任何类型
}
}