public static Dictionary <ObjectId, IList <Point3d> > FindDanglingLine(IList <ObjectId> objectIds) { var dictionary = new Dictionary <ObjectId, IList <Point3d> >(); //var points = new List<Point3d>(); var database = objectIds[0].Database; using (var tr = database.TransactionManager.StartTransaction()) { var reader = new DwgReader(); // var pmFixed3 = new PrecisionModel(3); // 读入多边形数据 foreach (ObjectId objectId in objectIds) { if (!objectId.IsValid) { continue; } IGeometry geom = reader.ReadEntityAsGeometry(tr, objectId); if (geom == null) { continue; } // 开始做Union var nodedLineString = UnaryUnionOp.Union(geom); var polygonizer = new Polygonizer(); polygonizer.Add(nodedLineString); var polygons = polygonizer.GetPolygons(); // 悬挂线 var points = new List <Point3d>(); foreach (ILineString lineString in polygons) { foreach (var coordinate in lineString.Coordinates) { // 如果是NaN直接设定为0 if (double.IsNaN(coordinate.Z)) { coordinate.Z = 0; } points.Add(new Point3d(coordinate.X, coordinate.Y, coordinate.Z)); } } if (points.Any()) { dictionary.Add(objectId, points); } } tr.Commit(); } return(dictionary); }
//public static IList<ObjectId> CheckValid(IList<ObjectId> objectIds) //{ // var result = IsValid(objectIds); // System.Diagnostics.Trace.WriteLine(result.Count); // return result.Keys.ToList(); //} public static Dictionary <ObjectId, SingleTopologyError> CheckValid(IList <ObjectId> objectIds) { var errorDictionary = new Dictionary <ObjectId, SingleTopologyError>(); if (!objectIds.Any()) { return(errorDictionary); } var database = objectIds[0].Database; var geometries = new List <IGeometry>(); using (var tr = database.TransactionManager.StartTransaction()) { var reader = new DwgReader(); foreach (ObjectId objectId in objectIds) { if (!objectId.IsValid) { continue; } IGeometry geom = reader.ReadEntityAsGeometry(tr, objectId); if (geom == null) { continue; } geometries.Add(geom); if (!geom.IsValid) { var ivop = new IsValidOp(geom); if (!ivop.IsValid) { Console.WriteLine(geom.AsText()); Console.Write(ivop.ValidationError); int errorType = (int)ivop.ValidationError.ErrorType; var point = new Point3d(ivop.ValidationError.Coordinate.X, ivop.ValidationError.Coordinate.Y, 0); var error = new SingleTopologyError((SingleTopologyErrors)errorType, point); errorDictionary.Add(objectId, error); } } } tr.Commit(); } return(errorDictionary); }