using System; using System.Collections.Generic; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.EditorInput; using acad = Autodesk.AutoCAD.ApplicationServices.Application; namespace HabrPlug_SearchAndRescue { public class ClassMyAutoCADDLL_SearchAndRescue { public class Commands : IExtensionApplication { // Autodesk.AutoCAD.Colors.Color color_Pink = Autodesk.AutoCAD.Colors.Color.FromRgb(255, 128, 255); Autodesk.AutoCAD.Colors.Color color_Blue = Autodesk.AutoCAD.Colors.Color.FromRgb(0, 200, 255); Autodesk.AutoCAD.Colors.Color color_LightGreen = Autodesk.AutoCAD.Colors.Color.FromRgb(128, 255, 64); // ID "layer-1" "layer-2" ObjectId layer_1; ObjectId layer_2; // public void createLayers() { // Document acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // using (DocumentLock docloc = acDoc.LockDocument()) { // using (Transaction tr = acCurDb.TransactionManager.StartTransaction()) { // LayerTable acLyrTbl = tr.GetObject(acCurDb.LayerTableId, OpenMode.ForWrite) as LayerTable; // LayerTableRecord acLyrTblRec_1 = new LayerTableRecord(); acLyrTblRec_1.Name = "layer-1"; // , ID layer_1 = acLyrTbl.Add(acLyrTblRec_1); // tr.AddNewlyCreatedDBObject(acLyrTblRec_1, true); // LayerTableRecord acLyrTblRec_2 = new LayerTableRecord(); acLyrTblRec_2.Name = "layer-2"; // , ID layer_2 = acLyrTbl.Add(acLyrTblRec_2); // tr.AddNewlyCreatedDBObject(acLyrTblRec_2, true); // tr.Commit(); } } } // "block-1" public void createBlock_1() { // Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; // const string blockName = "block-1"; // Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { // BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite); // , ; - if (bt.Has(blockName)) { return; } // , BlockTableRecord btr = new BlockTableRecord(); btr.Name = blockName; // bt.Add(btr); tr.AddNewlyCreatedDBObject(btr, true); // // Circle acCircle = new Circle(); // acCircle.SetDatabaseDefaults(); acCircle.Center = Point3d.Origin; acCircle.Radius = 25; // btr.AppendEntity(acCircle); tr.AddNewlyCreatedDBObject(acCircle, true); // Line acLine = new Line(new Point3d(18, 18, 0), new Point3d(35, 35, 0)); // acLine.SetDatabaseDefaults(); // btr.AppendEntity(acLine); tr.AddNewlyCreatedDBObject(acLine, true); // Polyline acPolyline = new Polyline(); // acPolyline.SetDatabaseDefaults(); // acPolyline.AddVertexAt(0, new Point2d(20, 35), 0, 0, 0); acPolyline.AddVertexAt(1, new Point2d(35, 35), 0, 0, 0); acPolyline.AddVertexAt(2, new Point2d(35, 20), 0, 0, 0); // btr.AppendEntity(acPolyline); tr.AddNewlyCreatedDBObject(acPolyline, true); // tr.Commit(); } } // "block-2" public void createBlock_2() { // Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; // const string blockName = "block-2"; // Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { // BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite); // , ; - if (bt.Has(blockName)) { return; } // , BlockTableRecord btr = new BlockTableRecord(); btr.Name = blockName; // bt.Add(btr); tr.AddNewlyCreatedDBObject(btr, true); // // Circle acCircle = new Circle(); // acCircle.SetDatabaseDefaults(); acCircle.Center = Point3d.Origin; acCircle.Radius = 25; // btr.AppendEntity(acCircle); tr.AddNewlyCreatedDBObject(acCircle, true); // Line acLine_1 = new Line(new Point3d(0, -25, 0), new Point3d(0, -50, 0)); // acLine_1.SetDatabaseDefaults(); // btr.AppendEntity(acLine_1); tr.AddNewlyCreatedDBObject(acLine_1, true); // Line acLine_2 = new Line(new Point3d(-7, -39, 0), new Point3d(7, -39, 0)); // acLine_2.SetDatabaseDefaults(); // btr.AppendEntity(acLine_2); tr.AddNewlyCreatedDBObject(acLine_2, true); // tr.Commit(); } } // public void layer_0_createObjects() { // Document doc = acad.DocumentManager.MdiActiveDocument; Database db = doc.Database; // using (Transaction tr = db.TransactionManager.StartTransaction()) { // BlockTable acBlkTbl; acBlkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; // (Model Space) - BlockTableRecord ms = tr.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // Line acLine_1 = new Line(new Point3d(225, 225, 0), new Point3d(225, 175, 0)); // acLine_1.SetDatabaseDefaults(); // acLine_1.Layer = "0"; acLine_1.Color = color_Pink; // ms.AppendEntity(acLine_1); tr.AddNewlyCreatedDBObject(acLine_1, true); // Line acLine_2 = new Line(new Point3d(250, 225, 0), new Point3d(250, 175, 0)); // acLine_2.SetDatabaseDefaults(); // acLine_2.Layer = "0"; acLine_2.Color = color_Blue; // ms.AppendEntity(acLine_2); tr.AddNewlyCreatedDBObject(acLine_2, true); // Line acLine_3 = new Line(new Point3d(275, 225, 0), new Point3d(275, 175, 0)); // acLine_3.SetDatabaseDefaults(); // acLine_3.Layer = "0"; acLine_3.Color = color_LightGreen; // ms.AppendEntity(acLine_3); tr.AddNewlyCreatedDBObject(acLine_3, true); // Polyline acPolyline = new Polyline(); // acPolyline.SetDatabaseDefaults(); // acPolyline.AddVertexAt(0, new Point2d(300, 225), 0, 0, 0); acPolyline.AddVertexAt(1, new Point2d(325, 175), 0, 0, 0); acPolyline.AddVertexAt(2, new Point2d(350, 225), 0, 0, 0); // acPolyline.Layer = "0"; acPolyline.Color = color_Pink; // ms.AppendEntity(acPolyline); tr.AddNewlyCreatedDBObject(acPolyline, true); // Circle acCircle = new Circle(); // acCircle.SetDatabaseDefaults(); acCircle.Center = new Point3d(400, 200, 0); acCircle.Radius = 25; // acCircle.Layer = "0"; acCircle.Color = color_Blue; // ms.AppendEntity(acCircle); tr.AddNewlyCreatedDBObject(acCircle, true); // DBText text = new DBText(); text.Position = new Point3d(450, 175, 0); text.Height = 50; text.TextString = "HABR!"; // text.Layer = "0"; text.Color = color_LightGreen; // ms.AppendEntity(text); tr.AddNewlyCreatedDBObject(text, true); // tr.Commit(); } } // "layer-1" public void layer_1_createObjects() { // Document doc = acad.DocumentManager.MdiActiveDocument; Database db = doc.Database; // using (Transaction tr = db.TransactionManager.StartTransaction()) { // // ( ) ObjectId currentLayer = db.Clayer; db.Clayer = layer_1; // BlockTable acBlkTbl; acBlkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; // (Model Space) - BlockTableRecord ms = tr.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // Line acLine_1 = new Line(new Point3d(225, 25, 0), new Point3d(225, -25, 0)); // acLine_1.SetDatabaseDefaults(); acLine_1.Color = color_Pink; // ms.AppendEntity(acLine_1); tr.AddNewlyCreatedDBObject(acLine_1, true); // Line acLine_2 = new Line(new Point3d(250, 25, 0), new Point3d(250, -25, 0)); // acLine_2.SetDatabaseDefaults(); acLine_2.Color = color_Blue; // ms.AppendEntity(acLine_2); tr.AddNewlyCreatedDBObject(acLine_2, true); // Circle acCircle = new Circle(); // acCircle.SetDatabaseDefaults(); acCircle.Center = new Point3d(300, 0, 0); acCircle.Radius = 25; acCircle.Color = color_LightGreen; // ms.AppendEntity(acCircle); tr.AddNewlyCreatedDBObject(acCircle, true); // DBText text = new DBText(); // text.Position = new Point3d(350, -25, 0); text.Height = 50; text.TextString = "HABR!"; text.Color = color_Pink; // ms.AppendEntity(text); tr.AddNewlyCreatedDBObject(text, true); // "block-1" // BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); // ObjectID ObjectId btrId = bt["block-1"]; // , ID BlockReference br = new BlockReference(new Point3d(600, 0, 0), btrId); // ms.AppendEntity(br); tr.AddNewlyCreatedDBObject(br, true); // db.Clayer = currentLayer; // tr.Commit(); } } // "layer-2" public void layer_2_createObjects() { // Document doc = acad.DocumentManager.MdiActiveDocument; Database db = doc.Database; // using (Transaction tr = db.TransactionManager.StartTransaction()) { // // ( ) ObjectId currentLayer = db.Clayer; db.Clayer = layer_2; // BlockTable acBlkTbl; acBlkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; // (Model Space) - BlockTableRecord ms = tr.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // Line acLine_1 = new Line(new Point3d(225, -175, 0), new Point3d(225, -225, 0)); // acLine_1.SetDatabaseDefaults(); acLine_1.Color = color_Pink; // ms.AppendEntity(acLine_1); tr.AddNewlyCreatedDBObject(acLine_1, true); // Arc acArc = new Arc(new Point3d(250, -200, 0), 25, -45 / 180.0 * Math.PI, 45 / 180.0 * Math.PI); // acArc.SetDatabaseDefaults(); acArc.Color = color_Blue; // ms.AppendEntity(acArc); tr.AddNewlyCreatedDBObject(acArc, true); // Circle acCircle = new Circle(); // acCircle.SetDatabaseDefaults(); acCircle.Center = new Point3d(325, -200, 0); acCircle.Radius = 25; acCircle.Color = color_LightGreen; // ms.AppendEntity(acCircle); tr.AddNewlyCreatedDBObject(acCircle, true); // "block-1" // BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); // ObjectID ObjectId btrId = bt["block-1"]; // , ID BlockReference br = new BlockReference(new Point3d(400, -200, 0), btrId); // ms.AppendEntity(br); tr.AddNewlyCreatedDBObject(br, true); // "block-2" // ObjectID btrId = bt["block-2"]; // , ID br = new BlockReference(new Point3d(475, -200, 0), btrId); // ms.AppendEntity(br); tr.AddNewlyCreatedDBObject(br, true); // db.Clayer = currentLayer; // tr.Commit(); } } // public void Initialize() { createLayers(); createBlock_1(); createBlock_2(); layer_0_createObjects(); layer_1_createObjects(); layer_2_createObjects(); } // Terminate() , IExtensionApplication public void Terminate() { } ////////////////////////////////////////////////////////////// // (<<MODEL SPACE>>) ////////////////////////////////////////////////////////////// // ( 1) [CommandMethod("Habr_IterateThroughAllObjects_1")] public void iterateThroughAllObjects_1() { // Database db = HostApplicationServices.WorkingDatabase; // using (Transaction tr = db.TransactionManager.StartTransaction()) { // (ModelSpace) BlockTableRecord ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead); // "" foreach (ObjectId id in ms) { // Entity Entity entity = (Entity)tr.GetObject(id, OpenMode.ForRead); // (entity.Layer), (entity.GetType().ToString()) (entity.Color) acad.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nLayer:{0}; Type:{1}; Color: {2},{3},{4}\n", entity.Layer, entity.GetType().ToString(), entity.Color.Red.ToString(), entity.Color.Green.ToString(), entity.Color.Blue.ToString())); } tr.Commit(); } } // ( 1) [CommandMethod("Habr_FindCircles_1")] public void findCircles_1() { // Database db = HostApplicationServices.WorkingDatabase; // using (Transaction tr = db.TransactionManager.StartTransaction()) { // (ModelSpace) BlockTableRecord ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead); // "" foreach (ObjectId id in ms) { // Entity Entity entity = (Entity)tr.GetObject(id, OpenMode.ForRead); // - , if (entity.GetType() == typeof(Circle)) { acad.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nLayer:{0}; Type:{1}; Color: {2},{3},{4}\n", entity.Layer, entity.GetType().ToString(), entity.Color.Red.ToString(), entity.Color.Green.ToString(), entity.Color.Blue.ToString())); } } tr.Commit(); } } // ( 1) [CommandMethod("Habr_FindLightGreenObjects_1")] public void findLightGreenObjects_1() { // Database db = HostApplicationServices.WorkingDatabase; // using (Transaction tr = db.TransactionManager.StartTransaction()) { // (ModelSpace) BlockTableRecord ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead); // "" foreach (ObjectId id in ms) { // Entity Entity entity = (Entity)tr.GetObject(id, OpenMode.ForRead); // - , , if (entity.Color == Autodesk.AutoCAD.Colors.Color.FromRgb(128, 255, 64)) { acad.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nLayer:{0}; Type:{1}; Color: {2},{3},{4}\n", entity.Layer, entity.GetType().ToString(), entity.Color.Red.ToString(), entity.Color.Green.ToString(), entity.Color.Blue.ToString())); } } tr.Commit(); } } // "block-1" ( 1) [CommandMethod("Habr_FindBlocks_1")] public void findBlocks_1() { // Database db = HostApplicationServices.WorkingDatabase; // using (Transaction tr = db.TransactionManager.StartTransaction()) { // (ModelSpace) BlockTableRecord ms = (BlockTableRecord)tr.GetObject(SymbolUtilityServices.GetBlockModelSpaceId(db), OpenMode.ForRead); // "" foreach (ObjectId id in ms) { // Entity Entity entity = (Entity)tr.GetObject(id, OpenMode.ForRead); // , if (entity.GetType() == typeof(BlockReference)) { // - BlockReference BlockReference br = (BlockReference)entity; // - "block-1", , if (br.Name == "block-1") { acad.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nLayer:{0}; Type:{1}; Color: {2},{3},{4}\n", entity.Layer, entity.GetType().ToString(), entity.Color.Red.ToString(), entity.Color.Green.ToString(), entity.Color.Blue.ToString())); } } } tr.Commit(); } } ////////////////////////////////////////////////////////////// // (<<EDITOR.SELECTALL>>) ////////////////////////////////////////////////////////////// // ( 2) [CommandMethod("Habr_IterateThroughAllObjects_2")] public void iterateThroughAllObjects_2() { // Editor Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // // ! ! PromptSelectionResult selRes = ed.SelectAll(); // - if (selRes.Status != PromptStatus.OK) { ed.WriteMessage("\nError!\n"); return; } // ID ObjectId[] ids = selRes.Value.GetObjectIds(); // using (Transaction tr = db.TransactionManager.StartTransaction()) { // "" foreach (ObjectId id in ids) { // Entity Entity entity = (Entity)tr.GetObject(id, OpenMode.ForRead); // (entity.Layer), (entity.GetType().ToString()) (entity.Color) acad.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nLayer:{0}; Type:{1}; Color: {2},{3},{4}\n", entity.Layer, entity.GetType().ToString(), entity.Color.Red.ToString(), entity.Color.Green.ToString(), entity.Color.Blue.ToString())); } tr.Commit(); } } // ( 2) [CommandMethod("Habr_FindCircles_2")] public void findCircles_2() { // Editor Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // , TypedValue[] filterlist = new TypedValue[1]; // (0) , // ("CIRCLE") - filterlist[0] = new TypedValue(0, "CIRCLE"); // SelectionFilter filter = new SelectionFilter(filterlist); // // ! ! PromptSelectionResult selRes = ed.SelectAll(filter); // - if (selRes.Status != PromptStatus.OK) { ed.WriteMessage("\nError!\n"); return; } // ID ObjectId[] ids = selRes.Value.GetObjectIds(); // using (Transaction tr = db.TransactionManager.StartTransaction()) { // "" foreach (ObjectId id in ids) { // Entity Entity entity = (Entity)tr.GetObject(id, OpenMode.ForRead); // , acad.DocumentManager.MdiActiveDocument.Editor.WriteMessage(string.Format("\nLayer:{0}; Type:{1}; Color: {2},{3},{4}\n", entity.Layer, entity.GetType().ToString(), entity.Color.Red.ToString(), entity.Color.Green.ToString(), entity.Color.Blue.ToString())); } tr.Commit(); } } ////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////// // [CommandMethod("Habr_EraseCircles_2")] public void eraseCircles_2() { // Editor Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // , TypedValue[] filterlist = new TypedValue[1]; // (0) , // ("CIRCLE") - filterlist[0] = new TypedValue(0, "CIRCLE"); // SelectionFilter filter = new SelectionFilter(filterlist); // // ! ! PromptSelectionResult selRes = ed.SelectAll(filter); // - if (selRes.Status != PromptStatus.OK) { ed.WriteMessage("\nError!\n"); return; } // ID ObjectId[] ids = selRes.Value.GetObjectIds(); // using (Transaction tr = db.TransactionManager.StartTransaction()) { // "" foreach (ObjectId id in ids) { // Entity Entity entity = (Entity)tr.GetObject(id, OpenMode.ForRead); // entity.UpgradeOpen(); // entity.Erase(); } tr.Commit(); } } // [CommandMethod("Habr_RepaintOrange_2")] public void repaintOrange_2() { // Editor Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // // ! ! PromptSelectionResult selRes = ed.SelectAll(); // - if (selRes.Status != PromptStatus.OK) { ed.WriteMessage("\nError!\n"); return; } // ID ObjectId[] ids = selRes.Value.GetObjectIds(); // using (Transaction tr = db.TransactionManager.StartTransaction()) { // "" foreach (ObjectId id in ids) { // Entity Entity entity = (Entity)tr.GetObject(id, OpenMode.ForRead); // entity.UpgradeOpen(); // entity.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(255, 128, 0); } tr.Commit(); } } // [CommandMethod("Habr_ModifyCircles_2")] public void modifyCircles_2() { // Editor Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // , TypedValue[] filterlist = new TypedValue[1]; // (0) , // ("CIRCLE") - filterlist[0] = new TypedValue(0, "CIRCLE"); // SelectionFilter filter = new SelectionFilter(filterlist); // // ! ! PromptSelectionResult selRes = ed.SelectAll(filter); // - if (selRes.Status != PromptStatus.OK) { ed.WriteMessage("\nError!\n"); return; } // ID ObjectId[] ids = selRes.Value.GetObjectIds(); // using (Transaction tr = db.TransactionManager.StartTransaction()) { // "" foreach (ObjectId id in ids) { // Circle Circle cir = (Circle)tr.GetObject(id, OpenMode.ForRead); // cir.UpgradeOpen(); // cir.Radius = cir.Radius * 2; } tr.Commit(); } } // [CommandMethod("Habr_MoveBlocks_2")] public void moveBlocks_2() { // Editor Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; // , TypedValue[] filterlist = new TypedValue[1]; // (0) , // ("INSERT") - filterlist[0] = new TypedValue(0, "INSERT"); // SelectionFilter filter = new SelectionFilter(filterlist); // // ! ! PromptSelectionResult selRes = ed.SelectAll(filter); // - if (selRes.Status != PromptStatus.OK) { ed.WriteMessage("\nError!\n"); return; } // ID ObjectId[] ids = selRes.Value.GetObjectIds(); // using (Transaction tr = db.TransactionManager.StartTransaction()) { // "" foreach (ObjectId id in ids) { // BlockReference BlockReference br = (BlockReference)tr.GetObject(id, OpenMode.ForRead); // br.UpgradeOpen(); // br.Position = Point3d.Origin; } tr.Commit(); } } ////////////////////////////////////////////////////////////// // (<< >>) ////////////////////////////////////////////////////////////// // ( 3) // ! ! [CommandMethod("Habr_IterateThroughAllObjects_3")] public void iterateThroughAllObjects_3() { Database db = HostApplicationServices.WorkingDatabase; Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; long amount = 0; Dictionary<string, int> d = new Dictionary<string, int>(); ObjectId id = ObjectId.Null; for (long i = db.BlockTableId.Handle.Value; i < db.Handseed.Value; i++) { Handle h = new Handle(i); if (db.TryGetObjectId(h, out id) && !id.IsNull && id.IsValid && !id.IsErased) { string t = id.ObjectClass.DxfName; amount++; if (d.ContainsKey(t)) d[t]++; else d.Add(t, 1); } } foreach (KeyValuePair<string, int> kvp in d) ed.WriteMessage("\n{0}: {1} ", kvp.Key, kvp.Value); ed.WriteMessage("\nTotal {0} objects in drawing\n", amount); } } } }