UG二次开发装配篇 添加/拖动/删除组件方法的实现
我們在UG裝配的過程中,經(jīng)常會(huì)遇到需要調(diào)整組件目錄位置,在軟件設(shè)計(jì)過程中可以通過在目錄樹里面拖動(dòng)組件來完成。
那么,如果要用程序?qū)崿F(xiàn)組件的移動(dòng)/拖動(dòng),我們要怎么做呢?
本節(jié)就完成了添加/拖動(dòng)/刪除組件方法的實(shí)現(xiàn),先看效果圖:
根節(jié)點(diǎn)test下,有SHCS_01、SHCS_02、SHCS_03、SHCS_04這四個(gè)組件。
下面分別給出了添加組件、移動(dòng)組件和刪除組件的方法。
一、添加組件
1、實(shí)現(xiàn)方法
/// <summary> /// 添加組件 /// </summary> /// <param name="templatePrt">模板路徑</param> /// <param name="basePoint">中心點(diǎn)坐標(biāo)位置</param> /// <param name="orientation">矢量方向</param> /// <param name="expModel">表達(dá)式集</param> public static void AddComponent(string templatePrt, Point3d basePoint, Matrix3x3 orientation, ExpressionModel expModel) {theUFSession = UFSession.GetUFSession();theSession = Session.GetSession();displayPart = theSession.Parts.Display;workPart = theSession.Parts.Work;componentNameList = new List<string>();BasePart basePart1;PartLoadStatus partLoadStatus1;step1:string fileName = "";string newfile = GetNewFile(templatePrt, out fileName); //先拷貝一個(gè)備份try{basePart1 = theSession.Parts.OpenBase(newfile, out partLoadStatus1);}catch (Exception){componentNameList.Add(fileName);goto step1;}partLoadStatus1.Dispose();#region 修正表達(dá)式ExpressionCollection expressionCollection = basePart1.Expressions;EventHelper.UpdateExpression(expressionCollection, expModel);#endregion#region 添加屬性basePart1.SetAttribute("模具編號(hào)", "", Update.Option.Now);basePart1.SetAttribute("材料標(biāo)準(zhǔn)", "", Update.Option.Now);basePart1.SetAttribute("塑膠材料", "", Update.Option.Now);basePart1.SetAttribute("縮水率", "", Update.Option.Now);basePart1.SetAttribute("穴數(shù)", "", Update.Option.Now);basePart1.SetAttribute("客戶", "", Update.Option.Now);basePart1.SetAttribute("項(xiàng)目編號(hào)", "", Update.Option.Now);basePart1.SetAttribute("產(chǎn)品名稱", "", Update.Option.Now);basePart1.SetAttribute("產(chǎn)品編號(hào)", "", Update.Option.Now);basePart1.SetAttribute("設(shè)計(jì)", "", Update.Option.Now);#endregionPartLoadStatus partLoadStatus3;NXOpen.Assemblies.Component component1;component1 = workPart.ComponentAssembly.AddComponent(newfile, "model", fileName, basePoint, orientation, -1, out partLoadStatus3, true); }public static string GetNewFile(string fullFileName, out string fileName) {fileName = "";string newFullFileName = "";displayPart = theSession.Parts.Display;FileInfo file = new FileInfo(fullFileName);List<Component> allComponents = new List<Component>();List<ComponentModel> componentList = new List<ComponentModel>();Component root = displayPart.ComponentAssembly.RootComponent;if (root != null){GetAllComponents(displayPart.ComponentAssembly.RootComponent, allComponents, componentList);}foreach (ComponentModel model in componentList){if (!componentNameList.Contains(model.instanceName)){componentNameList.Add(model.instanceName.ToLower());}}for (int i = 1; i < 100; i++){newFullFileName = AppDomain.CurrentDomain.BaseDirectory.ToString() + "temp\\" + GetCompantName(fullFileName) + "_0" + i.ToString() + ".prt";fileName = GetCompantName(fullFileName) + "_0" + i.ToString();FileInfo fi = new FileInfo(newFullFileName);if (!fi.Exists){file.CopyTo(newFullFileName);}if (componentNameList.Contains(fileName.ToLower())){continue;}else{break;}}return newFullFileName; }/// <summary> /// 修正表達(dá)式,自動(dòng)完全匹配 /// </summary> /// <param name="expCol"></param> /// <param name="expModel"></param> public static void UpdateExpression(ExpressionCollection expCol, ExpressionModel expModel) {Expression[] expressions = expCol.ToArray();foreach (var ex in expressions){foreach (PropertyInfo pi in expModel.GetType().GetProperties()){double value = 0.0;var name = pi.Name;string strVal = pi.GetValue(expModel, null).ToString();if (!string.IsNullOrEmpty(strVal)){value = double.Parse(strVal);}if (ex.Name == name){ex.Value = value == 0.0 ? ex.Value : value;}}} }添加組件,主要使用了workPart.ComponentAssembly.AddComponent方法來實(shí)現(xiàn),需要注意的是:
1、為了實(shí)現(xiàn)零件的重復(fù)添加,需要在添加組件的方法里做特殊處理,復(fù)制多個(gè)模板臨時(shí)文件并實(shí)現(xiàn)文件名的遞增命名
2、修正表達(dá)式是添加組件的一個(gè)重要方法,可以通過表達(dá)式的修正實(shí)現(xiàn)標(biāo)準(zhǔn)件的配置化
3、添加屬性,是為了方便組件管理,為后期出圖做鋪墊?
二、移動(dòng)/拖動(dòng)組件
1、實(shí)現(xiàn)方法
/// <summary> /// 移動(dòng)組件 /// </summary> /// <param name="origName">待移動(dòng)組件名</param> /// <param name="newParentName">父組件名</param> public static void MoveCompant(string origName, string newParentName) {Session theSession = Session.GetSession();Part workPart = theSession.Parts.Work;Component origComponent = GetComponentByDisplayName(origName);Component newParentComponent = GetComponentByDisplayName(newParentName);Part part1 = (Part)theSession.Parts.FindObject(newParentComponent.DisplayName);NXOpen.Assemblies.Component[] origComponents1 = new NXOpen.Assemblies.Component[1];NXOpen.Assemblies.Component component1 = origComponent;origComponents1[0] = component1;NXOpen.Assemblies.Component component2 = newParentComponent;NXOpen.Assemblies.Component[] newComponents1;ErrorList errorList1;part1.ComponentAssembly.RestructureComponents(origComponents1, component2, true, out newComponents1, out errorList1);errorList1.Dispose(); }public static Component GetComponentByDisplayName(string displayName) {List<Component> compList = new List<Component>();List<Body> bodyList = new List<Body>();GetBodyListFromComponet(ref compList, ref bodyList);foreach (Component comp in compList){if (comp.DisplayName == displayName)return comp;}return null; }/// <summary> /// 通過ufun獲取組件里的部件信息 /// </summary> public static void GetBodyListFromComponet(ref List<Component> compList, ref List<Body> bodyList) {theSession = Session.GetSession();theUFSession = UFSession.GetUFSession();workPart = theSession.Parts.Work;compList = new List<Component>();bodyList = new List<Body>();GetComponentList(workPart, compList);ComponentAssembly compAssembly = workPart.ComponentAssembly;Component rootComponent = compAssembly.RootComponent;foreach (Component c in compList){SetWorkPart(c);workPart = theSession.Parts.Work;Tag objTag = Tag.Null;theUFSession.Obj.CycleObjsInPart(workPart.Tag, UFConstants.UF_solid_type, ref objTag);while (objTag != Tag.Null){int type, subtype;theUFSession.Obj.AskTypeAndSubtype(objTag, out type, out subtype);if (type == 70 && subtype == 0){Body b = (Body)NXOpen.Utilities.NXObjectManager.Get(objTag);bodyList.Add(b);}theUFSession.Obj.CycleObjsInPart(workPart.Tag, UFConstants.UF_solid_type, ref objTag);}}SetWorkPart(rootComponent); }通過NXopen的方法part1.ComponentAssembly.RestructureComponents來實(shí)現(xiàn)組件移動(dòng)拖動(dòng):
想要移動(dòng)組件,先要弄清楚移動(dòng)哪個(gè)組件到哪個(gè)位置,所以移動(dòng)組件的在于待移動(dòng)組件和移動(dòng)到的父組件的識(shí)別
由于裝配是一個(gè)臨時(shí)的過程,所以在組件處理的時(shí)候我們不能像處理部件那樣,組件的每次操作都會(huì)引起組件tag的變化。
所以這里,我們移動(dòng)組件的參數(shù)用的是:待移動(dòng)組件名和父組件名。
通過封裝方法GetComponentByDisplayName,我們可以識(shí)別到需要的組件。
三、刪除組件
?1、實(shí)現(xiàn)方法
/// <summary> /// 刪除組件 /// </summary> /// <param name="displayName">待刪除組件名稱</param> public static void RemoveComponent(string displayName) {Session theSession = Session.GetSession();Component component = GetComponentByDisplayName(displayName);NXOpen.Session.UndoMarkId markId2 = theSession.SetUndoMark(NXOpen.Session.MarkVisibility.Visible, "Delete");;NXObject[] objects1 = new NXObject[1];objects1[0] = component;theSession.UpdateManager.AddToDeleteList(objects1);bool notifyOnDelete2 = theSession.Preferences.Modeling.NotifyOnDelete;int nErrs2 = theSession.UpdateManager.DoUpdate(markId2); }刪除組件比較簡單,是通過NXopen的theSession.UpdateManager對象來實(shí)現(xiàn)的,具體操作分一下幾步:
1、theSession 的初始化:Session theSession = Session.GetSession()
2、添加刪除列表:theSession.UpdateManager.AddToDeleteList(objects1)
3、提交刪除:theSession.UpdateManager.DoUpdate(markId2)
總結(jié)
以上是生活随笔為你收集整理的UG二次开发装配篇 添加/拖动/删除组件方法的实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AI测试|天猫精灵智能音箱测试策略与方法
- 下一篇: SpringBoot2基础篇