【AutoCAD .NET】创建Hatch时报错eInvalidInput

问题出处

https://forums.autodesk.com/t5/net/hatch-to-drawn-polyline-e-message-quot-einvalidinput-quot/m-p/9631373

问题描述

我使用以下语句绘制了一条多段线:
Acad.Application.DocumentManager.MdiActiveDocument.SendStringToExecute("PL ", true, false, true);

按如下方式得到多段线对象:
ObjectId plobj = Autodesk.AutoCAD.Internal.Utils.EntLast();

调用方法:
CommandWrapper.HatchPolyLine(plobj);

在这里,我试图使用以下代码填充到绘制的上面的多段线,但在收到错误消息:
'e.Message = "eInvalidInput"' at 'hatch.AppendLoop(HatchLoopTypes.Default, objectIdColletion);'

下面是我的方法:

        public static void HatchPolyLine(ObjectId plobj)
        {
            try
            {
                if (plobj != null)
                {
                    Polyline pline = null;
                    Document document = Application.DocumentManager.MdiActiveDocument;
                    using (Transaction transaction = document.Database.TransactionManager.StartTransaction())
                    {
                        pline = (Polyline)transaction.GetObject(plobj, OpenMode.ForWrite);
                        using (BlockTable blockTable = CADDBUtil.GetBlockTable(transaction))
                        using (BlockTableRecord modelSpace = CADDBUtil.GetModelSpace(transaction, blockTable))
                        using (Hatch hatch = new Hatch())
                        {
                            modelSpace.UpgradeOpen();

                            ObjectIdCollection objectIdColletion = new ObjectIdCollection();
                            objectIdColletion.Add(pline.Id);

                            hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
                            modelSpace.AppendEntity(hatch);
                            transaction.AddNewlyCreatedDBObject(hatch, true);
                            hatch.AppendLoop(HatchLoopTypes.Default, objectIdColletion);
                            hatch.EvaluateHatch(true);

                            pline.Color = hatch.Color;
                        }
                        transaction.Commit();
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

回答

您确定多段线是闭合的吗?

试试下面代码:

public static void HatchPolyLine(ObjectId plineId)
{
    try
    {
        if (plineId.IsNull)
            throw new ArgumentNullException("plineId");

        if (plineId.ObjectClass != RXObject.GetClass(typeof(Polyline)))
            throw new Autodesk.AutoCAD.Runtime.Exception(ErrorStatus.IllegalEntityType);

        var ids = new ObjectIdCollection();
        ids.Add(plineId);

        using (var tr = plineId.Database.TransactionManager.StartTransaction())
        {
            var pline = (Polyline)tr.GetObject(plineId, OpenMode.ForRead);
            if (!(pline.Closed || pline.GetPoint2dAt(0).IsEqualTo(pline.GetPoint2dAt(pline.NumberOfVertices - 1))))
                throw new InvalidOperationException("Opened polyline.");

            var owner = (BlockTableRecord)tr.GetObject(pline.OwnerId, OpenMode.ForWrite);
            var hatch = new Hatch();
            hatch.SetHatchPattern(HatchPatternType.PreDefined, "SOLID");
            owner.AppendEntity(hatch);
            tr.AddNewlyCreatedDBObject(hatch, true);
            hatch.Associative = true;
            hatch.AppendLoop(HatchLoopTypes.Default, ids);
            hatch.EvaluateHatch(true);
            tr.Commit();
        }
    }
    catch(System.Exception ex)
    {
        var ed = Application.DocumentManager.MdiActiveDocument.Editor;
        ed.WriteMessage($"{ex.Message}\n{ex.StackTrace}");
    }
}
posted @ 2024-03-14 14:17  greencode  阅读(171)  评论(0编辑  收藏  举报