/// <summary> /// 初始化CATIA环境并获取信息到全局变量 /// </summary> /// <returns></returns> private bool InitCatEnv() { try { CatApplication = (INFITF.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Catia.Application"); } catch (Exception) { this.WindowState = FormWindowState.Normal; this.StartPosition = FormStartPosition.CenterScreen; MessageBox.Show("未检测到打开的CATIA!,请重新运行CATIA!"); return(false); //throw; } CatApplication.set_Caption("正在运行瑞祥快速建模工具!"); // 获取当前活动ProductDocument try { CatDocument = (ProductDocument)CatApplication.ActiveDocument; } catch (Exception) { CatDocument = (ProductDocument)CatApplication.Documents.Add("Product"); try { CatDocument.Product.set_PartNumber("RxProduct"); } catch (Exception) { MessageBox.Show("未检测到活动Product,正在为您创建,请手动辅助完成!"); return(false); } //MessageBox.Show("未检测到活动Product,已自动为您创建对象!"); } // 添加一个新零件 string Name = "RXFastDesignTool"; try { PartID = ((PartDocument)CatApplication.Documents.Item(Name + ".CATPart")).Part; } catch (Exception) { try { CatDocument.Product.Products.AddNewComponent("Part", Name); } catch (Exception) { return(false); // throw; } PartID = ((PartDocument)CatApplication.Documents.Item(Name + ".CATPart")).Part; } return(true); }
/// <summary> /// 初始化CATIA环境并获取信息到全局变量 /// </summary> /// <param name="CatApplication">CATIA程序框架</param> /// <param name="CatDocument">CATIA 活动文档</param> /// <param name="PartID">CATIA 零件目标</param> /// <param name="form">当前活动窗口</param> /// <returns></returns> public bool InitCatEnv(ref INFITF.Application CatApplication, ref ProductDocument CatDocument, ref Part PartID, Form form, bool isCatia = true, Label mymessage = null) { Process[] AllProcess = Process.GetProcessesByName("CNEXT"); //Only When Get CATIA Process form.TopMost = false; #region MyRegion // Process[] AllProcess = Process.GetProcessesByName("Delmia"); //if (AllProcess.Length > 1) //{ // try // { // WAPI wAPI = new WAPI(); // AllProcess[0].WaitForInputIdle(); // string MainWintitle = AllProcess[0].MainWindowTitle; // int MainPID = AllProcess[0].Id; // IntPtr intPtr = AllProcess[0].MainWindowHandle; // IntPtr intPtr2 = WAPI.FindWindow(null, MainWintitle); // // CatApplication = (INFITF.Application)AllProcess[0].CreateObjRef(INFITF.Application); // var application = Marshal.GetObjectForNativeVariant(intPtr); // CatApplication = (INFITF.Application)application; // string CapName1 = CatApplication.get_Caption(); // CatApplication.set_Caption("正在运行锐锋快速建模工具!锐锋快捷设计中心 BY_安徽锐锋科技【工厂仿真组】叶朝成"); // AllProcess[1].WaitForInputIdle(); // IntPtr ProgHandle = AllProcess[1].Handle; // //CatApplication = (INFITF.Application)Marshal.GetObjectForNativeVariant(ProgId); // string CapName2 = CatApplication.get_Caption(); // //MessageBox.Show("当前打开超过1个CATIA,可能操控的CATIA非您需要的对象,请核实!"); // //GetObj(1, "Catia.Application"); // //GetObj(2, "Delmia.Application"); // // IntPtr Ptr = AllProcess[2].MainWindowHandle; // // string Pname = AllProcess[2].MainWindowTitle; // // int progid= AllProcess[2].; // // object Pobj = Marshal.GetActiveObject("Delmia.Application"); // //// object Tobj = Marshal.GetObjectForIUnknown(ptr2); // // object Pobj0 = Marshal.GetActiveObject(progid.ToString()); // // DSApplication = (INFITF.Application)Pobj; // // String tn = DSApplication.get_Caption(); // } // catch (Exception) // { // throw; // } //} #endregion try { if (isCatia) { CatApplication = (INFITF.Application)Marshal.GetActiveObject("Catia.Application"); if (mymessage != null) { mymessage.Text = "已建立CATIA通信!"; } } else { CatApplication = (INFITF.Application)Marshal.GetActiveObject("Delmia.Application"); if (mymessage != null) { mymessage.Text = "已建立Delmia通信!"; } } } catch (Exception) { form.WindowState = FormWindowState.Normal; form.StartPosition = FormStartPosition.CenterScreen; //MessageBox.Show("未检测到打开的CATIA!,请重新运行CATIA!"); if (mymessage != null) { mymessage.Text = "通信建立失败,所有功能将会失效请重新尝试或重新启动该软件!"; } return(false); //throw; } try { CatApplication.set_Caption("正在运行锐锋快速建模工具!锐锋快捷设计中心 BY_安徽锐锋科技【工厂仿真组】叶朝成"); } catch (Exception) { throw; } // 获取当前活动ProductDocument try { CatDocument = (ProductDocument)CatApplication.ActiveDocument; } catch (Exception) { CatDocument = (ProductDocument)CatApplication.Documents.Add("Product"); try { CatDocument.Product.set_PartNumber("RxProduct"); } catch (Exception) { MessageBox.Show("未检测到活动Product,正在为您创建,请手动辅助完成!"); return(false); } //MessageBox.Show("未检测到活动Product,已自动为您创建对象!"); } try { CatDocument.Product.ApplyWorkMode(CatWorkModeType.DESIGN_MODE); } catch (Exception) { Console.WriteLine("Change Design Model Faild!"); } Selection selection = CatDocument.Selection; selection.Clear(); CreatWorkPart(CatApplication, CatDocument, ref PartID); //Get New Part For Work return(true); }
private bool GetSelect(bool InitFrame) { this.WindowState = FormWindowState.Minimized; try { CatApplication = (INFITF.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Catia.Application"); } catch (Exception) { this.WindowState = FormWindowState.Normal; this.StartPosition = FormStartPosition.CenterScreen; MessageBox.Show("未检测到打开的CATIA!,请重新运行CATIA!"); return(false); //throw; } CatApplication.set_Caption("正在运行瑞祥快速建模工具!"); // 获取当前活动ProductDocument try { CatDocument = (ProductDocument)CatApplication.ActiveDocument; } catch (Exception) { CatDocument = (ProductDocument)CatApplication.Documents.Add("Product"); try { CatDocument.Product.set_PartNumber("RxProduct"); } catch (Exception) { MessageBox.Show("未检测到活动Product,正在为您创建,请手动辅助完成!"); return(false); } //MessageBox.Show("未检测到活动Product,已自动为您创建对象!"); } // 添加一个新零件 string Name = "RXFastDesignTool"; try { PartID = ((PartDocument)CatApplication.Documents.Item(Name + ".CATPart")).Part; } catch (Exception) { try { CatDocument.Product.Products.AddNewComponent("Part", Name); } catch (Exception) { return(false); // throw; } PartID = ((PartDocument)CatApplication.Documents.Item(Name + ".CATPart")).Part; OriginElements Tpart = PartID.OriginElements; AnyObject dxy = Tpart.PlaneXY; AnyObject dyz = Tpart.PlaneYZ; AnyObject dzx = Tpart.PlaneZX; Selection SelectT = CatDocument.Selection; VisPropertySet VP = SelectT.VisProperties; SelectT.Add(dxy); SelectT.Add(dyz); SelectT.Add(dzx); VP = (VisPropertySet)VP.Parent; VP.SetShow(CatVisPropertyShow.catVisPropertyNoShowAttr); SelectT.Clear(); } try { CatDocument.Product.ApplyWorkMode(CatWorkModeType.DESIGN_MODE); } catch (Exception) { Console.WriteLine("Change Design Model Faild!"); } this.WindowState = FormWindowState.Normal; this.StartPosition = FormStartPosition.CenterScreen; return(true); }
/// <summary> /// 连接CATIA COM 并获得选择集 /// </summary> /// <returns></returns> private Selection GetSelect() { this.WindowState = FormWindowState.Minimized; try { CatApplication = (INFITF.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Catia.Application"); } catch (Exception) { this.WindowState = FormWindowState.Normal; this.StartPosition = FormStartPosition.CenterScreen; MessageBox.Show("未检测到打开的CATIA!,请重新运行CATIA!"); return(null); } CatApplication.set_Caption("正在运行瑞祥快速建模工具!"); // 获取当前活动ProductDocument try { CatDocument = (ProductDocument)CatApplication.ActiveDocument; } catch (Exception) { CatDocument = (ProductDocument)CatApplication.Documents.Add("Product"); try { CatDocument.Product.set_PartNumber("RxProduct"); } catch (Exception) { MessageBox.Show("未检测到活动Product,正在为您创建,请手动辅助完成!"); return(null); } //MessageBox.Show("未检测到活动Product,已自动为您创建对象!"); } // 添加一个新零件 string Name = "RXFastDesignTool"; try { //PartID = ((PartDocument)CatApplication.Documents.Item(Name + ".CATPart")).Part; //PartID = ((PartDocument)CatApplication.Documents.Item(Name)).Part; Selection FindPart = CatApplication.ActiveDocument.Selection; FindPart.Search("Name=RXFastDesignTool,all"); if (FindPart.Count2 > 0) { PartID = (Part)FindPart.Item2(1).Value; //仅拾取带个并对第一个进行操作 } else { try { CatDocument.Product.Products.AddNewComponent("Part", Name); } catch (Exception) { return(null); // throw; } PartID = ((PartDocument)CatApplication.Documents.Item(Name + ".CATPart")).Part; OriginElements Tpart = PartID.OriginElements; AnyObject dxy = Tpart.PlaneXY; AnyObject dyz = Tpart.PlaneYZ; AnyObject dzx = Tpart.PlaneZX; Selection SelectT = CatDocument.Selection; VisPropertySet VP = SelectT.VisProperties; SelectT.Add(dxy); SelectT.Add(dyz); SelectT.Add(dzx); VP = (VisPropertySet)VP.Parent; VP.SetShow(CatVisPropertyShow.catVisPropertyNoShowAttr); SelectT.Clear(); } } catch (Exception) { return(null); } try { CatDocument.Product.ApplyWorkMode(CatWorkModeType.DESIGN_MODE); } catch (Exception) { Console.WriteLine("Change Design Model Faild!"); } Selection SelectArc = CatDocument.Selection; SelectArc.Clear(); var Result = SelectArc.SelectElement3(InputObjectType(), "请选择曲面", true, CATMultiSelectionMode.CATMultiSelTriggWhenSelPerf, false); if (Result == "Cancel") { return(null); } if (SelectArc.Count < 1) { MessageBox.Show("请先选择对象后再点此命令!"); return(null); } this.WindowState = FormWindowState.Normal; this.StartPosition = FormStartPosition.CenterScreen; return(SelectArc); }