在windchill创建对象(如创建零件或者部件)

public class createPart implements wt.method.RemoteAccess
{

private static String lenovoPart_type = "wt.part.WTPart|cn.jwis.kenny.LenovoPart";

private static String PartPath = "Default/NewPart";

/**
  * 创建一个新的部件
  *
  * @param args
  * @throws WTException
  * @throws WTPropertyVetoException
  * @throws InvocationTargetException
  * @throws RemoteException
  */
public static void main(String[] args) throws WTPropertyVetoException, WTException,   RemoteException, InvocationTargetException
{
  // TODO Auto-generated method stub
  WTPart part =RemoteMethod("001", lenovoPart_type);
  if(part!=null)
  {
   System.out.println("part 的名称是:"+ part.getName());
  }
}

//创建部件要用rmi
public static WTPart RemoteMethod(String partNumber,String partType) throws RemoteException, InvocationTargetException
{
  String CLASSNAME = (createPart.class).getName();
  Class argTypes[];
  Object svrArgs[];
  argTypes = (new Class[] { String.class,String.class });
  svrArgs = (new Object[] { partNumber,partType});
  return (WTPart) RemoteMethodServer.getDefault().invoke("newPart", CLASSNAME, null, argTypes, svrArgs);
}

/**
  * 创建一个新的部件,类似的代码可以实现修改part的基本属性
  *
  * @param number
  *            部件的编码
  * @param PartType
  *            部件的类型,部件在windchill中可以自定义很多中类型
  * @return
  * @throws WTException
  * @throws WTPropertyVetoException
  */
public static WTPart newPart(String number, String PartType) throws WTException, WTPropertyVetoException
{
  WTPart newPart = null;
  // 获取容器
  WTContainer container = getContainer("Fine 1500");
  System.out.println(1);
  // 获取容器的引用
  WTContainerRef ref = ref = WTContainerRef.newWTContainerRef(container);

  Folder folder = wt.folder.FolderHelper.service.getFolder(PartPath, ref);
  System.out.println(2);
  String strPartNumber = number;
  String strPartName = number;
  wt.vc.views.View view = ViewHelper.service.getView("Design");
  ViewReference viewRef = ViewReference.newViewReference(view);
  System.out.println(3);
  TypeDefinitionReference typeDefinitionRef = TypedUtility.getTypeDefinitionReference(PartType);
  System.out.println(4);
  // 创建一个部件,编码与名称
  newPart = WTPart.newWTPart(strPartNumber, "shangxuetang.cn");
  // 为部件设置容器
  newPart.setContainer(container);
  // 为部件设置类型
  newPart.setTypeDefinitionReference(typeDefinitionRef);
  // 为部件设置view
  newPart.setView(viewRef);
  // 为此部件设置存贮位置
  FolderHelper.assignLocation((FolderEntry) newPart, folder);
  newPart = (WTPart) PersistenceHelper.manager.save(newPart);
  // 从数据库中提取part
  newPart = (WTPart) PersistenceHelper.manager.refresh(newPart);
  return newPart;
}

/**
  * 产品是一个容器,这里通过一个产品的名称获取容器,产品的名称在windchill中具有唯一性
  *
  * @param containerName
  *            产品的名称
  * @return
  */
public static WTContainer getContainer(String containerName)
{

  try
  {
      QuerySpec qs = new QuerySpec(WTContainer.class);
   SearchCondition sc = new SearchCondition(WTContainer.class, WTContainer.NAME, "=", containerName);
   qs.appendWhere(sc);
   QueryResult qr = PersistenceHelper.manager.find(qs);
   while (qr.hasMoreElements())
   {
    WTContainer container = (WTContainer) qr.nextElement();
    return container;
   }
  } catch (QueryException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (WTException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return null;
}

}

你可能感兴趣的:(vc++)