我是靠谱客的博主 平常宝贝,这篇文章主要介绍CAD环境中批量求算DWG面积,现在分享给大家,希望可以做个参考。

由于DWG图层中,面积单元都是存放在FQ层中,所以需要批量计算FQ图层的面积。

主要代码如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
[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 }

由于采用了对话框,现将对话框代码粘贴如下:
选择文本路径触发事件:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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(); } } }
退出按钮点击触发事件:

复制代码
1
2
3
4
private void btnCancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; }

确定按钮点击触发事件:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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面积内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部