Windchill:一个CATDrawing关联多个部件,对每个部件关联PDF
原因:
当一个CATDrawing关联多个部件,签名最后生产的PDF只能关联在计算属性得到的部件中;
改进:
1、在签名之后得到PDF文件之后,修改获取部件的方法,从而获取到CATDrawing下面关联的多个部件
**CATDrawing**中的/**
* 通过epm获取部件, 构建 -> 绘图计算 ->
* 首先获取计算关联之后在获取内容关联
* 有多个部件存在的情况下可以返回多个部件
*
* @param epm EPMDocument
* @return WTPart
* @throws WTException WTException
*/
public static void getMorePartByEpm(EPMDocument epm, Set<WTPart> resultList) throws WTException {
boolean enforce = SessionServerHelper.manager.setAccessEnforced(false);
if (LOGGER.isDebugEnabled()) {
LOGGER.trace("getPartByEpm -> epm is " + epm);
}
WTPart part;
LatestConfigSpec latestConfigSpec = new LatestConfigSpec();
try{
//构建关联,直接获取CATDrawing中的关联部件,一般情况下会获取不到该部件
part = AssociateUtilities.getActiveAssociatedPart(epm);
if (LOGGER.isDebugEnabled()) {
LOGGER.trace("getPartByEpm -> part post getActiveAssociatedPart: " + part);
}
if(part == null){
// 计算关联 - 先获取参考模型,如存在参考模型,则再获取其构建部件
// 通过CATDrawing来获得CATPRODUCT结尾的附件,该附件下计算关联的部件只有一个
// 所以只会获一个部件
EPMDocument model= DocumentHelper.queryReferenceModel(epm, latestConfigSpec);
if(model!=null){
part = AssociateUtilities.getActiveAssociatedPart(model);
System.out.println("计算关联的为:"+part.getNumber());
resultList.add(part);
}
// 内容关联
// 有用户会在计算关联的情况下,再去关联内容关联的部件,这时得到所有内容关联的部件
// 将所有部件全部加入到resultList中
QueryResult queryResult = PersistenceHelper.manager.navigate(epm, EPMDescribeLink.DESCRIBES_ROLE,
EPMDescribeLink.class, true);
queryResult = latestConfigSpec.process(queryResult);
while(queryResult.hasMoreElements()){
WTPart iterated = (WTPart) queryResult.nextElement();
if (iterated != null) {
part = iterated;
System.out.println("内容关联的为:"+part.getNumber());
resultList.add(part);
}
}
}
else{
resultList.add(part);
}
if (LOGGER.isDebugEnabled()) {
LOGGER.trace("getPartByEpm -> part post calculated: " + part);
}
}catch (Exception e) {
e.printStackTrace();
throw new WTException(e.getStackTrace());
} finally {
SessionServerHelper.manager.setAccessEnforced(enforce);
}
}
2、在多个物料的情况下为多个物料关联PDF图纸,其中 filepath为系统自己定义的路径,其目的是临时存储相关部件名称的PDF图片。
/**
* 关联多个物料情况下
* 给多个物料关联pdf图纸
*/
public static void updateMorePartContent(ContentHolder contentHolder, File file, ContentRoleType roleType) throws Exception {
ApplicationData attachment = ApplicationData.newApplicationData(contentHolder);
WTPart part = (WTPart) contentHolder;
String name = part.getNumber().toUpperCase();
name = "2D_" + name + "_已签名.pdf";
//该name只限于在已签名图纸的情况下进行关联
String filepath="/home/ptc/Windchill_11.0/Windchill/tmp/Tmpflod/";
attachment.setFileName(name);
// System.out.println("文件分别为:" + name);
MethodContext.getContext().sendFeedback(new StatusFeedback("部件分别为:" + name));
// attachment.setFileName(file.getName());
attachment.setUploadedFromPath(file.getCanonicalPath());
//file.getCanonicalPath()和file.toPath()都是可以获取到文件的完整路径
// MethodContext.getContext().sendFeedback(new StatusFeedback("文件222路径为:" + file.toPath()));
attachment.setRole(roleType);
attachment.setFileSize(file.length());
File newfile =null;
MethodContext.getContext().sendFeedback(new StatusFeedback("文件名:" + file.getName()));
if(name.equals(file.getName())){
ContentServerHelper.service.updateContent(contentHolder, attachment, file.getCanonicalPath());
PersistenceHelper.manager.refresh(contentHolder, true, true);
}else{
newfile = new File(filepath+name);
MethodContext.getContext().sendFeedback(new StatusFeedback("文件路径为:" + newfile.getCanonicalPath()));
if (!newfile.exists()) {
try {
newfile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件已创建");
} else {
System.out.println("文件已存在");
}
// Files.copy(file.toPath(),newfile.toPath());
copyFileUsingStream(file,newfile);
ContentServerHelper.service.updateContent(contentHolder, attachment, newfile.getCanonicalPath());
PersistenceHelper.manager.refresh(contentHolder, true, true);
}
if (newfile != null) {
CHZipUtils.deleteFile(newfile);
}
}
/**
* 关联多个物料情况下
* 对pdf图纸进行复制更名和写入操作
*/
private static void copyFileUsingStream(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}