我需要在填充图案上切一个洞。洞是没有阴影的区域。我有两条闭合的折线。我需要孵化 poly1 - poly2 区域。
我尝试将两者都添加到 ObjectIdCollection,但这会引发错误。
我尝试创建区域并减去它。但是,我们可以为闭合的多段线创建区域,但不能为影线创建区域。
这是创建孵化的代码
[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)
{
}
}