static void Main(string[] args) { // Start an AutoCAD instance AcadApplication acad = new AcadApplication(); Console.WriteLine("AutoCAD started!"); //Open an AxDbDocument object obj = acad.GetInterfaceObject("ObjectDBX.AxDbDocument.22"); //Acad2018 AxDbDocument doc = obj as AxDbDocument; if (doc == null) { Console.WriteLine("Cannot open AxDbDocument for some reason!"); return; } else { doc.Open(@"a:\Dropbox\Infratools\2_Presentation\4.0 Automation\Dictionary\test.dwg"); List<string> dicts = AcDictToList(doc.Database); Console.WriteLine(dicts.Count + " Dictionaries found."); foreach (string s in dicts) Console.WriteLine(s); } //Get drawing information //Console.WriteLine("Entities in ModelSpace: " + doc.ModelSpace.Count); //You can make changes to the entities in AxDbDocument and save the changes back to the drawing file. //doc.Save(); doc = null; acad.Quit(); Console.Read(); }
/// <summary> /// 替换装配图中的明细表中的编号 /// </summary> /// <param name="entity"></param> /// <param name="acAppComObj"></param> /// <param name="ds"></param> public static void ReplaceDrawingCode(AcadEntity entity, AcadApplication acAppComObj) { if (entity.ObjectName == "AcmPartRef") { AXDBLib.AcadObject obj = entity as AXDBLib.AcadObject; McadSymbolBBMgr symbb = (McadSymbolBBMgr)acAppComObj.GetInterfaceObject("SymBBAuto.McadSymbolBBMgr"); McadBOMMgr bommgr = (McadBOMMgr)symbb.BOMMgr; oldCode = bommgr.GetPartAttribute(obj, "DESCR", false); string newCode = getByNewCode(oldCode); if (!string.IsNullOrEmpty(newCode)) { bommgr.SetPartAttribute(obj, "DESCR", newCode); } } }
/// <summary> /// Gets the application. /// </summary> /// <returns></returns> internal AeccRoadwayApplication GetApplication() { Utils.Log(string.Format("GetApplication started...", "")); string m_sAcadProdID = "AutoCAD.Application"; string[] progids = new string[] { "AeccXUiRoadway.AeccRoadwayApplication.13.2", // 2020 "AeccXUiRoadway.AeccRoadwayApplication.13.0", // 2019 "AeccXUiRoadway.AeccRoadwayApplication.12.0", // 2018 "AeccXUiRoadway.AeccRoadwayApplication.11.0", // 2017 "AeccXUiRoadway.AeccRoadwayApplication.10.5" // 2016 }; #if C2021 progids = new string[] { "AeccXUiRoadway.AeccRoadwayApplication.13.3" }; // 2021 #endif AcadApplication m_oAcadApp = System.Runtime.InteropServices.Marshal.GetActiveObject(m_sAcadProdID) as AcadApplication; // Roadway application dynamic output = null; foreach (string r_sAeccAppProgId in progids) { try { output = m_oAcadApp.GetInterfaceObject(r_sAeccAppProgId); if (output != null) { break; } } catch { continue; } } //return m_oAcadApp.GetInterfaceObject(r_sAeccAppProgId); Utils.Log(string.Format("GetApplication completed.", "")); return(output); }
/// <summary> /// 替换装配图中的明细表中的编号 /// </summary> /// <param name="entity"></param> /// <param name="acAppComObj"></param> /// <param name="ds"></param> public static void ReplaceDrawingCode(AcadEntity entity, AcadApplication acAppComObj) { if (entity.ObjectName == "AcmPartRef") { AXDBLib.AcadObject obj = entity as AXDBLib.AcadObject; McadSymbolBBMgr symbb = (McadSymbolBBMgr)acAppComObj.GetInterfaceObject("SymBBAuto.McadSymbolBBMgr"); McadBOMMgr bommgr = (McadBOMMgr)symbb.BOMMgr; oldCode = bommgr.GetPartAttribute(obj, "DESCR", false); string newCode = getByNewCode(oldCode); if (!string.IsNullOrEmpty(newCode)) bommgr.SetPartAttribute(obj, "DESCR", newCode); } }
private void iconButton3_Click(object sender, EventArgs e) { string nombre; foreach (string g in Modulo.listaFinal) { nombre = Modulo.RutaGuardado + "\\" + g + ".dwg"; File.Copy(Modulo.RutaAutocad, nombre, true); } List <Ejecucion> listaEjecucion = new List <Ejecucion>(); Ejecucion ejecucion; foreach (Localidades k in Modulo.matrizRaiz) { foreach (string z in Modulo.listaFinal) { if (z == k.nombre_plano) { ejecucion = new Ejecucion(); ejecucion.loc = k.id; ejecucion.ruta = k.nombre_plano; listaEjecucion.Add(ejecucion); } } } ejecucion = null; //inicializacion del autocad Autodesk.AutoCAD.Interop.AcadApplication AcadApp = new AcadApplication(); Autodesk.AutoCAD.Interop.AcadDocument doc; AcadApp.Visible = true; string rutaAutocad; //creacion de los objetos en listas obExcel f = new obExcel(); IEnumerable <Vanos> vanos; IEnumerable <Postes> postes; IEnumerable <Luminarias> luminarias; IEnumerable <Subestaciones> subestaciones; IEnumerable <VistasC> vistasCajetines; IEnumerable <VistasD> vistasDibujo; IEnumerable <Layers> capas; IEnumerable <Cajetines> cajetienes; double[] pto = new double[3]; double[] pto2 = new double[3]; double[] pto3 = new double[3]; double[] pto4 = new double[3]; pto[2] = 0.0; pto2[2] = 0.0; pto3[2] = 0.0; pto4[2] = 0.0; AcadLine lin; AcadBlockReference block; AcadLayer layer; AcadPViewport vistaCajetin; AcadPViewport vistaPlano; AcadAcCmColor color = null; AcadDimAligned dim; AcadDimAligned con; try { color = AcadApp.GetInterfaceObject("AutoCAD.AcCmColor.17") as AcadAcCmColor; } catch { }; try { color = AcadApp.GetInterfaceObject("AutoCAD.AcCmColor.18") as AcadAcCmColor; } catch { }; try { color = AcadApp.GetInterfaceObject("AutoCAD.AcCmColor.19") as AcadAcCmColor; } catch { }; try { color = AcadApp.GetInterfaceObject("AutoCAD.AcCmColor.20") as AcadAcCmColor; } catch { }; try { color = AcadApp.GetInterfaceObject("AutoCAD.AcCmColor.21") as AcadAcCmColor; } catch { }; try { color = AcadApp.GetInterfaceObject("AutoCAD.AcCmColor.22") as AcadAcCmColor; } catch { }; try { color = AcadApp.GetInterfaceObject("AutoCAD.AcCmColor.23") as AcadAcCmColor; } catch { }; var book = new ExcelQueryFactory(Modulo.RutaExcel); listaEjecucion.Distinct <Ejecucion>(); foreach (Ejecucion loc in listaEjecucion) { rutaAutocad = Modulo.RutaGuardado + "\\" + loc.ruta + ".dwg"; doc = AcadApp.Documents.Open(rutaAutocad); vanos = f.VanosData(loc.loc, book); postes = f.PostesData(loc.loc, book); luminarias = f.LuminariasData(loc.loc, book); subestaciones = f.SubestacionesData(loc.loc, book); vistasCajetines = f.VistasCData(book); vistasDibujo = f.VistasDData(loc.loc, book); cajetienes = f.CajetinesData(loc.loc, book); capas = f.LayersData(book); foreach (Layers g in capas) { color.SetRGB(g.red, g.green, g.blue); layer = doc.Layers.Add(g.nombre_capa); layer.TrueColor = color; } //dibujamos los vanos if (VanosBox.Checked) { foreach (Vanos g in vanos) { pto[0] = g.x_inicial; pto[1] = g.y_inicial; pto2[0] = g.x_final; pto2[1] = g.y_final; pto3[0] = pto[0] / 2 + pto2[0] / 2; pto3[1] = pto[1] / 2 + pto2[1] / 2; lin = doc.ModelSpace.AddLine(pto, pto2); lin.Layer = "_AereoCond"; dim = doc.ModelSpace.AddDimAligned(pto, pto2, pto3); dim.StyleName = "ACOT-LP-FRANK"; dim.VerticalTextPosition = AcDimVerticalJustification.acAbove; dim.Layer = "_CotaCond"; con = doc.ModelSpace.AddDimAligned(pto, pto2, pto3); con.TextOverride = g.conductor; con.StyleName = "ACOT-LP-FRANK"; con.VerticalTextPosition = AcDimVerticalJustification.acUnder; con.Layer = "_TipoCond"; } } //metodo de postes if (Postesbox.Checked) { foreach (Postes g in postes) { pto[0] = g.x_poste; pto[1] = g.y_poste; block = doc.ModelSpace.InsertBlock(pto, g.bloque, 1, 1, 1, 0); block.Layer = "_Postes"; var blocAtri = block.GetAttributes(); blocAtri[0].TextString = g.altura; blocAtri[1].TextString = g.estado; blocAtri[2].TextString = g.material; blocAtri[3].TextString = g.cod_poste; } } //metodo de luminarias if (LumBox.Checked) { foreach (Luminarias g in luminarias) { pto[0] = g.x_luminaria; pto[1] = g.y_luminaria; block = doc.ModelSpace.InsertBlock(pto, "luminaria", 1, 1, 1, 0); block.Layer = "_Luminaria"; var blocAtri = block.GetAttributes(); blocAtri[0].TextString = g.potencia; blocAtri[1].TextString = g.altura; blocAtri[2].TextString = g.codigo_luminaria; } } //metodo de subestaciones if (SedBox.Checked) { foreach (Subestaciones g in subestaciones) { pto[0] = g.x_subestacion; pto[1] = g.y_subestacion; block = doc.ModelSpace.InsertBlock(pto, g.bloque, 25, 25, 25, 0); block.Layer = "_Seds"; var blocAtri = block.GetAttributes(); blocAtri[0].TextString = g.potencia; blocAtri[1].TextString = g.codigo_subestacion; } } //metodo de creacion de vistas en cajetines if (CajBox.Checked) { foreach (VistasC g in vistasCajetines) { pto[0] = g.x_centro; pto[1] = g.y_centro; pto2[0] = g.x_inicial; pto2[1] = g.y_inicial; pto3[0] = g.x_final; pto3[1] = g.y_final; doc.ActiveSpace = AcActiveSpace.acPaperSpace; vistaCajetin = doc.PaperSpace.AddPViewport(pto, g.ancho, g.largo); vistaCajetin.Display(true); vistaCajetin.ViewportOn = true; doc.MSpace = true; AcadApp.ActiveDocument.ActivePViewport = vistaCajetin; AcadApp.ZoomWindow(pto2, pto3); doc.MSpace = false; } //metodo de creacion de vistas en planos foreach (VistasD g in vistasDibujo) { pto[0] = g.x_centro; pto[1] = g.y_centro; pto2[0] = g.x_inicial; pto2[1] = g.y_inicial; pto3[0] = g.x_final; pto3[1] = g.y_final; doc.ActiveSpace = AcActiveSpace.acPaperSpace; vistaPlano = doc.PaperSpace.AddPViewport(pto, g.ancho, g.largo); vistaPlano.Display(true); vistaPlano.ViewportOn = true; doc.MSpace = true; AcadApp.ActiveDocument.ActivePViewport = vistaPlano; AcadApp.ZoomWindow(pto2, pto3); doc.MSpace = false; } //metodo de cajetines foreach (Cajetines g in cajetienes) { pto[0] = 0; pto[1] = 0; block = doc.PaperSpace.InsertBlock(pto, "cajetin", 1, 1, 1, 0); block.Layer = "_MarcoPlano"; var blocAtri = block.GetAttributes(); blocAtri[0].TextString = g.escala; blocAtri[1].TextString = g.fecha; blocAtri[2].TextString = g.plano; blocAtri[3].TextString = g.n_expediente; blocAtri[4].TextString = g.departamento; blocAtri[5].TextString = g.provincia; blocAtri[6].TextString = g.distrito; blocAtri[7].TextString = g.revisado; blocAtri[8].TextString = g.aprobado; blocAtri[9].TextString = g.dibujado; blocAtri[10].TextString = g.anexo; } } doc.Save(); doc.Close(); } book.Dispose(); AcadApp.Quit(); }
public static void Launch_Autocad() { //object res; // "AutoCAD.Application.17" uses 2007 or 2008, // whichever was most recently run // "AutoCAD.Application.17.1" uses 2008, specifically // "AutoCAD.Application.16.2" uses 2006 // "AutoCAD.Application.18" uses 2010 const string progID = "AutoCAD.Application.18"; AcadApplication acApp = null; try { acApp = (AcadApplication)Marshal.GetActiveObject(progID); } catch { try { Type acType = Type.GetTypeFromProgID(progID); acApp = (AcadApplication)Activator.CreateInstance(acType, true); } catch { MessageBox.Show("Cannot create object of type \"" + progID + "\""); } } if (acApp != null) { try { // By the time this is reached AutoCAD is fully // functional and can be interacted with through code acApp.Visible = true; ActivCAD.ActivCAD.IActivCAD app = (ActivCAD.ActivCAD.IActivCAD)acApp.GetInterfaceObject("ActivCAD.Commands"); if (app != null) { //---------------------------------------- On dessine le repère -------------------------------------------------- //---------------------------------------------------------------------------------------------------------------- Draw_Repere(app); //---------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------- //New_Calc(); //---------------------------------------- On dessine les observations ------------------------------------------- //---------------------------------------------------------------------------------------------------------------- Draw_Observations(app); //---------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------- // Let's generate the arguments to pass in: // an integer and a double //x1 / y1 / x2 / y2 / epaisseur / R / G / B / linetype / linefile //Observations //object[] args_poly1 = { 12, 34, 22, 43, 2, 255, 255, 0, "legende", "Continuous", Properties.Settings.Default.LignePath }; //object[] args_poly2 = { 37, 19, 42, 50, 1, 55, 255, 0, "legende", "Continuous", Properties.Settings.Default.LignePath }; //object[] args_poly3 = { 136, 70, 166, 52, 1, 55, 205, 0, "legende", "Continuous", Properties.Settings.Default.LignePath }; //Hachures //object[] args_surf1 = { 69, 78, 47, 65, 120, 0.6, 252, 125, 252, "legende", "NET3", Properties.Settings.Default.HachurePath }; // Now let's call our method dynamically //object res = app.GetType().InvokeMember("Init_Autocad_Environment", BindingFlags.InvokeMethod, null, app, null); //res = app.GetType().InvokeMember("AjoutePolyline", BindingFlags.InvokeMethod, null, app, args_poly1); //res = app.GetType().InvokeMember("AjoutePolyline", BindingFlags.InvokeMethod, null, app, args_poly2); //res = app.GetType().InvokeMember("AjoutePolyline", BindingFlags.InvokeMethod, null, app, args_poly3); //res = app.GetType().InvokeMember("AjouteSurface", BindingFlags.InvokeMethod, null, app, args_surf1); //app.GetType().InvokeMember("MyPoly", BindingFlags.InvokeMethod, null, app, null); acApp.ZoomExtents(); //MessageBox.Show("AddNumbers returned: " + res.ToString()); } } catch (System.Exception ex) { MessageBox.Show("Problem executing component: " + ex.Message); } } }