Пример #1
0
        internal void DoIt(_AcAp.Document doc, string rbName, string fgLayer)
        {
            log.Debug("--------------------------");

            _FlaechenGrenzen.Clear();
            _Raumbloecke.Clear();

            var _AreaEngine = new AreaEngine();

            _AcGe.Matrix3d ucs = _AcGe.Matrix3d.Identity;
            try
            {
                ucs = doc.Editor.CurrentUserCoordinateSystem;
                doc.Editor.CurrentUserCoordinateSystem = _AcGe.Matrix3d.Identity;

                if (!string.IsNullOrEmpty(rbName))
                {
                    _RaumblockName = rbName;
                }
                if (!string.IsNullOrEmpty(fgLayer))
                {
                    _FgLayer = fgLayer;
                }

                _AreaEngine.SelectFgAndRb(_FlaechenGrenzen, _Raumbloecke, _FgLayer, _RaumblockName);

                if (_FlaechenGrenzen.Count == 0)
                {
                    return;
                }

                // todo: läuft nicht synchron - wird dzt in lisp ausgeführt
                //Globs.SetWorldUCS();

                ZoomToFlaechenGrenzen();

                // init div
                int fehlerKeinRb     = 0;
                int fehlerMehrRb     = 0;
                int fehlerWertFalsch = 0;

                _AcDb.Database           db  = doc.Database;
                _AcEd.Editor             ed  = doc.Editor;
                _AcDb.TransactionManager tm  = db.TransactionManager;
                _AcDb.Transaction        myT = tm.StartTransaction();
                try
                {
                    _AcGe.Point2d lu = new _AcGe.Point2d();
                    _AcGe.Point2d ro = new _AcGe.Point2d();

                    for (int i = 0; i < _FlaechenGrenzen.Count; i++)
                    {
                        log.Debug("--------------------------");

                        double         sumAF = 0;
                        int            rbInd = -1;
                        _AcDb.ObjectId elFG  = _FlaechenGrenzen[i];
                        log.DebugFormat("Flächengrenze {0}", elFG.Handle.ToString());

                        _AcDb.Extents3d ext    = GetExtents(tm, elFG);
                        _AcGe.Point3d   minExt = new _AcGe.Point3d(ext.MinPoint.X - ABSTANDTEXT, ext.MinPoint.Y - ABSTANDTEXT, ext.MinPoint.Z);
                        _AcGe.Point3d   maxExt = new _AcGe.Point3d(ext.MaxPoint.X + ABSTANDTEXT, ext.MaxPoint.Y + ABSTANDTEXT, ext.MaxPoint.Z);

                        List <_AcDb.ObjectId> rbsToIgnoreCol = GetFgAnz(minExt, maxExt, elFG);
                        if (rbsToIgnoreCol.Count > 0)
                        {
                            string handles = string.Join(",", rbsToIgnoreCol.Select(x => x.Handle.ToString()).ToArray());
                            log.DebugFormat("Zu ignorierende Raumblöcke: {0}", handles);
                        }

                        //    'raumbloecke holen
                        List <_AcDb.ObjectId> ssRB = selRB(minExt, maxExt);
                        if (ssRB.Count > 0)
                        {
                            string handles = string.Join(",", ssRB.Select(x => x.Handle.ToString()).ToArray());
                            log.DebugFormat("Raumblöcke: {0}", handles);
                        }


                        int rbAnz = 0;
                        //    'raumbloecke pruefen
                        for (int rbCnt = 0; rbCnt < ssRB.Count; rbCnt++)
                        {
                            _AcDb.ObjectId rbBlock2 = ssRB[rbCnt];

                            //      ' ignore rbs
                            _AcDb.ObjectId found = rbsToIgnoreCol.FirstOrDefault(x => x.Equals(rbBlock2));
                            if (found != default(_AcDb.ObjectId))
                            {
                                continue;
                            }

                            using (_AcDb.DBObject dbObj = tm.GetObject(rbBlock2, _AcDb.OpenMode.ForRead, false))
                            {
                                _AcGe.Point3d rbEp = ((_AcDb.BlockReference)dbObj).Position;

                                using (_AcDb.Entity elFGEnt = (_AcDb.Entity)tm.GetObject(elFG, _AcDb.OpenMode.ForRead, false))
                                {
                                    if (AreaEngine.InPoly(rbEp, elFGEnt))
                                    {
                                        log.DebugFormat("Raumblock {0} ist innerhalb der Flächengrenze.", rbBlock2.Handle.ToString());

                                        if (_Raumbloecke.Contains(rbBlock2))
                                        {
                                            _Raumbloecke.Remove(rbBlock2);
                                        }
                                        rbAnz++;
                                        rbInd = rbCnt;
                                    }
                                    else
                                    {
                                        log.DebugFormat("Außen liegender Raumblock {0} wird ignoriert.", rbBlock2.Handle.ToString());
                                    }
                                }
                            }
                        }


                        if (rbAnz < 1)
                        {
                            log.WarnFormat("Kein Raumblock in Flächengrenze {0}!", elFG.Handle.ToString());
                            //FehlerLineOrHatchPoly(elFG, _InvalidNrRb, 255, 0, 0, tm, Globs.GetLabelPoint(elFG));
                            fehlerKeinRb++;
                        }
                        else if (rbAnz > 1)
                        {
                            log.WarnFormat("Mehr als ein Raumblock in Flächengrenze {0}!", elFG.Handle.ToString());
                            //FehlerLineOrHatchPoly(elFG, _InvalidNrRb, 255, 0, 0, tm, Globs.GetLabelPoint(elFG));
                            fehlerMehrRb++;
                        }
                        else
                        {
                            using (var tr = doc.TransactionManager.StartTransaction())
                            {
                                var pt = Globs.GetLabelPoint(elFG);
                                if (pt.HasValue)
                                {
                                    var rblock = tr.GetObject(ssRB[rbInd], _AcDb.OpenMode.ForWrite) as _AcDb.BlockReference;

                                    var pos = rblock.GetCenter();
                                    if (!pos.HasValue)
                                    {
                                        pos = rblock.Position;
                                    }
                                    _AcGe.Vector3d acVec3d = pos.Value.GetVectorTo(pt.Value);
                                    rblock.TransformBy(_AcGe.Matrix3d.Displacement(acVec3d));
                                    ed.WriteMessage("\nCentroid is {0}", pt);
                                }
                                else
                                {
                                    var    poly = tr.GetObject(elFG, _AcDb.OpenMode.ForRead) as _AcDb.Polyline;
                                    string msg  = string.Format(CultureInfo.CurrentCulture, "\nFläche {0}. Centroid liegt außerhalb.", poly.Handle.ToString());
                                    ed.WriteMessage(msg);
                                    log.Warn(msg);
                                }

                                tr.Commit();
                            }
                        }
                    }

                    //if (_Raumbloecke.Count > 0)
                    //{
                    //    List<object> insPoints = new List<object>();
                    //    for (int i = 0; i < _Raumbloecke.Count; i++)
                    //    {
                    //        _AcIntCom.AcadBlockReference rbBlock = (_AcIntCom.AcadBlockReference)Globs.ObjectIdToAcadEntity(_Raumbloecke[i], tm);
                    //        insPoints.Add(rbBlock.InsertionPoint);
                    //    }

                    //    _AcCm.Color col = _AcCm.Color.FromRgb((byte)0, (byte)255, (byte)0);

                    //    Plan2Ext.Globs.InsertFehlerLines(insPoints, _LooseBlockLayer, 50, Math.PI * 1.25, col);

                    //}



                    if (fehlerKeinRb > 0 || fehlerMehrRb > 0 || fehlerWertFalsch > 0 || _Raumbloecke.Count > 0)
                    {
                        string msg = string.Format(CultureInfo.CurrentCulture, "Räume ohne Raumblock: {0}\nRäume mit mehr als einem Raumblock: {1}\nRäume mit falschem Wert in Raumblock: {2}\nRaumblöcke ohne entsprechende Flächengrenzen: {3}", fehlerKeinRb, fehlerMehrRb, fehlerWertFalsch, _Raumbloecke.Count);
                        log.Debug(msg);
                        _AcAp.Application.ShowAlertDialog(msg);
                    }

                    //If wucs = 0 Then
                    //    ThisDrawing.SendCommand "(command ""_.UCS"" ""_P"") "
                    //End If

                    myT.Commit();
                }
                finally
                {
                    myT.Dispose();
                }
            }
            finally
            {
                doc.Editor.CurrentUserCoordinateSystem = ucs;
            }
        }