private void btn_ScrExecute_Click_1(object sender, EventArgs e)
 {
     if (this.comboBox1.Text.Contains("LT"))
     {
         CMDHelper.ExecuteScr(this.textBox2.Text, this.textBox1.Text, Path.GetDirectoryName(this.CurAutoCADAccoreconsole));
         MyHelper.MyOpenFolder(Path.GetDirectoryName(this.textBox1.Text), Path.GetFileName(this.textBox1.Text));
     }
     else
     {
         CMDHelper.ExecuteCmd(this.textBox2.Text, this.textBox1.Text, this.CurAutoCADAccoreconsole);
         MessageBox.Show("关闭本程序,完成accoreconsole 命令的执行");
         MyHelper.MyOpenFolder(Path.GetDirectoryName(this.textBox1.Text), Path.GetFileName(this.textBox1.Text));
         Thread.CurrentThread.Abort();
     }
 }
示例#2
0
        private void 打印成PdfToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                var           acKey    = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Autodesk\Hardcopy", false);
                List <string> acValues = acKey.GetValueNames().Where(c => c.Contains("AutoCAD") && !c.Contains("AutoCAD LT")).ToList();//找未阉割的的autocad的位置
                string        strMess1 = string.Empty;
                if (acValues.Count == 0)
                {
                    acValues = acKey.GetValueNames().Where(c => c.Contains("AutoCAD LT")).ToList();//找lt的autocad的位置
                    strMess1 = "使用的是LT版本";
                }
                else
                {
                    strMess1 = "使用的是网络版本";
                }

                if (acValues.Count > 0)
                {
                    //Autodesk\AutoCAD\R21.0\ACAD-0001:409 for autocad
                    //Autodesk\\AutoCAD LT\\R21\\ACADLT-E001:409" for autocadlt
                    var acadLtVer = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\" + acValues[acValues.Count - 1], false).GetValue("ProductName");

                    var autocadLtLocation = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\" + acValues[acValues.Count - 1], false).GetValue("Location") + "\\accoreconsole.exe";
                    strMess1 += $"===>{acadLtVer}";
                    MessageBox.Show(strMess1);
                    OpenFileDialog ofd = new OpenFileDialog()
                    {
                        Multiselect      = false,
                        Filter           = "Dwg文件(*.dwg)|*.dwg",
                        RestoreDirectory = true,
                        FilterIndex      = 1,
                        Title            = "选取一张图纸"
                    };
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        int ver = MyHelper.GetAutoCADDrawingVer(ofd.FileName);
                        if (ver == 0)
                        {
                            MessageBox.Show("图纸是只读的,请检查文件的属性!!");
                            return;
                        }
                        if (MyHelper.GetAutoCADDrawingVer(ofd.FileName) > 2018)
                        {
                            MessageBox.Show("打开的图纸的版本过高,本软件最高支持AutoCAD2018 保存的文件!");
                            return;
                        }
                        List <NeedPrinPdfPageInfo> listTitleBlks = new List <NeedPrinPdfPageInfo>();

                        #region//读取图纸信息
                        using (Services svcs = new Services())
                        {
                            Database db = new Database(false, false);
                            db.ReadDwgFile(ofd.FileName, System.IO.FileShare.Read, false, null);
                            using (Transaction trans = db.TransactionManager.StartTransaction())
                            {
                                BlockTable       bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                                BlockTableRecord ms = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;

                                //获取全部的块信息
                                int i = 0;
                                foreach (ObjectId oid in ms)
                                {
                                    Entity ent = oid.GetObject(OpenMode.ForRead) as Entity;
                                    if (ent is BlockReference)
                                    {
                                        BlockReference blkref = ent as BlockReference;
                                        if (blkref.IsDynamicBlock)
                                        {
                                            var blk = blkref.DynamicBlockTableRecord.GetObject(OpenMode.ForRead) as BlockTableRecord;

                                            if (blk.Name == "KFWH_CP_SD_A3_H" || blk.Name == "KFWH_CP_SD_A3_V" || blk.Name == "KFWH_CP_SD_A4")//图框
                                            {
                                                NeedPrinPdfPageInfo pdfinfo = new NeedPrinPdfPageInfo();

                                                pdfinfo.leftbtm_X = blkref.GeometricExtents.MinPoint.X;
                                                pdfinfo.leftbtm_Y = blkref.GeometricExtents.MinPoint.Y;
                                                pdfinfo.RightUp_X = blkref.GeometricExtents.MaxPoint.X;
                                                pdfinfo.RightUp_Y = blkref.GeometricExtents.MaxPoint.Y;

                                                switch (blk.Name)
                                                {
                                                case "KFWH_CP_SD_A3_H":
                                                    pdfinfo.pageSize = PDFPageSize.ISO_full_bleed_A3_420_x_297_MM;

                                                    break;

                                                case "KFWH_CP_SD_A3_V":
                                                    pdfinfo.pageSize = PDFPageSize.ISO_full_bleed_A3_297_x_420_MM;

                                                    break;

                                                case "KFWH_CP_SD_A4":
                                                    pdfinfo.pageSize = PDFPageSize.ISO_full_bleed_A4_297_x_210_MM;

                                                    break;
                                                }

                                                var shtNo = blkref.ObjectId.GetAttributeInBlockReference("SHT");
                                                if (shtNo == string.Empty || shtNo == "")
                                                {
                                                    shtNo = blkref.ObjectId.GetAttributeInBlockReference("SH");
                                                }
                                                if (shtNo == string.Empty || shtNo == "")
                                                {
                                                    shtNo = (i + 1).ToString();
                                                }
                                                pdfinfo.SheetNum = shtNo;
                                                i++;
                                                listTitleBlks.Add(pdfinfo);
                                                continue;
                                            }
                                        }
                                    }
                                }
                            }
                            db.CloseInput(false);
                        }
                        #endregion

                        #region//打印完成
                        if (listTitleBlks.Count == 0)
                        {
                            MessageBox.Show("图纸内部不含有标准的shop drawing 图框");
                            return;
                        }
                        string       fn = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "PrintoPdfScript For " + Path.GetFileNameWithoutExtension(ofd.FileName) + "at " + DateTime.Now.ToLongDateString() + ".scr");
                        StreamWriter sw = new StreamWriter(fn);
                        sw.WriteLine("dynmode -3");
                        string strMess = string.Empty;
                        string strA3H  = string.Empty;
                        string strA3V  = string.Empty;
                        string strA4   = string.Empty;
                        var    temp    = listTitleBlks.OrderBy(c => c.pageSize).ThenBy(c => c.SheetNum).ToList();
                        for (int i = 0; i < temp.Count; i++)
                        {
                            var item = temp[i];
                            sw.WriteLine("-plot");
                            sw.WriteLine("yes");            //  Detailed plot configuration?[Yes / No] < No >: Yes
                            sw.WriteLine("Model");          //   Enter a layout name or[?] < Model >: Model
                            sw.WriteLine("DWG To PDF.pc3"); //Enter an output device name or[?] < None >: DWG To PDF.pc3
                            switch (item.pageSize)
                            {
                            case PDFPageSize.ISO_full_bleed_A3_420_x_297_MM:
                                sw.WriteLine("ISO_full_bleed_A3_(420.00_x_297.00_MM)".Replace("_", " ")); //图纸尺寸//Enter paper size or[?] < ISO full bleed A3(420.00 x 297.00 MM) >: ISO A3 (420.00 x 297.00 MM)
                                sw.WriteLine("M");                                                        //Enter paper units[Inches / Millimeters] < Millimeters >: M
                                sw.WriteLine("L");                                                        //Enter drawing orientation[Portrait / Landscape] < Landscape >: L
                                strA3H += "," + item.SheetNum;
                                break;

                            case PDFPageSize.ISO_full_bleed_A3_297_x_420_MM:
                                sw.WriteLine("ISO_full_bleed_A3_(297.00_x_420.00_MM)".Replace("_", " "));    //图纸尺寸
                                sw.WriteLine("M");
                                sw.WriteLine("P");
                                strA3V += "," + item.SheetNum;
                                break;

                            case PDFPageSize.ISO_full_bleed_A4_297_x_210_MM:
                                sw.WriteLine("ISO_full_bleed_A4_(297.00_x_210.00_MM)".Replace("_", " ")); //图纸尺寸
                                sw.WriteLine("M");                                                        //millimeters
                                sw.WriteLine("L");                                                        //Landscape
                                strA4 += "," + item.SheetNum;
                                break;
                            }
                            sw.WriteLine("No");                                                       //Plot upside down? [Yes/No] <No>: No
                            sw.WriteLine("Window");                                                   //Enter plot area[Display / Extents / Limits / View / Window] <Display>: W
                            sw.WriteLine(item.leftbtm_X + "," + item.leftbtm_Y);                      //Enter lower left corner of window<0.000000,0.000000>: 57890.4548,5664.4887
                            sw.WriteLine(item.RightUp_X + "," + item.RightUp_Y);                      //Enter upper right corner of window<0.000000,0.000000>: 74050.4544,17344.4885
                            sw.WriteLine("Fit");                                                      //Enter plot scale (Plotted Millimeters = Drawing Units) or[Fit] <Fit>: Fit
                            sw.WriteLine("Center");                                                   // Enter plot offset(x, y) or[Center] <11.55,-13.65>: center
                            sw.WriteLine("Yes");                                                      //Plot with plot styles?[Yes / No] <Yes>: y
                            sw.WriteLine("monochrome.ctb");                                           // Enter plot style table name or[?] (enter. for none) <>: monochrome.ctb
                            sw.WriteLine("Yes");                                                      //Plot with lineweights? [Yes/No] <Yes>: Yes
                            sw.WriteLine("As");                                                       //Enter shade plot setting[As displayed / legacy Wireframe / legacy Hidden / Visual styles / Rendered] <As displayed>: As
                            sw.WriteLine(ofd.FileName.Replace(".dwg", "-" + item.SheetNum + ".pdf")); //Enter file name<D:\MyDeskTop\B379-53-006-Shop Drawing-ALT.1--nans-Model.pdf>: c:\1.pdf
                            sw.WriteLine("Yes");                                                      //Save changes to page setup [Yes/No]? <N> Yes
                            sw.WriteLine("yes");                                                      //Proceed with plot [Yes/No] <Y>: Yes
                        }
                        sw.WriteLine("filedia");
                        sw.WriteLine("1");
                        sw.WriteLine(" ");
                        sw.Close();
                        strMess = $"A3横向图框, sht:{strA3H}\n A3竖向图框, sht:{strA3V} \n A4横向图框, sht:{strA4}";
                        #endregion
                        if (autocadLtLocation.Contains("LT"))
                        {
                            CMDHelper.ExecuteScr(fn, ofd.FileName, Path.GetDirectoryName(autocadLtLocation));
                            MyHelper.MyOpenFolder(Path.GetDirectoryName(ofd.FileName), Path.GetFileName(ofd.FileName));
                        }
                        else
                        {
                            if (MessageBox.Show($"关闭程序以完成打印! \n {strMess}", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.OK)
                            {
                                CMDHelper.ExecuteCmd(fn, ofd.FileName, autocadLtLocation);
                                MyHelper.MyOpenFolder(Path.GetDirectoryName(ofd.FileName), Path.GetFileName(ofd.FileName));
                                this.Close();
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show($" 检测到你的电脑上未安装 AutoCAD LT/AutoCAD 版本,无法加载插件!!!");
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show($" 检测到你的电脑上未安装 AutoCAD,无法加载插件!!!");
            }
        }