protected override IEnumerable <CheckResult> CheckImpl(IEnumerable <ObjectId> selectedObjectIds)
        {
            //var results = new List<CrossingCheckResult>();
            //var editor = Document.Editor;
            //var crossingObjects = new BreakCrossingObjects(editor, Tolerance);
            //crossingObjects.Check();
            //if (crossingObjects.CrossingPoints == null)
            //    return results;

            //foreach (var crossingPoint in crossingObjects.CrossingPoints)
            //{
            //    var checkResult = new CrossingCheckResult(crossingPoint);
            //    results.Add(checkResult);
            //}

            //return results;

            var editor    = Document.Editor;
            var algorithm = new BreakCrossingObjectsQuadTree(editor);

            algorithm.Check(selectedObjectIds);
            var result = new List <CrossingCheckResult>();

            foreach (var crossingInfo in algorithm.CrossingInfos)
            {
                var checkResult = new CrossingCheckResult(crossingInfo);
                result.Add(checkResult);
            }
            return(result);
        }
        public static Dictionary <ObjectId, List <Point3d> > CalculateIntersectionKdTree(IEnumerable <ObjectId> objectIds,
                                                                                         bool includeInline, Editor editor)
        {
            var segmentPairs  = PolygonGapSearcherKdTree.SearchNearSegments(objectIds, 0.1);
            var intersections = BreakCrossingObjectsQuadTree.GetIntersectionsFromNearPairs(segmentPairs, includeInline);

            // Group intersections by object id.
            var group = GroupIntersections(intersections);

            return(group);
        }
        public static Dictionary <ObjectId, List <Point3d> > CalculateIntersectionQuadTree(IEnumerable <ObjectId> objectIds,
                                                                                           bool includeInline, Editor editor)
        {
            var algorithm     = new BreakCrossingObjectsQuadTree(editor);
            var intersections = algorithm.SearchRealIntersections(objectIds, includeInline);

            // Group intersections by object id.
            var group = GroupIntersections(intersections);

            return(group);
        }
Beispiel #4
0
        /// <summary>
        /// New check - start the settings dialog and check.
        /// </summary>
        /// <returns></returns>
        public bool NewMapCleanCheckWithDialog()
        {
            var dialog       = DialogService.Instance.CreateDialog <ActionsSettingDialog>(null, _actionsSettingViewModel);
            var dialogResult = dialog.ShowDialog();

            if (dialogResult == null || !dialogResult.Value)
            {
                return(false);
            }

            // Clear all check results first.
            ClearCheckResults(raiseEvent: true);

            // Will check all curves in model space.
            OnlyCheckPolyline = false;
            var selectedIds = PrepareObjectIdsForCheck();

            if (selectedIds == null || !selectedIds.Any())
            {
                DialogService.Instance.ShowMessageBox(_actionsSettingViewModel, "图形区没有实体可检查", "提示:",
                                                      MessageBoxButton.OK, MessageBoxImage.Information, MessageBoxResult.OK);
                return(false);
            }

            // If breaking objects, we need to break all crosing objects first.
            if (_actionsSettingViewModel.ActionSelectVM.BreakCrossingObjects)
            {
                using (var toleranceSWitcher = new SafeToleranceOverride())
                {
                    var algorithm = new BreakCrossingObjectsQuadTree(Document.Editor);
                    algorithm.Check(selectedIds);

                    Document.Editor.WriteMessage("\n共找到{0}个交叉对象,开始打断...", algorithm.CrossingInfos.Count());
                    algorithm.Fix(eraseOld: true);
                    Document.Editor.WriteMessage("\n打断所有交叉对象成功!\n");
                }
            }

            // Create actions
            _executingActions.Clear();
            foreach (var action in _actionsSettingViewModel.ActionSelectVM.CheckedItems)
            {
                _executingActions.Add(action.ActionType);
            }

            // After objects are broken, need to update selectedIds.
            if (_actionsSettingViewModel.ActionSelectVM.BreakCrossingObjects)
            {
                selectedIds = PrepareObjectIdsForCheck();
            }

            NewCheckWithPrompt(selectedIds, "拓扑错误");
            return(true);
        }
        public override void Check(IEnumerable <ObjectId> selectedObjectIds)
        {
            if (!selectedObjectIds.Any())
            {
                return;
            }

            // 调低计算精度,否则有些交叉因为精度问题算不出来
            var oldTolerance = DoubleExtensions.STolerance;

            DoubleExtensions.STolerance = 1e-04;
            var watch    = Stopwatch.StartNew();
            var database = Editor.Document.Database;

            using (var transaction = database.TransactionManager.StartTransaction())
            {
                var intersects = new List <CurveCrossingInfo>();
                foreach (var selectedObjectId in selectedObjectIds)
                {
                    //var bspBuilder = new Curve2dBspBuilder(new ObjectId[] {selectedObjectId}, transaction);
                    //IEnumerable<CurveCrossingInfo> duplicateEntities = null;
                    //var crossingInfos = bspBuilder.SearchRealIntersections(true, out duplicateEntities);
                    //intersects.AddRange(crossingInfos);
                    var algorithm = new BreakCrossingObjectsQuadTree(Editor);
                    algorithm.Check(new ObjectId[] { selectedObjectId });
                    if (algorithm.CrossingInfos != null && algorithm.CrossingInfos.Any())
                    {
                        intersects.AddRange(algorithm.CrossingInfos);
                    }
                }

                _selfIntersects = intersects;
                transaction.Commit();
            }

            // 恢复默认的计算精度值
            DoubleExtensions.STolerance = oldTolerance;
            watch.Stop();
            var elapseMs = watch.ElapsedMilliseconds;

#if DEBUG
            System.Diagnostics.Debug.WriteLine("查找自相交花费{0}毫秒", elapseMs);
#endif
        }
        protected override bool CheckAndFixAllImpl(IEnumerable <ObjectId> ids)
        {
            var editor    = Document.Editor;
            var algorithm = new BreakCrossingObjectsQuadTree(editor);

            var checkedIds = ids;
            var first      = true;
            var lastCount  = 0;

            using (var waitCursor = new WaitCursorSwitcher())
                while (true)
                {
                    var checkMessage = String.Format("\n{0}检查交叉对象...", first?"开始":"继续");
                    if (first)
                    {
                        first = false;
                    }
                    editor.WriteMessage(checkMessage);

                    algorithm.Check(checkedIds);
                    var count = algorithm.CrossingInfos.Count();
                    if (count == 0 || count == lastCount)
                    {
                        editor.WriteMessage("\n检测到0处交叉,无需修复\n");
                        editor.WriteMessage("\n提示:在删除重复对象之后,请再执行一次打断交叉对象,保证图形能够完全清理成功\n");
                        return(true);
                    }

                    lastCount = count;
                    var message = String.Format("\n检测到{0}处交叉,是否打断?", count);
                    if (!AcadPromptUtil.AskContinue(message, editor))
                    {
                        return(true);
                    }

                    editor.WriteMessage("\n开始打断...");
                    var breakIdPairs = algorithm.Fix(eraseOld: true);

                    // Need recheck
                    // TODO: this is a temporary solution, will be modified for the new checked Ids set.
                    checkedIds = MapCleanService.Instance.PrepareObjectIdsForCheck();
                }
        }
Beispiel #7
0
        public static ApartmentContourInfo CalcContour(Document doc, IEnumerable <ObjectId> objectIdsList)
        {
            var newCreatedIds = new List <ObjectId>();
            IEnumerable <ObjectId> curveIds       = new List <ObjectId>();
            IEnumerable <ObjectId> splitSourceIds = new List <ObjectId>();
            List <ObjectId>        duplicateIds   = new List <ObjectId>();

            using (var waitCursor = new WaitCursorSwitcher())
                using (var tolerance = new SafeToleranceOverride())
                {
                    // 1. Break down all lines
                    doc.Editor.WriteMessage("\n打断所有交叉线...\n");
                    var breakCrossingAlgorithm = new BreakCrossingObjectsQuadTree(doc.Editor);
                    breakCrossingAlgorithm.Check(objectIdsList);
                    var breakIdPairs = breakCrossingAlgorithm.Fix(eraseOld: false).ToList();
                    splitSourceIds = breakIdPairs.Select(it => it.Key);
                    var checkIds = objectIdsList.Except(splitSourceIds).ToList();
                    foreach (var idPair in breakIdPairs)
                    {
                        newCreatedIds.AddRange(idPair.Value);
                        checkIds.AddRange(idPair.Value);
                    }
                    // 2. Erase the duplcate curves
                    doc.Editor.WriteMessage("\n排除重复线...\n");
                    var duplicateEraserAlgorithm = new DuplicateEntityEraserKdTree(doc.Editor, 0.0005);
                    duplicateEraserAlgorithm.Check(checkIds);
                    var crossingInfos = duplicateEraserAlgorithm.CrossingInfos;
                    if (crossingInfos != null)
                    {
                        foreach (var curveCrossingInfo in crossingInfos)
                        {
                            checkIds.Remove(curveCrossingInfo.TargetId);
                            duplicateIds.Add(curveCrossingInfo.TargetId);
                        }

                        // Deal with the source duplicate
                        var groups = crossingInfos.GroupBy(it => it.TargetId);
                        foreach (var g in groups)
                        {
                            if (g.Count() <= 1)
                            {
                                continue;
                            }
                            bool first = true;
                            foreach (var curveCrossingInfo in g)
                            {
                                if (first)
                                {
                                    first = false;
                                    continue;
                                }
                                checkIds.Remove(curveCrossingInfo.SourceId);
                                duplicateIds.Add(curveCrossingInfo.SourceId);
                            }
                        }
                    }

                    // 3. Extend undershoot
                    doc.Editor.WriteMessage("\n调整未及点...\n");
                    var extendUndershoots = new ExtendUnderShoots(doc.Editor, 0.001);
                    extendUndershoots.Check(checkIds);
                    var splittedIds = extendUndershoots.Fix(breakTarget: true);
                    foreach (var pair in splittedIds)
                    {
                        checkIds.Remove(pair.Key);
                        checkIds.AddRange(pair.Value);

                        // Also recorded in newCreatedIds which will be removed at the end.
                        if (newCreatedIds.Contains(pair.Key))
                        {
                            newCreatedIds.Remove(pair.Key);
                            newCreatedIds.AddRange(pair.Value);
                        }
                    }

                    // 4. 排除0面积闭合线
                    doc.Editor.WriteMessage("\n排除零面积闭合线...\n");
                    var zeroAreaLoopIds = CurveUtils.GetZeroAreaLoop(checkIds, doc.Database);
                    foreach (var zeroAreaLoopId in zeroAreaLoopIds)
                    {
                        checkIds.Remove(zeroAreaLoopId);
                        duplicateIds.Add(zeroAreaLoopId);
                    }

                    // 5. 删除0长度对象
                    doc.Editor.WriteMessage("\n排除零长度对象...\n");
                    var zeroLengthEraser = new ZeroLengthEraser(doc.Editor);
                    zeroLengthEraser.Check(checkIds);
                    foreach (ObjectId zeroLengthId in zeroLengthEraser.ZerolengthObjectIdCollection)
                    {
                        checkIds.Remove(zeroLengthId);
                        duplicateIds.Add(zeroLengthId);
                    }

                    curveIds = checkIds;

                    //// Test code
                    //using (var transaction = doc.Database.TransactionManager.StartTransaction())
                    //{
                    //    var color = Color.FromColorIndex(ColorMethod.ByAci, 3); // Green
                    //    ObjectId layerId = LayerUtils.AddNewLayer(doc.Database, "temp-poly", "Continuous", color);
                    //    foreach (var polygonId in polygonIds)
                    //    {
                    //        var entity = (Entity)transaction.GetObject(polygonId, OpenMode.ForWrite);
                    //        entity.Color = color;
                    //        entity.LayerId = layerId;
                    //    }
                    //    transaction.Commit();
                    //}
                    //return new List<Point2d>();
                }

            // 2. Make polygons.
            doc.Editor.WriteMessage("开始分析外墙轮廓...");
            var resultIds = new List <ObjectId>();

            if (curveIds.Any())
            {
                var      color   = Color.FromColorIndex(ColorMethod.ByAci, 3); // Green
                ObjectId layerId = LayerUtils.AddNewLayer(doc.Database, "temp-poly", "Continuous", color);

                using (var tolerance = new SafeToleranceOverride())
                    using (var waitCursor = new WaitCursorSwitcher())
                    {
                        //var polygons = MinimalLoopSearcher2.Search(curveIds, doc);
                        //using (var transaction = doc.Database.TransactionManager.StartTransaction())
                        //{
                        //    var modelspaceId = SymbolUtilityServices.GetBlockModelSpaceId(doc.Database);
                        //    var modelspace = (BlockTableRecord)transaction.GetObject(modelspaceId, OpenMode.ForWrite);
                        //    foreach (var polyline in polygons)
                        //    {
                        //        polyline.Color = color;
                        //        polyline.LayerId = layerId;
                        //        var id = modelspace.AppendEntity(polyline);
                        //        resultIds.Add(id);
                        //        transaction.AddNewlyCreatedDBObject(polyline, true);
                        //    }
                        //    transaction.Commit();
                        //}
                        resultIds = NtsUtils.PolygonizeLineStrings(doc.Database, curveIds, "temp-poly", color, 0.0001);
                    }
            }

            // 3. Union all polygons
            var database    = doc.Database;
            var partitioner = new DrawingPartitioner(database);

            partitioner.Check(resultIds);
            // 4. Get largest are polygon which is the apartment's contour
            var polylines = new List <Polyline>();

            foreach (var region in partitioner.IsolatedRegions)
            {
                var polyline = new Polyline(region.Contour.Count);
                int i        = 0;
                foreach (var point in region.Contour)
                {
                    polyline.AddVertexAt(i++, new Point2d(point.X, point.Y), 0, 0, 0);
                }
                polyline.Closed = true;
                polylines.Add(polyline);
            }
            polylines.Sort((poly1, poly2) =>
            {
                if (poly1.Area > poly2.Area)
                {
                    return(-1);
                }
                return(1);
            });
            if (polylines.Count >= 2)
            {
                var first  = polylines[0];
                var second = polylines[1];
                // Exclude the situation if the largest polyline is a drawing frame.
                if (IsRectangle(first) && HaveSomeTextsOnBottom(first, database) &&
                    PolygonIncludeSearcher.IsInclude(first, second, null))
                {
                    polylines.RemoveAt(0);
                }
            }
            Polyline largestPolyline = polylines.FirstOrDefault();
            var      resultPoints    = new List <Point2d>();

            if (largestPolyline != null)
            {
                resultPoints = CurveUtils.GetDistinctVertices2D(largestPolyline, null);
                if (resultPoints[0] != resultPoints[resultPoints.Count - 1])
                {
                    resultPoints.Add(resultPoints[0]);
                }
                var clockwise = ComputerGraphics.ClockWise2(resultPoints.ToArray());
                if (clockwise)
                {
                    resultPoints.Reverse();
                }

                //// Test code !
                //using (var transaction = database.TransactionManager.StartTransaction())
                //{
                //    var modelspaceId = SymbolUtilityServices.GetBlockModelSpaceId(database);
                //    var modelspace = (BlockTableRecord)transaction.GetObject(modelspaceId, OpenMode.ForWrite);

                //    var color = Color.FromColorIndex(ColorMethod.ByAci, 3); // Green
                //    ObjectId layerId = LayerUtils.AddNewLayer(doc.Database, "temp-poly", "Continuous", color);
                //    largestPolyline.Color = color;
                //    largestPolyline.LayerId = layerId;
                //    modelspace.AppendEntity(largestPolyline);
                //    transaction.AddNewlyCreatedDBObject(largestPolyline, add: true);


                //    foreach (var polyline in polylines)
                //    {
                //        if (polyline == largestPolyline)
                //            continue;

                //        polyline.Color = color;
                //        polyline.LayerId = layerId;
                //        modelspace.AppendEntity(polyline);
                //        transaction.AddNewlyCreatedDBObject(polyline, add: true);
                //    }

                //    transaction.Commit();
                //}
            }

            // Get contour linesegments from resultPoints
            var contourSegments = new List <LineSegment3d>();
            var innerSegments   = new List <LineSegment3d>();

            if (resultPoints.Count > 0)
            {
                for (var i = 0; i < resultPoints.Count - 1; i++)
                {
                    var start   = new Point3d(resultPoints[i].X, resultPoints[i].Y, 0);
                    var end     = new Point3d(resultPoints[i + 1].X, resultPoints[i + 1].Y, 0);
                    var segment = new LineSegment3d(start, end);
                    contourSegments.Add(segment);
                }
                // Get inner linesegments
                using (var tr = doc.Database.TransactionManager.StartTransaction())
                {
                    var contourArray = resultPoints.ToArray();
                    foreach (var objId in curveIds)
                    {
                        var point2ds = CurveUtils.GetDistinctVertices2D(objId, tr);

                        for (var i = 0; i < point2ds.Count - 1; i++)
                        {
                            var start = point2ds[i];
                            var end   = point2ds[i + 1];
                            if (start.IsEqualTo(end))
                            {
                                continue;
                            }

                            // TODO: no need to calculate again for startInPoly.
                            var startInPoly = ComputerGraphics.IsInPolygon2(start, contourArray);
                            var endInPoly   = ComputerGraphics.IsInPolygon2(end, contourArray);

                            if ((startInPoly == 0 && endInPoly == 0) ||
                                (startInPoly == -1 && endInPoly == -1))
                            {
                                continue;
                            }
                            var segment = new LineSegment3d(new Point3d(start.X, start.Y, 0),
                                                            new Point3d(end.X, end.Y, 0));
                            innerSegments.Add(segment);
                        }
                    }
                    tr.Commit();
                }
            }

            // 5. Delete the polygons of resultIds
            using (var tr = doc.Database.TransactionManager.StartTransaction())
            {
                foreach (var objId in resultIds)
                {
                    if (objId.IsErased)
                    {
                        continue;
                    }
                    var dbObj = tr.GetObject(objId, OpenMode.ForWrite);
                    dbObj.Erase();
                }

                ////////////////////////////////////////////////////////////////////////////////////////
                tr.Commit();
            }

            // Delete the splited curves
            using (var tr = doc.Database.TransactionManager.StartTransaction())
            {
                foreach (var newCreatedId in newCreatedIds)
                {
                    if (newCreatedId.IsErased)
                    {
                        continue;
                    }
                    var dbObj = tr.GetObject(newCreatedId, OpenMode.ForWrite);
                    dbObj.Erase();
                }

                ////////////////////////////////////////////////////////////////////////////////////////
                tr.Commit();
            }

            var result = new ApartmentContourInfo()
            {
                Contour          = contourSegments,
                InternalSegments = innerSegments
            };

            return(result);
        }