public static void Save(string targetFolder, string fileName, sw_DocType docType) { SldWorks swApp; ModelDoc2 swModel; string doc; swApp = SolidWorksSingleton.GetApplication(); swModel = (ModelDoc2)swApp.ActiveDoc; if (docType == sw_DocType.assembly) { doc = ".sldasm"; } else if (docType == sw_DocType.part) { doc = ".sldprt"; } else if (docType == sw_DocType.drawing) { doc = ".slddrw"; } else { throw new Exception("Kayıt yapılamadı"); } swModel.SaveAs3(targetFolder + "\\" + fileName + doc, (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_CopyAndOpen); swApp.CloseDoc(fileName + doc); }
public override void CreatePart() { swApp = SolidWorksSingleton.GetApplication(); DocumentManager.CreateNewPartDoc(); swModel = (ModelDoc2)swApp.ActiveDoc; swFeature = swModel.FeatureByPositionReverse(3); swFeature.Name = "Front"; swModel.Extension.SelectByID2("Front", "PLANE", 0, 0, 0, false, 0, null, 0); swModel.InsertSketch2(true); swModel.CreateCircleByRadius2(0, 0, 0, InsideDiameter / 2); swModel.CreateCircleByRadius2(0, 0, 0, OutsideDiameter / 2); swModel.InsertSketch2(true); swFeature = swModel.FeatureManager.FeatureExtrusion3(true, false, false, 0, 0, Lenght, 0, false, false, false, false, 0, 0, false, false, false, false, false, false, false, 0, 0, false); BasicOpertations.ChangeEntityName("FACE", MateOutsideFace, OutsideDiameter / 2, 0, Lenght / 2); double dim = (OutsideDiameter - InsideDiameter) / 4; BasicOpertations.ChangeEntityName("FACE", MateBase, (InsideDiameter / 2) + dim, 0, 0); DocumentManager.Save(TargetFolder, FileName, DocumentManager.sw_DocType.part); }
public static void AddComponent(Pipe pipe, AnglePart anglePart) { SldWorks swApp; ModelDoc2 swModel; AssemblyDoc swAssy; object components; object compNames; object transformationMatrix; object coorSysNames; swApp = SolidWorksSingleton.GetApplication(); swModel = (ModelDoc2)swApp.ActiveDoc; swAssy = (AssemblyDoc)swApp.ActiveDoc; string[] xcompnames = new string[2]; xcompnames[0] = pipe.TargetFolder + "\\" + pipe.FileName + ".sldprt"; xcompnames[1] = anglePart.TargetFolder + "\\" + anglePart.FileName + ".sldprt"; string[] xcoorsysnames = new string[2]; xcoorsysnames[0] = "Coordinate System1"; xcoorsysnames[1] = "Coordinate System1"; var tMatrix = new double[] { 0, 0, 1, 0, 1, 0, -1, 0, 0, pipe.Lenght, anglePart.YLenght, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; compNames = xcompnames; coorSysNames = xcoorsysnames; transformationMatrix = tMatrix; components = swAssy.AddComponents3(compNames, transformationMatrix, coorSysNames); swAssy = (AssemblyDoc)swApp.ActiveDoc; string comp = anglePart.FileName + "-1@" + anglePart.AssemblyName; swModel.Extension.SelectByID2(comp, "COMPONENT", 0, 0, 0, false, 0, null, 0); swAssy.FixComponent(); }
/// <summary> /// 导出天花DXF图纸和Cutlist /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void btnExportDxf_Click(object sender, EventArgs e) { if (subAssyExecList.Count == 0) { return; } btnExportDxf.Enabled = false; //计算时间 DateTime startTime = DateTime.Now; tspbStatus.Value = 0; tspbStatus.Step = 1; tspbStatus.Maximum = subAssyExecList.Count; //创建下料图文件夹,默认再D盘MyProjects目录下(先判断文件夹是否存在) string dxfPath = @"D:\MyProjects\" + cobODPNo.Text + @"\DXF-CUTLIST"; if (!Directory.Exists(dxfPath)) { Directory.CreateDirectory(dxfPath); } //以异步的方式开启SolidWorks程序并导图 try { tsslStatus.Text = "正在打开(/连接)SolidWorks程序..."; swApp = await SolidWorksSingleton.GetApplicationAsync(); //遍历execList,创建项目模型存放地址,判断模型类型,查询参数,执行SolidWorks foreach (var item in subAssyExecList) { tsslStatus.Text = "子装配体(" + item.SubAssyName + ")正在导图..."; //以异步的方式执行,让窗口可操作并且进度条更新 await exportCeilingDxfAsync(item, dxfPath, Program.ObjCurrentUser.UserId); tspbStatus.Value += 1; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { swApp.CommandInProgress = false;//及时关闭外部命令调用,否则影响SolidWorks的使用 } TimeSpan timeSpan = DateTime.Now - startTime; tsslStatus.Text = "导出DXF图完成,总共耗时:" + timeSpan.TotalSeconds + "秒"; tspbStatus.Value = subAssyExecList.Count; //btnSubAll_Click(null, null);//清除执行数据 foreach (var item in subAssyExecList) { subAssyWaitingList.Add(item); } subAssyExecList.Clear(); btnExportDxf.Enabled = true; }
public static void SimpleFillet(double radius) { SldWorks swApp; ModelDoc2 swModel; swApp = SolidWorksSingleton.GetApplication(); swModel = swApp.ActiveDoc; swModel.FeatureManager.FeatureFillet3((int)swFeatureFilletOptions_e.swFeatureFilletUniformRadius, radius, 0, 0, 0, 0, 0, null, null, null, null, null, null, null); }
public static void SimpleCut() { SldWorks swApp; ModelDoc2 swModel; Feature swFeature; swApp = SolidWorksSingleton.GetApplication(); swModel = swApp.ActiveDoc; swFeature = swModel.FeatureManager.FeatureCut3(true, false, false, (int)swEndConditions_e.swEndCondThroughAll, (int)swEndConditions_e.swEndCondBlind, 0, 0, false, false, false, false, 0, 0, false, false, false, false, false, true, true, false, false, false, (int)swEndConditions_e.swEndCondMidPlane, 0, true); }
/// <summary> /// 执行SolidWorks作图程序 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void btnExec_Click(object sender, EventArgs e) { if (execList.Count == 0) { return; } btnExec.Enabled = false; //计算时间 DateTime startTime = DateTime.Now; tspbStatus.Value = 0; tspbStatus.Step = 1; tspbStatus.Maximum = execList.Count; //创建项目文件夹,默认再D盘MyProjects目录下(先判断文件夹是否存在) string projectPath = @"D:\MyProjects\" + cobODPNo.Text; if (!Directory.Exists(projectPath)) { Directory.CreateDirectory(projectPath); } //以异步的方式开启SolidWorks程序并自动作图 try { tsslStatus.Text = "正在打开(/连接)SolidWorks程序..."; swApp = await SolidWorksSingleton.GetApplicationAsync(); //遍历execList,创建项目模型存放地址,判断模型类型,查询参数,执行SolidWorks foreach (var item in execList) { tsslStatus.Text = item.Item + "(" + item.Module + ")正在作图..."; //6.根据工厂提供的选择,执行具体的接口实现方式 //以异步的方式执行,让窗口可操作并且进度条更新 await AutoDrawingAsync(item, projectPath); tspbStatus.Value += 1; } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { swApp.CommandInProgress = false;//及时关闭外部命令调用,否则影响SolidWorks的使用 } TimeSpan timeSpan = DateTime.Now - startTime; tsslStatus.Text = "作图完成,总共耗时:" + timeSpan.TotalSeconds + "秒"; tspbStatus.Value = execList.Count; btnSubAll_Click(null, null);//清除执行数据 btnExec.Enabled = true; }
public static void CreateNewPartDoc() { SldWorks swApp; ModelDoc2 swModel; string defaultPartTemplate; swApp = SolidWorksSingleton.GetApplication(); defaultPartTemplate = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart); swApp.NewDocument(defaultPartTemplate, 0, 0, 0); }
public static void CreateAssemblyDoc(string filePath, string FileName) { SldWorks swApp; ModelDoc2 swModel; string defaultAssemblyTemplate; swApp = SolidWorksSingleton.GetApplication(); defaultAssemblyTemplate = swApp.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplateAssembly); swApp.NewDocument(defaultAssemblyTemplate, 0, 0, 0); swModel = (ModelDoc2)swApp.ActiveDoc; swModel.SaveAs3(filePath + "\\" + FileName + ".sldasm", (int)swSaveAsVersion_e.swSaveAsCurrentVersion, (int)swSaveAsOptions_e.swSaveAsOptions_CopyAndOpen); }
public static void ChangeEntityName(string Etype, string Ename, double CoorX, double CoorY, double CoorZ) { SldWorks swApp; ModelDoc2 swModel; SelectionMgr swSelMgr; Face2 swFace; PartDoc swPart; swApp = SolidWorksSingleton.GetApplication(); swModel = swApp.ActiveDoc; swModel.Extension.SelectByID2("", Etype, CoorX, CoorY, CoorZ, false, 0, null, 0); swSelMgr = swModel.SelectionManager; swFace = swSelMgr.GetSelectedObject6(1, -1); swPart = swApp.ActiveDoc; swPart.SetEntityName(swFace, Ename); }
public static void AddDistanceMate(string Comp1, string MateFace1, string Comp2, string MateFace2, double dimension) { SldWorks swApp; ModelDoc2 swModel; PartDoc swPart; AssemblyDoc swAssy; Mate2 mate; Component2 swComponent; Entity swEntity; Entity swFace1; Entity swFace2; bool bRet; int errorCode1; swApp = SolidWorksSingleton.GetApplication(); swAssy = (AssemblyDoc)swApp.ActiveDoc; swComponent = swAssy.GetComponentByName(Comp1 + "-1"); swModel = swComponent.GetModelDoc2(); swPart = (PartDoc)swModel; swEntity = swPart.GetEntityByName(MateFace1, (int)swSelectType_e.swSelFACES); swFace1 = swComponent.GetCorrespondingEntity(swEntity); swComponent = swAssy.GetComponentByName(Comp2 + "-1"); swModel = swComponent.GetModelDoc2(); swPart = (PartDoc)swModel; swEntity = swPart.GetEntityByName(MateFace2, (int)swSelectType_e.swSelFACES); swFace2 = swComponent.GetCorrespondingEntity(swEntity); bRet = swFace1.Select4(false, null); bRet = swFace2.Select4(true, null); mate = swAssy.AddMate3((int)swMateType_e.swMateDISTANCE, (int)swMateAlign_e.swMateAlignALIGNED, false, dimension, dimension, dimension, 0, 0, 0, 0, 0, false, out errorCode1); swModel.ForceRebuild3(false); }
/// <summary> /// 导出天花烟罩发货清单按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void btnCeilingPackingList_Click(object sender, EventArgs e) { //计算时间 DateTime startTime = DateTime.Now; tspbStatus.Value = 0; tspbStatus.Step = 1; tspbStatus.Maximum = 2; btnCeilingPackingList.Enabled = false; List <CeilingAccessory> ceilingAccessoriesList = null; List <CeilingAccessory> ceilingPackingList = null; if (dgvCeilingPackingList.RowCount > 0) { DialogResult result = MessageBox.Show("发货清单有内容,标准配件将不会重复添加,如果需要添加标准件请手动添加或者删除本数据后再生成发货清单,继续请按YES,不生成发货清单请按NO?", "生成发货清单询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question); if (result == DialogResult.No) { btnCeilingPackingList.Enabled = true; return; } } else { //【1】查询项目的所有配件部分 if (txtTypeName.Text == "日本项目") { ceilingAccessoriesList = objCeilingAccessoryService.GetCeilingAccessoriesForJapan(); } else { ceilingAccessoriesList = objCeilingAccessoryService.GetCeilingAccessoriesForNotJapan(); } foreach (var item in ceilingAccessoriesList) { item.ProjectId = objProject.ProjectId; item.UserId = Program.ObjCurrentUser.UserId; item.Location = objDrawingPlanService.GetDrawingPlanByProjectId(objProject.ProjectId.ToString())[0].Item; } //将查询到的标准配件统一提交到SQL服务器 objCeilingAccessoryService.ImportCeilingPackingListByTran(ceilingAccessoriesList); } tspbStatus.Value = 1; //【2】打开装配体,获得所有零件名,查询配件和数量导入SQL //预先将装配体的关键字写入到零件中 //以异步的方式开启SolidWorks程序并导图 try { tsslStatus.Text = "正在打开(/连接)SolidWorks程序..."; swApp = await SolidWorksSingleton.GetApplicationAsync(); //执行SolidWorks,打开总装并执行导出发货清单程序 tsslStatus.Text = "正在导出发货清单..."; //以异步的方式执行,让窗口可操作并且进度条更新 await exportCeilingPackingListAsync(txtMainAssyPath.Text, objProject); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { swApp.CommandInProgress = false;//及时关闭外部命令调用,否则影响SolidWorks的使用 } //更新发货清单显示 dgvCeilingPackingList.DataSource = objCeilingAccessoryService.GetCeilingPackingListByProjectId(objProject.ProjectId.ToString()); //MessageBox.Show("发货清单导出成功!", "提示信息"); TimeSpan timeSpan = DateTime.Now - startTime; tsslStatus.Text = "导出发货清单完成,总共耗时:" + timeSpan.TotalSeconds + "秒"; tspbStatus.Value = 2; btnCeilingPackingList.Enabled = true; }
public override void CreatePart() { swApp = SolidWorksSingleton.GetApplication(); DocumentManager.CreateNewPartDoc(); swModel = (ModelDoc2)swApp.ActiveDoc; swFeature = swModel.FeatureByPositionReverse(3); swFeature.Name = "Front"; swModel.Extension.SelectByID2("Front", "PLANE", 0, 0, 0, false, 0, null, 0); swModel.InsertSketch2(true); swModel.CreateLine2(0, 0, 0, XLenght, 0, 0); //swModel.AddDimension2(0,0,0); swModel.CreateLine2(0, 0, 0, 0, YLenght, 0); //swModel.AddDimension2(0, 0, 0); //int markHorizontal = 2; //int markVertical = 4; //swModel.Extension.SelectByID2("Point1@Origin", "EXTSKETCHSEGMENT",0,0,0,false, markHorizontal|markVertical,null,0); object datumDisp = "Point1@Origin"; swModel.SketchManager.FullyDefineSketch(true, true, (int)swSketchFullyDefineRelationType_e.swSketchFullyDefineRelationType_Vertical | (int)swSketchFullyDefineRelationType_e.swSketchFullyDefineRelationType_Horizontal, true, (int)swAutodimScheme_e.swAutodimSchemeBaseline, datumDisp, (int)swAutodimScheme_e.swAutodimSchemeBaseline, datumDisp, (int)swAutodimHorizontalPlacement_e.swAutodimHorizontalPlacementBelow, (int)swAutodimVerticalPlacement_e.swAutodimVerticalPlacementLeft); swModel.InsertSketch2(true); swFeature = swModel.FeatureByPositionReverse(0); swFeature.Name = "Sketch1"; swModel.Extension.SelectByID2("Sketch1", "SKETCH", 0, 0, 0, false, 0, null, 0); swModel.FeatureManager.InsertSheetMetalBaseFlange2(Thickness, false, Thickness, Width, 0, true, 0, 0, 0, null, false, 0, 0, 0, 0, false, false, false, false); BasicOpertations.ChangeEntityName("FACE", MateRef1, Thickness, YLenght / 2, -Width / 2); swModel.Extension.SelectByID2("", "FACE", XLenght / 2, Thickness, -Width / 2, false, 0, null, 0); swModel.InsertSketch2(true); swModel.CreateCircleByRadius2(XLenght - X1, X2, 0, BoltHoles); swModel.CreateCircleByRadius2(XLenght - X1, Width - X2, 0, BoltHoles); swModel.InsertSketch2(true); swFeature = swModel.FeatureByPositionReverse(0); swFeature.Name = "BoltHoles"; swModel.Extension.SelectByID2("BoltHoles", "SKETCH", 0, 0, 0, false, 0, null, 0); BasicOpertations.SimpleCut(); swPart = (PartDoc)swApp.ActiveDoc; swEntity = swPart.GetEntityByName(MateRef1, (int)swSelectType_e.swSelFACES); swEntity.Select4(false, null); swModel.InsertSketch2(true); swModel.CreateCircleByRadius2(Width / 2, YLenght - (PipeHole * 2), 0, PipeHole); swModel.AddDiameterDimension(0, 0, 0); swModel.InsertSketch2(true); BasicOpertations.SimpleCut(); BasicOpertations.ChangeEntityName("FACE", MateRefHole, Thickness / 2, YLenght - PipeHole, -Width / 2); swModel = (ModelDoc2)swApp.ActiveDoc; swModel.Extension.SelectByID2("", "EDGE", XLenght, Thickness / 2, 0, false, 0, null, 0); swModel.Extension.SelectByID2("", "EDGE", XLenght, Thickness / 2, -Width, true, 0, null, 0); BasicOpertations.SimpleFillet(Rad2); swModel.Extension.SelectByID2("", "EDGE", Thickness / 2, YLenght, 0, false, 0, null, 0); swModel.Extension.SelectByID2("", "EDGE", Thickness / 2, YLenght, -Width, true, 0, null, 0); BasicOpertations.SimpleFillet(Rad1); swModel.ClearSelection2(true); swModel.ViewZoomtofit2(); DocumentManager.Save(TargetFolder, FileName, DocumentManager.sw_DocType.part); }