/// <summary> /// 新建AutoCAD进程 /// </summary> /// <returns></returns> private static AcadApplication NewCAD() { AcadApplication app=null; bool flag = false; while (app == null) { try { app = new AcadApplicationClass(); } catch { } } while (!flag) { try { app.Visible = false; app.ActiveDocument.Close(); flag = true; } catch { flag = false; } } return app; }
public AcadApplication AutoCADConnector() { AcadApplication cad; try { // Upon creation, attempt to retrieve running instance cad = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.18"); cad.Visible = false; } catch { try { // Create an instance and set flag to indicate this cad = new AcadApplicationClass(); cad.Visible = false; return cad; } catch (Exception e2) { MessageBox.Show("请安装AutoCAD2006或以上版本。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return null; } } return cad; }
private void DWGLoad() { AcadApplication acadApp = new AcadApplicationClass(); IGeometryCollection pGeometryCollection = new PolygonClass(); foreach (DataGridViewRow eRow in dataGridView1.Rows) { if (eRow.Cells[0].Value != null && (bool)eRow.Cells[0].Value == true) { if (eRow.Cells["FileSuffix"].Value.ToString().ToLower() == "dwg") { object o = Type.Missing; string strFileName = eRow.Cells["FilePath"].Value.ToString(); AcadDocument acadDoc = acadApp.Documents.Open(strFileName, true, null); System.Windows.Forms.Application.DoEvents(); AcadLayer acadLyer = acadDoc.Layers.Item(1); AcadSelectionSet ssetObj = acadDoc.SelectionSets.Add("FWX"); short[] vFilterType = null; object[] vFilterData = null; vFilterType = new short[1]; vFilterType[0] = 8; vFilterData = new object[1]; vFilterData[0] = "FWX"; //ISegmentCollection pSegmentCollection = new RingClass(); //pSegmentCollection.AddSegment() ssetObj.Select(AcSelect.acSelectionSetAll, null, null, vFilterType, vFilterData); foreach (AcadObject eEntity in ssetObj) { if (eEntity.ObjectName == "AcDbPolyline") { AcadLWPolyline pPline = (AcadLWPolyline)eEntity; double[] polyLinePoint; polyLinePoint = (Double[])pPline.Coordinates; int i, pointCount = polyLinePoint.Length / 2; IPointCollection pPointColl = new RingClass(); for (i = 0; i < polyLinePoint.Length - 1; i = i + 2) { IPoint pPoint = new PointClass(); pPoint.X = polyLinePoint[i]; pPoint.Y = polyLinePoint[i + 1]; pPointColl.AddPoint(pPoint, ref o, ref o); } pGeometryCollection.AddGeometry(pPointColl as IRing, ref o, ref o); } } } else if (eRow.Cells["FileSuffix"].Value.ToString().ToLower() == "txt") { object o = Type.Missing; string strFileName = eRow.Cells["FilePath"].Value.ToString(); m_strPointArray.Clear(); StreamReader ReadFile = new StreamReader(strFileName, System.Text.Encoding.Default); while (!ReadFile.EndOfStream) { m_strPointArray.Add(ReadFile.ReadLine()); } ReadFile.Close(); IPointCollection pPointColl = new RingClass(); for (int i = 0; i < m_strPointArray.Count; i++) { if (m_strPointArray[i].StartsWith("J")) { string[] split = m_strPointArray[i].Split(new Char[] { ',', ',' }); IPoint pPoint = new PointClass(); pPoint.X = Convert.ToDouble(split[2]); pPoint.Y = Convert.ToDouble(split[3]); pPointColl.AddPoint(pPoint, ref o, ref o); } } pGeometryCollection.AddGeometry(pPointColl as IRing, ref o, ref o); } } } acadApp.Quit(); //DWGLoaded(this.m_Popfrm, new EventArgs());//引发完成事件,有异常待研究 System.Windows.Forms.Application.DoEvents(); if (pGeometryCollection.GeometryCount > 0) { m_pFeature = EngineFuntions.m_Layer_BusStation.FeatureClass.CreateFeature(); m_pFeature.Shape = pGeometryCollection as IPolygon; IFields fields = m_pFeature.Fields; int nIndex = fields.FindField("任务号"); m_pFeature.set_Value(nIndex, m_strFolder.Substring(m_strFolder.LastIndexOf("\\") + 1)); m_pFeature.Store(); EngineFuntions.ZoomTo(m_pFeature.ShapeCopy); } else { m_pFeature = null; } }