using System; using System.Collections.Generic; // AutoCAD references #if ARX_APP using AcAp = Autodesk.AutoCAD.ApplicationServices; using AcCo = Autodesk.AutoCAD.Colors; using AcDb = Autodesk.AutoCAD.DatabaseServices; using AcEd = Autodesk.AutoCAD.EditorInput; #elif BRX_APP using AcAp = Bricscad.ApplicationServices; using AcCo = Teigha.Colors; using AcDb = Teigha.DatabaseServices; using AcEd = Bricscad.EditorInput; #endif #if ARX_APP namespace MRAppsACAD #elif BRX_APP namespace MRAppsBCAD #endif { public class MRHalo { [AcRt2.CommandMethod("Halo")] public static void Halo() { string haloLayer = "halotext255"; AcAp.Document doc = AcAp.Application.DocumentManager.MdiActiveDocument; AcDb.Database db = doc.Database; AcEd.Editor ed = doc.Editor; bool cancelled; AcEd.SelectionSet ss = Utilities.SelTextMText(out cancelled); if (!cancelled) { LayerTools.AddALayer(haloLayer, 255, "Continuous", 8, "White_200", "Halo background"); List originalEnts = Utilities.GetSelectedEntities(ss), copiedEnts = Utilities.CopySelectionSet(originalEnts, true, haloLayer); using (AcDb.Transaction tr = db.TransactionManager.StartTransaction()) { AcDb.DrawOrderTable dot = GetModelSpaceDrawOrderTable(db); for (int cei = 0; cei < copiedEnts.Count; cei++) { AcDb.Entity ent = copiedEnts[cei]; AcDb.ObjectIdCollection ids = new AcDb.ObjectIdCollection(); ids.Add(ent.ObjectId); dot.MoveBelow(ids, originalEnts[cei].ObjectId); } tr.Commit(); } } Utilities.SendACommand("Regenall "); } static AcDb.DrawOrderTable GetModelSpaceDrawOrderTable(AcDb.Database CurrDBase) { AcDb.DrawOrderTable dot = null; using (AcDb.Transaction t = CurrDBase.TransactionManager.StartTransaction()) { AcDb.BlockTable bt = t.GetObject(CurrDBase.BlockTableId, AcDb.OpenMode.ForRead) as AcDb.BlockTable; AcDb.BlockTableRecord btr = t.GetObject(bt[AcDb.BlockTableRecord.ModelSpace], AcDb.OpenMode.ForRead) as AcDb.BlockTableRecord; dot = t.GetObject(btr.DrawOrderTableId, AcDb.OpenMode.ForWrite) as AcDb.DrawOrderTable; t.Commit(); } return dot; } } public static class LayerTools { public static void AddALayer(string LayerName, short ColorNum, string LineType, Int32 LineWeightVal, string PlotStyle, string LyrDescrip) { AcEd.Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor; try { //string plotstyle = "Black_025"; // Set the required plot style name AddPlotStyle(PlotStyle); // Ensure that the plot style is added to the database //ObjectId id = this.CreateLayer(LayerName, ColorNum, LineType, LineWeightVal); // Create the layer AcDb.ObjectId id = CreateALayer(LayerName, ColorNum, LineType, LineWeightVal); AddPlotStyleToLayer(id, PlotStyle, LyrDescrip); // Set the plot style to the layer AddDescriptionToLayer(id, LyrDescrip); // Set the description to the layer } catch (System.Exception ex) { ed.WriteMessage("\n" + ex.Message + "\n" + ex.StackTrace); } } public static void AddDescriptionToLayer(AcDb.ObjectId id, string LyrDescrip) { AcDb.Database db = AcAp.Application.DocumentManager.MdiActiveDocument.Database; using (AcDb.Transaction tr = db.TransactionManager.StartTransaction()) { AcDb.LayerTableRecord ltr = (AcDb.LayerTableRecord)tr.GetObject(id, AcDb.OpenMode.ForWrite); ltr.Description = LyrDescrip; tr.Commit(); } } public static void AddPlotStyle(string PlotStyleName) { AcDb.Database db = AcAp.Application.DocumentManager.MdiActiveDocument.Database; AcDb.DictionaryWithDefaultDictionary dict = null; if (db.PlotStyleMode == false) // Check whether Colour-dependent or named plot styles. False = Named Plot Styles { AcDb.ObjectId pSId = db.PlotStyleNameDictionaryId; using (AcDb.Transaction tr = db.TransactionManager.StartTransaction()) { dict = (AcDb.DictionaryWithDefaultDictionary)tr.GetObject(pSId, AcDb.OpenMode.ForRead); if (!dict.Contains(PlotStyleName)) { dict.UpgradeOpen(); AcDb.PlaceHolder hldr = new AcDb.PlaceHolder(); dict.SetAt(PlotStyleName, hldr); tr.AddNewlyCreatedDBObject(hldr, true); } tr.Commit(); } } } public static void AddPlotStyleToLayer(AcDb.ObjectId id, string PlotStyleName, string LyrDescrip) { AcDb.Database db = AcAp.Application.DocumentManager.MdiActiveDocument.Database; if (db.PlotStyleMode == false) { using (AcDb.Transaction tr = db.TransactionManager.StartTransaction()) { AcDb.DictionaryWithDefaultDictionary d = (AcDb.DictionaryWithDefaultDictionary)tr.GetObject (db.PlotStyleNameDictionaryId, AcDb.OpenMode.ForRead); AcDb.LayerTableRecord ltr = (AcDb.LayerTableRecord)tr.GetObject(id, AcDb.OpenMode.ForWrite); ltr.PlotStyleNameId = d.GetAt(PlotStyleName); tr.Commit(); } } } public static AcDb.ObjectId CreateALayer(string LayerName, short ColorNum, string LineType, Int32 LineWeightVal) { AcEd.Editor ed = AcAp.Application.DocumentManager.MdiActiveDocument.Editor; AcDb.ObjectId layerId = AcDb.ObjectId.Null; AcDb.Database db = AcDb.HostApplicationServices.WorkingDatabase; AcDb.Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { AcDb.LayerTable lt = (AcDb.LayerTable)tr.GetObject(db.LayerTableId, AcDb.OpenMode.ForRead); if (!lt.Has(LayerName)) { AcDb.LayerTableRecord ltr = new AcDb.LayerTableRecord(); ltr.Name = LayerName; if (ColorNum > 255) { ed.WriteMessage(string.Format(" ERROR: Could not use LayerColor \"{0}\", using 0 instead.", ColorNum)); ColorNum = 0; } ltr.Color = AcCo.Color.FromColorIndex(AcCo.ColorMethod.ByAci, ColorNum); AcDb.LinetypeTable acLinTbl; acLinTbl = tr.GetObject(db.LinetypeTableId, AcDb.OpenMode.ForRead) as AcDb.LinetypeTable; if (acLinTbl.Has(LineType) == true) { // Set the linetype for the layer ltr.LinetypeObjectId = acLinTbl[LineType]; } // ========================================================================== // LINEWEIGHT switch (LineWeightVal) { case 1: // 0.18 ltr.LineWeight = AcDb.LineWeight.LineWeight018; break; case 2: // 0.25 ltr.LineWeight = AcDb.LineWeight.LineWeight025; break; case 3: // 0.35 ltr.LineWeight = AcDb.LineWeight.LineWeight035; break; case 4: // 0.50 ltr.LineWeight = AcDb.LineWeight.LineWeight050; break; case 5: // 0.7 ltr.LineWeight = AcDb.LineWeight.LineWeight070; break; case 6: // 1.0 ltr.LineWeight = AcDb.LineWeight.LineWeight100; break; case 7: //1.4 ltr.LineWeight = AcDb.LineWeight.LineWeight140; break; case 8: //2.0 ltr.LineWeight = AcDb.LineWeight.LineWeight200; break; } lt.UpgradeOpen(); layerId = lt.Add(ltr); tr.AddNewlyCreatedDBObject(ltr, true); tr.Commit(); } } return layerId; } } public class Utilities { public static List GetSelectedEntities(AcEd.SelectionSet ObjectSelection) { AcAp.Document doc = AcAp.Application.DocumentManager.MdiActiveDocument; AcDb.Database db = doc.Database; List ents = new List(); using (AcDb.Transaction tr = db.TransactionManager.StartTransaction()) { AcDb.ObjectId[] ids = ObjectSelection.GetObjectIds(); foreach (AcDb.ObjectId id in ids) { AcDb.Entity ent = tr.GetObject(id, AcDb.OpenMode.ForRead, false) as AcDb.Entity; ents.Add(ent); } } return ents; } public static List CopySelectionSet(List OriginalEntities, bool NewLayer = false, string NewLayerName = "") { AcAp.Document doc = AcAp.Application.DocumentManager.MdiActiveDocument; AcDb.Database db = doc.Database; List copiedSelection = new List(); using (AcDb.Transaction tr = db.TransactionManager.StartTransaction()) { AcDb.BlockTableRecord btr = tr.GetObject(db.CurrentSpaceId, AcDb.OpenMode.ForWrite) as AcDb.BlockTableRecord; foreach (AcDb.Entity ent in OriginalEntities) { AcDb.Entity copiedEnt = ent.Clone() as AcDb.Entity; if (NewLayer) { copiedEnt.Layer = NewLayerName; copiedEnt.LineWeight = AcDb.LineWeight.ByLayer; copiedEnt.ColorIndex = 256; } btr.AppendEntity(copiedEnt); tr.AddNewlyCreatedDBObject(copiedEnt, true); copiedSelection.Add(copiedEnt); } tr.Commit(); } return copiedSelection; } public static AcEd.SelectionSet SelTextMText(out bool Cancelled) { AcAp.Document acDoc = AcAp.Application.DocumentManager.MdiActiveDocument; AcDb.Database acCurDb = acDoc.Database; AcEd.Editor acDocEd = acDoc.Editor; // Initialise a Selection Set AcEd.SelectionSet acSSet = null; using (AcDb.Transaction tr = acCurDb.TransactionManager.StartTransaction()) { // Select only the objects on the layer name //TypedValue[] tvs = new TypedValue[2]; //tvs[0] = new TypedValue() //tvs[0] = new TypedValue((int)DxfCode.Start, "TEXT"); //tvs[1] = new TypedValue((int)DxfCode.Start, "MTEXT"); AcDb.TypedValue[] tvs = { new AcDb.TypedValue(-4, "") }; AcEd.SelectionFilter sf = new AcEd.SelectionFilter(tvs); AcEd.PromptSelectionOptions pso = new AcEd.PromptSelectionOptions(); pso.SingleOnly = false; // Request for objects to be selected in the drawing area AcEd.PromptSelectionResult acSSPrompt = acDocEd.GetSelection(pso, sf); Cancelled = false; // If the prompt status is OK, objects were selected if (acSSPrompt.Status == AcEd.PromptStatus.OK) { acSSet = acSSPrompt.Value; } else if (acSSPrompt.Status != AcEd.PromptStatus.OK) Cancelled = true; return acSSet; } } public static void SendACommand(string CommandText) { Object acObj = AcAp.Application.AcadApplication; object acDoc = acObj.GetType().InvokeMember("ActiveDocument", System.Reflection.BindingFlags.GetProperty, null, acObj, null); acDoc.GetType().InvokeMember("SendCommand", System.Reflection.BindingFlags.InvokeMethod, null, acDoc, new object[] { CommandText }); } } }