/// <summary> /// 打印FPC软板批量卡和单体图以及辅料卡 /// </summary> /// <param name="xlBook">当前工作簿</param> /// <param name="row">取得的数据行</param> /// <param name="lotId">当前批量卡序号</param> /// <param name="danTiTuFullPath">当前单体图全路径</param> /// <param name="dgvRow">当前dgv行</param> /// <param name="cadApp">cad对象</param> /// <param name="xlApp">excel对象</param> /// <param name="productNum">生产编号</param> /// <param name="iMaxPnlQty">最多多少pnl需要打一张辅料卡</param> private void PrintFpcLotCard( Excel.Workbook xlBook, DataRow row, string lotId, string danTiTuFullPath, DataGridViewRow dgvRow, GcadApplication cadApp, Excel.Application xlApp, string productNum, decimal iMaxPnlQty ) { //当前工作表 var xlSheet = (Excel.Worksheet)xlBook.Worksheets[1]; //设置值 //流程卡卡号 xlSheet.Shapes.AddTextEffect( MsoPresetTextEffect.msoTextEffect2, lotId, "宋体", 18f, MsoTriState.msoFalse, MsoTriState.msoFalse, 430f, 15f ); //所有需要填写数据的单元格 var xlRange = xlSheet.Range[xlSheet.Cells[5, 13], xlSheet.Cells[17, 15]]; xlRange.ShrinkToFit = true; //下单日期 xlSheet.Range["N5"].Value = Convert.ToDateTime(row["order_date"]).ToString("yyyy-MM-dd"); //交货日期 xlSheet.Range["N6"].Value = Convert.ToDateTime(row["need_date"]).ToString("yyyy-MM-dd"); //订单数量 xlSheet.Range["N7"].Value = string.Format("{0} pcs", row["order_pcs_qty"]); //总投料量 xlSheet.Range["N8"].Value = string.Format("{0} pcs", row["total_pcs_qty"]); //本批数量 xlSheet.Range["N9"].Value = string.Format("{0} pnl", row["total_pnl_qty"]); //单元格拆分 xlRange = xlSheet.Range[xlSheet.Cells[11, 13], xlSheet.Cells[17, 15]]; xlRange.MergeCells = false; //合并单元格 for (int i = 11; i < 16; i++) { xlRange = xlSheet.Range[string.Format("N{0}:O{0}", i)]; xlRange.Merge(); } for (int i = 16; i < 18; i++) { xlRange = xlSheet.Range[string.Format("M{0}:O{0}", i)]; xlRange.Merge(); } //边框线 xlRange = xlSheet.Range[xlSheet.Cells[5, 13], xlSheet.Cells[17, 15]]; xlRange.ShrinkToFit = true; xlRange.Borders.LineStyle = Excel.XlLineStyle.xlContinuous; xlRange.Borders.Weight = Excel.XlBorderWeight.xlThin; //投料单号 xlSheet.Range["N10"].Value = row["order_num"].ToString(); //起始卡号 xlSheet.Range["M11"].Value = "起始卡号"; xlSheet.Range["N11"].Value = row["first_lot_id"].ToString(); //结束卡号 xlSheet.Range["M12"].Value = "结束卡号"; xlSheet.Range["N12"].Value = row["last_lot_id"].ToString(); //单据状态 xlSheet.Range["M13"].Value = "单据状态"; xlSheet.Range["N13"].Value = row["state"].ToString(); //pnl含pcs数 xlSheet.Range["M14"].Value = "pnl含pcs数"; xlSheet.Range["N14"].Value = row["pnl_count_pcs"].ToString(); //pnl面积 xlSheet.Range["M15"].Value = "pnl面积"; xlSheet.Range["N15"].Value = string.Format("{0} m2", row["pnl_area"]); //lot数 var iFirst = Convert.ToInt32(row["first_lot_id"].ToString().Substring(4).Replace("S", string.Empty)); var iLast = Convert.ToInt32(row["last_lot_id"].ToString().Substring(4).Replace("S", string.Empty)); if (iLast < iFirst) { var i = iLast; iLast = iFirst; iFirst = i; } var iCurrent = Convert.ToInt32(lotId.Substring(4).Replace("S", string.Empty)); xlSheet.Range["M16"].Value = string.Format("{0} LOT 共 {1} LOT", iCurrent - iFirst + 1, iLast - iFirst + 1); //本卡开料pnl数 xlRange = xlSheet.Range["M17"]; xlRange.Font.Bold = true; xlRange.Font.Size = 18; xlRange.Value = string.Format("{0} pnl", row["pnl_qty"]); //当前路径 string strPath = Application.ExecutablePath; int j = strPath.LastIndexOf(@"\"); strPath = strPath.Remove(j + 1); //待打印成的图片文件的名称 string imgName = string.Format("{0}dwg2jpg.jpg", strPath); //将单体图读取到lot卡中 switch (cboPrintSettings.Text) { case "不打印只保存单元图": case "不打印保存批量卡和cov卡以及单元图": case "只打印单元图": case "打印批量卡和cov卡以及单元图": //显示状态 dgvRow.Cells["print_msg"].Value = "正在打开单体图..."; //各列列宽 dgvList.AutoResizeColumns(); //显示 Application.DoEvents(); //删除之前的文件 if (File.Exists(imgName)) { File.Delete(imgName); } //显示状态 dgvRow.Cells["print_msg"].Value = "正在生成单体图图片..."; //各列列宽 dgvList.AutoResizeColumns(); //显示 Application.DoEvents(); //执行打印单体图生成图片文件 string strOutPut = ydDwg2Jpg.PrintDanTiTuImage(cadApp, danTiTuFullPath, imgName, cboDwgLayout.Text, cboDwgWay.SelectedIndex); //for (int iii = 0; iii < 12; iii++) //{ // ydDwg2Jpg.PrintDanTiTuImage(cadApp, danTiTuFullPath, imgName + iii + ".jpg", iii); //} //检测文件是否存在 if (strOutPut.Length > 0 || !File.Exists(imgName)) { //返回错误 dgvRow.Cells["print_msg"].Value = strOutPut; return; } else { //显示状态 dgvRow.Cells["print_msg"].Value = "正在插入单体图图片到批量卡..."; //各列列宽 dgvList.AutoResizeColumns(); //显示 Application.DoEvents(); //设置为活动 ((Excel._Workbook)xlBook).Activate(); ((Excel._Worksheet)xlSheet).Select(); //在excel中插入该图片并设置分页 string strError = string.Empty; if (!InsertDanTiTuImageToExcelSheet(xlSheet, imgName, out strError)) { //返回错误 dgvRow.Cells["print_msg"].Value = "插入单体图图片到批量卡失败!"; return; } } //退出 break; } //待打印成的文件名称 string tmpFileName = string.Format("{0}lotcard.xls", strPath); //删除之前的文件 if (File.Exists(tmpFileName)) { File.Delete(tmpFileName); } //打印设置 var xlPageSetup = xlSheet.PageSetup; xlPageSetup.FitToPagesWide = 1; xlPageSetup.FitToPagesTall = 1; xlPageSetup.RightFooter = @"&""宋体,倾斜""&10打印时间:&D &T"; xlPageSetup.FooterMargin = xlApp.InchesToPoints(0.18d); //保存该批量卡 xlBook.SaveAs(tmpFileName); //不打印只保存批量卡和cov卡 //不打印只保存单元图 //不打印保存批量卡和cov卡以及单元图 //只打印批量卡和cov卡 //只打印单元图 //打印批量卡和cov卡以及单元图 //打印 switch (cboPrintSettings.Text.Trim()) { case "打印批量卡和cov卡以及单元图": //显示状态 dgvRow.Cells["print_msg"].Value = "正在打印批量卡和cov卡以及单元图..."; //各列列宽 dgvList.AutoResizeColumns(); //显示 Application.DoEvents(); //打印LOT卡和单体图 xlSheet.PrintOut(From: 1, To: 2, Copies: 1, Collate: true); break; case "只打印批量卡": //显示状态 dgvRow.Cells["print_msg"].Value = "正在打印批量卡..."; //各列列宽 dgvList.AutoResizeColumns(); //显示 Application.DoEvents(); //打印LOT卡 xlSheet.PrintOut(From: 1, To: 1, Copies: 1, Collate: true); break; case "只打印批量卡和cov卡": //显示状态 dgvRow.Cells["print_msg"].Value = "正在打印批量卡和cov卡..."; //各列列宽 dgvList.AutoResizeColumns(); //显示 Application.DoEvents(); //打印LOT卡 xlSheet.PrintOut(From: 1, To: 1, Copies: 1, Collate: true); break; case "只打印单元图": //显示状态 dgvRow.Cells["print_msg"].Value = "正在打印单元图..."; //各列列宽 dgvList.AutoResizeColumns(); //显示 Application.DoEvents(); //打印单体图 xlSheet.PrintOut(From: 2, To: 2, Copies: 1, Collate: true); break; } //打印辅料卡 switch (cboPrintSettings.Text.Trim()) { case "只打印批量卡和cov卡": case "打印批量卡和cov卡以及单元图": //之前已经累加存入数量 int iTotalPnlQty = 0; //检测是否已经打印过辅料卡 if (!_dicProductNums.ContainsKey(productNum)) { //添加到已经打印过的生产编号清单中 _dicProductNums.Add(productNum, Convert.ToInt32(row["pnl_qty"])); } else { //获取数量 _dicProductNums.TryGetValue(productNum, out iTotalPnlQty); //检测数量 if (iTotalPnlQty >= iMaxPnlQty) { //总数量置0 iTotalPnlQty = 0; } //累加已经打印过的生产编号的数量 _dicProductNums[productNum] = iTotalPnlQty + Convert.ToInt32(row["pnl_qty"]); } //总数量为0则需要打印辅料卡 if (iTotalPnlQty == 0 && xlBook.Worksheets.Count > 1) { //取第二张表 var xlSheet2 = (Excel.Worksheet)xlBook.Worksheets[2]; //显示状态 dgvRow.Cells["print_msg"].Value = "正在打印cov卡..."; //各列列宽 dgvList.AutoResizeColumns(); //显示 Application.DoEvents(); //设置为最多两页高 xlSheet2.PageSetup.FitToPagesWide = 1; xlSheet2.PageSetup.FitToPagesTall = 2; //打印辅料卡 try { xlSheet2.PrintOut(From: 1, To: 1, Copies: 1, Collate: true); xlSheet2.PrintOut(From: 2, To: 2, Copies: 1, Collate: true); } catch { } //注销excel相关对象 xlSheet2 = null; } //退出 break; } //关闭工作表 xlBook.Close(SaveChanges: false); //删除生成的图片文件 if (File.Exists(imgName)) { File.Delete(imgName); } //返回成功 //不打印只保存批量卡 //不打印只保存批量卡和cov卡 //不打印只保存单元图 //不打印保存批量卡和cov卡以及单元图 //只打印批量卡 //只打印批量卡和cov卡 //只打印单元图 //打印批量卡和cov卡以及单元图 //打印 switch (cboPrintSettings.Text.Trim()) { case "不打印只保存批量卡": case "不打印只保存批量卡和cov卡": case "不打印只保存单元图": case "不打印保存批量卡和cov卡以及单元图": dgvRow.Cells["print_msg"].Value = "保存成功!"; //退出 break; default: dgvRow.Cells["print_msg"].Value = "打印成功!"; //退出 break; } //注销excel相关对象 xlSheet = null; }
/// <summary> /// 执行打印当前行的批量卡 /// </summary> /// <param name="dgvRow">当前dgv行</param> /// <param name="xlApp">当前excel对象</param> /// <param name="cadApp">当前cad对象</param> /// <param name="da">数据适配器获取批量卡数据</param> /// <param name="iMaxPnlQty">最多多少pnl需要打一张辅料卡</param> private void PintNow( DataGridViewRow dgvRow, Excel.Application xlApp, GcadApplication cadApp, SqlDataAdapter da, decimal iMaxPnlQty ) { //检测批量卡卡号 object obj = dgvRow.Cells["lot_id"].Value; if (obj == null || obj == DBNull.Value || obj.ToString().Trim().Length == 0) { //返回错误 dgvRow.Cells["print_msg"].Value = "未找到批量卡号!"; return; } //批量卡序号 string lotId = obj.ToString().Trim(); //检测批量卡序号 if (lotId == null || lotId.Length < 6) { dgvRow.Cells["print_msg"].Value = "未提供批量卡序号参数或者批量卡号参数长度太短!"; return; } //生产编号 obj = dgvRow.Cells["product_num"].Value; if (obj == null || obj == DBNull.Value || obj.ToString().Trim().Length == 0) { //返回错误 dgvRow.Cells["print_msg"].Value = "未找到生产编号!"; return; } //取得生产编号 string productNum = obj.ToString().Trim().ToUpper(); try { //生产编号中的数字起始字符索引 int iLayerStart = 0; //检测生产编号的正确性 string strReg = @"^[LS]-HD\d{4,}(-\d{4,})*[0A-Z][0-9]$"; if (!Regex.IsMatch(productNum, strReg)) { //返回错误 dgvRow.Cells["print_msg"].Value = "生产编号或样板编号不符合规则!"; return; } //第二个字符为减号则从第三个字符开始 iLayerStart += 4; //抓取生产编号中的数字序号串 string pNum = productNum.Substring(iLayerStart, productNum.Length - iLayerStart - 2); //最后两位为版本号 string pVersion = productNum.Substring(productNum.Length - 2); //生产编号转数字 int k = pNum.IndexOf("-"); int iNum; string strFileNameNum = string.Empty; if (k > 0) { iNum = int.Parse(pNum.Substring(0, k)); } else { iNum = int.Parse(pNum); } //当前生产编号数字或者生产编号数字段 strFileNameNum = iNum.ToString("0000"); //根目录编号段 int iNumBegin = (iNum / 1000) * 1000 + 1; int iNumEnd = iNumBegin + 1000 - 1; //拼接文件夹路径 var strPathHead = @"\\192.168.1.2\工程资料\"; //var strPathHead = @"E:\工程资料\"; var sbLotCard = new StringBuilder(strPathHead); //确定顶级文件夹名称 sbLotCard.Append(iNumBegin.ToString("0000") + "-" + iNumEnd.ToString("0000")); //确定次级文件夹名称 sbLotCard.Append(@"\" + pNum); //加入版本文件夹 sbLotCard.Append(@"\" + pNum + pVersion); //批量卡末级文件夹名称 sbLotCard.Append(@"\MI"); //存储路径用来取得单体图路径 var sbDanTiTu = new StringBuilder(sbLotCard.ToString()); //是否找到流程卡 var isExistLotCard = false; //是否找到单元图 var isExistDanTiTu = false; //在该目录下找流程卡与单元图 foreach (var item in new DirectoryInfo(sbLotCard.ToString()).GetFiles()) { //文件名称 var fileName = item.Name.ToUpper(); //检测文件名称 if (!isExistLotCard && ( (lotId.Contains("S") && ( fileName.EndsWith("样品流程卡.XLS") || (fileName.Contains("样品流程卡-") && fileName.EndsWith(".XLS")) )) || (!lotId.Contains("S") && ( fileName.EndsWith("流程卡.XLS") || (fileName.Contains("流程卡-") && fileName.EndsWith(".XLS")) )) )) { sbLotCard.Append(@"\" + fileName); isExistLotCard = true; } else if (!isExistDanTiTu && (fileName.EndsWith("单元图.DWG") || fileName.EndsWith("单体图.DWG"))) { sbDanTiTu.Append(@"\" + fileName); isExistDanTiTu = true; } //是否全部已经找到 if (isExistLotCard && isExistDanTiTu) { break; } } //得到批量卡文件全路径 string lotCardFullPath = sbLotCard.ToString(); //得到单体图文件全路径 string danTiTuFullPath = sbDanTiTu.ToString(); //不打印只保存批量卡 //不打印只保存批量卡和cov卡 //不打印只保存单元图 //不打印保存批量卡和cov卡以及单元图 //只打印批量卡 //只打印批量卡和cov卡 //只打印单元图 //打印批量卡和cov卡以及单元图 //检查文件是否存在 if (!isExistLotCard) { switch (cboPrintSettings.Text) { case "不打印只保存批量卡": case "不打印只保存批量卡和cov卡": case "不打印保存批量卡和cov卡以及单元图": case "只打印批量卡": case "只打印批量卡和cov卡": case "打印批量卡和cov卡以及单元图": //返回错误 dgvRow.Cells["print_msg"].Value = "工程批量卡不存在!" + System.Environment.NewLine + lotCardFullPath; return; } } if (!isExistDanTiTu) { switch (cboPrintSettings.Text) { case "不打印只保存单元图": case "不打印保存批量卡和cov卡以及单元图": case "只打印单元图": case "打印批量卡和cov卡以及单元图": //返回错误 dgvRow.Cells["print_msg"].Value = "工程单元图不存在!" + System.Environment.NewLine + danTiTuFullPath; return; } } //显示状态 dgvRow.Cells["print_msg"].Value = "正在获取批量卡数据..."; //各列列宽 dgvList.AutoResizeColumns(); //显示 Application.DoEvents(); //设置参数值 da.SelectCommand.Parameters["@LotId"].Value = lotId; //获取数据 var tab = new DataTable(); da.Fill(tab); //检测是否获取到数据 if (tab.Rows.Count <= 0) { //返回错误 dgvRow.Cells["print_msg"].Value = "PMC或样板部门录入的批量卡不存在!"; return; } //首行 DataRow row = tab.Rows[0]; //显示状态 dgvRow.Cells["print_msg"].Value = "正在打开批量卡..."; //各列列宽 dgvList.AutoResizeColumns(); //显示 Application.DoEvents(); //打开工作簿 var xlBook = xlApp.Workbooks.Open(lotCardFullPath, ReadOnly: true); //打印FPC软板批量卡和单体图以及辅料卡 PrintFpcLotCard( xlBook, row, lotId, danTiTuFullPath, dgvRow, cadApp, xlApp, productNum, iMaxPnlQty ); //注销excel对象 xlBook = null; } catch (Exception ex) { //返回错误 dgvRow.Cells["print_msg"].Value = ex.Message; return; } }
/// <summary> /// 本控制台应用程序需要两个参数 /// </summary> /// <param name="cadApp">浩辰cad对象实例</param> /// <param name="xianLuTuFullPath">线路菲林图文件地址</param> /// <param name="imgName">输出的图片文件地址</param> internal static string PrintXianLuTuImage(GcadApplication cadApp, string xianLuTuFullPath, string imgName) { //打开该cad文件 try { //显示 cadApp.Visible = false; //打开文档 var cadDoc = cadApp.Documents.Open(Name: xianLuTuFullPath, ReadOnly: true); //是否找到线路菲林图布局 bool isExist = false; //待打印的布局 GcadLayout cadLayPrint = null; //得到待打印的布局 foreach (GcadLayout cadLay in cadDoc.Layouts) { //实名查找 if (cadLay.Name.Trim() == "模型") { //找到线路菲林图布局 isExist = true; //待打印的布局 cadLayPrint = cadLay; //退出循环 break; } } //模糊查找 if (!isExist) { foreach (GcadLayout cadLay in cadDoc.Layouts) { //实名查找 if (cadLay.Name.Trim().Contains("模型")) { //找到线路菲林图布局 isExist = true; //待打印的布局 cadLayPrint = cadLay; //退出循环 break; } } } //检测是否找到待打印的布局 if (!isExist) { //待返回的状态结果 return "线路菲林图dwg文件中不存在模型布局!" + xianLuTuFullPath; } //设置当前布局 cadDoc.ActiveLayout = cadLayPrint; //打印属性 var cadPlot = cadDoc.Plot; //设置打印页 cadPlot.SetLayoutsToPlot(new string[] { cadLayPrint.Name }); //指定布局或打印配置的类型 cadLayPrint.PlotType = GcPlotType.gcExtents; ////刷新打印机列表 cadLayPrint.RefreshPlotDeviceInfo(); ////启用静默错误模式 cadDoc.Plot.QuietErrorMode = true; //打印机名称 cadLayPrint.ConfigName = "PublishToWeb JPG.pc3"; //打印样式 cadLayPrint.StyleSheet = "gcad.ctb"; //纸张大小 cadLayPrint.PaperUnits = GcPlotPaperUnits.gcMillimeters; cadLayPrint.PlotRotation = GcPlotRotation.gc90degrees; cadLayPrint.StandardScale = GcPlotScale.gcScaleToFit; cadLayPrint.UseStandardScale = true; cadLayPrint.CenterPlot = true; //打印份数 cadPlot.NumberOfCopies = 1; cadDoc.SetVariable("BACKGROUNDPLOT", 0); //执行打印成jpg图片文件 cadPlot.PlotToFile(imgName); //关闭CAD cadDoc.Close(SaveChanges: false); //返回空字符串 return string.Empty; } catch (Exception exx) { return exx.Message; } }
private void btnPrint_Click(object sender, EventArgs e) { //检测有无数据 if (dgvList.SelectedRows.Count <= 0) { MessageBox.Show("当前未选中任何行!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //清除之前的生产编号 _dicProductNums.Clear(); //excel对象 var xlApp = new Excel.Application(); //设置excel对象属性 xlApp.Visible = false; xlApp.DisplayAlerts = false; //cad对象 var cadApp = new GcadApplication(); //设置cad对象属性 cadApp.Visible = false; //通过首行检测是否样板 object obj = dgvList.Rows[0].Cells["lot_id"].Value; if (obj != null && obj != DBNull.Value && obj.ToString().Trim().Length == 0) { MessageBox.Show("首行无批量卡号!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } bool isSample = obj.ToString().Trim().ToUpper().Contains("S"); //查询语句 string sqlstr; if (isSample) { sqlstr = "select top 1 isnull([first_lot_id],'') as [first_lot_id]," + "isnull([last_lot_id],'') as [last_lot_id]," + "[pnl_qty],[total_pnl_qty],[total_pcs_qty],[order_pcs_qty],[state],[is_supplement]," + "[pnl_count_pcs],[pnl_area]," + "[add_person],[order_num],[stock_pcs_qty],[order_date],[product_date],[need_date] " + "from [v_sample_lot_card_join_order] " + "where [lot_id]=@LotId"; } else { sqlstr = "select top 1 isnull([first_lot_id],'') as [first_lot_id]," + "isnull([last_lot_id],'') as [last_lot_id]," + "[pnl_qty],[total_pnl_qty],[total_pcs_qty],[order_pcs_qty],[state],[is_supplement]," + "[pnl_count_pcs],[pnl_area]," + "[add_person],[order_num],[stock_pcs_qty],[order_date],[product_date],[need_date] " + "from [v_ppc_lot_card_join_order] " + "where [lot_id]=@LotId"; } //实例化连接对象和查询对象 using (var conn = new SqlConnection(ydConnInfo.ConnStrYdErpGrh)) using (var cmd = new SqlCommand(sqlstr, conn)) using (var da = new SqlDataAdapter(cmd)) { //打开连接 conn.Open(); //设置超时 cmd.CommandTimeout = 50; //设置查询类型 cmd.CommandType = CommandType.Text; //设置参数 cmd.Parameters.Clear(); cmd.Parameters.Add("@LotId", SqlDbType.VarChar, 10); //最多多少pnl需要打一张辅料卡 decimal iMaxPnlQty = nudMaxPnlQty.Value; //放不同颜色的纸张 for (int i = 0; i < 3; i++) { //是否已经弹出过确认对话框 bool isAlertAccept = true; //遍历行 foreach (DataGridViewRow row in dgvList.Rows) { //是否需要本次循环中打印 bool isNeedPrint = false; //检测是否选中 obj = row.Cells[0].Value; if (obj != null && obj != DBNull.Value && Convert.ToBoolean(obj)) { //是否补料单/是否新单 bool isSupplement = false; bool isNewOrder = false; //补料 obj = row.Cells["is_supplement"].Value; if (obj != null && obj != DBNull.Value && Convert.ToBoolean(obj)) { isSupplement = true; } //新单 obj = row.Cells["state"].Value; if (obj != null && obj != DBNull.Value && obj.ToString().Contains("新单")) { isNewOrder = true; } //第一次循环,检测补料的 if (i == 0 && isSupplement) { //需要打印 isNeedPrint = true; //未弹出过确认对话框 if (isAlertAccept) { MessageBox.Show("请放入红色纸张打印补料单,确认后再继续打印!", "红色纸", MessageBoxButtons.OK); isAlertAccept = false; } } //第二次循环,检测新单的 else if (i == 1 && !isSupplement && isNewOrder) { //需要打印 isNeedPrint = true; //未弹出过确认对话框 if (isAlertAccept) { MessageBox.Show("请放入黄色纸张打印新单,确认后再继续打印!", "黄色纸", MessageBoxButtons.OK); isAlertAccept = false; } } //第三次循环,正常单 else if (i == 2 && !isSupplement && !isNewOrder) { //需要打印 isNeedPrint = true; //未弹出过确认对话框 if (isAlertAccept) { MessageBox.Show("请放入白色纸张打印其他单,确认后再继续打印!", "白色纸", MessageBoxButtons.OK); isAlertAccept = false; } } //需要打印 if (isNeedPrint) { //显示状态 row.Cells["print_msg"].Value = "正在检测打印参数..."; //各列列宽 dgvList.AutoResizeColumns(); //显示 Application.DoEvents(); //执行打印批量卡 PintNow(row, xlApp, cadApp, da, iMaxPnlQty); //各列列宽 dgvList.AutoResizeColumns(); //显示 Application.DoEvents(); } } } } } //关闭excel文档 foreach (Excel.Workbook itm in xlApp.Workbooks) { itm.Close(SaveChanges: false); } //退出excel xlApp.Quit(); //注销excel相关对象 xlApp = null; //关闭全部文档 foreach (GcadDocument itm in cadApp.Documents) { itm.Close(SaveChanges: false); } //注销对象 cadApp.Quit(); cadApp = null; //结束cad进程 var ps = Process.GetProcessesByName("gcad.exe"); for (int i = ps.Length - 1; i >= 0; i--) { try { if (!ps[i].CloseMainWindow()) { ps[i].Kill(); } } catch { } } ////资源回收 //GC.Collect(); //各列列宽 dgvList.AutoResizeColumns(); }
/// <summary> /// 本控制台应用程序需要两个参数 /// </summary> /// <param name="cadApp">浩辰cad对象实例</param> /// <param name="danTiTuFullPath">单体图文件地址</param> /// <param name="imgName">输出的图片文件地址</param> /// <param name="dwgLayout">使用哪个布局</param> /// <param name="dwgWay">按哪种方式打印</param> internal static string PrintDanTiTuImage( GcadApplication cadApp, string danTiTuFullPath, string imgName, string dwgLayout, int dwgWay ) { //打开该cad文件 try { //显示 cadApp.Visible = false; //打开文档 var cadDoc = cadApp.Documents.Open(Name: danTiTuFullPath, ReadOnly: true); //是否找到单体图布局 bool isExist = false; //待打印的布局 GcadLayout cadLayPrint = null; //得到待打印的布局 foreach (GcadLayout cadLay in cadDoc.Layouts) { //实名查找 if (cadLay.Name.Trim() == "单体图" || cadLay.Name.Trim() == "单元图") { //找到单体图布局 isExist = true; //待打印的布局 cadLayPrint = cadLay; //退出循环 break; } } //模糊查找 if (!isExist) { foreach (GcadLayout cadLay in cadDoc.Layouts) { //实名查找 if (cadLay.Name.Trim().Contains("单体图") || cadLay.Name.Trim().Contains("单元图")) { //找到单体图布局 isExist = true; //待打印的布局 cadLayPrint = cadLay; //退出循环 break; } } } //找到单体图或者单元图就按照gth的方法处理设置打印样式 if (isExist) { cadLayPrint.PlotType = GcPlotType.gcExtents; } else { ////得到待打印的布局 //foreach (GcadLayout cadLay in cadDoc.Layouts) //{ // //实名查找 // //if (cadLay.Name.Trim().ToLower() == "布局1") // //if (cadLay.Name.Trim().ToLower() == "model") // if (cadLay.Name.Trim().ToLower() == dwgLayout) // { // //找到单体图布局 // isExist = true; // //待打印的布局 // cadLayPrint = cadLay; // //退出循环 // break; // } //} //模糊查找 foreach (GcadLayout cadLay in cadDoc.Layouts) { //实名查找 if (cadLay.Name.Trim().ToLower() == "模型") { //找到单体图布局 isExist = true; //待打印的布局 cadLayPrint = cadLay; //退出循环 break; } } //模糊查找 if (!isExist) { foreach (GcadLayout cadLay in cadDoc.Layouts) { //实名查找 if (cadLay.Name.Trim().ToLower() == "model") { //找到单体图布局 isExist = true; //待打印的布局 cadLayPrint = cadLay; //退出循环 break; } } } //模糊查找 if (!isExist) { foreach (GcadLayout cadLay in cadDoc.Layouts) { //实名查找 if (cadLay.Name.Trim().ToLower() == "布局1") { //找到单体图布局 isExist = true; //待打印的布局 cadLayPrint = cadLay; //退出循环 break; } } } //指定布局或打印配置的类型 if (dwgWay == 0) { cadLayPrint.PlotType = GcPlotType.gcExtents; } else if (dwgWay == 1) { cadLayPrint.PlotType = GcPlotType.gcWindow; } else if (dwgWay == 2) { cadLayPrint.PlotType = GcPlotType.gcLimits; } else if (dwgWay == 3) { cadLayPrint.PlotType = GcPlotType.gcView; } else if (dwgWay == 4) { cadLayPrint.PlotType = GcPlotType.gcLayout; } else if (dwgWay == 5) { cadLayPrint.PlotType = GcPlotType.gcDisplay; } else if (dwgWay == 6) { cadLayPrint.PlotType = GcPlotType.acWindow; } else if (dwgWay == 7) { cadLayPrint.PlotType = GcPlotType.acView; } else if (dwgWay == 8) { cadLayPrint.PlotType = GcPlotType.acLimits; } else if (dwgWay == 9) { cadLayPrint.PlotType = GcPlotType.acLayout; } else if (dwgWay == 10) { cadLayPrint.PlotType = GcPlotType.acExtents; } else if (dwgWay == 11) { cadLayPrint.PlotType = GcPlotType.acDisplay; } } //检测是否找到待打印的布局 if (!isExist) { //待返回的状态结果 return "dwg文件中不存在单体图布局!" + danTiTuFullPath; } //设置当前布局 cadDoc.ActiveLayout = cadLayPrint; //打印属性 var cadPlot = cadDoc.Plot; //设置打印页 cadPlot.SetLayoutsToPlot(new string[] { cadLayPrint.Name }); ////刷新打印机列表 cadLayPrint.RefreshPlotDeviceInfo(); ////启用静默错误模式 cadDoc.Plot.QuietErrorMode = true; //打印机名称 cadLayPrint.ConfigName = "PublishToWeb JPG.pc3"; //打印样式 cadLayPrint.StyleSheet = "gcad.ctb"; //纸张大小 cadLayPrint.PaperUnits = GcPlotPaperUnits.gcMillimeters; cadLayPrint.PlotRotation = GcPlotRotation.gc90degrees; cadLayPrint.StandardScale = GcPlotScale.gcScaleToFit; cadLayPrint.UseStandardScale = true; cadLayPrint.CenterPlot = true; //打印份数 cadPlot.NumberOfCopies = 1; cadDoc.SetVariable("BACKGROUNDPLOT", 0); //执行打印成jpg图片文件 cadPlot.PlotToFile(imgName); //关闭CAD cadDoc.Close(SaveChanges: false); //返回空字符串 return string.Empty; } catch (Exception exx) { return exx.Message; } }