static void AddHolesToParcel(ObjectId parcelId, ObjectId groupId, ObjectId[] holeIds) { Database database = parcelId.Database; // 将扣除的孔洞等图形放到孔洞图层,以绿色醒目显示 var layerId = LayerUtils.GetLayerByName(database, "孔洞"); if (!layerId.IsValid) { layerId = LayerUtils.AddNewLayer(database, "孔洞", "Continuous", /*GREEN*/ 3); } // 看看选择的地块是不是真的在地块里面了 var availableHoles = new List <ObjectId>(); using (var tr = database.TransactionManager.StartTransaction()) { var reader = new DwgReader(); var curve = tr.GetObject(parcelId, OpenMode.ForRead) as Curve; if (curve != null && curve.Closed) { var polygon1 = reader.ReadCurveAsPolygon(tr, curve); foreach (var holeId in holeIds) { var hole = tr.GetObject(holeId, OpenMode.ForRead) as Curve; // 只添加地块为封闭的 if (hole == null || !hole.Closed) { continue; } // 继续,看看是不是在地块中间,不在就跳过 var polygon2 = reader.ReadCurveAsPolygon(tr, hole); if (!polygon1.Contains(polygon2)) { continue; } // 如果是,添加地块 hole.UpgradeOpen(); CadUtils.SetCassFlag(hole, CassFlagIsland); hole.LayerId = layerId; availableHoles.Add(holeId); // 如果组不是空,直接加入组 if (!groupId.IsNull) { GroupUtils.AppendEntityIntoGroup(database, groupId, holeId); } } } tr.Commit(); } // 如果没有group,创建group if (groupId.IsNull) { var ids = new ObjectIdCollection() { parcelId }; foreach (var availableHole in availableHoles) { ids.Add(availableHole); } GroupUtils.CreateNewGroup(database, "*A", ids); } }