The AutoCAD .NET API allows you to manage an AutoCAD application and drawing files at the program level using available assemblies or libraries. These objects may be available for a variety of different programming languages ​​and various software development environments.
ObjectARX is a large set of libraries designed to develop applications for AutoCAD in the Microsoft Visual C ++ programming environment. AutoCAD itself is designed using ObjectARX.
using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.EditorInput; namespace clExample { public class clExample { [CommandMethod("addEntity")] // , Autocad public void addEntity() { // Database dbCurrent = Application.DocumentManager.MdiActiveDocument.Database; // , : using (Transaction trAdding = dbCurrent.TransactionManager.StartTransaction()) { // Circle cNewCircle = new Circle(); cNewCircle.Center = new Point3d(0, 0, 0); cNewCircle.Radius = 100; cNewCircle.ColorIndex = 5; // ( ) BlockTableRecord btrCurrSpace = trAdding.GetObject (dbCurrent.CurrentSpaceId, OpenMode.ForWrite) as BlockTableRecord; // ObjectId oidCircle = btrCurrSpace.AppendEntity(cNewCircle); trAdding.AddNewlyCreatedDBObject(cNewCircle, true); // trAdding.Commit(); } } } }
[CommandMethod("createBlock")] // , Autocad public void createBlock() { // Database dbCurrent = Application.DocumentManager.MdiActiveDocument.Database; // Editor edCurrent = Application.DocumentManager.MdiActiveDocument.Editor; // , : using (Transaction trAdding = dbCurrent.TransactionManager.StartTransaction()) { // BlockTable btTable = (BlockTable)trAdding.GetObject(dbCurrent.BlockTableId, OpenMode.ForRead); // PromptStringOptions psoOpt = new PromptStringOptions("\n : "); psoOpt.AllowSpaces = true; string strBlockName = ""; // do { // PromptResult prRes = edCurrent.GetString(psoOpt); // . if (prRes.Status != PromptStatus.OK) return; try { // SymbolUtilityServices.ValidateSymbolName( prRes.StringResult, false ); // if (btTable.Has(prRes.StringResult)) edCurrent.WriteMessage("\nA block with this name already exists."); else // , :) strBlockName = prRes.StringResult; } catch { edCurrent.WriteMessage("\nInvalid block name."); } } while (strBlockName == ""); // // , Line lNewLine = new Line(); lNewLine.StartPoint = new Point3d(0, 0, 0); lNewLine.EndPoint = new Point3d(50, 50, 0); lNewLine.ColorIndex = 3; Circle cNewCircle = new Circle(); cNewCircle.Center = new Point3d(0, 0, 0); cNewCircle.Radius = 100; cNewCircle.ColorIndex = 5; // AttributeDefinition adAttr = new AttributeDefinition(); adAttr.Position = new Point3d(0, 0, 0); adAttr.Tag = "ATTRDEF"; // BlockTableRecord btrRecord = new BlockTableRecord(); btrRecord.Name = strBlockName; btTable.UpgradeOpen(); // ObjectId btrId = btTable.Add(btrRecord); trAdding.AddNewlyCreatedDBObject(btrRecord, true); // btrRecord.AppendEntity(lNewLine); trAdding.AddNewlyCreatedDBObject(lNewLine, true); btrRecord.AppendEntity(cNewCircle); trAdding.AddNewlyCreatedDBObject(cNewCircle, true); // btrRecord.AppendEntity(adAttr); trAdding.AddNewlyCreatedDBObject(adAttr, true); // // BlockTableRecord btrModelSpace = (BlockTableRecord)trAdding.GetObject( btTable[BlockTableRecord.ModelSpace], OpenMode.ForWrite ); // BlockReference brRefBlock = new BlockReference(Point3d.Origin, btrId); // btrModelSpace.AppendEntity(brRefBlock); trAdding.AddNewlyCreatedDBObject(brRefBlock, true); // AttributeReference arAttr = new AttributeReference(); arAttr.SetAttributeFromBlock(adAttr, brRefBlock.BlockTransform); arAttr.TextString = "!"; brRefBlock.AttributeCollection.AppendAttribute(arAttr); trAdding.AddNewlyCreatedDBObject(arAttr, true); // trAdding.Commit(); } }
Source: https://habr.com/ru/post/149546/
All Articles