Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在图案填充中剪切孔-Autocad_C#_Autocad - Fatal编程技术网

C# 如何在图案填充中剪切孔-Autocad

C# 如何在图案填充中剪切孔-Autocad,c#,autocad,C#,Autocad,我需要在填充图案上剪个洞。孔是未填充的区域。 我有两条闭合的多段线。我需要填充poly1-poly2区域 我尝试将这两个元素都添加到ObjectdCollection,但这会引发一个错误 我试着创建区域并减去它。但是,我们可以为闭合多段线创建区域,但不能为图案填充创建区域 下面是创建图案填充的代码 [CommandMethod("TestHatchHole", CommandFlags.UsePickSet)] public static void CreateHatch()

我需要在填充图案上剪个洞。孔是未填充的区域。 我有两条闭合的多段线。我需要填充poly1-poly2区域

我尝试将这两个元素都添加到ObjectdCollection,但这会引发一个错误

我试着创建区域并减去它。但是,我们可以为闭合多段线创建区域,但不能为图案填充创建区域

下面是创建图案填充的代码

   [CommandMethod("TestHatchHole", CommandFlags.UsePickSet)]
    public static void CreateHatch()
    {

        Document doc = Application.DocumentManager.MdiActiveDocument;
        Database db = doc.Database;
        Editor ed = doc.Editor;
        try
        {
            using (Transaction Tx = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction())
            {

                ObjectId ModelSpaceId =
                SymbolUtilityServices.GetBlockModelSpaceId(db);

                BlockTableRecord btr = Tx.GetObject(ModelSpaceId,
                                  OpenMode.ForWrite) as BlockTableRecord;

                Point2d pt = new Point2d(0.0, 0.0);
                Autodesk.AutoCAD.DatabaseServices.Polyline plBox =
                          new Autodesk.AutoCAD.DatabaseServices.Polyline(4);

                plBox.Normal = Vector3d.ZAxis;
                plBox.AddVertexAt(0, pt, 0.0, -1.0, -1.0);
                plBox.AddVertexAt(1,
                             new Point2d(pt.X + 50, pt.Y), 0.0, -1.0, -1.0);
                plBox.AddVertexAt(2,
                          new Point2d(pt.X + 50, pt.Y + 50), 0.0, -1.0, -1.0);
                plBox.AddVertexAt(3,
                              new Point2d(pt.X, pt.Y + 50), 0.0, -1.0, -1.0);
                plBox.Closed = true;

                ObjectId pLineId;
                pLineId = btr.AppendEntity(plBox);
                Tx.AddNewlyCreatedDBObject(plBox, true);


                Point2d pt1 = new Point2d(0.0, 0.0);
                Autodesk.AutoCAD.DatabaseServices.Polyline plHole =
                          new Autodesk.AutoCAD.DatabaseServices.Polyline(4);

                plHole.Normal = Vector3d.ZAxis;
                plHole.AddVertexAt(0, pt1, 0.0, -1.0, -1.0);
                plHole.AddVertexAt(1,
                             new Point2d(pt1.X + 5, pt1.Y), 0.0, -1.0, -1.0);
                plHole.AddVertexAt(2,
                          new Point2d(pt1.X + 5, pt1.Y + 5), 0.0, -1.0, -1.0);
                plHole.AddVertexAt(3,
                              new Point2d(pt1.X, pt1.Y + 5), 0.0, -1.0, -1.0);
                plHole.Closed = true;

                var plHoleId = btr.AppendEntity(plHole);
                Tx.AddNewlyCreatedDBObject(plHole, true);




                ObjectIdCollection ObjIds = new ObjectIdCollection();
                ObjIds.Add(pLineId);

                Hatch oHatch = new Hatch();
                Vector3d normal = new Vector3d(0.0, 0.0, 1.0);
                oHatch.Normal = normal;
                oHatch.Elevation = 0.0;
                oHatch.PatternScale = 2.0;
                string hatchPattern = "ANSI31";
                oHatch.SetHatchPattern(HatchPatternType.PreDefined, hatchPattern);
                oHatch.ColorIndex = 1;

                btr.AppendEntity(oHatch);
                Tx.AddNewlyCreatedDBObject(oHatch, true);
                //this works ok  
                oHatch.Associative = true;
                oHatch.AppendLoop((int)HatchLoopTypes.Default, ObjIds);

                oHatch.EvaluateHatch(true);

                Tx.Commit();
            }
        }
        catch (Autodesk.AutoCAD.Runtime.Exception ex)
        {

        }
        catch (System.Exception ex)
        {

        }
    }

使用AppendLoop方法,使用HatchLoopTypes.External添加图案填充的初始外部边界。任何附加边界都需要使用HatchLoopTypes.Default枚举单独追加

下面是一个例子:

using (Transaction acTrans = acDb.TransactionManager.StartTransaction())
{
    var acBlkTbl = (BlockTable)acTrans.GetObject(acDb.BlockTableId, OpenMode.ForRead);
    var acBlkTblRec = (BlockTableRecord)acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite);

    // Create the rectangle
    var acPoint = new Point2d(0, 0);
    var acPoly = new Polyline(4);
    acPoly.Normal = Vector3d.ZAxis;
    acPoly.AddVertexAt(0, acPoint, 0, -1, -1);
    acPoly.AddVertexAt(1, new Point2d(acPoint.X + 20, acPoint.Y), 0, -1, -1);
    acPoly.AddVertexAt(2, new Point2d(acPoint.X + 20, acPoint.Y + 20), 0.0, -1.0, -1.0);
    acPoly.AddVertexAt(3, new Point2d(acPoint.X, acPoint.Y + 20), 0.0, -1.0, -1.0);
    acPoly.Closed = true;

    // Add the rectangle to the block table record
    acBlkTblRec.AppendEntity(acPoly);
    acTrans.AddNewlyCreatedDBObject(acPoly, true);

    // Create the rectangle hole
    var acHole = new Polyline(4);
    acHole.Normal = Vector3d.ZAxis;
    acPoint = new Point2d(5,5);
    acHole.AddVertexAt(0, acPoint, 0.0, -1.0, -1.0);
    acHole.AddVertexAt(1, new Point2d(acPoint.X + 5, acPoint.Y), 0.0, -1.0, -1.0);
    acHole.AddVertexAt(2, new Point2d(acPoint.X + 5, acPoint.Y + 5), 0.0, -1.0, -1.0);
    acHole.AddVertexAt(3, new Point2d(acPoint.X, acPoint.Y + 5), 0.0, -1.0, -1.0);
    acHole.Closed = true;

    // Add the hole rectangle to the block table record
    acBlkTblRec.AppendEntity(acHole);
    acTrans.AddNewlyCreatedDBObject(acHole, true);

    // Create the hatch
    var acHatch = new Hatch();
    acBlkTblRec.AppendEntity(acHatch);
    acTrans.AddNewlyCreatedDBObject(acHatch, true);
    acHatch.SetDatabaseDefaults();
    acHatch.SetHatchPattern(HatchPatternType.PreDefined, "ANSI31");
    acHatch.PatternScale = 10;
    acHatch.Associative = true;

    // Add the outer boundary
    acHatch.AppendLoop(HatchLoopTypes.External, new ObjectIdCollection { acPoly.ObjectId });

    // Add the inner boundary
    acHatch.AppendLoop(HatchLoopTypes.Default, new ObjectIdCollection { acHole.ObjectId });

    // Validate the hatch
    acHatch.EvaluateHatch(true);

    acTrans.Commit();
}