public void SaveCgrToProject(string docName) { Documents docs = myCATIA.Documents; ProductDocument prodDoc = (ProductDocument)docs.Add("Product"); Product prod = prodDoc.Product; Products prods = prod.Products; Array arr = Array.CreateInstance(typeof(object), 1); arr.SetValue(docName + ".cgr", 0); prods.AddComponentsFromFiles(arr, "All"); string dir = docName.Substring(0, docName.LastIndexOf('\\') + 1); string name = docName.Substring(docName.LastIndexOf('\\') + 1); docName = dir + RemoveDiacritics(name); prodDoc = (ProductDocument)myCATIA.ActiveDocument; if (System.IO.File.Exists(docName + ".CATProduct")) { System.IO.File.Delete(docName + ".CATProduct"); } try { prodDoc.SaveAs(docName + ".CATProduct"); } catch (Exception) { Console.WriteLine("CGR document '" + docName + "' could not be saved as CATProduct."); } if (prodDoc != null) { prodDoc.Close(); } }
static void Main(string[] args) { INFITF.Application catia; try { catia = (INFITF.Application)Marshal.GetActiveObject("CATIA.Application"); } catch (Exception) { catia = (INFITF.Application)Activator.CreateInstance(Type.GetTypeFromProgID("CATIA.Application")); } catia.Visible = true; ProductDocument pdctDoc = (ProductDocument)catia.Documents.Add("Product"); Product pdct = pdctDoc.Product; Products pdcts = pdct.Products; object[] tpath = { @"C:\Users\517-11\Desktop\saori\Automation\Catia-v5-Automation\Titanicp.40(Assy)\Assembly\Hull.CATPart" }; pdcts.AddComponentsFromFiles(tpath, "*"); Product pdct1 = pdcts.AddNewProduct("ass1"); //Products ass1 = pdct1.Products; object[] tpath1 = { @"C:\Users\517-11\Desktop\saori\Automation\Catia-v5-Automation\Titanicp.40(Assy)\Assembly\Castle.CATPart", @"C:\Users\517-11\Desktop\saori\Automation\Catia-v5-Automation\Titanicp.40(Assy)\Assembly\Funnel.CATPart" }; pdct1.Products.AddComponentsFromFiles(tpath1, "*"); //ass1.AddComponentsFromFiles(tpath1,"*"); Product pdctRef = pdct1.ReferenceProduct; object[] aDistance = { 1, 0, 0, 0, 1, 0, 0, 0, 1, 60, 0, 0 }; //x-vector x,y,z / y(x,y,z) / z(x,y,z) / 이동x,y,z Product pdct2 = pdcts.AddComponent(pdctRef); pdct2.Move.Apply(aDistance); aDistance[9] = 120; Product pdct3 = pdcts.AddComponent(pdctRef); pdct3.Move.Apply(aDistance); pdct.ExtractBOM(CatFileType.catFileTypeText, @"C:\Users\517-11\Desktop\saori\Automation\Catia-v5-Automation\Titanicp.40(Assy)\Bom.txt"); pdct.ExtractBOM(CatFileType.catFileTypeHTML, @"C:\Users\517-11\Desktop\saori\Automation\Catia-v5-Automation\Titanicp.40(Assy)\BOM.html"); }
private void InsGun_Click(object sender, EventArgs e) { this.TopMost = false; this.WindowState = FormWindowState.Minimized; progressBar.Value = 0; DataGrid.AllowUserToAddRows = false; if (DataGrid.RowCount < 1) { MessageBox.Show("未检测到任何数据请先导入EXCEL数据再执行该操作!"); return; } if (CatDocument == null) { CATIA_Class.InitCatEnv(ref CatApplication, ref CatDocument, ref PartID, this, ConCatia.Checked, myMessage); } Product Cproduct; try { Cproduct = CatDocument.Product; } catch (Exception) { CATIA_Class.InitCatEnv(ref CatApplication, ref CatDocument, ref PartID, this, ConCatia.Checked, myMessage); Cproduct = CatDocument.Product; } Products Cps = Cproduct.Products; string GunPath = string.Empty; this.TopMost = true; object[] oPositionMatrix = new object[12]; object[] oPositionSafeMatrix = new object[12] { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 }; double oRx, oRy, oRz; progressBar.Maximum = DataGrid.RowCount; progressBar.Step = 1; for (int i = 0; i < DataGrid.RowCount; i++) { string TName; string NewName = DataGrid.Rows[i].Cells[1].Value.ToString(); if (NewName.Length > 8) { string tn = NewName.Substring(0, 8); } if (NewName.Length > 8 && skipViaPoint.Checked && (NewName.Substring(0, 8) == "ViaPoint" || NewName.Substring(0, 3) == "LHP")) { goto Skip; } try { TName = DataGrid.Rows[i].Cells[1].Value.ToString(); //读取选择的曲面名称 String GunName = null; if (xlsFileName != null) { GunName = xlsFileName; } else { GunName = DataGrid.Rows[i].Cells[1].Value.ToString() + "ASS"; } if (TName == "ChangeGun") { GunPath = GetTargetByDs(CatApplication); if (string.IsNullOrEmpty(GunPath)) { return; //终止焊枪导入 } else { Cproduct = AddProduct(CatDocument.Product.Products, GunName); Cps = Cproduct.Products; } continue; } else { if (string.IsNullOrEmpty(GunPath)) //常规模式下读取焊枪 一次即可 { Cproduct = AddProduct(CatDocument.Product.Products, GunName); Cps = Cproduct.Products; try { GunPath = GetTargetByDs(CatApplication); if (GunPath == null) { return; } } catch (Exception e1) { MessageBox.Show("Err: " + e1.Message); } } } } catch (Exception) { continue; } double oPi = 3.1415926536; if (ARCChange.Checked) //弧度值 { oRx = Convert.ToDouble(DataGrid.Rows[i].Cells[7].Value.ToString()); //DELMIA Tag点导出集合该坐标为Z oRy = Convert.ToDouble(DataGrid.Rows[i].Cells[6].Value.ToString()); //DELMIA Tag点导出集合该坐标为Y oRz = Convert.ToDouble(DataGrid.Rows[i].Cells[5].Value.ToString()); //DELMIA Tag点导出集合该坐标为X } else { oRx = Convert.ToDouble(DataGrid.Rows[i].Cells[5].Value.ToString()) * oPi / 180; //转换弧度进行运算 oRy = Convert.ToDouble(DataGrid.Rows[i].Cells[6].Value.ToString()) * oPi / 180; //转换弧度进行运算 oRz = Convert.ToDouble(DataGrid.Rows[i].Cells[7].Value.ToString()) * oPi / 180; //转换弧度进行运算 } oPositionMatrix[0] = Math.Round(Math.Cos(oRy) * Math.Cos(oRz), 5); oPositionMatrix[1] = Math.Round(Math.Cos(oRy) * Math.Sin(oRz), 5); oPositionMatrix[2] = Math.Round(-Math.Sin(oRy), 5); oPositionMatrix[3] = Math.Round((Math.Sin(oRx) * Math.Sin(oRy) * Math.Cos(oRz)) - (Math.Cos(oRx) * Math.Sin(oRz)), 5); oPositionMatrix[4] = Math.Round((Math.Sin(oRx) * Math.Sin(oRy) * Math.Sin(oRz)) + (Math.Cos(oRx) * Math.Cos(oRz)), 5); oPositionMatrix[5] = Math.Round((Math.Sin(oRx) * Math.Cos(oRy)), 5); oPositionMatrix[6] = Math.Round((Math.Cos(oRx) * Math.Sin(oRy) * Math.Cos(oRz)) + (Math.Sin(oRx) * Math.Sin(oRz)), 5); oPositionMatrix[7] = Math.Round((Math.Cos(oRx) * Math.Sin(oRy) * Math.Sin(oRz)) - (Math.Sin(oRx) * Math.Cos(oRz)), 5); oPositionMatrix[8] = Math.Round(Math.Cos(oRx) * Math.Cos(oRy), 5); oPositionMatrix[9] = Convert.ToDouble(DataGrid.Rows[i].Cells[2].Value.ToString()); oPositionMatrix[10] = Convert.ToDouble(DataGrid.Rows[i].Cells[3].Value.ToString()); oPositionMatrix[11] = Convert.ToDouble(DataGrid.Rows[i].Cells[4].Value.ToString()); //oPositionMatrix =new object[12]{ 1,0,0,0,0.707,0.707,0,-0.707,0.707,10,20,30};//测试 object[] arrayOfVariantOfBSTR1 = new object[1] { GunPath }; Cps.AddComponentsFromFiles(arrayOfVariantOfBSTR1, "All"); Cps.Item(Cps.Count).Position.SetComponents(oPositionMatrix);// 相对世界坐标设定位置 if (xlsFileName != null) { NewName = xlsFileName + "_" + NewName; } try { Cps.Item(Cps.Count).set_PartNumber(NewName); Cps.Item(Cps.Count).set_Name(NewName); } catch (Exception) { //throw; } Skip : progressBar.PerformStep(); } ShowCenter(); progressBar.Maximum = 100; progressBar.Value = 100; }
private void InsGun_Click(object sender, EventArgs e) { this.TopMost = false; this.WindowState = FormWindowState.Minimized; if (DataGrid.RowCount < 1) { MessageBox.Show("未检测到任何数据请先导入EXCEL数据再执行该操作!"); return; } if (CatDocument == null) { InitCatEnv(); } Product Cproduct; try { Cproduct = CatDocument.Product; } catch (Exception) { InitCatEnv(); Cproduct = CatDocument.Product; } Products Cps = Cproduct.Products; string GunPath = string.Empty; this.TopMost = true; object[] oPositionMatrix = new object[12]; object[] oPositionSafeMatrix = new object[12] { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1 }; double oRx, oRy, oRz; for (int i = 0; i < DataGrid.RowCount; i++) { string TName; try { TName = DataGrid.Rows[i].Cells[1].Value.ToString(); //读取选择的曲面名称 String GunName = DataGrid.Rows[i + 1].Cells[1].Value.ToString(); if (TName == "ChangeGun") { A : GunPath = Cps.Application.FileSelectionBox("请选择焊枪", "*.cgr;*.wrl;*.CATPart", 0); if (string.IsNullOrEmpty(GunPath)) { var Result = MessageBox.Show("未选择任何焊枪,是否重新选择?(Y/N/C)", "请做出选择", MessageBoxButtons.YesNoCancel); switch (Result) { case DialogResult.None: break; case DialogResult.OK: break; case DialogResult.Cancel: this.TopMost = true; this.WindowState = FormWindowState.Normal; this.StartPosition = FormStartPosition.CenterScreen; return; //终止焊枪导入 case DialogResult.Abort: break; case DialogResult.Retry: break; case DialogResult.Ignore: break; case DialogResult.Yes: goto A; //回到插入焊枪阶段 case DialogResult.No: this.TopMost = true; this.WindowState = FormWindowState.Normal; this.StartPosition = FormStartPosition.CenterScreen; return; //终止焊枪导入 default: break; } } else { Cproduct = AddProduct(CatDocument.Product.Products, GunName); Cps = Cproduct.Products; } continue; } else { if (string.IsNullOrEmpty(GunPath)) //常规模式下读取焊枪 一次即可 { Cproduct = AddProduct(CatDocument.Product.Products, GunName); Cps = Cproduct.Products; GunPath = Cps.Application.FileSelectionBox("请选择焊枪", "*.cgr;*.wrl;*.CATPart", 0); } } } catch (Exception) { continue; } double oPi = 3.1415926536; if (ARCChange.Checked) //弧度值 { oRx = Convert.ToDouble(DataGrid.Rows[i].Cells[7].Value.ToString()); //DELMIA Tag点导出集合该坐标为Z oRy = Convert.ToDouble(DataGrid.Rows[i].Cells[6].Value.ToString()); //DELMIA Tag点导出集合该坐标为Y oRz = Convert.ToDouble(DataGrid.Rows[i].Cells[5].Value.ToString()); //DELMIA Tag点导出集合该坐标为X } else { oRx = Convert.ToDouble(DataGrid.Rows[i].Cells[5].Value.ToString()) * oPi / 180; //转换弧度进行运算 oRy = Convert.ToDouble(DataGrid.Rows[i].Cells[6].Value.ToString()) * oPi / 180; //转换弧度进行运算 oRz = Convert.ToDouble(DataGrid.Rows[i].Cells[7].Value.ToString()) * oPi / 180; //转换弧度进行运算 } oPositionMatrix[0] = Math.Round(Math.Cos(oRy) * Math.Cos(oRz), 5); oPositionMatrix[1] = Math.Round(Math.Cos(oRy) * Math.Sin(oRz), 5); oPositionMatrix[2] = Math.Round(-Math.Sin(oRy), 5); oPositionMatrix[3] = Math.Round((Math.Sin(oRx) * Math.Sin(oRy) * Math.Cos(oRz)) - (Math.Cos(oRx) * Math.Sin(oRz)), 5); oPositionMatrix[4] = Math.Round((Math.Sin(oRx) * Math.Sin(oRy) * Math.Sin(oRz)) + (Math.Cos(oRx) * Math.Cos(oRz)), 5); oPositionMatrix[5] = Math.Round((Math.Sin(oRx) * Math.Cos(oRy)), 5); oPositionMatrix[6] = Math.Round((Math.Cos(oRx) * Math.Sin(oRy) * Math.Cos(oRz)) + (Math.Sin(oRx) * Math.Sin(oRz)), 5); oPositionMatrix[7] = Math.Round((Math.Cos(oRx) * Math.Sin(oRy) * Math.Sin(oRz)) - (Math.Sin(oRx) * Math.Cos(oRz)), 5); oPositionMatrix[8] = Math.Round(Math.Cos(oRx) * Math.Cos(oRy), 5); oPositionMatrix[9] = Convert.ToDouble(DataGrid.Rows[i].Cells[2].Value.ToString()); oPositionMatrix[10] = Convert.ToDouble(DataGrid.Rows[i].Cells[3].Value.ToString()); oPositionMatrix[11] = Convert.ToDouble(DataGrid.Rows[i].Cells[4].Value.ToString()); //oPositionMatrix =new object[12]{ 1,0,0,0,0.707,0.707,0,-0.707,0.707,10,20,30};//测试 object[] arrayOfVariantOfBSTR1 = new object[1] { GunPath }; Cps.AddComponentsFromFiles(arrayOfVariantOfBSTR1, "All"); Cps.Item(Cps.Count).Position.SetComponents(oPositionMatrix);// 相对世界坐标设定位置 string NewName = DataGrid.Rows[i].Cells[1].Value.ToString(); Cps.Item(Cps.Count).set_PartNumber(NewName); } ShowCenter(); }