AutoCAD .NET-从折线创建剖面线

时间:2018-08-02 14:41:48

标签: c# .net autocad

我一直在从事AutoCAD项目。我的任务是创建一种从自定义折线绘制阴影的方法。在下面,我试图创建一个函数,该函数将从用户那里获取一些要点,然后将最后一个点连接到第一个点,并将多义线括起来。

但是,在acHatch.AppendLoop的行中,我收到一个异常消息:

Autodesk.AutoCAD.Runtime.Exception: 'eInvalidInput'

我一直试图弄清楚为什么这可能行不通。例如,使用相同的代码绘制一条封闭的折线,然后手动使用hatch命令似乎可行。

public static void DrawArea()
    {
        Document acDoc = Application.DocumentManager.MdiActiveDocument;
        Database acCurDb = acDoc.Database;

        PromptPointResult ptRes;
        Point2dCollection colPt = new Point2dCollection();
        PromptPointOptions ptOpts = new PromptPointOptions("");
        ptOpts.Keywords.Add("Stop");

        using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
        {
            BlockTable acBt;
            acBt = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;

            BlockTableRecord acBtr;
            acBtr = acTrans.GetObject(acBt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;

            ptOpts.Message = "\nSpecify point: ";
            ptRes = acDoc.Editor.GetPoint(ptOpts);
            colPt.Add(new Point2d(ptRes.Value.X, ptRes.Value.Y));

            int cnt = 0;

            while (cnt < 3)
            {
                ptOpts.UseBasePoint = true;
                ptOpts.BasePoint = ptRes.Value;

                ptRes = acDoc.Editor.GetPoint(ptOpts);
                colPt.Add(new Point2d(ptRes.Value.X, ptRes.Value.Y));

                if (ptRes.StringResult == "Stop") break;
                cnt++;
            }

            using (Polyline acPoly = new Polyline())
            {

                int i = 0;
                foreach (Point2d point in colPt)
                {
                    acPoly.AddVertexAt(i, point, 0, 0, 0);
                }

                int last = colPt.Count;

                acPoly.AddVertexAt(i, colPt[0], 0, 0, 0);

                ObjectId oid = new ObjectId();
                oid = acBtr.AppendEntity(acPoly);
                //acTrans.AddNewlyCreatedDBObject(acPoly, true);

                acPoly.Closed = true;

                ObjectIdCollection oidCol = new ObjectIdCollection();
                oidCol.Add(oid);

                using (Hatch acHatch = new Hatch())
                {
                    acBtr.AppendEntity(acHatch);
                    acTrans.AddNewlyCreatedDBObject(acHatch, true);

                    acHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
                    acHatch.Associative = true;
                    acHatch.AppendLoop(HatchLoopTypes.Polyline, oidCol);
                    acHatch.EvaluateHatch(true);

                    using (DBText acLabel = new DBText())
                    {
                        acLabel.TextString = acHatch.Area.ToString();
                        acLabel.Position = new Point3d(colPt[3].X + 15, colPt[3].Y, 0);
                        acLabel.Height = 25;

                        acBtr.AppendEntity(acLabel);
                        acTrans.AddNewlyCreatedDBObject(acLabel, true);
                    }
                }
            }

            acTrans.Commit();
        }
    }

1 个答案:

答案 0 :(得分:0)

我也在Autodesk论坛上问过这个问题。使用以下行创建填充图案:acHatch.AppendLoop(HatchLoopTypes.External,oidCol);

原始答案here