コード例 #1
0
        public void exportleo()
        {
            DocumentCollection docCol = Application.DocumentManager;
            Database           db     = docCol.MdiActiveDocument.Database;
            Editor             editor = docCol.MdiActiveDocument.Editor;
            Document           doc    = docCol.MdiActiveDocument;


            using (Transaction tx = db.TransactionManager.StartTransaction())
            {
                var plines = QuickSelection.SelectAll("LWPOLYLINE").QWhere(x => x.Layer == "RØR INSTRUMENT SIGNAL").ToList();

                editor.WriteMessage($"\nPlines collected {plines.Count}");

                plines.QOpenForWrite <Polyline>(listToExplode =>
                {
                    foreach (var poly in listToExplode)
                    {
                        Modify.Explode(poly.ObjectId);
                    }
                });

                var rumPolyIds = QuickSelection.SelectAll("LWPOLYLINE").QWhere(x => x.Layer == "RUM DELER").ToList();

                editor.WriteMessage($"\nCollected {rumPolyIds.Count} rum polylines.");

                rumPolyIds.QOpenForWrite <Polyline>(listToClean =>
                {
                    foreach (var poly in listToClean)
                    {
                        Algorithms.PolyClean_RemoveDuplicatedVertex(poly);
                    }
                });

                List <(string Tag, string RoomNr)> tagRoomlist = new List <(string Tag, string RoomNr)>();

                int i = 0;

                List <BlockReference> AllBlocks = new List <BlockReference>();

                var rumPolys = rumPolyIds.QOpenForRead <Polyline>();

                foreach (Polyline pline in rumPolys)
                {
                    i++;
                    editor.WriteMessage($"\nProcessing polyline number {i}.");

                    List <BlockReference> blocks = new List <BlockReference>();

                    PromptSelectionResult selection =
                        editor.SelectByPolyline(pline, ExtensionMethods.PolygonSelectionMode.Window, new TypedValue(0, "INSERT"));

                    if (selection.Status == PromptStatus.OK)
                    {
                        SelectionSet set = selection.Value;
                        foreach (SelectedObject selObj in set)
                        {
                            if (selObj != null)
                            {
                                BlockReference block = tx.GetObject(selObj.ObjectId, OpenMode.ForRead) as BlockReference;
                                blocks.Add(block);
                                AllBlocks.Add(block);
                            }
                        }
                    }
                    else
                    {
                        editor.WriteMessage($"\nPolyline number {i} failed the selection!");
                    }

                    BlockReference roomNumberBlock = blocks.Where(x => x.Name == "rumnummer").FirstOrDefault();
                    if (roomNumberBlock == null)
                    {
                        continue;
                    }
                    string roomNumber = roomNumberBlock.GetBlockAttribute("ROOMNO");
                    editor.WriteMessage($"\nRoom nr.: {roomNumber}");

                    foreach (BlockReference br in blocks)
                    {
                        string tagValue = string.Empty;
                        var    attrs    = br.GetBlockAttributes();
                        if (attrs.ContainsKey("TEXT1"))
                        {
                            tagValue = attrs["TEXT1"];
                        }
                        else if (attrs.ContainsKey("TAG"))
                        {
                            tagValue = attrs["TAG"];
                        }
                        else
                        {
                            continue;
                        }

                        if (!string.IsNullOrEmpty(roomNumber))
                        {
                            tagRoomlist.Add((tagValue, roomNumber));
                        }
                    }
                }

                //Sort the pairs list
                tagRoomlist = tagRoomlist.OrderBy(x => x.Tag).ToList();

                //Export to excel
                xel.Application excel = new xel.Application();
                if (null == excel)
                {
                    throw new System.Exception("Failed to start EXCEL!");
                }
                excel.Visible = true;
                xel.Workbook  workbook = excel.Workbooks.Add(Missing.Value);
                xel.Worksheet worksheet;
                worksheet      = excel.ActiveSheet as xel.Worksheet;
                worksheet.Name = "LeoExport";
                worksheet.Columns.ColumnWidth = 15;

                int row = 1;
                int col = 1;

                foreach (var pair in tagRoomlist)
                {
                    worksheet.Rows[row].Cells[col]     = pair.Tag;
                    worksheet.Rows[row].Cells[col + 1] = pair.RoomNr;
                    //worksheet.Cells[row, col] = pair.Item1;
                    //worksheet.Cells[row, col+1] = pair.Item2;
                    row++;
                }
            }
        }