ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C#/AutoCAD] Entity 추가
    C#/AutoCAD API 2023. 7. 15. 07:44

    1. 현재 활성화 되어있는 문서를 가져와 Transaction을 시작하고 Entity를 생성한 후 Commit을 한다. (Winform으로 생성시에는 DocumentLock 필요)

    2. 아래의 기본 코드에 'Write code here'에 Entity를 생성하는 코드를 작성하면 된다.

    Document doc = Application.DocumentManager.MdiActiveDocument;
    using (DocumentLock docLock = doc.LockDocument())
    {
        Database db = doc.Database;
        using (Transaction tr = db.TransactionManager.StartTransaction())
        {
            BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
            BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
            
            // Write code here.
            
            tr.Commit()
        }
    }

    - Line 생성

            Line line = new Line(new Point3d(0,0,0), new Point3d(100,0,0));
            btr.AppendEntity(line);
            tr.AddNewlyCreatedDBObject(line, true);

    - Polyline 생성

            using (Polyline poly = new Polyline())
            {
                poly.AddVertexAt(0, new Point2d(2, 4), 0, 0, 0);
                poly.AddVertexAt(1, new Point2d(4, 2), 0, 0, 0);
                poly.AddVertexAt(2, new Point2d(6, 4), 0, 0, 0);
                // Add the new object to the block table record and the transaction
                btr.AppendEntity(poly);
                tr.AddNewlyCreatedDBObject(poly, true);
            }

    - Arc 생성

            // (center pos, radius, start radian, end radian)
            Arc arc = new Arc(new Point3d(0,0,0), 1000, Math.PI / 180 * 180, Math.PI / 180 * 270);
            btr.AppendEntity(arc);
            tr.AddNewlyCreatedDBObject(arc, true);

    - MText 생성

            using (MText mText = new MText())
            {
                mText.Location = new Point3d(0,0,0);
                mText.TextHeight = 500;
                mText.Color = Color.FromColorIndex(ColorMethod.ByAci, 2);
                mText.Attachment = AttachmentPoint.TopRight;
                mText.Contents = "Hello World";
                btr.AppendEntity(mText);
                tr.AddNewlyCreatedDBObject(mText, true);
            }

    - Hatch 생성

            ObjectIdCollection lines = new ObjectIdCollection();
            lines.Add(lineObjId1);
            lines.Add(lineObjId2);
            lines.Add(lineObjId3);
            lines.Add(lineObjId4);
            using (Hatch hatch = new Hatch())
            {
                hatch.SetDatabaseDefaults();
                hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
                hatch.AppendLoop(HatchLoopTypes.Outermost, lines);
                hatch.Layer = "0";
                hatch.Color = Color.FromColorIndex(ColorMethod.ByAci, 7);
                // Transparency(127) => 50%
                hatch.Transparency = new Transparency(127);
                hatch.EvaluateHatch(true);
                btr.AppendEntity(hatch);
                tr.AddNewlyCreatedDBObject(hatch, true);
            }
Designed by Tistory.