public static string disclaimer = " AutoCAD. – .";
using System; using System.IO; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.ApplicationServices; using acad = Autodesk.AutoCAD.ApplicationServices.Application; namespace HabrPlug_Primitives { public class ClassMyAutoCADDLL { public class Commands : IExtensionApplication { // AutoCAD "HabrCommand" [CommandMethod("HabrCommand")] public void HabrCommand() { // Document acDoc = acad.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // (Model Space) - BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // Line acLine = new Line(new Point3d(25, 25, 0), new Point3d(33, 33, 0)); // acLine.SetDatabaseDefaults(); // acBlkTblRec.AppendEntity(acLine); // acTrans.AddNewlyCreatedDBObject(acLine, true); // acTrans.Commit(); } } // Initialize() Terminate() , IExtensionApplication public void Initialize() { } public void Terminate() { } } } }
CopyLocal
parameter to False
).Model Space
) - this is, in fact, our drawing. A clearer definition of Model Space can be found here and there . Since we are going to add a new object to the drawing, we need to request read and write access ( OpenMode.ForWrite
).SetDatabaseDefaults()
function to do this. A mean male description of this function can be found in the ObjectARX Reference .AppendEntity()
function.AddNewlyCreatedDBObject()
function, which was already discussed in the previous article.Commit()
method. [CommandMethod("HabrCommand")] public void HabrCommand() { // Document acDoc = acad.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // (Model Space) - BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // Polyline acPolyline = new Polyline(); // acPolyline.SetDatabaseDefaults(); // acPolyline.AddVertexAt(0, new Point2d(2, 4), 0, 0, 0); acPolyline.AddVertexAt(1, new Point2d(4, 8), 0, 0, 0); acPolyline.AddVertexAt(2, new Point2d(6, 6), 0, 0, 0); acPolyline.AddVertexAt(3, new Point2d(8, 11), 0, 0, 0); // acBlkTblRec.AppendEntity(acPolyline); // acTrans.AddNewlyCreatedDBObject(acPolyline, true); // acTrans.Commit(); } }
[CommandMethod("HabrCommand")] public void HabrCommand() { // Document acDoc = acad.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // (Model Space) - BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // Polyline acPolyline = new Polyline(); // acPolyline.SetDatabaseDefaults(); // acPolyline.AddVertexAt(0, new Point2d(100, 100), 0, 0, 0); acPolyline.AddVertexAt(1, new Point2d(100, 650), 0, 0, 0); acPolyline.AddVertexAt(2, new Point2d(1050, 650), 0, 0, 0); acPolyline.AddVertexAt(3, new Point2d(650, 1150), 0, 0, 0); acPolyline.AddVertexAt(4, new Point2d(1050, 650), 0, 0, 0); acPolyline.AddVertexAt(5, new Point2d(2050, 650), 0, 0, 0); acPolyline.AddVertexAt(6, new Point2d(2050, 100), 0, 0, 0); acPolyline.AddVertexAt(7, new Point2d(1950, 100), 0, 0, 0); acPolyline.AddVertexAt(8, new Point2d(1950, 400), 0, 0, 0); acPolyline.AddVertexAt(9, new Point2d(1800, 400), 0, 0, 0); acPolyline.AddVertexAt(10, new Point2d(1800, 100), 0, 0, 0); acPolyline.AddVertexAt(11, new Point2d(1950, 100), 0, 0, 0); acPolyline.AddVertexAt(12, new Point2d(1700, 100), 0, 0, 0); acPolyline.AddVertexAt(13, new Point2d(1660, 170), 0, 0, 0); acPolyline.AddVertexAt(14, new Point2d(1600, 225), 0, 0, 0); acPolyline.AddVertexAt(15, new Point2d(1500, 225), 0, 0, 0); acPolyline.AddVertexAt(16, new Point2d(1440, 170), 0, 0, 0); acPolyline.AddVertexAt(17, new Point2d(1400, 100), 0, 0, 0); acPolyline.AddVertexAt(18, new Point2d(850, 100), 0, 0, 0); acPolyline.AddVertexAt(19, new Point2d(1200, 100), 0, 0, 0); acPolyline.AddVertexAt(20, new Point2d(1200, 400), 0, 0, 0); acPolyline.AddVertexAt(21, new Point2d(1000, 400), 0, 0, 0); acPolyline.AddVertexAt(22, new Point2d(1000, 100), 0, 0, 0); acPolyline.AddVertexAt(23, new Point2d(1100, 100), 0, 0, 0); acPolyline.AddVertexAt(24, new Point2d(1100, 400), 0, 0, 0); acPolyline.AddVertexAt(25, new Point2d(1100, 100), 0, 0, 0); acPolyline.AddVertexAt(26, new Point2d(850, 100), 0, 0, 0); acPolyline.AddVertexAt(27, new Point2d(810, 170), 0, 0, 0); acPolyline.AddVertexAt(28, new Point2d(750, 225), 0, 0, 0); acPolyline.AddVertexAt(29, new Point2d(650, 225), 0, 0, 0); acPolyline.AddVertexAt(30, new Point2d(590, 170), 0, 0, 0); acPolyline.AddVertexAt(31, new Point2d(550, 100), 0, 0, 0); acPolyline.AddVertexAt(32, new Point2d(100, 100), 0, 0, 0); acPolyline.AddVertexAt(33, new Point2d(450, 100), 0, 0, 0); acPolyline.AddVertexAt(34, new Point2d(450, 400), 0, 0, 0); acPolyline.AddVertexAt(35, new Point2d(250, 400), 0, 0, 0); acPolyline.AddVertexAt(36, new Point2d(250, 100), 0, 0, 0); acPolyline.AddVertexAt(37, new Point2d(350, 100), 0, 0, 0); acPolyline.AddVertexAt(38, new Point2d(350, 400), 0, 0, 0); // acBlkTblRec.AppendEntity(acPolyline); // acTrans.AddNewlyCreatedDBObject(acPolyline, true); // acTrans.Commit(); } }
[CommandMethod("HabrCommand")] public void HabrCommand() { // Document acDoc = acad.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // (Model Space) - BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // Circle acCircle = new Circle(); // acCircle.SetDatabaseDefaults(); acCircle.Center = new Point3d(2.5, 3.14, 0); acCircle.Radius = 4.25; // acBlkTblRec.AppendEntity(acCircle); // acTrans.AddNewlyCreatedDBObject(acCircle, true); // acTrans.Commit(); } }
[CommandMethod("HabrCommand")] public void HabrCommand() { // Document acDoc = acad.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // (Model Space) - BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // Point3d center = Point3d.Origin; Vector3d normal = Vector3d.ZAxis; Vector3d majorAxis = 100 * Vector3d.XAxis; double radiusRatio = 0.5; double startAng = 0.0; double endAng = Math.PI * 2; // , Ellipse acEllipse = new Ellipse(center, normal, majorAxis, radiusRatio, startAng, endAng); // acBlkTblRec.AppendEntity(acEllipse); // acTrans.AddNewlyCreatedDBObject(acEllipse, true); // acTrans.Commit(); } }
Point3d center = Point3d.Origin; Vector3d normal = Vector3d.ZAxis; Vector3d majorAxis = 100 * Vector3d.XAxis + 100 * Vector3d.YAxis; double radiusRatio = 0.5; double startAng = Math.PI * 0.1; double endAng = Math.PI * 1.3; Ellipse acEllipse = new Ellipse(center, normal, majorAxis, radiusRatio, startAng, endAng);
startAng
greater than zero, the upper part of the ellipse does not start from the axis, but a little further. Since endAng
doesn’t reach endAng
much, the lower part of the ellipse ends much earlier than it should be.majorAxis = 100 * Vector3d.XAxis + 100 * Vector3d.YAxis
). Since the X component of this vector is equal in absolute value to the Y component, we obtain a slope of 45 degrees.Hatch
( documentation , translation ). [CommandMethod("HabrCommand")] public void HabrCommand() { // Document acDoc = acad.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // (Model Space) - BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // 1) - Circle acCircle = new Circle(); // 2) acCircle.SetDatabaseDefaults(); acCircle.Center = new Point3d(2.5, 3.14, 0); acCircle.Radius = 4.25; // 3) acBlkTblRec.AppendEntity(acCircle); acTrans.AddNewlyCreatedDBObject(acCircle, true); // 4) - ObjectIdCollection acObjIdColl = new ObjectIdCollection(); acObjIdColl.Add(acCircle.ObjectId); // 5) Hatch acHatch = new Hatch(); // 6) acBlkTblRec.AppendEntity(acHatch); acTrans.AddNewlyCreatedDBObject(acHatch, true); // 7) acHatch.SetDatabaseDefaults(); acHatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID"); acHatch.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(0, 200, 0); acHatch.Associative = true; acHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl); acHatch.EvaluateHatch(true); // acTrans.Commit(); } }
setDatabaseDefaults()
function, which sets the hatching parameters to be the default parameters. The purpose of this function was discussed in the first section.SetHatchPattern()
function. In this case, we want to draw a normal circle, so we choose the type SOLID
- “solid”.SOLID
we could specify something else. For example, specifying HOUND
, we would get the following result:HatchPatternType.PreDefined
), you can use third-party ones that were previously connected to AutoCAD. Here is a link to an example.Color
property) using the FromRgb(byte ref, byte blue, byte green)
function FromRgb(byte ref, byte blue, byte green)
.Associative
property follows - it determines whether the hatching will change size when its borders are resized.Associative = true
) as the hatching boundaries change:Associative = false
:Associative
property must be set after adding the shriches to the block table (that is, the model space) and before calling the AppendLoop
method.AppendLoop
method allows AppendLoop
to specify the external border of the hatching and takes two parameters as input: the type of the border and the array of border-hatch objects. As the first parameter, we specified HatchLoopTypes.Outermost
, thereby denoting that we want to set the outer boundary of the hatching. As the second, we have specified the previously created array of border-hatch objects. [CommandMethod("HabrCommand")] public void HabrCommand() { // Document acDoc = acad.DocumentManager.MdiActiveDocument; Database acCurDb = acDoc.Database; // using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction()) { // BlockTable acBlkTbl; acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable; // (Model Space) - BlockTableRecord acBlkTblRec; acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // Polyline acPolyline = new Polyline(); acPolyline.SetDatabaseDefaults(); acPolyline.AddVertexAt(0, new Point2d(50, 50), 0, 0, 0); acPolyline.AddVertexAt(1, new Point2d(150, 285), 0, 0, 0); acPolyline.AddVertexAt(2, new Point2d(250, 50), 0, 0, 0); acPolyline.AddVertexAt(3, new Point2d(25, 200), 0, 0, 0); acPolyline.AddVertexAt(4, new Point2d(275, 200), 0, 0, 0); acPolyline.AddVertexAt(5, new Point2d(50, 50), 0, 0, 0); acBlkTblRec.AppendEntity(acPolyline); acTrans.AddNewlyCreatedDBObject(acPolyline, true); // - ObjectIdCollection acObjIdColl_OUTER = new ObjectIdCollection(); acObjIdColl_OUTER.Add(acPolyline.ObjectId); // Circle acCircleOut = new Circle(); acCircleOut.SetDatabaseDefaults(); acCircleOut.Center = new Point3d(150, 165, 0); acCircleOut.Radius = 25; acBlkTblRec.AppendEntity(acCircleOut); acTrans.AddNewlyCreatedDBObject(acCircleOut, true); // - ObjectIdCollection acObjIdColl_INNER = new ObjectIdCollection(); acObjIdColl_INNER.Add(acCircleOut.ObjectId); // Hatch acHatch = new Hatch(); acBlkTblRec.AppendEntity(acHatch); acTrans.AddNewlyCreatedDBObject(acHatch, true); // acHatch.SetDatabaseDefaults(); acHatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID"); acHatch.Color = Autodesk.AutoCAD.Colors.Color.FromRgb(200, 0, 0); acHatch.Associative = false; acHatch.HatchStyle = HatchStyle.Normal; // acHatch.AppendLoop(HatchLoopTypes.Outermost, acObjIdColl_OUTER); // acHatch.AppendLoop(HatchLoopTypes.Default, acObjIdColl_INNER); acHatch.EvaluateHatch(true); // acTrans.Commit(); } }
EvaluateHatch()
. DONUT < > < > <>
DONUT 0 25 50,50 DONUT 25 75 200,50
Source: https://habr.com/ru/post/257129/
All Articles