cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

WT Part

Amirtharaj_K
15-Moonstone

WT Part

Hi Guys,

 

I would appreciate your help finding a solution - I am working on WT Parts, and wherever I create a WT Part it's only stored in one Library, not in the same context.

 

Thanks in Advance,

Amir

 

 

4 REPLIES 4
Fadel
22-Sapphire II
(To:Amirtharaj_K)

are you creating the parts using API ?

Fede
Amirtharaj_K
15-Moonstone
(To:Fadel)

Thank you for the replay Mr. Fadel,

 

I tried to make a .class using eclipse for this & and i don't have an idea about API. Please brief on this.

 

Regards,

Amir

Fadel
22-Sapphire II
(To:Amirtharaj_K)

can you follow the same approach below for WTDocument, the same should work for WTPart :

String name = "TSTest";//name
String number = "TSTest";//number
String lifeCycleName = "Basic";//lifecycle name
String container_path = "/wt.inf.container.OrgContainer=demo organization/wt.pdmlink.PDMLinkProduct=Product-Test";//the container where the document will be created/located
String folder_path = "/Default";//folder path
WTDocument doc = WTDocument. newWTDocument();
doc.setName(name);
doc.setNumber(number);

WTContainerRef containerRef = WTContainerHelper.service .getByPath(container_path);
doc.setContainerReference(containerRef);

Folder folder = FolderHelper.service .getFolder(folder_path,containerRef);
FolderHelper.assignLocation((FolderEntry)doc,folder);

LifeCycleTemplate lct = LifeCycleHelper.service .getLifeCycleTemplate(lifeCycleName , doc.getContainerReference());
doc = (WTDocument) LifeCycleHelper. setLifeCycle(doc, lct);
            

doc = (WTDocument) wt.fc.PersistenceHelper.manager.store(doc);
Fede
Amirtharaj_K
15-Moonstone
(To:Fadel)

Thanks for the sharing Mr. Fadel,

Sorry to ask, You want me to create this in .class file.

 

I create this in .class

package com.example.custompartcontext;

import wt.fc.*;
import wt.folder.Folder;
import wt.folder.FolderHelper;
import wt.folder.FolderReference;
import wt.inf.container.WTContainer;
import wt.inf.container.WTContainerRef;
import wt.method.RemoteAccess;
import wt.part.WTPart;
import wt.part.WTPartHelper;
import wt.part.WTPartMaster;
import wt.pom.PersistenceException;
import wt.util.WTException;
import wt.session.SessionServerHelper;

public class CustomPartContextHandler implements RemoteAccess, CustomPartContextService {
public void setProductContextForPart(WTPart newPart, String organizationName, String libraryName, String folderName) throws WTException {
try {
// Start a Windchill session.
SessionServerHelper.manager.setAccessEnforced(false);

// Find or create the organization.
WTContainerRef organization = findOrCreateOrganization(organizationName);

// Find or create the library within the organization.
WTPartMaster library = findOrCreateLibrary(organization, libraryName);

// Find or create the "WT Parts" folder within the library.
Folder folder = findOrCreateFolder(library, folderName);

// Set the product context for the new part.
if (folder != null) {
WTPartHelper.service.setUsesTemplate(newPart, folder);
PersistenceHelper.manager.save(newPart);
}
} catch (Exception e) {
throw new WTException(e);
} finally {
// End the Windchill session.
SessionServerHelper.manager.setAccessEnforced(true);
}
}

private WTContainerRef findOrCreateOrganization(String organizationName) throws WTException {
// Attempt to find the organization.
WTContainerRef organization = findOrganizationByName(organizationName);

// If the organization doesn't exist, create it.
if (organization == null) {
organization = createOrganization(organizationName);
}

return organization;
}

private WTContainerRef findOrganizationByName(String organizationName) throws WTException {
QuerySpec qs = new QuerySpec(WTContainerRef.class);
SearchCondition sc = new SearchCondition(WTContainerRef.class, "name", SearchCondition.EQUAL, organizationName);
qs.appendWhere(sc);
QueryResult qr = PersistenceHelper.manager.find(qs);

if (qr.hasMoreElements()) {
return (WTContainerRef) qr.nextElement();
}

return null; // Organization not found.
}

private WTContainerRef createOrganization(String organizationName) throws WTException, PersistenceException {
WTContainerRef organization = WTContainerRef.newWTContainerRef();
organization.setName(organizationName);
organization = (WTContainerRef) PersistenceHelper.manager.save(organization);

return organization;
}

private WTPartMaster findOrCreateLibrary(WTContainerRef organization, String libraryName) throws WTException {
// Attempt to find the library.
WTPartMaster library = findLibraryByName(organization, libraryName);

// If the library doesn't exist, create it.
if (library == null) {
library = createLibrary(organization, libraryName);
}

return library;
}

private WTPartMaster findLibraryByName(WTContainerRef organization, String libraryName) throws WTException {
QuerySpec qs = new QuerySpec(WTPartMaster.class);
SearchCondition sc = new SearchCondition(WTPartMaster.class, "containerReference.key.id", SearchCondition.EQUAL, organization.getPersistInfo().getObject().getObjectIdentifier().getId());
qs.appendWhere(sc);
qs.appendAnd();
sc = new SearchCondition(WTPartMaster.class, "containerReference.key.className", SearchCondition.EQUAL, organization.getClassname());
qs.appendWhere(sc);
qs.appendAnd();
sc = new SearchCondition(WTPartMaster.class, "name", SearchCondition.EQUAL, libraryName);
qs.appendWhere(sc);
QueryResult qr = PersistenceHelper.manager.find(qs);

if (qr.hasMoreElements()) {
return (WTPartMaster) qr.nextElement();
}

return null; // Library not found.
}

private WTPartMaster createLibrary(WTContainerRef organization, String libraryName) throws WTException, PersistenceException {
WTPartMaster library = WTPartMaster.newWTPartMaster();
library.setName(libraryName);
library = (WTPartMaster) PersistenceHelper.manager.save(library);

// Set the library's container reference.
library.setContainerReference(organization);
PersistenceHelper.manager.update(library);

return library;
}

private Folder findOrCreateFolder(WTPartMaster library, String folderName) throws WTException {
// Attempt to find the folder.
Folder folder = findFolderByName(library, folderName);

// If the folder doesn't exist, create it.
if (folder == null) {
folder = createFolder(library, folderName);
}

return folder;
}

private Folder findFolderByName(WTPartMaster library, String folderName) throws WTException {
Folder folder = null;
QueryResult qr = FolderHelper.service.findFolders(library, folderName);

if (qr.hasMoreElements()) {
folder = (Folder) qr.nextElement();
}

return folder;
}

private Folder createFolder(WTPartMaster library, String folderName) throws WTException {
Folder folder = Folder.newFolder();
folder.setName(folderName);
folder = (Folder) PersistenceHelper.manager.save(folder);

// Set the folder's reference to the library.
FolderReference folderRef = FolderReference.newFolderReference(folder, library);
folderRef.setFolderType(FolderReference.STANDARD_TYPE);
PersistenceHelper.manager.save(folderRef);

return folder;
}
}

 

Regards,

Amir

Announcements


Top Tags