UG二次开发装配篇 添加/拖动/删除组件方法的实现
我們在UG裝配的過程中,經常會遇到需要調整組件目錄位置,在軟件設計過程中可以通過在目錄樹里面拖動組件來完成。
那么,如果要用程序實現組件的移動/拖動,我們要怎么做呢?
本節就完成了添加/拖動/刪除組件方法的實現,先看效果圖:
根節點test下,有SHCS_01、SHCS_02、SHCS_03、SHCS_04這四個組件。
下面分別給出了添加組件、移動組件和刪除組件的方法。
一、添加組件
1、實現方法
/// <summary> /// 添加組件 /// </summary> /// <param name="templatePrt">模板路徑</param> /// <param name="basePoint">中心點坐標位置</param> /// <param name="orientation">矢量方向</param> /// <param name="expModel">表達式集</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); //先拷貝一個備份try{basePart1 = theSession.Parts.OpenBase(newfile, out partLoadStatus1);}catch (Exception){componentNameList.Add(fileName);goto step1;}partLoadStatus1.Dispose();#region 修正表達式ExpressionCollection expressionCollection = basePart1.Expressions;EventHelper.UpdateExpression(expressionCollection, expModel);#endregion#region 添加屬性basePart1.SetAttribute("模具編號", "", Update.Option.Now);basePart1.SetAttribute("材料標準", "", Update.Option.Now);basePart1.SetAttribute("塑膠材料", "", Update.Option.Now);basePart1.SetAttribute("縮水率", "", Update.Option.Now);basePart1.SetAttribute("穴數", "", Update.Option.Now);basePart1.SetAttribute("客戶", "", Update.Option.Now);basePart1.SetAttribute("項目編號", "", Update.Option.Now);basePart1.SetAttribute("產品名稱", "", Update.Option.Now);basePart1.SetAttribute("產品編號", "", Update.Option.Now);basePart1.SetAttribute("設計", "", 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> /// 修正表達式,自動完全匹配 /// </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方法來實現,需要注意的是:
1、為了實現零件的重復添加,需要在添加組件的方法里做特殊處理,復制多個模板臨時文件并實現文件名的遞增命名
2、修正表達式是添加組件的一個重要方法,可以通過表達式的修正實現標準件的配置化
3、添加屬性,是為了方便組件管理,為后期出圖做鋪墊?
二、移動/拖動組件
1、實現方法
/// <summary> /// 移動組件 /// </summary> /// <param name="origName">待移動組件名</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來實現組件移動拖動:
想要移動組件,先要弄清楚移動哪個組件到哪個位置,所以移動組件的在于待移動組件和移動到的父組件的識別
由于裝配是一個臨時的過程,所以在組件處理的時候我們不能像處理部件那樣,組件的每次操作都會引起組件tag的變化。
所以這里,我們移動組件的參數用的是:待移動組件名和父組件名。
通過封裝方法GetComponentByDisplayName,我們可以識別到需要的組件。
三、刪除組件
?1、實現方法
/// <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對象來實現的,具體操作分一下幾步:
1、theSession 的初始化:Session theSession = Session.GetSession()
2、添加刪除列表:theSession.UpdateManager.AddToDeleteList(objects1)
3、提交刪除:theSession.UpdateManager.DoUpdate(markId2)
總結
以上是生活随笔為你收集整理的UG二次开发装配篇 添加/拖动/删除组件方法的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AI测试|天猫精灵智能音箱测试策略与方法
- 下一篇: UG装配函数