public static string disclaimer = " AutoCAD. – .";
CopyLocal
) and connect the already familiar AcMgd and AcDbMgd libraries . Next, we put there the functions that create the objects for our example. 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() { } } } }
Initialize()
method.Initialize()
. Ideally, there should be only layer_0_createObjects()
function), belonging to this layer is clearly indicated for each object created. Example: acLine_1.Layer = "0";
layer_1_createObjects()
code to insert a line from the function layer_0_createObjects()
into the drawing, forgetting to change the layer to a new one. The search for the error and its correction took some time. Realizing that this would have to be done with all the primitives, the author went out of line and decided to do everything in a different way.Clayer
property.Clayer
property of the database of the current document. You can view this value like this: Document doc = acad.DocumentManager.MdiActiveDocument; Database db = doc.Database; ObjectId currentLayer = db.Clayer;
Document doc = acad.DocumentManager.MdiActiveDocument; Database db = doc.Database; db.Clayer = layer_1; // layer_1 - ObjectID
ObjectID
layer, knowing its name: 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; ObjectId layer_objID = acLyrTbl["someLayer"] } }
layer_1
and layer_2
, into which I save the ObjectID
layers “layer-1” and “layer-2” when they were created. And later, in the layer_1_createObjects()
and layer_2_createObjects()
procedures, I use the following construction: // ... ObjectId currentLayer = db.Clayer; db.Clayer = layer_1; // ... // // ... db.Clayer = currentLayer; // ...
Clayer
value and restore it at the end of the function?ModelSpace
and obtaining identifiers of drawing objects using a method Editor.SelectAll()
.ModelSpace
ModelSpace
) and get links to all the objects inside it. Then we cast these objects to type Entity
and process the properties we need. [CommandMethod("Habr_IterateThroughAllObjects_1")] public void iterateThroughAllObjects() { // 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(); } }
WorkingDatabase
HostApplicationServices
. , ( ) . — GetBlockModelSpaceId()
SymbolUtilityServices
, ObjectId
( ModelSpace
). // ( 1) [CommandMethod("Habr_IterateThroughAllObjects_1")] public void iterateThroughAllObjects() { // Document doc = acad.DocumentManager.MdiActiveDocument; Database db = doc.Database; // using (Transaction tr = db.TransactionManager.StartTransaction()) { // (ModelSpace) // BlockTable acBlkTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; // (Model Space) - BlockTableRecord ms = tr.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // "" 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(); } }
Entity
( ) , . if (entity.GetType() == typeof(Circle)) // - ,
[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(); } }
if (entity.GetType() == typeof(Line))
if (entity.GetType() == typeof(DBText))
if (entity.GetType() == typeof(BlockReference))
if (entity.GetType() == typeof(Circle))
if (entity.Color == Autodesk.AutoCAD.Colors.Color.FromRgb(128, 255, 64))
// ( 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(); } }
if (entity.Layer == "0")
Entity test; test.
" — IntelliSense . Entity:Entity
, : , . . , - , , .Entity
; Name
BlockReference
. [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(); } }
Editor.SelectAll()
. Since no filters are specified, we must return the identifiers ( ObjectID
) of all objects in the drawing. They are written to a type variable PromptSelectionResult
. Just in case, you need to make sure that the method worked correctly - for this we check the status of the result ( PromptSelectionResult.Status
). If something is wrong, the value of this property will be different from PromptStatus.OK
- in this case we terminate the function.Editor.SelectAll()
worked correctly, we get the identifiers of all objects returned by this method. For this we use the method PromptSelectionResult.Value.GetObjectIds()
. After that, we simply process all the objects in the loop - just like in the first section, when we turned to ModelSpace
.Editor.SelectAll()
, ( locked ) ( frozen ). , , : Editor.SelectAll()
, , . Kean Walmsley (.), AutoCAD Devblog (.), Autodesk (.).Editor.SelectAll()
, it is obviously necessary to have a class object Editor
. When working with a document that is directly open in AutoCAD, there will be no problems; but using this method to process databases of third-party documents (which are not currently open in AutoCAD) will not work (Engl.).Editor.SelectAll()
there is an opportunity to act differently.SelectionFilter
, and then we apply this filter in the method Editor.SelectAll()
. As a result, we are left with only objects that satisfy the condition of the filter. [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(); } }
Editor.SelectAll()
can take on an input an object of the type SelectionFilter
that sets the filter. This filter is initialized with an array of type objects TypedValue
. The constructor TypedValue
takes two parameters as input: public TypedValue(int typeCode, object value);
The document is a specific version. This is a group code. A group code is the value of the data element follows. The data element for a given object (or record) type. DXF format. The DXF format is the same when used with applications (AutoLISP and ARX). However, there are some minor data differences for some data groups.
typeCode
value 0. Where it came from and what it means - you can see in the list (eng.)0: Text string indicating the entity type (fixed)
Autodesk.AutoCAD.DatabaseServices.DxfCode
: [Wrapper("AcDb::DxfCode")] public enum DxfCode { Invalid = -9999, XDictionary = -6, PReactors = -5, Operator = -4, XDataStart = -3, FirstEntityId = -2, HeaderId = -2, End = -1, Start = 0, XRefPath = 1, Text = 1, AttributeTag = 2, ShapeName = 2, BlockName = 2, SymbolTableName = 2, MlineStyleName = 2, SymbolTableRecordName = 2, Description = 3, TextFontFile = 3, AttributePrompt = 3, LinetypeProse = 3, DimStyleName = 3, DimPostString = 3, CLShapeName = 4, DimensionAlternativePrefixSuffix = 4, TextBigFontFile = 4, SymbolTableRecordComments = 4, Handle = 5, DimensionBlock = 5, LinetypeName = 6, DimBlk1 = 6, DimBlk2 = 7, TextStyleName = 7, LayerName = 8, CLShapeText = 9, XCoordinate = 10, YCoordinate = 20, ZCoordinate = 30, Elevation = 38, Thickness = 39, TxtSize = 40, ViewportHeight = 40, Real = 40, ViewWidth = 41, TxtStyleXScale = 41, ViewportAspect = 41, TxtStylePSize = 42, ViewLensLength = 42, ViewFrontClip = 43, ViewBackClip = 44, ShapeXOffset = 44, ViewHeight = 45, ShapeYOffset = 45, ShapeScale = 46, PixelScale = 47, LinetypeScale = 48, DashLength = 49, MlineOffset = 49, LinetypeElement = 49, ViewportSnapAngle = 50, Angle = 50, ViewportTwist = 51, Visibility = 60, LayerLinetype = 61, Color = 62, HasSubentities = 66, ViewportVisibility = 67, ViewportActive = 68, ViewportNumber = 69, Int16 = 70, ViewMode = 71, TxtStyleFlags = 71, RegAppFlags = 71, CircleSides = 72, LinetypeAlign = 72, ViewportZoom = 73, LinetypePdc = 73, ViewportIcon = 74, ViewportSnap = 75, ViewportGrid = 76, ViewportSnapStyle = 77, ViewportSnapPair = 78, Int32 = 90, Subclass = 100, EmbeddedObjectStart = 101, ControlString = 102, DimVarHandle = 105, UcsOrg = 110, UcsOrientationX = 111, UcsOrientationY = 112, XReal = 140, ViewBrightness = 141, ViewContrast = 142, Int64 = 160, XInt16 = 170, NormalX = 210, NormalY = 220, NormalZ = 230, XXInt16 = 270, Int8 = 280, RenderMode = 281, Bool = 290, XTextString = 300, BinaryChunk = 310, ArbitraryHandle = 320, SoftPointerId = 330, HardPointerId = 340, SoftOwnershipId = 350, HardOwnershipId = 360, LineWeight = 370, PlotStyleNameType = 380, PlotStyleNameId = 390, ExtendedInt16 = 400, LayoutName = 410, ColorRgb = 420, ColorName = 430, Alpha = 440, GradientObjType = 450, GradientPatType = 451, GradientTintType = 452, GradientColCount = 453, GradientAngle = 460, GradientShift = 461, GradientTintVal = 462, GradientColVal = 463, GradientName = 470, Comment = 999, ExtendedDataAsciiString = 1000, ExtendedDataRegAppName = 1001, ExtendedDataControlString = 1002, ExtendedDataLayerName = 1003, ExtendedDataBinaryChunk = 1004, ExtendedDataHandle = 1005, ExtendedDataXCoordinate = 1010, ExtendedDataWorldXCoordinate = 1011, ExtendedDataWorldXDisp = 1012, ExtendedDataWorldXDir = 1013, ExtendedDataYCoordinate = 1020, ExtendedDataWorldYCoordinate = 1021, ExtendedDataWorldYDisp = 1022, ExtendedDataWorldYDir = 1023, ExtendedDataZCoordinate = 1030, ExtendedDataWorldZCoordinate = 1031, ExtendedDataWorldZDisp = 1032, ExtendedDataWorldZDir = 1033, ExtendedDataReal = 1040, ExtendedDataDist = 1041, ExtendedDataScale = 1042, ExtendedDataInteger16 = 1070, ExtendedDataInteger32 = 1071, }
filterlist[0] = new TypedValue(0, "CIRCLE");
filterlist[0] = new TypedValue((int)DxfCode.Start, "CIRCLE");
Autodesk.AutoCAD.DatabaseServices.DxfCode
, (, , ). (, , ). ! — , . filterlist[0] = new TypedValue((int)DxfCode.Start, "LINE");
filterlist[0] = new TypedValue((int)DxfCode.Start, "INSERT");
filterlist[0] = new TypedValue((int)DxfCode.LayerName, "layer-1");
TypedValue
you can list several names separated by commas. In this case, the conditions will be combined with the operation "OR" ("OR"). filterlist[0] = new TypedValue((int)DxfCode.Start, "LINE,CIRCLE");
filterlist[0] = new TypedValue((int)DxfCode.LayerName, "layer-1,layer-2");
TypedValue
to an array of conditions. In this case, the conditions will be combined with the operation "AND" ("AND"). TypedValue[] filterlist = new TypedValue[2]; filterlist[0] = new TypedValue((int)DxfCode.Start, "CIRCLE"); filterlist[1] = new TypedValue((int)DxfCode.LayerName, "0");
TypedValue[] filterlist = new TypedValue[2]; filterlist[0] = new TypedValue((int)DxfCode.Start, "INSERT"); filterlist[1] = new TypedValue((int)DxfCode.BlockName, "block-1");
TypedValue[] filterlist = new TypedValue[2]; filterlist[0] = new TypedValue((int)DxfCode.Start, "LINE"); filterlist[1] = new TypedValue((int)DxfCode.LayerName, "0,layer-1");
TypedValue
: , , — .TypedValue
: TypedValue[] filterlist = new TypedValue[10]; filterlist[0] = new TypedValue((int)DxfCode.Operator, "<OR"); filterlist[1] = new TypedValue((int)DxfCode.Operator, "<AND"); filterlist[2] = new TypedValue((int)DxfCode.LayerName, "layer-1"); filterlist[3] = new TypedValue((int)DxfCode.Start, "LINE"); filterlist[4] = new TypedValue((int)DxfCode.Operator, "AND>"); filterlist[5] = new TypedValue((int)DxfCode.Operator, "<AND"); filterlist[6] = new TypedValue((int)DxfCode.LayerName, "layer-2"); filterlist[7] = new TypedValue((int)DxfCode.Start, "CIRCLE"); filterlist[8] = new TypedValue((int)DxfCode.Operator, "AND>"); filterlist[9] = new TypedValue((int)DxfCode.Operator, "OR>");
Layer:layer-2; Type:Autodesk.AutoCAD.DatabaseServices.Circle; Color: 128,255,64
Layer:layer-1; Type:Autodesk.AutoCAD.DatabaseServices.Line; Color: 0,200,255
Layer:layer-1; Type:Autodesk.AutoCAD.DatabaseServices.Line; Color: 255,128,255
Editor.SelectAll()
, there are a number of other ways to search (more precisely, selection) objects in the drawing. Here are the links: English documentation , Russian translation .Editor.SelectAll()
. But we considered the most powerful of them.ModelSpace
), . , , , .. // ( 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); }
ObjectID
, we open the object itself, bring it to the desired type and make the necessary changes to the properties. Well, or remove the object from the drawing, using the appropriate method.Erase();
[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(); } }
UpgradeOpen()
, : Entity entity = (Entity)tr.GetObject(id, OpenMode.ForWrite); entity.Erase();
try...catch
. - : Entity entity = (Entity)tr.GetObject(id, OpenMode.ForRead); try { entity.UpgradeOpen(); entity.Erase(); } catch () { ed.WriteMessage("\nSomething went wrong...\n"); }
try...catch
— . , eOnLockedLayer
, , Autodesk.AutoCAD.Runtime.Exception
( ErrorStatus
) — - : catch (Autodesk.AutoCAD.Runtime.Exception ex) { if (ex.ErrorStatus == ErrorStatus.OnLockedLayer) { ... } }
GetObject
, generally speaking, has the following signature: GetObject(ObjectId id, OpenMode mode, Boolean openErased, Boolean forceOpenOnLockedLayer)
Entity entity = (Entity)tr.GetObject(id, OpenMode.ForWrite, false, true); entity.Erase();
try...catch
you can use a parameter forceOpenOnLockedLayer
. In this case, we discard the method UpgradeOpen()
and immediately open the object with GetObject()
the write method ( OpenMode.ForWrite
) with the flag set forceOpenOnLockedLayer
. [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(); } }
Entity
. , 146% , . — forceOpenOnLockedLayer
try...catch
.Entity
, , , . Circle
: [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(); } }
Circle
, for example, the occurrence of a block will not end well:try...catch
! Or at least provide filtering, as in our example, or check the type immediately before the cast: Entity ent = (Entity)tr.GetObject(id, OpenMode.ForRead); if (ent.GetType() == typeof(Circle)) { Circle cir = (Circle)tr.GetObject(id, OpenMode.ForRead); }
eOnLockedLayer
. To combat it, use either a flag forceOpenOnLockedLayer
, or a construct try...catch
, or filters / checks that will cut off blocked objects. [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) , // ("CIRCLE") - 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(); } }
Position
for a circle is a property Center
. 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); } } } }
Source: https://habr.com/ru/post/262953/
All Articles