public static string disclaimer = " AutoCAD. – ."
CopyLocal
, specifying the version of .NET, and the like — this has been discussed many times in past posts of the cycle. In this example, we will need references to AcMgd and AcDBMgd libraries . There will be little code; Immediately bring it all, and then analyze the details. using System; using System.IO; 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_SimpleBlock { public class ClassMyAutoCADDLL_SimpleBlock { public class Commands : IExtensionApplication { // AutoCAD "HabrCommand" [CommandMethod("HabrCommand")] public void HabrCommand() { // Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; // "Editor" AutoCAD Editor ed = doc.Editor; // const string blockName = "pvtBlock"; // Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { //*** // 1 - //*** // BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite); // , // - if (bt.Has(blockName)) { ed.WriteMessage("\nA block with the name \"" + blockName + "\" already exists."); return; } // , BlockTableRecord btr = new BlockTableRecord(); btr.Name = blockName; // , // ID ( ) ObjectId btrId = bt.Add(btr); tr.AddNewlyCreatedDBObject(btr, true); //*** // 2 - //*** // Polyline poly = new Polyline(); poly.SetDatabaseDefaults(); poly.AddVertexAt(0, new Point2d(-50, -125), 0, 0, 0); poly.AddVertexAt(1, new Point2d(-50, 105), 0, 0, 0); poly.AddVertexAt(2, new Point2d(-20, 125), 0, 0, 0); poly.AddVertexAt(3, new Point2d(20, 125), 0, 0, 0); poly.AddVertexAt(4, new Point2d(50, 105), 0, 0, 0); poly.AddVertexAt(5, new Point2d(50, -125), 0, 0, 0); poly.AddVertexAt(6, new Point2d(-50, -125), 0, 0, 0); // btr.AppendEntity(poly); tr.AddNewlyCreatedDBObject(poly, true); // Circle cir = new Circle(); cir.SetDatabaseDefaults(); cir.Center = new Point3d(0, 90, 0); cir.Radius = 15; // btr.AppendEntity(cir); tr.AddNewlyCreatedDBObject(cir, true); // DBText text = new DBText(); text.Position = new Point3d(-25, -95, 0); text.Height = 35; text.TextString = "BC"; // btr.AppendEntity(text); tr.AddNewlyCreatedDBObject(text, true); //*** // 3 - //*** // BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); // , ID BlockReference br = new BlockReference(Point3d.Origin, btrId); // ms.AppendEntity(br); tr.AddNewlyCreatedDBObject(br, true); // tr.Commit(); } } // Initialize() Terminate() , IExtensionApplication public void Initialize() { } public void Terminate() { } } } }
Editor
property, set the name of the new block, and begin the transaction. There is nothing new in this, except perhaps Editor
properties.Editor
class. I would be glad if someone from more knowledgeable people prompts a link to the imputed description of this class and its methods (or at least where to find it in the ObjectARX Reference). In this example, the Editor
class is used only to display a message that a block with the same name already exists in the database. In AutoCAD, this message looks like this:eDuplicateRecordName
, which we absolutely do not need);BlockReference
class used to create a new block entry. This constructor takes two parameters: the block placement point (the base entry point of the block will be combined with this point) and the block definition ID. In other words, we need to tell AutoCAD which block we want to see and where . In our example, the origin of the block is the origin ( Point3d.Origin
), and we saved the block definition ID in the first step when creating this definition.ObjectID
this definition. In our case, the code can be rewritten as: [CommandMethod("HabrCommand")] public void HabrCommand() { // Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; // "Editor" AutoCAD Editor ed = doc.Editor; // const string blockName = "pvtBlock"; // Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { //*** // 1 - ( ID ) //*** // BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForRead); // , // - ID ObjectId btrId; if (bt.Has(blockName)) { btrId = bt[blockName]; } else { // bt.UpgradeOpen(); // , BlockTableRecord btr = new BlockTableRecord(); btr.Name = blockName; // , ID btrId = bt.Add(btr); // tr.AddNewlyCreatedDBObject(btr, true); //*** // 2 - //*** // Polyline poly = new Polyline(); poly.SetDatabaseDefaults(); poly.AddVertexAt(0, new Point2d(-50, -125), 0, 0, 0); poly.AddVertexAt(1, new Point2d(-50, 105), 0, 0, 0); poly.AddVertexAt(2, new Point2d(-20, 125), 0, 0, 0); poly.AddVertexAt(3, new Point2d(20, 125), 0, 0, 0); poly.AddVertexAt(4, new Point2d(50, 105), 0, 0, 0); poly.AddVertexAt(5, new Point2d(50, -125), 0, 0, 0); poly.AddVertexAt(6, new Point2d(-50, -125), 0, 0, 0); // btr.AppendEntity(poly); tr.AddNewlyCreatedDBObject(poly, true); // Circle cir = new Circle(); cir.SetDatabaseDefaults(); cir.Center = new Point3d(0, 90, 0); cir.Radius = 15; // btr.AppendEntity(cir); tr.AddNewlyCreatedDBObject(cir, true); // DBText text = new DBText(); text.Position = new Point3d(-33, -95, 0); text.Height = 35; text.TextString = "BC"; // btr.AppendEntity(text); tr.AddNewlyCreatedDBObject(text, true); } //*** // 3 - //*** // BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); // , ID BlockReference br = new BlockReference(Point3d.Origin, btrId); // ms.AppendEntity(br); tr.AddNewlyCreatedDBObject(br, true); // tr.Commit(); } }
UpgradeOpen()
example, I used the UpgradeOpen()
method for a UpgradeOpen()
, which quite often appears in the examples and documentation. Let's see how it works.OpenMode.ForWrite
), now we first open it only for reading ( OpenMode.ForRead
). This is enough for us to view the table and find out whether it contains a block with our name. If there is such a block, then we can read its ObjectID
, for which, again, we only have access for reading. If there is no block with our name in the table, then we need to add it, and for this we need write access. To get this access, the UpgradeOpen()
method is used - after calling this method we can work with the block table, as if we opened it with the OpenMode.ForWrite
access OpenMode.ForWrite
.UpgradeOpen()
is probably good practice. At least Kean Walmsley does exactly that. But I don’t have detailed information about what treasures and life benefits come to those who use UpgradeOpen()
. I myself did not use this construction in my work and did not feel any particular disappointment about this. If someone can submit their position on the use of UpgradeOpen()
- I will be glad to see it in the comments or drugs.Origin
class property is used for this purpose BlockTableRecord
. As an example, let's move the base definition point of a block to the bottom left corner. To do this, after specifying the block name, set the property Origin
: // btr.Name = blockName; // btr.Origin = new Point3d(-50, -125, 0);
BlockReference
- for example, like this: BlockReference br = new BlockReference(new Point3d(150, 150, 0), btrId);
Position
class property BlockReference
: BlockReference br = new BlockReference(Point3d.Origin, btrId); br.Position = new Point3d(150, 150, 0);
BlockReference
. [CommandMethod("HabrCommand")] public void HabrCommand() { // Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; // "Editor" AutoCAD Editor ed = doc.Editor; // const string blockName = "ltBlock"; // const string auxBlockName = "starBlock"; // Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { // BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite); //*** // 0 - ( ID ) //*** // , // - ID ObjectId btrIdAux; if (bt.Has(auxBlockName)) { btrIdAux = bt[auxBlockName]; } else { // , BlockTableRecord btr = new BlockTableRecord(); btr.Name = auxBlockName; // , ID btrIdAux = bt.Add(btr); // tr.AddNewlyCreatedDBObject(btr, true); // Polyline poly = new Polyline(); poly.SetDatabaseDefaults(); poly.AddVertexAt(0, new Point2d(0, -6), 0, 0, 0); poly.AddVertexAt(1, new Point2d(-9, -12), 0, 0, 0); poly.AddVertexAt(2, new Point2d(-7, -2), 0, 0, 0); poly.AddVertexAt(3, new Point2d(-14, 6), 0, 0, 0); poly.AddVertexAt(4, new Point2d(-5, 6), 0, 0, 0); poly.AddVertexAt(5, new Point2d(0, 15), 0, 0, 0); poly.AddVertexAt(6, new Point2d(5, 6), 0, 0, 0); poly.AddVertexAt(7, new Point2d(14, 6), 0, 0, 0); poly.AddVertexAt(8, new Point2d(7, -2), 0, 0, 0); poly.AddVertexAt(9, new Point2d(9, -12), 0, 0, 0); poly.AddVertexAt(10, new Point2d(0, -6), 0, 0, 0); // btr.AppendEntity(poly); tr.AddNewlyCreatedDBObject(poly, true); } //*** // ( ID ) //*** //*** // 1 - ( ID ) //*** // , // - ID ObjectId btrId; if (bt.Has(blockName)) { btrId = bt[blockName]; } else { // , BlockTableRecord btr = new BlockTableRecord(); btr.Name = blockName; // , ID btrId = bt.Add(btr); // tr.AddNewlyCreatedDBObject(btr, true); //*** // 2 - //*** // Polyline polyExt = new Polyline(); polyExt.SetDatabaseDefaults(); polyExt.AddVertexAt(0, new Point2d(-50, -125), 0, 0, 0); polyExt.AddVertexAt(1, new Point2d(-50, 105), 0, 0, 0); polyExt.AddVertexAt(2, new Point2d(-20, 125), 0, 0, 0); polyExt.AddVertexAt(3, new Point2d(20, 125), 0, 0, 0); polyExt.AddVertexAt(4, new Point2d(50, 105), 0, 0, 0); polyExt.AddVertexAt(5, new Point2d(50, -125), 0, 0, 0); polyExt.AddVertexAt(6, new Point2d(-50, -125), 0, 0, 0); // btr.AppendEntity(polyExt); tr.AddNewlyCreatedDBObject(polyExt, true); // Polyline polyIn = new Polyline(); polyIn.SetDatabaseDefaults(); polyIn.AddVertexAt(0, new Point2d(-5, -125), 0, 0, 0); polyIn.AddVertexAt(1, new Point2d(-5, 125), 0, 0, 0); polyIn.AddVertexAt(2, new Point2d(5, 125), 0, 0, 0); polyIn.AddVertexAt(3, new Point2d(5, -125), 0, 0, 0); // btr.AppendEntity(polyIn); tr.AddNewlyCreatedDBObject(polyIn, true); // Circle cir = new Circle(); cir.SetDatabaseDefaults(); cir.Center = new Point3d(0, 90, 0); cir.Radius = 15; // btr.AppendEntity(cir); tr.AddNewlyCreatedDBObject(cir, true); // BlockReference starLeft = new BlockReference(new Point3d(-27, -75, 0), btrIdAux); btr.AppendEntity(starLeft); tr.AddNewlyCreatedDBObject(starLeft, true); BlockReference starRight = new BlockReference(new Point3d(27, -75, 0), btrIdAux); btr.AppendEntity(starRight); tr.AddNewlyCreatedDBObject(starRight, true); } //*** // 3 - //*** // BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); // , ID BlockReference br = new BlockReference(new Point3d(150, 150, 0), btrId); // ms.AppendEntity(br); tr.AddNewlyCreatedDBObject(br, true); // tr.Commit(); } }
using System; using System.IO; 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_SimpleBlock { public class ClassMyAutoCADDLL_SimpleBlock { public class Commands : IExtensionApplication { // , public void drawFigure(double x, double y) { // Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; // Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { // BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite); // BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); // Polyline polyExt = new Polyline(); polyExt.SetDatabaseDefaults(); polyExt.AddVertexAt(0, new Point2d(x - 50, y - 125), 0, 0, 0); polyExt.AddVertexAt(1, new Point2d(x - 50, y + 105), 0, 0, 0); polyExt.AddVertexAt(2, new Point2d(x - 20, y + 125), 0, 0, 0); polyExt.AddVertexAt(3, new Point2d(x + 20, y + 125), 0, 0, 0); polyExt.AddVertexAt(4, new Point2d(x + 50, y + 105), 0, 0, 0); polyExt.AddVertexAt(5, new Point2d(x + 50, y - 125), 0, 0, 0); polyExt.AddVertexAt(6, new Point2d(x - 50, y - 125), 0, 0, 0); // ms.AppendEntity(polyExt); tr.AddNewlyCreatedDBObject(polyExt, true); // Polyline polyIn = new Polyline(); polyIn.SetDatabaseDefaults(); polyIn.AddVertexAt(0, new Point2d(x - 5, y - 125), 0, 0, 0); polyIn.AddVertexAt(1, new Point2d(x - 5, y + 125), 0, 0, 0); polyIn.AddVertexAt(2, new Point2d(x + 5, y + 125), 0, 0, 0); polyIn.AddVertexAt(3, new Point2d(x + 5, y - 125), 0, 0, 0); // ms.AppendEntity(polyIn); tr.AddNewlyCreatedDBObject(polyIn, true); // Circle cir = new Circle(); cir.SetDatabaseDefaults(); cir.Center = new Point3d(x, y + 90, 0); cir.Radius = 15; // ms.AppendEntity(cir); tr.AddNewlyCreatedDBObject(cir, true); // , Polyline starLeft= new Polyline(); starLeft.SetDatabaseDefaults(); starLeft.AddVertexAt(0, new Point2d(x - 27, y - 75 - 6), 0, 0, 0); starLeft.AddVertexAt(1, new Point2d(x - 27 - 9, y - 75 - 12), 0, 0, 0); starLeft.AddVertexAt(2, new Point2d(x - 27 - 7, y - 75 - 2), 0, 0, 0); starLeft.AddVertexAt(3, new Point2d(x - 27 - 14, y - 75 + 6), 0, 0, 0); starLeft.AddVertexAt(4, new Point2d(x - 27 - 5, y - 75 + 6), 0, 0, 0); starLeft.AddVertexAt(5, new Point2d(x - 27, y - 75 + 15), 0, 0, 0); starLeft.AddVertexAt(6, new Point2d(x - 27 + 5, y - 75 + 6), 0, 0, 0); starLeft.AddVertexAt(7, new Point2d(x - 27 + 14, y - 75 + 6), 0, 0, 0); starLeft.AddVertexAt(8, new Point2d(x - 27 + 7, y - 75 - 2), 0, 0, 0); starLeft.AddVertexAt(9, new Point2d(x - 27 + 9, y - 75 - 12), 0, 0, 0); starLeft.AddVertexAt(10, new Point2d(x - 27, y - 75 - 6), 0, 0, 0); ms.AppendEntity(starLeft); tr.AddNewlyCreatedDBObject(starLeft, true); Polyline starRight = new Polyline(); starRight.SetDatabaseDefaults(); starRight.AddVertexAt(0, new Point2d(x + 27, y - 75 - 6), 0, 0, 0); starRight.AddVertexAt(1, new Point2d(x + 27 - 9, y - 75 - 12), 0, 0, 0); starRight.AddVertexAt(2, new Point2d(x + 27 - 7, y - 75 - 2), 0, 0, 0); starRight.AddVertexAt(3, new Point2d(x + 27 - 14, y - 75 + 6), 0, 0, 0); starRight.AddVertexAt(4, new Point2d(x + 27 - 5, y - 75 + 6), 0, 0, 0); starRight.AddVertexAt(5, new Point2d(x + 27, y - 75 + 15), 0, 0, 0); starRight.AddVertexAt(6, new Point2d(x + 27 + 5, y - 75 + 6), 0, 0, 0); starRight.AddVertexAt(7, new Point2d(x + 27 + 14, y - 75 + 6), 0, 0, 0); starRight.AddVertexAt(8, new Point2d(x + 27 + 7, y - 75 - 2), 0, 0, 0); starRight.AddVertexAt(9, new Point2d(x + 27 + 9, y - 75 - 12), 0, 0, 0); starRight.AddVertexAt(10, new Point2d(x + 27, y - 75 - 6), 0, 0, 0); ms.AppendEntity(starRight); tr.AddNewlyCreatedDBObject(starRight, true); // tr.Commit(); } } // , public void drawBlock(double x, double y) { // Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; // "Editor" AutoCAD Editor ed = doc.Editor; // const string blockName = "ltBlock"; const string auxBlockName = "starBlock"; // Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { // BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite); //*** // 0 - ( ID ) //*** ObjectId btrIdAux; if (bt.Has(auxBlockName)) { btrIdAux = bt[auxBlockName]; } else { // , BlockTableRecord btr = new BlockTableRecord(); btr.Name = auxBlockName; // , ID btrIdAux = bt.Add(btr); // tr.AddNewlyCreatedDBObject(btr, true); // Polyline poly = new Polyline(); poly.SetDatabaseDefaults(); poly.AddVertexAt(0, new Point2d(0, -6), 0, 0, 0); poly.AddVertexAt(1, new Point2d(-9, -12), 0, 0, 0); poly.AddVertexAt(2, new Point2d(-7, -2), 0, 0, 0); poly.AddVertexAt(3, new Point2d(-14, 6), 0, 0, 0); poly.AddVertexAt(4, new Point2d(-5, 6), 0, 0, 0); poly.AddVertexAt(5, new Point2d(0, 15), 0, 0, 0); poly.AddVertexAt(6, new Point2d(5, 6), 0, 0, 0); poly.AddVertexAt(7, new Point2d(14, 6), 0, 0, 0); poly.AddVertexAt(8, new Point2d(7, -2), 0, 0, 0); poly.AddVertexAt(9, new Point2d(9, -12), 0, 0, 0); poly.AddVertexAt(10, new Point2d(0, -6), 0, 0, 0); // btr.AppendEntity(poly); tr.AddNewlyCreatedDBObject(poly, true); } //*** // 1 - ( ID ) //*** // , // - ID ObjectId btrId; if (bt.Has(blockName)) { btrId = bt[blockName]; } else { // , BlockTableRecord btr = new BlockTableRecord(); btr.Name = blockName; // , ID btrId = bt.Add(btr); // tr.AddNewlyCreatedDBObject(btr, true); //*** // 2 - //*** // Polyline polyExt = new Polyline(); polyExt.SetDatabaseDefaults(); polyExt.AddVertexAt(0, new Point2d(-50, -125), 0, 0, 0); polyExt.AddVertexAt(1, new Point2d(-50, 105), 0, 0, 0); polyExt.AddVertexAt(2, new Point2d(-20, 125), 0, 0, 0); polyExt.AddVertexAt(3, new Point2d(20, 125), 0, 0, 0); polyExt.AddVertexAt(4, new Point2d(50, 105), 0, 0, 0); polyExt.AddVertexAt(5, new Point2d(50, -125), 0, 0, 0); polyExt.AddVertexAt(6, new Point2d(-50, -125), 0, 0, 0); // btr.AppendEntity(polyExt); tr.AddNewlyCreatedDBObject(polyExt, true); // Polyline polyIn = new Polyline(); polyIn.SetDatabaseDefaults(); polyIn.AddVertexAt(0, new Point2d(-5, -125), 0, 0, 0); polyIn.AddVertexAt(1, new Point2d(-5, 125), 0, 0, 0); polyIn.AddVertexAt(2, new Point2d(5, 125), 0, 0, 0); polyIn.AddVertexAt(3, new Point2d(5, -125), 0, 0, 0); // btr.AppendEntity(polyIn); tr.AddNewlyCreatedDBObject(polyIn, true); // Circle cir = new Circle(); cir.SetDatabaseDefaults(); cir.Center = new Point3d(0, 90, 0); cir.Radius = 15; // btr.AppendEntity(cir); tr.AddNewlyCreatedDBObject(cir, true); // BlockReference starLeft = new BlockReference(new Point3d(-27, -75, 0), btrIdAux); btr.AppendEntity(starLeft); tr.AddNewlyCreatedDBObject(starLeft, true); BlockReference starRight = new BlockReference(new Point3d(27, -75, 0), btrIdAux); btr.AppendEntity(starRight); tr.AddNewlyCreatedDBObject(starRight, true); } //*** // 3 - //*** // BlockTableRecord ms = (BlockTableRecord)tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite); // , ID BlockReference br = new BlockReference(new Point3d(x, y, 0), btrId); // ms.AppendEntity(br); tr.AddNewlyCreatedDBObject(br, true); // tr.Commit(); } } // AutoCAD "HabrCommand_DrawFigures" [CommandMethod("HabrCommand_DrawFigures")] public void HabrCommand_DrawFigures() { for (int x = 0; x < 100; x++) { for (int y = 0; y < 100; y++) { drawFigure(125 * x, 300 * y); } } } // AutoCAD "HabrCommand_DrawBlocks" [CommandMethod("HabrCommand_DrawBlocks")] public void HabrCommand_DrawBlocks() { for (int x = 0; x < 100; x++) { for (int y = 0; y < 100; y++) { drawBlock(125 * x, 300 * y); } } } // Initialize() Terminate() , IExtensionApplication public void Initialize() { } public void Terminate() { } } } }
drawFigure(double x, double y)
and drawBlock(double x, double y)
. The first draws a set of shapes at a given point that exactly repeats our block, and the second creates an entry of our block at a given point. [CommandMethod("HabrCommand_MoveButton")] public void HabrCommand_MoveButton() { // Document doc = Application.DocumentManager.MdiActiveDocument; Database db = doc.Database; // "Editor" AutoCAD Editor ed = doc.Editor; // const string blockName = "ltBlock"; // Transaction tr = db.TransactionManager.StartTransaction(); using (tr) { // BlockTable bt = (BlockTable)tr.GetObject(db.BlockTableId, OpenMode.ForWrite); // , // - if (!bt.Has(blockName)) { ed.WriteMessage("Block not found!"); return; } else { // BlockTableRecord btr = tr.GetObject(bt[blockName], OpenMode.ForWrite) as BlockTableRecord; // ID foreach (ObjectId id in btr) { // ID , DBObject obj = tr.GetObject(id, OpenMode.ForRead); // - (Circle), if (obj.GetType() == typeof(Circle)) { obj.UpgradeOpen(); (obj as Circle).Center = new Point3d(0, 100, 0); } } // tr.Commit(); } }
Source: https://habr.com/ru/post/259303/
All Articles