Teamcenter SOA开发如何添加更改BOM的子件

alwaysmove / 2024-02-03 / 原文

1、关键是addOrUpdateChildrenToParentLine方法

2、主要执行完addOrUpdateChildrenToParentLine,要对BOMWindow进行保存!

void test(){
		try{
			DataManagementService dmService = DataManagementService.getService(AppXSession.getConnection());
			ServiceData serviceData = dmService.loadObjects(new String[]{"gxxxQIeYZqxMrD"});
			ItemRevision modelObjectIndex = (ItemRevision) serviceData.getPlainObject(0);
			StructureManagementService structService = StructureManagementService.getService(AppXSession.getConnection());
			RevisionRule revisionRule = tcbomUtil.getBOMRevisionRuleByName("Latest Working");
			BOMWindow bomWindow = null;
			try{
				bomWindow = tcbomUtil.createBOMWindow(modelObjectIndex,structService,revisionRule);
				BOMLine topBomLine = (BOMLine) propertyUtil.getPropertyModel(bomWindow,"top_line");

				List<ModelObject> modelObjectList =  queryUtil.executeQuery("Item Revision...",new String[]{"零组件 ID","类型"},new String[]{"121968","零组件版本"});
				ItemRevision itemRevision = (ItemRevision) modelObjectList.get(0);
				StructureManagement.ItemLineInfo itemLineInfo = new StructureManagement.ItemLineInfo();
				Map<String,String> propertyMap = new HashMap<String, String>();
				propertyMap.put("bl_quantity","1");
				itemLineInfo.item = itemRevision.get_items_tag();
				itemLineInfo.itemRev = itemRevision;
				itemLineInfo.itemLineProperties = propertyMap;

				StructureManagement.ItemLineInfo[] itemLineInfoArr = new StructureManagement.ItemLineInfo[]{itemLineInfo};
				StructureManagement.AddOrUpdateChildrenToParentLineInfo lineInfo = new StructureManagement.AddOrUpdateChildrenToParentLineInfo();
				lineInfo.parentLine = topBomLine;
				lineInfo.items = itemLineInfoArr;

				com.teamcenter.services.strong.bom.StructureManagementService structureManagementService = com.teamcenter.services.strong.bom.StructureManagementService.getService(AppXSession.getConnection());
				/**
				 * ###关键代码###
				 */
				StructureManagement.AddOrUpdateChildrenToParentLineResponse response = structureManagementService.addOrUpdateChildrenToParentLine(new StructureManagement.AddOrUpdateChildrenToParentLineInfo[] { lineInfo });
				ServiceData serviceData1 = response.serviceData;
				String bomError = ServiceDataUtil.getServiceData2Error(serviceData1);
				if(StringUtils.isNotEmpty(bomError)){
					throw new RuntimeException("搭建BOM失败!失败原因:"+bomError);
				}
				/**
				 * 最后要保存,不然不会生效
				 */
				com.teamcenter.services.strong.cad._2008_06.StructureManagement.SaveBOMWindowsResponse saveBOMWindowsResponse = structService.saveBOMWindows(new BOMWindow[]{bomWindow});
				ServiceData serviceData2 = saveBOMWindowsResponse.serviceData;
				String errorStr = ServiceDataUtil.getServiceData2Error(serviceData2);
				if(StringUtils.isNotEmpty(errorStr)){
					throw new RuntimeException("BOM保存失败,失败原因:"+errorStr);
				}
			}catch (Exception e){
				e.printStackTrace();
			}finally {
				/**
				 * 时候需要关闭BOMWindow,取决于你是还要用这个BOMWidnow。一般来说,前台就不用手动关闭了。但是后台是需要手动关闭的
				 */
				com.teamcenter.services.strong.cad._2007_01.StructureManagement.CloseBOMWindowsResponse closeBOMWindowsResponse = structService.closeBOMWindows(new BOMWindow[]{bomWindow});
				String error = ServiceDataUtil.getServiceData2Error(closeBOMWindowsResponse.serviceData);
				if(StringUtils.isNotEmpty(error)){
					throw new RuntimeException(error);
				}
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}