UG二次開發 實體轉裝配零件並將實體屬性填寫到裝配零件中(C#)
阿新 • • 發佈:2021-11-29
1 /// <summary> 2 /// 實體轉化為裝配 3 /// </summary> 4 /// <param name="prtfull">全路徑</param> 5 /// <param name="bodys">實體</param> 6 /// <param name="SaveBool">是否儲存零件</param> 7 /// <returns>返回prtTag </returns> 8public void CreatObjPartToAssembly(string prtfull, Body bodys, bool SaveBool, ref Tag prtTag) 9 { 10 NXOpen.FileNew fileNew1; 11 fileNew1 = base.theSession.Parts.FileNew(); 12 fileNew1.TemplateFileName = "model-plain-1-mm-template.prt"; 13 fileNew1.UseBlankTemplate = false; 14 fileNew1.ApplicationName = "ModelTemplate"; 15 fileNew1.Units = NXOpen.Part.Units.Millimeters; 16 fileNew1.RelationType = ""; 17 fileNew1.UsesMasterModel = "No"; 18 fileNew1.TemplateType = NXOpen.FileNewTemplateType.Item; 19 fileNew1.ItemType = ""; 20 fileNew1.Specialization = ""; 21 fileNew1.SetCanCreateAltrep(false); 22 fileNew1.NewFileName = prtfull; 23 fileNew1.MasterFileName = ""; 24 fileNew1.MakeDisplayedPart = false; 25 NXOpen.Assemblies.CreateNewComponentBuilder createNewComponentBuilder1; 26 createNewComponentBuilder1 = workPart.AssemblyManager.CreateNewComponentBuilder(); 27 createNewComponentBuilder1.ReferenceSet = NXOpen.Assemblies.CreateNewComponentBuilder.ComponentReferenceSetType.Other; 28 createNewComponentBuilder1.ReferenceSetName = "PART"; 29 createNewComponentBuilder1.OriginalObjectsDeleted = false; 30 string name = Path.GetFileNameWithoutExtension(prtfull); 31 createNewComponentBuilder1.NewComponentName = name.ToUpper(); 32 bool added1; 33 added1 = createNewComponentBuilder1.ObjectForNewComponent.Add(bodys); 34 createNewComponentBuilder1.NewFile = fileNew1; 35 NXOpen.NXObject nXObject1; 36 nXObject1 = createNewComponentBuilder1.Commit(); 37 Tag occTag = nXObject1.Tag; 38 Tag InstanceTag = theUfSession.Assem.AskInstOfPartOcc(occTag); 39 prtTag = theUfSession.Assem.AskChildOfInstance(InstanceTag); 40 NXOpen.BasePart bpart = (NXOpen.BasePart)NXObjectManager.Get(prtTag); 41 if (SaveBool) 42 { 43 bpart.Save(BasePart.SaveComponents.True, BasePart.CloseAfterSave.False); 44 } 45 NXObject.AttributeInformation[] allAttrs = bodys.GetUserAttributes(); 46 foreach (NXObject.AttributeInformation attrinfo in allAttrs) 47 { 48 bpart.SetUserAttribute(attrinfo, Update.Option.Now); 49 } 50 createNewComponentBuilder1.Destroy(); 51 }