Exemple #1
0
        public WordSpecExport(CopyToClipboard cpc, BackgroundWorker ExportWorker, string filename)
        {
            Microsoft.Office.Interop.Word.Application  oWord  = null;
            Microsoft.Office.Interop.Excel.Application oExcel = null;


            try
            {
                List <SveraClass.FromDataBaseWide> rowgrid = SveraCl.PereformSpec(cpc, TypeOfExport.WordSpec);

                // return;

                int maxofprog = rowgrid.Count + 2 * 8;
                maxofprog += rowgrid.Count;
                int counter = 0;
                ExportWorker.ReportProgress(maxofprog, Operation.init);

                string exportPath = FunctionUse.GetExportPath(filename);
                try
                {
                    exportPath += "\\";
                    exportPath += (filename != null && filename != "") ? Path.GetFileNameWithoutExtension(filename) : "specification";
                    exportPath += ".docx";
                    if (cpc.ExportP.TipSpec == SveraExportParam.RowsDatagridExportNames.TipSpecAndVed.standart)
                    {
                        File.WriteAllBytes(exportPath, SveraExport.Properties.Resources.Specification);
                    }
                    else if (cpc.ExportP.TipSpec == SveraExportParam.RowsDatagridExportNames.TipSpecAndVed.rev)
                    {
                        File.WriteAllBytes(exportPath, SveraExport.Properties.Resources.specification_del);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Файл открыт и используется. Закройте файл и повторите попытку", "Svera Export Word", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                Microsoft.Office.Interop.Word.Document   oDoc;
                Microsoft.Office.Interop.Excel.Workbook  oBook;
                Microsoft.Office.Interop.Excel.Worksheet oSheet;
                //Microsoft.Office.Interop.Word.Table oTable;
                //Microsoft.Office.Interop.Word.Paragraph oPara1;


                counter += 2;
                ExportWorker.ReportProgress(counter, Operation.change);
                if (ExportWorker.CancellationPending)
                {
                    ExitWord(oWord, oExcel, true);
                    return;
                }



                try
                {
                    oWord  = new Microsoft.Office.Interop.Word.Application();
                    oDoc   = oWord.Documents.Open(exportPath);
                    oExcel = new Microsoft.Office.Interop.Excel.Application();
                    oBook  = oExcel.Workbooks.Add();
                    oSheet = oExcel.Worksheets[1];

                    podgotovWord(oWord, oExcel, false, false);

                    oDoc.SaveAs(exportPath);
                    oBook.SaveAs(Path.GetDirectoryName(exportPath) + "\\" + Path.GetFileNameWithoutExtension(exportPath) + ".xlsx");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Файл " + Path.GetDirectoryName(exportPath) + "\\" + Path.GetFileNameWithoutExtension(exportPath) + " уже открыт. Закройте файл. Повторите попытку", "Svera Export Word", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    ExitWord(oWord, oExcel, true);
                    return;
                }


                counter += 2;
                ExportWorker.ReportProgress(counter, Operation.change);
                if (ExportWorker.CancellationPending)
                {
                    ExitWord(oWord, oExcel, true);
                    return;
                }



                oSheet.PageSetup.Orientation = Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
                oDoc.PageSetup.Orientation   = Microsoft.Office.Interop.Word.WdOrientation.wdOrientLandscape;
                oDoc.AutoHyphenation         = true;

                oDoc.Content.Font.Name = "Times New Roman";
                oDoc.Content.Font.Size = 10;

                int numberCOl = 9;
                if (cpc.ExportP.TipSpec == SveraExportParam.RowsDatagridExportNames.TipSpecAndVed.standart)
                {
                    numberCOl = 9;
                }
                else if (cpc.ExportP.TipSpec == SveraExportParam.RowsDatagridExportNames.TipSpecAndVed.rev)
                {
                    numberCOl = 11;
                }

                string[,] OtableStr = new string[rowgrid.Count, numberCOl];

                if (cpc.ExportP.TipSpec == SveraExportParam.RowsDatagridExportNames.TipSpecAndVed.standart)
                {
                    oSheet.Columns[1].ColumnWidth = 5;
                    oSheet.Columns[2].ColumnWidth = 30;
                    oSheet.Columns[3].ColumnWidth = 20;
                    oSheet.Columns[4].ColumnWidth = 12;
                    oSheet.Columns[5].ColumnWidth = 15;
                    oSheet.Columns[6].ColumnWidth = 5;
                    oSheet.Columns[7].ColumnWidth = 7;
                    oSheet.Columns[8].ColumnWidth = 8;
                    oSheet.Columns[9].ColumnWidth = 15;
                }
                else if (cpc.ExportP.TipSpec == SveraExportParam.RowsDatagridExportNames.TipSpecAndVed.rev)
                {
                    oSheet.Columns[1].ColumnWidth  = 5;
                    oSheet.Columns[2].ColumnWidth  = 30;
                    oSheet.Columns[3].ColumnWidth  = 20;
                    oSheet.Columns[4].ColumnWidth  = 8;
                    oSheet.Columns[5].ColumnWidth  = 8;
                    oSheet.Columns[6].ColumnWidth  = 10;
                    oSheet.Columns[7].ColumnWidth  = 8;
                    oSheet.Columns[8].ColumnWidth  = 8;
                    oSheet.Columns[9].ColumnWidth  = 8;
                    oSheet.Columns[10].ColumnWidth = 8;
                    oSheet.Columns[11].ColumnWidth = 12;
                }


                counter += 2;
                ExportWorker.ReportProgress(counter, Operation.change);
                if (ExportWorker.CancellationPending)
                {
                    ExitWord(oWord, oExcel, true);
                    return;
                }

                int numberRow = 0;
                for (int i = 0; i <= rowgrid.Count - 1; i++)
                {
                    addrowInSpec(cpc, rowgrid[i], OtableStr, oSheet, ref numberRow);
                }

                counter += 2;
                ExportWorker.ReportProgress(counter, Operation.change);
                if (ExportWorker.CancellationPending)
                {
                    ExitWord(oWord, oExcel, true);
                    return;
                }

                if (rowgrid.Count > 0)
                {
                    Microsoft.Office.Interop.Excel.Range rangeEx = oSheet.Range[oSheet.Cells[1, 1], oSheet.Cells[OtableStr.GetUpperBound(0) + 1, OtableStr.GetUpperBound(1) + 1]];
                    rangeEx.Value     = OtableStr;
                    rangeEx.Font.Name = "Times New Roman";
                    rangeEx.Font.Size = 10;
                    rangeEx.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].LineStyle         = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                    rangeEx.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].LineStyle          = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                    rangeEx.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].LineStyle       = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                    rangeEx.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].LineStyle        = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                    rangeEx.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].LineStyle   = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                    rangeEx.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
                    rangeEx.WrapText = true;
                }



                counter += 2;
                ExportWorker.ReportProgress(counter, Operation.change);
                if (ExportWorker.CancellationPending)
                {
                    ExitWord(oWord, oExcel, true);
                    return;
                }


                if (rowgrid.Count > 0)
                {
                    for (int i = 1; i <= OtableStr.GetUpperBound(1) + 1; i++)
                    {
                        if (i != 2 && i != 3)
                        {
                            oSheet.Columns[i].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                            oSheet.Columns[i].VerticalAlignment   = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
                        }
                        else
                        {
                            oSheet.Columns[i].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
                            oSheet.Columns[i].VerticalAlignment   = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
                        }
                    }

                    counter += 2;
                    ExportWorker.ReportProgress(counter, Operation.change);
                    if (ExportWorker.CancellationPending)
                    {
                        ExitWord(oWord, oExcel, true);
                        return;
                    }
                }


                for (int i = 0; i <= rowgrid.Count - 1; i++)
                {
                    if (rowgrid[i].RowTypes == RowsType.TipOborud)
                    {
                        oSheet.Cells[i + 1, 2].Font.Bold           = true;
                        oSheet.Cells[i + 1, 2].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                    }
                    else if (rowgrid[i].RowTypes == RowsType.TipPostav)
                    {
                        oSheet.Cells[i + 1, 2].Font.Bold           = true;
                        oSheet.Cells[i + 1, 2].Font.Underline      = Microsoft.Office.Interop.Excel.XlUnderlineStyle.xlUnderlineStyleSingle;
                        oSheet.Cells[i + 1, 2].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                    }
                    else if (rowgrid[i].RowTypes == RowsType.Etap)
                    {
                        oSheet.Cells[i + 1, 2].Font.Bold           = true;
                        oSheet.Cells[i + 1, 2].Font.Underline      = Microsoft.Office.Interop.Excel.XlUnderlineStyle.xlUnderlineStyleSingle;
                        oSheet.Cells[i + 1, 2].HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter;
                    }
                }

                counter += 2;
                ExportWorker.ReportProgress(counter, Operation.change);
                if (ExportWorker.CancellationPending)
                {
                    ExitWord(oWord, oExcel, true);
                    return;
                }


                oSheet.Rows.AutoFit();

                oExcel.ActiveWindow.SplitColumn = 0;
                oExcel.ActiveWindow.SplitRow    = 2;
                oExcel.ActiveWindow.FreezePanes = true;
                oExcel.ActiveWindow.Zoom        = 82;


                Microsoft.Office.Interop.Word.Style Stylus = oWord.Selection.get_Style();
                Stylus.Font.Name = "Times New Roman";
                Stylus.Font.Size = 10;
                Stylus.ParagraphFormat.SpaceBefore = 0;
                Stylus.ParagraphFormat.SpaceAfter  = 0;

                if (rowgrid.Count > 0)
                {
                    oSheet.Range[oSheet.Cells[1, 1], oSheet.Cells[OtableStr.GetUpperBound(0) + 1, OtableStr.GetUpperBound(1) + 1]].Copy();
                }
                oWord.Selection.Paste();
                oExcel.CutCopyMode = XlCutCopyMode.xlCopy;

                if (oDoc.Tables.Count > 1)
                {
                    oDoc.Tables[1].Rows.HeightRule = WdRowHeightRule.wdRowHeightAtLeast;
                    oDoc.Tables[1].Rows.Height     = oWord.CentimetersToPoints(0.58F);

                    oDoc.Tables[1].AllowAutoFit = false;
                    oDoc.Tables[1].LeftPadding  = oDoc.Tables[2].LeftPadding;
                    oDoc.Tables[1].RightPadding = oDoc.Tables[2].RightPadding;

                    if (oDoc.Tables[2].Columns.Count >= numberCOl && oDoc.Tables[1].Columns.Count >= numberCOl)
                    {
                        for (int i = 1; i <= numberCOl; i++)
                        {
                            oDoc.Tables[1].Columns[i].Width = oDoc.Tables[2].Columns[i].Width;
                        }
                    }
                    oDoc.Tables[2].Delete();

                    oDoc.Tables[1].Rows.SetLeftIndent(oWord.CentimetersToPoints(0.03F), Microsoft.Office.Interop.Word.WdRulerStyle.wdAdjustNone);
                }

                counter += 2;
                ExportWorker.ReportProgress(counter, Operation.change);
                if (ExportWorker.CancellationPending)
                {
                    ExitWord(oWord, oExcel, true);
                    return;
                }


                oDoc.Save();
                oBook.Save();
                ExitWord(oWord, oExcel, false);
                oWord  = null;
                oExcel = null;
            }
            catch (Exception ex)
            {
                ExitWord(oWord, oExcel, false);

                throw new Exception("WordExport", ex);
            }
        }
Exemple #2
0
        public MicrostationExport(CopyToClipboard cpc, BackgroundWorker ExportWorker, string filename)
        {
            string exportPath = FunctionUse.GetExportPath(filename);

            List <SveraClass.FromDataBaseWide> rowgrid = SveraCl.PereformSpec(cpc, TypeOfExport.Microstation);

            int maxofprog = rowgrid.Count + 2 * 5;

            maxofprog += rowgrid.Count;
            int counter = 0;

            ExportWorker.ReportProgress(maxofprog, Operation.init);

            try
            {
                exportPath += "\\";
                exportPath += (filename != null && filename != "") ? Path.GetFileNameWithoutExtension(filename) + "Spec" : "specification";
                exportPath += ".dgn";
                File.WriteAllBytes(exportPath, MicroSvera.Properties.Resources.seed2d);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Файл открыт и используется. Закройте файл и повторите попытку", "Svera Export Microstation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            counter += 2;
            ExportWorker.ReportProgress(counter, Operation.change);
            if (ExportWorker.CancellationPending)
            {
                return;
            }


            MS   = null;
            MSex = null;
            try
            {
                try
                {
                    MSex = new MicroStationDGN.ApplicationObjectConnector();
                    MS   = MSex.Application;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Невозможно запустить Microstation. Проверьте версию программы. Необходим Select Series 2", "Svera Export Microstation", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    MessageBox.Show(ex.Message);
                    return;
                }

                counter += 2;
                ExportWorker.ReportProgress(counter, Operation.change);
                if (ExportWorker.CancellationPending)
                {
                    MS.Quit();
                    return;
                }

                currentDoc = MS.OpenDesignFile(exportPath);

                currentset = MS.ActiveSettings;
                currentset.TextStyle.Height = visfont;
                currentset.TextStyle.Width  = shirfont;

                currentset.TextStyle.Font            = currentDoc.Fonts["txt"];
                currentset.TextStyle.NodeLineSpacing = mezhstroch;
                currentset.Color = mainCOlor;

                counter += 2;
                ExportWorker.ReportProgress(counter, Operation.change);
                if (ExportWorker.CancellationPending)
                {
                    MS.Quit();
                    return;
                }

                GetShapka();

                counter += 2;
                ExportWorker.ReportProgress(counter, Operation.change);
                if (ExportWorker.CancellationPending)
                {
                    MS.Quit();
                    return;
                }



                double lowPOint = pos.Range.Low.Y;
                for (int i = 0; i <= rowgrid.Count - 1; i++)
                {
                    counter += 1;
                    ExportWorker.ReportProgress(counter, Operation.change);
                    if (ExportWorker.CancellationPending)
                    {
                        MS.Quit();
                        return;
                    }
                    //  if (rowgrid[i].KolicByDetalize > 0)
                    //  {
                    MicroStationDGN.ShapeElement posElem   = null;
                    MicroStationDGN.ShapeElement oboznElem = null;
                    MicroStationDGN.ShapeElement naimElem  = null;
                    MicroStationDGN.ShapeElement kolElem   = null;
                    MicroStationDGN.ShapeElement massaElem = null;
                    MicroStationDGN.ShapeElement primElem  = null;

                    MicroStationDGN.TextNodeElement posElemText   = null;
                    MicroStationDGN.TextNodeElement oboznElemText = null;
                    MicroStationDGN.TextNodeElement naimElemText  = null;
                    MicroStationDGN.TextNodeElement kolElemText   = null;
                    MicroStationDGN.TextNodeElement massaElemText = null;
                    MicroStationDGN.TextNodeElement primElemText  = null;


                    REcANdTExt(ref posElem, ref posElemText, new PointF((float)pos.Range.Low.X, (float)lowPOint), new SizeF((float)(pos.Range.High.X - pos.Range.Low.X), -4F), ((rowgrid[i].PosicToExp > 0) ? (rowgrid[i].PosicToExp).ToString() : "").ToString(), false, false, true, 1, true);
                    REcANdTExt(ref oboznElem, ref oboznElemText, new PointF((float)(obozn.Range.Low.X), (float)lowPOint), new SizeF((float)(obozn.Range.High.X - obozn.Range.Low.X), (float)(posElem.Range.Low.Y - posElem.Range.High.Y)), rowgrid[i].Markirov, false, false, true, 1, true);
                    REcANdTExt(ref naimElem, ref naimElemText, new PointF((float)(naim.Range.Low.X), (float)lowPOint), new SizeF((float)(naim.Range.High.X - naim.Range.Low.X), (float)(oboznElem.Range.Low.Y - oboznElem.Range.High.Y)), rowgrid[i].GetNaim(false), false, false, true, 3, true);
                    REcANdTExt(ref kolElem, ref kolElemText, new PointF((float)(kol.Range.Low.X), (float)lowPOint), new SizeF((float)(kol.Range.High.X - kol.Range.Low.X), (float)(naimElem.Range.Low.Y - naimElem.Range.High.Y)), ((rowgrid[i].KolicByDetalizeR != 0) ? (rowgrid[i].KolicByDetalizeR).ToString() : "").ToString(), false, false, true, 1, true);
                    REcANdTExt(ref massaElem, ref massaElemText, new PointF((float)(massa.Range.Low.X), (float)lowPOint), new SizeF((float)(massa.Range.High.X - massa.Range.Low.X), (float)(kolElem.Range.Low.Y - kolElem.Range.High.Y)), rowgrid[i].MassaStr, false, false, true, 1, true);
                    REcANdTExt(ref primElem, ref primElemText, new PointF((float)(prim.Range.Low.X), (float)lowPOint), new SizeF((float)(prim.Range.High.X - prim.Range.Low.X), (float)(massaElem.Range.Low.Y - massaElem.Range.High.Y)), rowgrid[i].PrimMicro, false, false, true, 1, true);



                    List <MicroStationDGN.ShapeElement> elStolb = new List <MicroStationDGN.ShapeElement>
                    {
                        posElem,
                        oboznElem,
                        naimElem,
                        kolElem,
                        massaElem,
                        primElem
                    };

                    List <MicroStationDGN.TextNodeElement> elStolbText = new List <MicroStationDGN.TextNodeElement>
                    {
                        posElemText,
                        oboznElemText,
                        naimElemText,
                        kolElemText,
                        massaElemText,
                        primElemText
                    };

                    lowPOint = elStolb.Min <MicroStationDGN.ShapeElement, double>(el => el.Range.Low.Y);
                    for (int j = 0; j < elStolb.Count; j++)
                    {
                        if (lowPOint < elStolb[j].Range.Low.Y)
                        {
                            elStolb[j].ModifyVertex(2, MS.Point3dFromXY(elStolb[j].get_Vertex(3).X, lowPOint));
                            elStolb[j].ModifyVertex(3, MS.Point3dFromXY(elStolb[j].get_Vertex(4).X, lowPOint));

                            elStolb[j].Rewrite();
                            elStolb[j].Redraw();

                            int position = 0;
                            if (elStolbText[j] != null)
                            {
                                if (elStolbText[j].Justification == MicroStationDGN.MsdTextJustification.msdTextJustificationCenterCenter)
                                {
                                    position = 1;
                                }
                                else if (elStolbText[j].Justification == MicroStationDGN.MsdTextJustification.msdTextJustificationLeftTop)
                                {
                                    position = 3;
                                }
                                MicroStationDGN.Point3d POintIns = POintIns = GetPOintText(position, new PointF((float)elStolb[j].Range.Low.X, (float)elStolb[j].Range.High.Y), new SizeF((float)(elStolb[j].Range.High.X - elStolb[j].Range.Low.X), (float)(elStolb[j].Range.High.Y - elStolb[j].Range.Low.Y)));
                                elStolbText[j].Move(MS.Point3dFromXY(POintIns.X - elStolbText[j].Origin.X, POintIns.Y - elStolbText[j].Origin.Y));

                                elStolbText[j].Rewrite();
                                elStolbText[j].Redraw();
                            }
                        }
                    }



                    // }
                }



                currentDoc.Save();

                int statusSaveCHanges  = (int)MS.GetCExpressionValue("userPrefsP->extFlags.immediatelySaveChanges");
                int statusDesignOnExit = (int)MS.GetCExpressionValue("userPrefsP->extFlags.fileDesignOnExit");
                MS.SetCExpressionValue("userPrefsP->extFlags.immediatelySaveChanges", 1);
                MS.SetCExpressionValue("userPrefsP->extFlags.fileDesignOnExit", 0);

                currentDoc.Close();

                MS.SetCExpressionValue("userPrefsP->extFlags.immediatelySaveChanges", statusSaveCHanges);
                MS.SetCExpressionValue("userPrefsP->extFlags.fileDesignOnExit", statusDesignOnExit);

                MS.Visible = true;
                currentDoc = MS.OpenDesignFile(exportPath);
                currentDoc.Views[1].DisplaysFill = true;
                try
                {
                    currentDoc.Views[1].Fit(false);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Svera Export Microstation", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                currentDoc.Views[1].Redraw();


                counter += 2;
                ExportWorker.ReportProgress(counter, Operation.change);
                if (ExportWorker.CancellationPending)
                {
                    MS.Quit();
                    return;
                }
            }
            catch (Exception ex)
            {
                if (MS != null)
                {
                    MS.Quit();
                    MS = null;
                }
                if (MSex != null)
                {
                    MSex = null;
                }

                //MessageBox.Show(ex.Message);
                //MessageBox.Show(ex.StackTrace);
                //if (ex.InnerException != null)
                //{
                //    MessageBox.Show(ex.InnerException.ToString());
                //}
                throw new Exception("MicrostationExport", ex);
            }
        }