/// <summary> /// 添加SubAssy /// </summary> /// <param name="objSubAssy"></param> /// <returns></returns> public int AddSubAssy(SubAssy objSubAssy) { string sql = "insert into SubAssy (ProjectId,SubAssyName)"; sql += " values({0},'{1}');select @@identity"; sql = string.Format(sql, objSubAssy.ProjectId, objSubAssy.SubAssyName); try { return(Convert.ToInt32(SQLHelper.GetSingleResult(sql))); } catch (SqlException ex) { //2627 if (ex.Number == 2627) { throw new Exception("子装配重复,不能添加重复的子装配信息"); } else { throw new Exception("添加子装配时数据库访问异常" + ex.Message); } } catch (Exception ex) { throw ex; } }
/// <summary> /// 打印Ceiling的CutList /// </summary> /// <param name="drawing"></param> /// <param name="tree"></param> /// <param name="dgv"></param> /// <returns></returns> public bool ExecPrintCeilingCutList(SubAssy subAssy, DataGridView dgv) { Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); string excelBookPath = Environment.CurrentDirectory + "\\CutList.xlsx"; excelApp.Workbooks.Add(excelBookPath); Microsoft.Office.Interop.Excel.Worksheet workSheet = excelApp.Worksheets[1]; workSheet.Cells[1, 2] = subAssy.ProjectName; workSheet.Cells[2, 2] = subAssy.ODPNo; workSheet.Cells[3, 2] = subAssy.SubAssyName; workSheet.Cells[4, 2] = ""; workSheet.Cells[5, 2] = ""; workSheet.Cells[7, 2] = dgv.Rows[0].Cells["AddedDate"].Value; workSheet.Cells[8, 2] = dgv.Rows[0].Cells["UserAccount"].Value; FillCutListDate(workSheet, dgv); //预览 //excelApp.Visible = true; //excelApp.Sheets.PrintPreview(true); //打印 workSheet.PrintOutEx(); KillProcess(excelApp); excelApp = null; //对象置空 GC.Collect(); //垃圾回收机制 return(true); }
/// <summary> /// 异步方式开始导出dxf图纸 /// </summary> /// <param name="item"></param> /// <param name="dxfPath"></param> /// <returns></returns> private Task exportCeilingDxfAsync(SubAssy item, string dxfPath, int userId) { return(Task.Run(() => { try { new ExprotDxf().CeilingAssyToDxf(swApp, item, dxfPath, userId); } catch (Exception ex) { throw ex; } })); }
/// <summary> /// 打印Cutlist /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnPrintCutList_Click(object sender, EventArgs e) { if (dgvCutList.RowCount == 0) { return; } btnPrintCutList.Enabled = false; btnPrintCutList.Text = "打印中..."; SubAssy objSubAssy = objSubAssyService.GetSubAssyId(dgvCutList.Rows[0].Cells["SubAssyId"].Value.ToString()); if (new PrintReports().ExecPrintCeilingCutList(objSubAssy, dgvCutList)) { MessageBox.Show("CutList打印完成", "打印完成"); } btnPrintCutList.Enabled = true; btnPrintCutList.Text = "打印CustList"; }
/// <summary> /// 根据ID返回单个子装配对象 /// </summary> /// <param name="subAssyId"></param> /// <returns></returns> public SubAssy GetSubAssyId(string subAssyId) { string sql = "select SubAssyId,SubAssy.ProjectId,SubAssyName,SubAssyPath,ODPNo,ProjectName from SubAssy"; sql += " inner join Projects on SubAssy.ProjectId=Projects.ProjectId"; sql += string.Format(" where SubAssyId={0}", subAssyId); SqlDataReader objReader = SQLHelper.GetReader(sql); SubAssy objSubAssy = null; while (objReader.Read()) { objSubAssy = new SubAssy() { SubAssyId = Convert.ToInt32(objReader["SubAssyId"]), ProjectId = Convert.ToInt32(objReader["ProjectId"]), ODPNo = objReader["ODPNo"].ToString(), ProjectName = objReader["ProjectName"].ToString(), SubAssyName = objReader["SubAssyName"].ToString(), SubAssyPath = objReader["SubAssyPath"].ToString() }; } objReader.Close(); return(objSubAssy); }
/// <summary> /// 天花子装配导出DXF图纸 /// </summary> /// <param name="swApp"></param> /// <param name="tree"></param> /// <param name="dxfPath"></param> /// <param name="userId"></param> public void CeilingAssyToDxf(SldWorks swApp, SubAssy subAssy, string dxfPath, int userId) { swApp.CommandInProgress = true; List <CeilingCutList> celingCutLists = new List <CeilingCutList>(); string assyPath = subAssy.SubAssyPath; if (assyPath.Length == 0) { return; } try { //打开模型 ModelDoc2 swModel = swApp.OpenDoc6(assyPath, (int)swDocumentTypes_e.swDocASSEMBLY, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings) as ModelDoc2; if (swModel == null) { MessageBox.Show("模型不存在,请认真检查", "模型不存在"); return; } string modulePath = dxfPath + @"\" + subAssy.SubAssyName; if (!Directory.Exists(modulePath)) { Directory.CreateDirectory(modulePath); } swModel.ForceRebuild3(true); AssemblyDoc swAssy = swModel as AssemblyDoc; //获取所有零部件集合 var compList = swAssy.GetComponents(false); //遍历集合中的所有零部件对象 foreach (var swComp in compList) { //判断零件是否被压缩,不显示,封套,零件名称不是以sldprt或SLDPRT结尾 if (swComp.Visible == 1 && !swComp.IsEnvelope() && !swComp.IsSuppressed() && (swComp.GetPathName().EndsWith(".sldprt") || swComp.GetPathName().EndsWith(".SLDPRT"))) { Component2 swParentComp = swComp.GetParent(); //总装没有父装配体 if (swParentComp == null) { ConfigurationManager swConfigMgr = swModel.ConfigurationManager; Configuration swConfig2 = swConfigMgr.ActiveConfiguration; swParentComp = swConfig2.GetRootComponent3(true); } //判断父装配体是否可视,并且不封套 if (swParentComp.Visible == 1 && !swParentComp.IsEnvelope() && !swComp.IsSuppressed()) { PartDoc swPart = swComp.GetModelDoc2(); //获取文档中的额Body对象集合 var bodyList = swPart.GetBodies2(0, false); //遍历集合中的所有Body对象,判断是否为钣金 foreach (var swBody in bodyList) { //如果是钣金则将零件地址添加到列表中 if (swBody.IsSheetMetal()) { if (sheetMetaDic.ContainsKey(swComp.GetPathName())) { sheetMetaDic[swComp.GetPathName()] += 1; } else { sheetMetaDic.Add(swComp.GetPathName(), 1); } } } } } } //关闭装配体零件 swApp.CloseDoc(assyPath); //遍历钣金零件 foreach (var sheetMeta in sheetMetaDic) { //打开模型 ModelDoc2 swPart = swApp.OpenDoc6(sheetMeta.Key, (int)swDocumentTypes_e.swDocPART, (int)swOpenDocOptions_e.swOpenDocOptions_Silent, "", ref errors, ref warnings) as ModelDoc2; Feature swFeat = (Feature)swPart.FirstFeature(); CeilingCutList cutRecord = new CeilingCutList() { SubAssyId = subAssy.SubAssyId, Quantity = sheetMeta.Value, UserId = userId }; while (swFeat != null) { var suppStatus = swFeat.IsSuppressed2((int)swInConfigurationOpts_e.swThisConfiguration, null); if (suppStatus[0] == false && swFeat.GetTypeName() == "SolidBodyFolder") { BodyFolder swBodyFolder = (BodyFolder)swFeat.GetSpecificFeature2(); swBodyFolder.SetAutomaticCutList(true); swBodyFolder.SetAutomaticUpdate(true); Feature SubFeat = swFeat.GetFirstSubFeature(); if (SubFeat != null) { Feature ownerFeature = SubFeat.GetOwnerFeature(); BodyFolder swSubBodyFolder = ownerFeature.GetSpecificFeature2(); swSubBodyFolder.UpdateCutList(); string val = string.Empty; string valout = string.Empty; bool wasResolved = false; bool linkToProp = false; SubFeat.CustomPropertyManager.Get4("Bounding Box Length", false, out val, out valout); cutRecord.Length = Convert.ToDecimal(valout); SubFeat.CustomPropertyManager.Get4("Bounding Box Width", false, out val, out valout); cutRecord.Width = Convert.ToDecimal(valout); SubFeat.CustomPropertyManager.Get4("Sheet Metal Thickness", false, out val, out valout); cutRecord.Thickness = Convert.ToDecimal(valout); SubFeat.CustomPropertyManager.Get4("Material", false, out val, out valout); cutRecord.Materials = valout; swPart.GetActiveConfiguration().CustomPropertyManager.Get6("Description", false, out valout, out val, out wasResolved, out linkToProp); cutRecord.PartDescription = valout; cutRecord.PartNo = swPart.GetTitle().Substring(0, swPart.GetTitle().Length - 7); celingCutLists.Add(cutRecord);//将信息添加到集合中 } } swFeat = swFeat.GetNextFeature(); } PartToDxf(swApp, swPart, modulePath); //关闭零件 swApp.CloseDoc(sheetMeta.Key); } } catch (Exception ex) { throw new Exception(assyPath + "导图过程发生异常,详细:" + ex.Message); } finally { sheetMetaDic.Clear(); swApp.CloseDoc(assyPath); //关闭,很快 swApp.CommandInProgress = false; //及时关闭外部命令调用,否则影响SolidWorks的使用 } //基于事务ceilingCutLists提交SQLServer if (celingCutLists.Count == 0) { return; } try { if (objCeilingCutListService.ImportCutList(celingCutLists)) { celingCutLists.Clear(); } } catch (Exception ex) { throw new Exception("Cutlist导入数据库失败" + ex.Message); } }