Ejemplo n.º 1
0
        public static void MagicReplaceCommand()
        {
            // objects initializing
            var nomutt = Convert.ToInt32(Autodesk.AutoCAD.ApplicationServices.Core.Application.GetSystemVariable("nomutt"));
            var doc    = Autodesk.AutoCAD.ApplicationServices.Core.Application.DocumentManager.MdiActiveDocument;
            var ed     = doc.Editor;
            var db     = doc.Database;

            try {
                using (doc.LockDocument()) {
                    using (var tr = new QuickTransaction()) {
                        var toreplace = tr.GetImpliedOrSelect(new PromptSelectionOptions());
                        if (toreplace == null)
                        {
                            return;
                        }
                        ed.WriteMessage("\nSelect destinion block: ");
                        var totype = Quick.SelectSingle();
                        ed.WriteMessage("\n");
                        if (totype == null)
                        {
                            return;
                        }
                        var masterblock = (BlockTableRecord)tr.GetObject(((BlockReference)tr.GetObject(totype ?? ObjectId.Null, OpenMode.ForWrite)).BlockTableRecord, OpenMode.ForRead);
                        var bt          = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                        var @new        = bt[masterblock.Name];
                        Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("nomutt", 0);
                        var notparsed = 0;
                        var flattern  = false;
                        var objs      = toreplace.Cast <SelectedObject>().Select(o => tr.GetObject(o.ObjectId, OpenMode.ForRead) as Entity).ToArray();
                        if (objs.Any(o => (o as BlockReference)?.Position.Z > 0))
                        {
                            flattern = Quick.AskQuestion("Should flattern blocks with Z value", true) ?? false;
                        }
                        var os = new List <ObjectId>();
                        foreach (var ent in objs)
                        {
                            var oldblk = ent as BlockReference;
                            if (oldblk == null)
                            {
                                //not block..
                                notparsed++;
                                continue;
                            }
                            var p      = oldblk.Position;
                            var ip     = flattern ? new Point3d(p.X, p.Y, 0) : p;
                            var scl    = oldblk.ScaleFactors;
                            var rot    = oldblk.Rotation;
                            var newblk = new BlockReference(ip, @new);
                            newblk.SetPropertiesFrom(ent);
                            newblk.Rotation     = rot;
                            newblk.ScaleFactors = scl;
                            tr.BlockTableRecordCurrentSpace.AppendEntity(newblk);
                            tr.AddNewlyCreatedDBObject(newblk, true);
                            ApplyAttributes(db, tr, newblk);
                            oldblk.UpgradeOpen();
                            oldblk.Erase();
                            oldblk.Dispose();
                            os.Add(newblk.ObjectId);
                        }
                        Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("nomutt", 1);
                        tr.Commit();
                        if (notparsed > 0)
                        {
                            ed.WriteMessage($"{notparsed} are not blocks and were not replaced.\n");
                        }
                        ed.WriteMessage($"{toreplace.Count} were replaced to block {masterblock.Name} successfully.\n");
                        Quick.SetSelected(os.ToArray());
                    }
                }
            } catch (Exception ex) {
                ed.WriteMessage(ex.Message + "\n" + ex.StackTrace);
            } finally {
                Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("nomutt", nomutt);
            }
        }