【AutoCAD .NET】建立Hatch時報錯eInvalidInput

greencode發表於2024-03-14

問題出處

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}");
    }
}

相關文章