概述
由于DWG图层中,面积单元都是存放在FQ层中,所以需要批量计算FQ图层的面积。
主要代码如下:
[CommandMethod("dwgarea")]
public void CalCADAllDwgArea()
{
frmDwgArea pFrmDwgArea = new frmDwgArea();
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(pFrmDwgArea);
//#region CAD命令下
//string strAppExcel = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "JHBArea.xls");
//HSSFWorkbook wk = null;
//ISheet pSheet = null;
//if (File.Exists(strAppExcel))
//{
// using (FileStream fs = new FileStream(strAppExcel, FileMode.OpenOrCreate, FileAccess.ReadWrite))
// {
// wk = new HSSFWorkbook(fs);
// pSheet = wk.GetSheet("mysheet");
// if (pSheet == null)
// {
// pSheet = wk.CreateSheet("mysheet");
// }
// CreateHeader(pSheet, wk, new string[] { strTHField, strDwgAreaField });
// }
//}
//else
//{
// using (FileStream fs = new FileStream(strAppExcel, FileMode.CreateNew, FileAccess.ReadWrite))
// {
// wk = new HSSFWorkbook();
// pSheet = wk.GetSheet("mysheet");
// if (pSheet == null)
// {
// pSheet = wk.CreateSheet("mysheet");
// }
// CreateHeader(pSheet, wk, new string[] { strTHField, strDwgAreaField });
// }
//}
//string strAllDwgPath = @"D:GeiHeNingNing";
//if (!Directory.Exists(strAllDwgPath))
//{
// Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(strAllDwgPath + "路径不存在");
// return;
//}
//string strDWGAreaLayerName = "FQ";
//PromptStringOptions pStrOpts = new PromptStringOptions("n请输入结合表二维线图层名称: ");
//pStrOpts.AllowSpaces = false;
//pStrOpts.UseDefaultValue = true;
//pStrOpts.DefaultValue = strDWGAreaLayerName;
//ArrayList layerArrayList = null;
//int iRowCount = 0;
//string[] strDwgPathArray = Directory.GetFiles(strAllDwgPath, "*.dwg", SearchOption.TopDirectoryOnly);
//Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
//DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
//Database dbOld = HostApplicationServices.WorkingDatabase;
//bool blValidateFQLayer = false;
//foreach (string strEveryDwg in strDwgPathArray)
//{
// if (File.Exists(strEveryDwg))
// {
// using (Database db = new Database(false, true))
// {
// db.ReadDwgFile(strEveryDwg, FileShare.ReadWrite,false,null);
// db.Fillmode = true;
// HostApplicationServices.WorkingDatabase = db;
// if (!blValidateFQLayer)
// {
// PromptResult pStrRes = ed.GetString(pStrOpts);
// if (layerArrayList == null)
// {
// layerArrayList = GetLayerName(db);
// }
// if (pStrRes.Status == PromptStatus.OK)
// {
// strDWGAreaLayerName = pStrRes.StringResult;
// ed.WriteMessage("您输入的DWG计算面积图层名称为" + strDWGAreaLayerName);
// if (!layerArrayList.Contains(strDWGAreaLayerName))
// {
// Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("未找到DWG计算面积图层名称" + strDWGAreaLayerName);
// blValidateFQLayer = false;
// HostApplicationServices.WorkingDatabase = dbOld;
// docLock.Dispose();
// return;
// }
// blValidateFQLayer = true;
// }
// }
// using (Transaction acTrans = db.TransactionManager.StartTransaction())
// {
// ObjectIdCollection objDwgFQEntityCol = GetObjectidsAtLayer(strDWGAreaLayerName);
// if (objDwgFQEntityCol.Count == 1)
// {
// foreach (ObjectId DwgFQEntityID in objDwgFQEntityCol)
// {
// Entity pDwgFQEntity = acTrans.GetObject(DwgFQEntityID, OpenMode.ForRead) as Entity;
// Polyline2d pPolyline2d = pDwgFQEntity as Polyline2d;
// if (pPolyline2d != null)
// {
// string strTH = System.IO.Path.GetFileNameWithoutExtension(strEveryDwg);
// int iRowIndex = GetTHRowIndexFromNOPIExcel(strTH, pSheet);
// IRow pRow = null;
// if (iRowIndex == -1)
// {
// pRow = pSheet.CreateRow(++iRowCount);
// ed.WriteMessage("n正在写入第" + iRowCount.ToString() + "个@@@单元名:" + strTH + "@@@的面积");
// }
// else
// {
// pRow = pSheet.GetRow(iRowIndex);
// ed.WriteMessage("n正在写入第" + String.Format("{0}", iRowIndex) + "个@@@单元名:" + strTH + "@@@的面积");
// }
// ICell pCell = pRow.CreateCell(GetFieldColumnIndexFromNOPIExcel(strTHField, pSheet) > -1 ? GetFieldColumnIndexFromNOPIExcel(strTHField, pSheet) : 0);
// pCell.SetCellValue(strTH);
// pCell = pRow.CreateCell(GetFieldColumnIndexFromNOPIExcel(strDwgAreaField, pSheet) > -1 ? GetFieldColumnIndexFromNOPIExcel(strDwgAreaField, pSheet) : 0);
// pCell.SetCellValue(pPolyline2d.Area.ToString("f4"));
// }
// }
// }
// acTrans.Commit();
// }
// db.SaveAs(strEveryDwg, DwgVersion.Current);
// HostApplicationServices.WorkingDatabase = dbOld;
// }
// }
//}
//ed.WriteMessage("处理完成:总共处理" + strDwgPathArray.Length.ToString() + "个单元;图号面积存储在" + strAppExcel);
//docLock.Dispose();
//using (FileStream fs = File.OpenWrite(strAppExcel))
//{
// wk.Write(fs);
// fs.Close();
//}
//#endregion
}
由于采用了对话框,现将对话框代码粘贴如下:
选择文本路径触发事件:
private void btnNewDwgBrowse_Click(object sender, EventArgs e)
{
FolderBrowserDialog pFolderBrowserDialog = new FolderBrowserDialog();
pFolderBrowserDialog.ShowNewFolderButton = false;
pFolderBrowserDialog.Description = "选择DWG文件路径";
if (!String.IsNullOrEmpty(tbxNewDWGPath.Text))
{
pFolderBrowserDialog.SelectedPath = tbxNewDWGPath.Text;
}
if (pFolderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if(pFolderBrowserDialog.SelectedPath.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()))
{
tbxNewDWGPath.Text= pFolderBrowserDialog.SelectedPath;
}
else
{
tbxNewDWGPath.Text = pFolderBrowserDialog.SelectedPath+System.IO.Path.DirectorySeparatorChar.ToString();
}
}
}
退出按钮点击触发事件:
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
确定按钮点击触发事件:
private void btnOK_Click(object sender, EventArgs e)
{
string strAllDwgPath = tbxNewDWGPath.Text;
if (String.IsNullOrEmpty(tbxNewDWGPath.Text))
{
MessageBox.Show("DWG文件夹路径为空");
return;
}
if (!Directory.Exists(strAllDwgPath))
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(strAllDwgPath + "路径不存在");
return;
}
string strAppExcel = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "JHBArea.xls");
HSSFWorkbook wk = null;
ISheet pSheet = null;
if (File.Exists(strAppExcel))
{
using (FileStream fs = new FileStream(strAppExcel, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
try
{
wk = new HSSFWorkbook(fs);
}
catch
{
File.Delete(strAppExcel);
wk = new HSSFWorkbook();
}
pSheet = wk.GetSheet("mysheet");
if (pSheet == null)
{
pSheet = wk.CreateSheet("mysheet");
}
CADJHBArea.CreateHeader(pSheet, wk, new string[] { CADJHBArea.strTHField, CADJHBArea.strDwgAreaField });
}
}
else
{
using (FileStream fs = new FileStream(strAppExcel, FileMode.CreateNew, FileAccess.ReadWrite))
{
wk = new HSSFWorkbook();
pSheet = wk.GetSheet("mysheet");
if (pSheet == null)
{
pSheet = wk.CreateSheet("mysheet");
}
CADJHBArea.CreateHeader(pSheet, wk, new string[] { CADJHBArea.strTHField, CADJHBArea.strDwgAreaField });
}
}
string strDWGAreaLayerName = "FQ";
PromptStringOptions pStrOpts = new PromptStringOptions("n请输入结合表二维线图层名称: ");
pStrOpts.AllowSpaces = false;
pStrOpts.UseDefaultValue = true;
pStrOpts.DefaultValue = strDWGAreaLayerName;
ArrayList layerArrayList = null;
int iRowCount = 0;
string[] strDwgPathArray = Directory.GetFiles(strAllDwgPath, "*.dwg", SearchOption.TopDirectoryOnly);
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
DocumentLock docLock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument();
Database dbOld = HostApplicationServices.WorkingDatabase;
bool blValidateFQLayer = false;
foreach (string strEveryDwg in strDwgPathArray)
{
if (File.Exists(strEveryDwg))
{
using (Database db = new Database(false, true))
{
db.ReadDwgFile(strEveryDwg, FileShare.ReadWrite, false, null);
db.Fillmode = true;
HostApplicationServices.WorkingDatabase = db;
if (!blValidateFQLayer)
{
PromptResult pStrRes = ed.GetString(pStrOpts);
if (layerArrayList == null)
{
layerArrayList = CADJHBArea.GetLayerName(db);
}
if (pStrRes.Status == PromptStatus.OK)
{
strDWGAreaLayerName = pStrRes.StringResult;
ed.WriteMessage("您输入的DWG计算面积图层名称为" + strDWGAreaLayerName);
if (!layerArrayList.Contains(strDWGAreaLayerName))
{
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("未找到DWG计算面积图层名称" + strDWGAreaLayerName);
blValidateFQLayer = false;
HostApplicationServices.WorkingDatabase = dbOld;
docLock.Dispose();
return;
}
blValidateFQLayer = true;
}
}
using (Transaction acTrans = db.TransactionManager.StartTransaction())
{
ObjectIdCollection objDwgFQEntityCol = CADJHBArea.GetObjectidsAtLayer(strDWGAreaLayerName);
if (objDwgFQEntityCol.Count == 1)
{
foreach (ObjectId DwgFQEntityID in objDwgFQEntityCol)
{
Entity pDwgFQEntity = acTrans.GetObject(DwgFQEntityID, OpenMode.ForRead) as Entity;
Polyline2d pPolyline2d = pDwgFQEntity as Polyline2d;
if (pPolyline2d != null)
{
string strTH = System.IO.Path.GetFileNameWithoutExtension(strEveryDwg);
int iRowIndex = CADJHBArea.GetTHRowIndexFromNOPIExcel(strTH, pSheet);
IRow pRow = null;
if (iRowIndex == -1)
{
pRow = pSheet.CreateRow(++iRowCount);
ed.WriteMessage("n正在写入第" + iRowCount.ToString() + "个@@@单元名:" + strTH + "@@@的面积");
}
else
{
pRow = pSheet.GetRow(iRowIndex);
ed.WriteMessage("n正在写入第" + String.Format("{0}", iRowIndex) + "个@@@单元名:" + strTH + "@@@的面积");
}
ICell pCell = pRow.CreateCell(CADJHBArea.GetFieldColumnIndexFromNOPIExcel(CADJHBArea.strTHField, pSheet) > -1 ? CADJHBArea.GetFieldColumnIndexFromNOPIExcel(CADJHBArea.strTHField, pSheet) : 0);
pCell.SetCellValue(strTH);
pCell = pRow.CreateCell(CADJHBArea.GetFieldColumnIndexFromNOPIExcel(CADJHBArea.strDwgAreaField, pSheet) > -1 ? CADJHBArea.GetFieldColumnIndexFromNOPIExcel(CADJHBArea.strDwgAreaField, pSheet) : 0);
pCell.SetCellValue(pPolyline2d.Area.ToString("f4"));
}
}
}
acTrans.Commit();
}
db.SaveAs(strEveryDwg, DwgVersion.Current);
HostApplicationServices.WorkingDatabase = dbOld;
}
}
}
ed.WriteMessage("处理完成:总共处理" + strDwgPathArray.Length.ToString() + "个单元;图号面积存储在" + strAppExcel);
docLock.Dispose();
using (FileStream fs = File.OpenWrite(strAppExcel))
{
wk.Write(fs);
fs.Close();
}
this.DialogResult = DialogResult.OK;
}
以下是对话框图片:
说明如下:里面凡是涉及到CADJHBArea类的静态方法,请参考我的上一篇博客:CAD环境中求算接合表面积
最后
以上就是平常宝贝为你收集整理的CAD环境中批量求算DWG面积的全部内容,希望文章能够帮你解决CAD环境中批量求算DWG面积所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复