public void OnButton2Pressed(IRibbonControl control) { try { AcadApp = (Autodesk.AutoCAD.Interop.AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application"); AcadDoc = AcadApp.ActiveDocument; } catch { OpenFileDialog op = new OpenFileDialog(); op.Filter = "CAD文件(*.dwg)|*.dwg|CAD图形文件(*.dxf)|*.dxf"; op.Title = "打开CAD文件"; op.ShowDialog(); string filePath = op.FileName; if (filePath == "") { MessageBox.Show("选择CAD文件无效!!", "文件无效!!"); } else { AcadApp = new Autodesk.AutoCAD.Interop.AcadApplication(); AcadDoc = AcadApp.Documents.Open(filePath, null, null); } } AcadApp.Application.Visible = true; //使CAD程序跳到在最前面,需要添加引用“Microsoft.VisualBasic” Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption); //让CAD自动执行netload命令加载程序集DLL,如果注册表加载方法无效的话 AcadDoc.SendCommand("(command \"_netload\" \"" + @"C:\\Windows\\System\\xl2cad\\xl2cad.dll" + "\") "); AcadDoc.SendCommand("zselect "); }
private void Send_btn_Click(object sender, EventArgs e) { if (Tabs_cbx.Text == "") { MessageBox.Show("Please Select a Tab", "Select a Tab", MessageBoxButtons.OK, MessageBoxIcon.Error); Tabs_cbx.Focus(); return; } PlanSwift.Tab CurrentTab; CurrentTab = TGlobal.pc.Tabs.ByName(Tabs_cbx.Text); CurrentTab.MakeActive(); AuCad = new Autodesk.AutoCAD.Interop.AcadApplication(); AuCad.Visible = true; AuDocument = AuCad.ActiveDocument; for (int ItemIndex = 0; ItemIndex <= CurrentTab.Count - 1; ItemIndex++) { GetItems(CurrentTab[ItemIndex]); } AuCad.ZoomAll(); AuCad = null; AuDocument = null; this.Close(); }
public override void DeleteCADObject(Autodesk.AutoCAD.ApplicationServices.Document doc) { string[] Layers = null; if (this.SURVEY_ID.StartsWith("WS")) { Layers = new string[] { "0", "WSPoint", "WSText" } } ; else { Layers = new string[] { "0", "YSPoint", "YSText" } }; Autodesk.AutoCAD.Interop.AcadDocument AcadDoc = doc.AcadDocument as Autodesk.AutoCAD.Interop.AcadDocument; Editor ed = doc.Editor; int count = 0; foreach (string Layer in Layers) { TypedValue[] tvs = new TypedValue[] { new TypedValue((int)DxfCode.LayerName, Layer) }; SelectionFilter sf = new SelectionFilter(tvs); PromptSelectionResult psr = ed.SelectAll(sf); if (psr.Status == PromptStatus.OK) { SelectionSet SS = psr.Value; ObjectId[] idArray = SS.GetObjectIds(); for (int i = 0; i < idArray.Length; i++) { if (this is IPipePoint && count == 2) { break; } else if (this is IPIPELine && count == 3) { break; } //Entity ent = (Entity)Tools.GetDBObject(idArray[i]); AcadEntity pAcadEntity = AcadDoc.ObjectIdToObject(idArray[i].OldIdPtr.ToInt64()) as AcadEntity; string ObjectID = GetPointObjectID(pAcadEntity); if (ObjectID.Equals(this.ID)) { pAcadEntity.Delete(); count++; } } } } AcadDoc.Save(); }
//Ribbon界面的回调函数,响应事件,将Excel表格数据写入CAD public void OnButton1Pressed(IRibbonControl control) { //选取Excel表格数据 wbk = app.ActiveWorkbook; wsh = (Worksheet)wbk.ActiveSheet; Excel.Range rngLeftTop = null; Excel.Range rngRightButtom = null; rngLeftTop = (Excel.Range)app.InputBox("点击左上角单元格", Type: 8); rngRightButtom = (Excel.Range)app.InputBox("点击右下角单元格", Type: 8); object[,] data = (object[, ])wsh.Range[rngLeftTop.Address + ":" + rngRightButtom.Address].Value2; //把数据导入Access数据库 try { AccessDataBase.WriteDB(data); MessageBox.Show("选定的数据读取完毕,点击确定导入CAD"); } catch (SystemException ex) { MessageBox.Show(ex.ToString()); } //选定的数据读取完毕,点击确定导入CAD if (rngLeftTop != null & rngRightButtom != null) { //打开cad try { AcadApp = (Autodesk.AutoCAD.Interop.AcadApplication)System.Runtime.InteropServices.Marshal.GetActiveObject("AutoCAD.Application"); AcadDoc = AcadApp.ActiveDocument; } catch { OpenFileDialog op = new OpenFileDialog(); op.Filter = "CAD文件(*.dwg)|*.dwg|CAD图形文件(*.dxf)|*.dxf"; op.Title = "打开CAD文件"; op.ShowDialog(); string filePath = op.FileName; if (filePath == "") { MessageBox.Show("选择CAD文件无效!!", "文件无效!!"); } else { AcadApp = new Autodesk.AutoCAD.Interop.AcadApplication(); AcadDoc = AcadApp.Documents.Open(filePath, null, null); } } AcadApp.Application.Visible = true; //使CAD程序跳到在最前面,需要添加引用“Microsoft.VisualBasic” Microsoft.VisualBasic.Interaction.AppActivate(AcadApp.Caption); //让CAD自动执行netload命令加载程序集DLL,如果注册表加载方法无效的话 AcadDoc.SendCommand("(command \"_netload\" \"" + @"C:\\Windows\\System\\xl2cad\\xl2cad.dll" + "\") "); AcadDoc.SendCommand("zdbtext "); } else { MessageBox.Show("没有选择数据!"); } }
public override void DrawCADObject(Autodesk.AutoCAD.Interop.AcadDocument AcadDoc) { double pX = double.Parse(this.X); double pY = double.Parse(this.Y); double[] InsertPoint = new double[] { pX, pY, 0 }; string BlockName = "一般管线点"; int LayerID = GetLayerIndex("0", AcadDoc); //string TrueType = GetTrueType(this.Type); if (this.Type == "雨水篦") { BlockName = "雨篦"; } else if (this.Type == "检修井") { BlockName = "排水检修井"; } else if (this.Type == "出水口") { BlockName = "出水口"; } if (!string.IsNullOrEmpty(this.Type)) { if (this.SURVEY_ID.StartsWith("WS")) { LayerID = GetLayerIndex("WSPoint", AcadDoc); } else if (this.SURVEY_ID.StartsWith("YS")) { LayerID = GetLayerIndex("YSPoint", AcadDoc); } } AcadDoc.ActiveLayer = AcadDoc.Layers.Item(LayerID); AcadMInsertBlock pAcadMInsertBlock = AcadDoc.ModelSpace.AddMInsertBlock(InsertPoint, BlockName, 1, 1, 1, 0, 1, 1, 1, 1); //pAcadMInsertBlock.TrueColor.SetRGB(255, 255, 255); AcadDictionary pAcadDictionary = pAcadMInsertBlock.GetExtensionDictionary(); //pAcadDictionary.AddXRecord(ClassName); pAcadDictionary.AddXRecord(this.ID); LayerID = GetLayerIndex("0", AcadDoc); if (!string.IsNullOrEmpty(this.Type)) { if (this.SURVEY_ID.StartsWith("WS")) { LayerID = GetLayerIndex("WSText", AcadDoc); } else if (this.SURVEY_ID.StartsWith("YS")) { LayerID = GetLayerIndex("YSText", AcadDoc); } } AcadDoc.ActiveLayer = AcadDoc.Layers.Item(LayerID); AcadText pAcadText = AcadDoc.ModelSpace.AddText(this.SURVEY_ID, InsertPoint, 2.0); pAcadDictionary = pAcadText.GetExtensionDictionary(); //pAcadDictionary.AddXRecord(ClassName); pAcadDictionary.AddXRecord(this.ID); AcadDoc.Save(); }
public override void DrawCADObject(Autodesk.AutoCAD.Interop.AcadDocument AcadDoc) { string Linetype = "合流"; int LayerID = GetLayerIndex("YSLine", AcadDoc); if (this.US_SURVEY_ID.StartsWith("WS")) { LayerID = GetLayerIndex("WSLine", AcadDoc); Linetype = "污水"; } else if (this.US_SURVEY_ID.StartsWith("YS")) { Linetype = "雨水"; } AcadDoc.ActiveLayer = AcadDoc.Layers.Item(LayerID); IPCPoint SPoint = GetPointByID(this.US_SURVEY_ID); IPCPoint EPoint = GetPointByID(this.DS_SURVEY_ID); double[] StartPoint = new double[3] { double.Parse(SPoint.X), double.Parse(SPoint.Y), 0 }; double[] EndPoint = new double[3] { double.Parse(EPoint.X), double.Parse(EPoint.Y), 0 }; AcadLine pAcadLine = AcadDoc.ModelSpace.AddLine(StartPoint, EndPoint); AcadDictionary pAcadDictionary = pAcadLine.GetExtensionDictionary(); //pAcadDictionary.AddXRecord(ClassName); pAcadDictionary.AddXRecord(ID); string MinArrowVal = CIni.ReadINI("DrawCAD", "ArrowMin"); bool IsDrawArrow = false; if (string.IsNullOrEmpty(MinArrowVal)) { IsDrawArrow = true; } else { double MinArrow = double.Parse(MinArrowVal); if (pAcadLine.Length < MinArrow) { IsDrawArrow = false; } else { IsDrawArrow = true; } } double[] MidPoint = new double[3] { (double.Parse(SPoint.X) + double.Parse(EPoint.X)) / 2, (double.Parse(SPoint.Y) + double.Parse(EPoint.Y)) / 2, 0 }; if (IsDrawArrow) { string WidthValue = this.Width; AcadMInsertBlock pBlock = AcadDoc.ModelSpace.AddMInsertBlock(MidPoint, "GP4", 1, 1, 1, 0, 1, 1, 1, 1); pBlock.Rotate(MidPoint, pAcadLine.Angle); pAcadDictionary = pBlock.GetExtensionDictionary(); pAcadDictionary.AddXRecord(ID); } string MinLableVal = CIni.ReadINI("DrawCAD", "LableMin"); if (!string.IsNullOrEmpty(MinLableVal)) { double MinLable = double.Parse(MinLableVal); if (pAcadLine.Length < MinLable) { return; } } string pUS_INVERT_LEVEL = double.Parse(SPoint.INVERT_LEVEL).ToString("0.000"); string pDS_INVERT_LEVEL = double.Parse(EPoint.INVERT_LEVEL).ToString("0.000"); string LineLable = string.Format("{0} {1}m {2}Φ{3} {4}m", Linetype, pUS_INVERT_LEVEL, this.MATERIAL, this.Width, pDS_INVERT_LEVEL); LayerID = GetLayerIndex("YSZJ", AcadDoc); if (this.US_SURVEY_ID.StartsWith("WS")) { LayerID = GetLayerIndex("WSZJ", AcadDoc); } else { LayerID = GetLayerIndex("YSZJ", AcadDoc); } AcadDoc.ActiveLayer = AcadDoc.Layers.Item(LayerID); AcadText pAcadText = AcadDoc.ModelSpace.AddText(LineLable, MidPoint, 2.0); double LineAngle = pAcadLine.Angle; if (LineAngle > Math.PI / 2 && LineAngle < 3 * Math.PI / 2) { LineAngle = LineAngle - Math.PI; } pAcadText.Rotate(MidPoint, LineAngle); pAcadDictionary = pAcadText.GetExtensionDictionary(); //pAcadDictionary.AddXRecord(ClassName); pAcadDictionary.AddXRecord(ID); //} AcadDoc.Save(); }