Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Autodesk.AutoCAD.DatabaseServices;
- using Autodesk.AutoCAD.EditorInput;
- using Autodesk.AutoCAD.Geometry;
- using Autodesk.AutoCAD.Runtime;
- using lab6_task3;
- using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
- [assembly: CommandClass(typeof(MyCommands))]
- namespace lab6_task3
- {
- public class MyCommands
- {
- [CommandMethod("addAnEnt")]
- public void AddAnEnt()
- {
- var ed = Application.DocumentManager.MdiActiveDocument.Editor;
- var getWhichEntityOptions = new PromptKeywordOptions(
- "Which entity do you want to create? [Circle/Block] : ", "Circle Block");
- var getWhichEntityResult = ed.GetKeywords(getWhichEntityOptions);
- if (getWhichEntityResult.Status == PromptStatus.OK)
- {
- switch (getWhichEntityResult.StringResult)
- {
- case "Circle":
- var getPointOptions = new PromptPointOptions("Pick Center Point : ");
- var getPointResult = ed.GetPoint(getPointOptions);
- if (getPointResult.Status == PromptStatus.OK)
- {
- var getRadiusOptions = new PromptDistanceOptions("Pick Radius : ")
- {
- BasePoint = getPointResult.Value,
- UseBasePoint = true
- };
- var getRadiusResult = ed.GetDistance(getRadiusOptions);
- if (getRadiusResult.Status == PromptStatus.OK)
- {
- var dwg = ed.Document.Database;
- var trans = dwg.TransactionManager.StartTransaction();
- try
- {
- var circle = new Circle(getPointResult.Value, Vector3d.ZAxis, getRadiusResult.Value);
- var btr = (BlockTableRecord)trans.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite);
- btr.AppendEntity(circle);
- trans.AddNewlyCreatedDBObject(circle, true);
- trans.Commit();
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage("problem due to " + ex.Message);
- }
- finally
- {
- trans.Dispose();
- }
- }
- }
- break;
- case "Block":
- var blockNameOptions = new PromptStringOptions("Enter name of the Block to create : ")
- {
- AllowSpaces = false
- };
- var blockNameResult = ed.GetString(blockNameOptions);
- if (blockNameResult.Status == PromptStatus.OK)
- {
- var dwg = ed.Document.Database;
- var trans = dwg.TransactionManager.StartTransaction();
- try
- {
- var newBlockDef = new BlockTableRecord
- {
- Name = blockNameResult.StringResult
- };
- var blockTable = (BlockTable) trans.GetObject(dwg.BlockTableId, OpenMode.ForRead);
- if (blockTable.Has(blockNameResult.StringResult) == false)
- {
- blockTable.UpgradeOpen();
- blockTable.Add(newBlockDef);
- trans.AddNewlyCreatedDBObject(newBlockDef, true);
- var circle1 = new Circle(new Point3d(0, 0, 0), Vector3d.ZAxis, 10);
- newBlockDef.AppendEntity(circle1);
- var circle2 = new Circle(new Point3d(20, 10, 0), Vector3d.ZAxis, 10);
- newBlockDef.AppendEntity(circle2);
- trans.AddNewlyCreatedDBObject(circle1, true);
- trans.AddNewlyCreatedDBObject(circle2, true);
- var blockRefPointOptions =
- new PromptPointOptions("Pick insertion point of BlockRef : ");
- var blockRefPointResult = ed.GetPoint(blockRefPointOptions);
- if (blockRefPointResult.Status != PromptStatus.OK)
- {
- trans.Dispose();
- return;
- }
- var blockRef = new BlockReference(blockRefPointResult.Value, newBlockDef.ObjectId);
- var curSpace =
- (BlockTableRecord) trans.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite);
- curSpace.AppendEntity(blockRef);
- trans.AddNewlyCreatedDBObject(blockRef, true);
- trans.Commit();
- }
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage("a problem occurred because " + ex.Message);
- }
- finally
- {
- trans.Dispose();
- }
- }
- break;
- }
- }
- }
- [CommandMethod("additionalTask")]
- public void additionalTask()
- {
- var ed = Application.DocumentManager.MdiActiveDocument.Editor;
- var blockNameOptions = new PromptStringOptions("Enter name of the Block to create : ")
- {
- AllowSpaces = false
- };
- var blockNameResult = ed.GetString(blockNameOptions);
- if (blockNameResult.Status == PromptStatus.OK)
- {
- var dwg = ed.Document.Database;
- var trans = dwg.TransactionManager.StartTransaction();
- try
- {
- var newBlockDef = new BlockTableRecord
- {
- Name = blockNameResult.StringResult
- };
- var blockTable = (BlockTable)trans.GetObject(dwg.BlockTableId, OpenMode.ForRead);
- if (blockTable.Has(blockNameResult.StringResult) == false)
- {
- blockTable.UpgradeOpen();
- blockTable.Add(newBlockDef);
- trans.AddNewlyCreatedDBObject(newBlockDef, true);
- var circles = new Circle[5];
- var x = 0;
- for (var i = 0; i < 5; i++, x += 10)
- {
- circles[i] = new Circle(new Point3d(2*x, 0, 0), Vector3d.ZAxis, 5);
- newBlockDef.AppendEntity(circles[i]);
- trans.AddNewlyCreatedDBObject(circles[i], true);
- }
- var blockRefPointOptions = new PromptPointOptions("Pick insertion point of BlockRef : ");
- var blockRefPointResult = ed.GetPoint(blockRefPointOptions);
- if (blockRefPointResult.Status != PromptStatus.OK)
- {
- trans.Dispose();
- return;
- }
- var blockRef = new BlockReference(blockRefPointResult.Value, newBlockDef.ObjectId);
- var curSpace = (BlockTableRecord)trans.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite);
- curSpace.AppendEntity(blockRef);
- trans.AddNewlyCreatedDBObject(blockRef, true);
- trans.Commit();
- }
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage("a problem occurred because " + ex.Message);
- }
- finally
- {
- trans.Dispose();
- }
- }
- }
- [CommandMethod("addTask")]
- public void addTask()
- {
- var ed = Application.DocumentManager.MdiActiveDocument.Editor;
- var blockNameOptions = new PromptStringOptions("Enter name of the Block to create : ")
- {
- AllowSpaces = false
- };
- var blockNameResult = ed.GetString(blockNameOptions);
- if (blockNameResult.Status == PromptStatus.OK)
- {
- var dwg = ed.Document.Database;
- var trans = dwg.TransactionManager.StartTransaction();
- try
- {
- var newBlockDef = new BlockTableRecord
- {
- Name = blockNameResult.StringResult
- };
- var blockTable = (BlockTable)trans.GetObject(dwg.BlockTableId, OpenMode.ForRead);
- if (blockTable.Has(blockNameResult.StringResult) == false)
- {
- blockTable.UpgradeOpen();
- blockTable.Add(newBlockDef);
- trans.AddNewlyCreatedDBObject(newBlockDef, true);
- const int number = 50;
- var circles = new Circle[number];
- for (var i = 0; i < number; i++)
- {
- const int leafs = 10;
- const int leaf_size = 4;
- const int rad = 20;
- var r = rad + leaf_size*Math.Sin(2*leafs*Math.PI*i/number);
- var x = r*Math.Cos(2*Math.PI*i/number);
- var y = r*Math.Sin(2*Math.PI*i/number);
- circles[i] = new Circle(new Point3d(x, y, 0), Vector3d.ZAxis, 1);
- newBlockDef.AppendEntity(circles[i]);
- trans.AddNewlyCreatedDBObject(circles[i], true);
- }
- var blockRefPointOptions = new PromptPointOptions("Pick insertion point of BlockRef : ");
- var blockRefPointResult = ed.GetPoint(blockRefPointOptions);
- if (blockRefPointResult.Status != PromptStatus.OK)
- {
- trans.Dispose();
- return;
- }
- var blockRef = new BlockReference(blockRefPointResult.Value, newBlockDef.ObjectId);
- var curSpace = (BlockTableRecord)trans.GetObject(dwg.CurrentSpaceId, OpenMode.ForWrite);
- curSpace.AppendEntity(blockRef);
- trans.AddNewlyCreatedDBObject(blockRef, true);
- trans.Commit();
- }
- }
- catch (System.Exception ex)
- {
- ed.WriteMessage("a problem occurred because " + ex.Message);
- }
- finally
- {
- trans.Dispose();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement