建立自訂元件
元件架構主題所述,每個 ThingWorx Navigate 元件都由兩個子元件、使用者介面子元件與企業邏輯子元件組成。企業邏輯子元件也由兩部份組成,即「基礎企業邏輯」與「企業邏輯實行」,我們將在本主題中分別將這兩者稱為基礎元件與實行。
依預設,每個基礎元件都有一或多個實行。每個實行都設計為在使用者介面中顯示特定資訊。欲顯示不同的資訊,您可透過建立自訂實行來自訂您的元件。例如,現成的「項目標識」基礎元件有兩個實行:「零件標識」與「任務標識」。您也可以建立項目標識基礎元件的自訂實行,例如「文件標識」。
建立元件的自訂實行包括延伸現有基礎元件或現有實行,然後再取代必要的服務。下面幾部份的內容包含詳細步驟與範例。
透過延伸基礎元件來建立自訂實行 
請遵循下列步驟從現有基礎元件建立自訂實行:
1. 選擇現有基礎元件作為自訂實行的起點。根據您的 ThingWorx Navigate 版本,在下表中找到基礎元件並記下其「物範本」與「專案」。稍後您會用到此資訊。
基礎元件
物範本
專案 (ThingWorx Navigate 9.0)
專案 (ThingWorx Navigate 9.1 及更新版本)
屬性
PTC.Nav.AttributesBusinessLogicThingTemplate
PTC.Nav.AttributesProject
PTC.Nav.AttributesBusinessLogicProject
項目標識
PTC.Nav.ItemIdentityBusinessLogicThingTemplate
PTC.Nav.ItemIdentityProject
PTC.Nav.ItemIdentityBusinessLogicProject
項目清單
PTC.Nav.ItemListBusinessLogicThingTemplate
PTC.Nav.ItemListProject
PTC.Nav.ItemListBusinessLogicProject
進度
PTC.Nav.ProgressBusinessLogicThingTemplate
PTC.Nav.ProgressProject
PTC.Nav.ProgressBusinessLogicProject
圖標
PTC.Nav.TilesBusinessLogicThingTemplate
PTC.Nav.TilesProject
PTC.Nav.TilesBusinessLogicProject
2. 為您的自訂實行建立專案。在「專案相依性」下,輸入在步驟 1 記下的專案。儲存專案。
3. 為您的自訂實行建立物範本。在「專案」下,輸入在步驟 2 建立的專案。在「基礎物範本」下,輸入在步驟 1 記下的物範本。
4. 為您的自訂實行建立物件。在「專案」下,輸入在步驟 2 建立的專案。在「基礎物範本」下,輸入在步驟 3 建立的物範本。
5. 在您建立的物件中,按一下「服務」標籤。然後按一下 來取代 GetImplementationConfigurationVersion 服務,並為您的實行設定版本。您可針對此版本使用任何值。儲存物件。
6. 找出您需要針對自訂實行取代哪些服務。如需有關現成元件之服務的詳細資訊,請在可用元件下找到您要延伸之元件的主題,並查看「自訂服務」表。
7. 在您建立的物範本中,按一下「服務」標籤。然後按一下 來取代必要服務。建議在取代其他服務之前先取代 GetConfigurations 服務。
GetConfigurations 服務的程式碼中,將 implementationConfigurationVersion 的版本設定為符合在步驟 5 針對 GetImplementationConfigurationVersion 所設定的版本。略過此步驟可能會造成未來 ThingWorx Navigate 發行版本中的相容性問題。
8. 取代剩餘的必要服務。當取代會接收組態作為參數的服務時,請先在您的程式碼中呼叫 Migrate 服務,然後再傳遞基礎與實行版本。
欲確定要在 Migrate 服務中用於 targetBaseVersion 的值,請針對您在步驟 4 建立的物件執行 GetBaseConfigurationVersion 服務。請勿呼叫 GetBaseConfigurationVersion 程式碼服務並傳回其值。
當在 Migrate 服務中設定 targetImplementationVersion 時,請使用您之前在步驟 5 設定的版本。
如需有關呼叫 Migrate 服務的詳細資訊,請參閱版本化與移轉主題。
* 
取代服務時,請勿以類別 PTC.Nav.Private 取代服務。
9. 儲存物範本。現在您的自訂實行即已準備就緒,可供使用。欲使用實行,請建立混搭。將實行的基礎元件新增至混搭,然後按一下 。在「組態類型」下拉選單中,選取屬於您自訂實行的組態。
透過延伸實行來建立自訂實行 
請遵循下列步驟從現有實行建立自訂實行:
1. 選擇現有實行作為自訂實行的起點。
如果您使用現成的實行,請根據您的 ThingWorx Navigate 版本,在下表中找到該實行並記下其「物範本」與「專案」。稍後您會用到此資訊。
如果您使用的是自訂實行,請記下其「物範本」與「專案」。稍後您會用到此資訊。
實行
物範本
專案 (ThingWorx Navigate 9.0)
專案 (ThingWorx Navigate 9.1 及更新版本)
活動
PTC.Nav.ActivityBusinessLogicThingTemplate
PTC.Nav.ItemListProject
PTC.Nav.ActivityBusinessLogicProject
受影響項目
PTC.Nav.AffectedItemsBusinessLogicThingTemplate
PTC.Nav.ItemListProject
PTC.Nav.AttachmentsBusinessLogicProject
附件
PTC.Nav.AttachmentsBusinessLogicThingTemplate
PTC.Nav.ItemListProject
PTC.Nav.AffectedItemsBusinessLogicProject
泛用屬性
PTC.Nav.AttributesGenericBusinessLogicThingTemplate
PTC.Nav.AttributesProject
PTC.Nav.AttributesGenericBusinessLogicProject
零件標識
PTC.Nav.PartIdentityBusinessLogicThingTemplate
PTC.Nav.ItemIdentityProject
PTC.Nav.PartIdentityBusinessLogicProject
任務標識
PTC.Nav.TaskIdentityBusinessLogicThingTemplate
PTC.Nav.ItemIdentityProject
PTC.Nav.TaskIdentityBusinessLogicProject
任務進度
PTC.Nav.TaskProgressBusinessLogicThingTemplate
PTC.Nav.ProgressProject
PTC.Nav.TaskProgressBusinessLogicProject
任務
PTC.Nav.TaskListBusinessLogicThingTemplate
PTC.Nav.ItemListProject
PTC.Nav.TaskListBusinessLogicProject
泛用圖標
PTC.Nav.TilesGenericBusinessLogicThingTemplate
PTC.Nav.TilesProject
PTC.Nav.TilesGenericBusinessLogicProject
2. 為您的自訂實行建立專案。在「專案相依性」下,輸入在步驟 1 記下的專案。儲存專案。
3. 為您的自訂實行建立物範本。在「專案」下,輸入在步驟 2 建立的專案。在「基礎物範本」下,輸入在步驟 1 記下的物範本。
4. 為您的自訂實行建立物件。在「專案」下,輸入在步驟 2 建立的專案。在「基礎物範本」下,輸入在步驟 3 建立的物範本。
5. 在您建立的物件中,按一下「服務」標籤。
按一下 GetBaseMigrationVersion 取代為您的 ThingWorx Navigate 版本,例如 9.0.0 或 9.1.0。
按一下 GetImplementationConfigurationVersion 取代為您的 ThingWorx Navigate 版本,例如 9.0.0 或 9.1.0。
6. 儲存物件。
7. 找出您需要針對自訂實行取代哪些服務。如需有關現成元件之服務的詳細資訊,請在可用元件下找到您要延伸之元件的主題,並查看「自訂服務」表。
8. 在您建立的物範本中,按一下「服務」標籤。然後按一下 來取代必要服務。建議在取代其他服務之前先取代 GetConfigurations 服務。
* 
取代 GetConfigurations 服務時,不支援從其現成結構變更組態結構。請僅變更個別 JSON 內容的值。如需詳細資訊,請參閱主題組態內容的變更範例
在組態中,請務必將 implementationConfigurationVersion 內容的值設定為與 GetImplementationConfigurationVersion 服務傳回的值相同。
9. 取代剩餘的必要服務。當取代會接收組態作為參數的服務時,請先在您的程式碼中呼叫 Migrate 服務,然後再傳遞基礎與實行版本。
欲確定要傳遞給 Migrate 服務的基礎版本,請針對您在步驟 4 建立的物件執行 GetBaseConfigurationVersion 服務。請勿呼叫 GetBaseConfigurationVersion 服務並傳回其值。
欲確定要傳遞給 Migrate 服務的實行版本,請針對您在步驟 4 建立的物件執行 GetImplementationConfigurationVersion 服務。請勿呼叫 GetImplementationConfigurationVersion 服務並傳回其值。
如需有關呼叫 Migrate 服務的詳細資訊,請參閱版本化與移轉主題。
* 
取代服務時,請勿以類別 PTC.Nav.Private 取代服務。
10. 儲存物範本。現在您的自訂實行即已準備就緒,可供使用。欲使用實行,請建立混搭。將實行的基礎元件新增至混搭,然後按一下 。在「組態類型」下拉選單中,選取屬於您自訂實行的組態。
範例:建立零件清單實行 
以下範例顯示如何建立稱為「零件清單」的自訂實行,這是「項目清單」基礎元件的實行。「零件清單」會接收零件作為輸入,並傳回它所包含之零件的第一層級。
* 
以下範例設計為在 ThingWorx Navigate 9.0 中工作,而且在程式碼片段中使用的版本值對於該版本的 ThingWorx Navigate 而言正確無誤。欲在較新版本的 ThingWorx Navigate 中遵循此範例,請務必在您的程式碼中使用正確版本值。
1. ThingWorx Composer 中,建立一個名為 Demo.PartsListProject 的新專案。
ThingWorx Navigate 9.0 中,於「專案相依性」下輸入 PTC.Nav.ItemListProject
ThingWorx Navigate 9.1 及更新版本中,於「專案相依性」下輸入 PTC.Nav.ItemListBusinessLogicProject
2. 建立一個名為 Demo.PartsListBusinessLogicThingTemplate 的新物範本。將「專案」設定為 Demo.PartsListProject,並將「基礎物範本」設定為 PTC.Nav.ItemListBusinessLogicThingTemplate
3. 建立一個名為 Demo.PartsListBusinessLogicThing 的物件。將「專案」設定為 Demo.PartsListProject,並將「基礎物範本」設定為 Demo.PartsListBusinessLogicThingTemplate
4. 按一下「服務」標籤並尋找 GetImplementationConfigurationVersion 服務。按一下 以取代服務,並使用下列程式碼將版本設定為 1.0.0:
result = "1.0.0";
按一下「完成」。然後,按一下「儲存」以儲存此物件。
5. 檢查項目清單的「自訂服務」表,並識別物範本中需要取代的服務。針對此自訂實行,我們需要取代三個服務:
GetConfigurations - 此服務會傳回適用于實行的組態。我們將對其進行編輯,以定義「零件清單」的組態。
GetImplementationLabel - 此服務會以其在 Mashup Builder 中顯示的方式傳回實行的名稱。我們要將其設定為顯示名稱 Parts List Demo
GetItems - 此服務會接收輸入與組態作為參數,並傳回 CCO 格式的 JSON,其中的資料會顯示在「項目清單」使用者介面中。我們會將其設定為傳回指定零件的第一層級零件,以及第一層級零件的屬性。
6. 開啟 Demo.ItemListBusinessLogicThingTemplate 物範本。按一下「服務」標籤。
7. 按一下 以取代 GetConfigurations 服務。將下列程式碼貼上至此服務中。此程式碼包括零件清單的一個組態,其在 Mashup Builder 與 ThingWorx Navigate 使用者介面中皆標記為 Parts List
result = {
"partsList": {
"label": "Parts List",
"configuration": {
"selectionType": {
"selectedKey": "single"
},
"waitForInput": {
"value": true
},
"actionBarConfiguration": {
"value": {
"actionBarConfigurationJSON": {
"value": ""
}
}
},
"baseConfigurationVersion": {
"value": "9.0.0"
},
"implementationConfigurationVersion": {
"value": "1.0.0"
},
"defaultSortFields": {
"selectedValues": {
"data": [{
"itemListsData": [{
"items": [{
"additionalData": {
"itemListDefaultSortFieldDefinition": {
"defaultSortDirection": {
"selectedKey": "asc"
}
}
},
"id": "Name"
}
],
"objectType": "PTC.ProdMgmt.Part"
}
],
"adapter": {
"instanceName": "windchill",
"thingName": "PTC.WCAdapter"
}
}
]
}
},
"attributes": {
"selectedValues": {
"data": [{
"itemListsData": [{
"items": [{
"id": "Name",
"ordinal": 1
}, {
"id": "State#Display",
"ordinal": 2
}, {
"id": "Version",
"ordinal": 3
}, {
"id": "Number",
"ordinal": 0
}, {
"id": "ID",
"ordinal": 4
}
],
"objectType": "PTC.ProdMgmt.Part"
}
],
"adapter": {
"instanceName": "windchill",
"thingName": "PTC.WCAdapter"
}
}
]
},
"inTailoring": false
},
"label": {
"value": "Parts List"
},
"showExportAction": {
"value": true
},
"enableSearch": {
"additionalData": {
"itemListSearchDefinition": {
"searchHintText": {
"value": "Find"
}
}
},
"value": true
},
"maxNumberOfRowsInGrid": {
"inTailoring": false,
"value": 50
}
}
}
};
這是 GetConfigurations 的另一個範例程式碼,它包含兩個組態,而不是一個。第一個組態與上述程式碼中的組態相同。第二個組態用於迷你版的零件清單。它只會顯示三個屬性及最多五列。它在 Mashup Builder 中會標記為 Parts List mini,在 ThingWorx Navigate 使用者介面中會標記為 Parts List
result = {
"partsList": {
"label": "Parts List",
"configuration": {
"selectionType": {
"selectedKey": "single"
},
"waitForInput": {
"value": true
},
"actionBarConfiguration": {
"value": {
"actionBarConfigurationJSON": {
"value": ""
}
}
},
"baseConfigurationVersion": {
"value": "9.0.0"
},
"implementationConfigurationVersion": {
"value": "1.0.0"
},
"defaultSortFields": {
"selectedValues": {
"data": [{
"itemListsData": [{
"items": [{
"additionalData": {
"itemListDefaultSortFieldDefinition": {
"defaultSortDirection": {
"selectedKey": "asc"
}
}
},
"id": "Name"
}
],
"objectType": "PTC.ProdMgmt.Part"
}
],
"adapter": {
"instanceName": "windchill",
"thingName": "PTC.WCAdapter"
}
}
]
}
},
"attributes": {
"selectedValues": {
"data": [{
"itemListsData": [{
"items": [{
"id": "Name",
"ordinal": 1
}, {
"id": "State#Display",
"ordinal": 2
}, {
"id": "Version",
"ordinal": 3
}, {
"id": "Number",
"ordinal": 0
}, {
"id": "ID",
"ordinal": 4
}
],
"objectType": "PTC.ProdMgmt.Part"
}
],
"adapter": {
"instanceName": "windchill",
"thingName": "PTC.WCAdapter"
}
}
]
},
"inTailoring": false
},
"label": {
"value": "Parts List"
},
"showExportAction": {
"value": true
},
"enableSearch": {
"additionalData": {
"itemListSearchDefinition": {
"searchHintText": {
"value": "Find"
}
}
},
"value": true
},
"maxNumberOfRowsInGrid": {
"inTailoring": false,
"value": 50
}
}
},
"partsListMini": {
"label": "Parts List mini",
"configuration": {
"selectionType": {
"selectedKey": "single"
},
"waitForInput": {
"value": true
},
"actionBarConfiguration": {
"value": {
"actionBarConfigurationJSON": {
"value": ""
}
}
},
"baseConfigurationVersion": {
"value": "9.0.0"
},
"implementationConfigurationVersion": {
"value": "1.0.0"
},
"defaultSortFields": {
"selectedValues": {
"data": [{
"itemListsData": [{
"items": [{
"additionalData": {
"itemListDefaultSortFieldDefinition": {
"defaultSortDirection": {
"selectedKey": "asc"
}
}
},
"id": "Name"
}
],
"objectType": "PTC.ProdMgmt.Part"
}
],
"adapter": {
"instanceName": "windchill",
"thingName": "PTC.WCAdapter"
}
}
]
}
},
"attributes": {
"selectedValues": {
"data": [{
"itemListsData": [{
"items": [{
"id": "Name",
"ordinal": 1
}, {
"id": "Version",
"ordinal": 2
}, {
"id": "Number",
"ordinal": 0
}
],
"objectType": "PTC.ProdMgmt.Part"
}
],
"adapter": {
"instanceName": "windchill",
"thingName": "PTC.WCAdapter"
}
}
]
},
"inTailoring": false
},
"label": {
"value": "Parts List"
},
"showExportAction": {
"value": true
},
"enableSearch": {
"additionalData": {
"itemListSearchDefinition": {
"searchHintText": {
"value": "Find"
}
}
},
"value": true
},
"maxNumberOfRowsInGrid": {
"inTailoring": false,
"value": 5
}
}
}
};
新增程式碼之後,按一下「完成」
8. 取代 GetImplementationLabel 服務。使用下列程式碼設定實行標籤:
result = "Parts List Demo";
按一下「完成」
9. GetItems 服務將使用下列流程從 Windchill 中擷取第一層級零件:
a. 使用要建立的 ConvertAttributesToCCO 服務將組態的屬性部份轉換為 CCO 結構。
b. 將產生的 CCO 格式的 JSON 傳遞至 PTC.WCAdapter 上的 GetRelatedItems 服務。此服務會將資料擷取為 UsageLink 物件。
c. 使用要建立的 ConvertUsageLinkToPart 服務將 UsageLink 物件轉換為 Part 物件。
在取代 GetItems 服務之前,必須先建立 ConvertAttributesToCCOConvertUsageLinkToPart 服務。
10. 建立一個新服務,並將其命名為 ConvertAttributesToCCO。在「輸入」下,新增下列輸入:
configuration,其「基礎類型」JSON
attributesPrefix,其「基礎類型」STRING
replaceObjectType,其「基礎類型」STRING
「輸出」下,從下拉選單中選取 JSON
11. 將下列程式碼貼上至 ConvertAttributesToCCO 服務中:
let attributesData = configuration.attributes.selectedValues;
attributesPrefix = attributesPrefix || "";
let objectType = replaceObjectType || attributesData.data[0].itemListsData[0].objectType;
attributesData.data[0].itemListsData[0].objectType = objectType;
let items = attributesData.data[0].itemListsData[0].items;
let newItems = [];
let newObj = {};
let hasID = false;
//convert to CCO
for(var i = 0; i < items.length; i++) {
let item = items[i];
if (item.id === 'ID') {
hasID = true;
}
newObj[attributesPrefix + item.id] = {"value":""};
}
//add ID if needed
if (!hasID) {
newObj[attributesPrefix + 'ID'] = {"value":""};
}
newItems.push (newObj);
attributesData.data[0].itemListsData[0].items = newItems;
//create date
var dateValue = new Date();
var y = dateValue.getFullYear();
var mo = dateValue.getMonth()+1;
if(mo<10) {mo='0'+mo;}
var d = dateValue.getDate();
if(d<10) {d='0'+d;}
//create time
var h = dateValue.getHours();
if(h<10) {h='0'+h;}
var mi = dateValue.getMinutes();
if(mi<10) {mi='0'+mi;}
var s = dateValue.getSeconds();
if(s<10) {s='0'+s;}
var ms = dateValue.getMilliseconds();
if(ms<10) {ms='00'+ms;} else {if(ms>=10 && ms<100) {ms='0'+ms;}}
attributesData.timeStamp = y + "-" + mo + "-" + d + " " + h + ":" + mi + ":" + s + "." + ms;
attributesData.version = "";
attributesData.statusMessage = "OK";
attributesData.status = "200";
result = attributesData;
按一下「完成」
12. 建立一個新服務,並將其命名為 ConvertUsageLinkToPart。在「輸入」下,新增下列輸入:
usageLinkCCO,其「基礎類型」JSON
「輸出」下,從下拉選單中選取 JSON
13. 將下列程式碼貼上至 ConvertUsageLinkToPart 服務中:
let itemListsData = usageLinkCCO.data[0].itemListsData[0];
let tempMetadata = itemListsData.metadata;
let items = itemListsData.items;
//convert metadata
delete tempMetadata.ID;
let newMetadata = {};
for (var key in tempMetadata) {
let newKey = key.replace(/^(Uses\|)/,"");
newMetadata[newKey] = tempMetadata[key];
}
newMetadata['id'] = newMetadata.ID;
itemListsData['metadata'] = newMetadata;
//convert Items
let newItems = [];
for (var i=0 ; i<items.length ; i++) {
let item = items[i];
let newItem = {};
for (key in item) {
let newKey = key.replace(/^(Uses\|)/,"");
newItem[newKey] = item[key];
}
newItem['id'] = newItem.ID;
newItems.push(newItem);
}
itemListsData['items'] = newItems;
itemListsData['objectType'] = 'PTC.ProdMgmt.Part';
//update CCO
let partsListCCO = usageLinkCCO;
partsListCCO.data[0].itemListsData = [itemListsData];
result = partsListCCO;
按一下「完成」
14. 按一下 以取代 GetItems 服務。將下列程式碼貼上至 GetItems 服務中:
var migratedConfig = me.Migrate({
targetImplementationVersion: "1.0.0" /* STRING */,
data: configuration /* JSON */,
migrationType: "Configuration" /* STRING */,
targetBaseVersion: "9.0.0" /* STRING */
});
var attributesConfigurationCCO = me.ConvertAttributesToCCO({
configuration:migratedConfig /* JSON */,
attributesPrefix: "Uses|" /* STRING */,
replaceObjectType: "PTC.ProdMgmt.PartUse" /* STRING */
});
// result: JSON
var tempResult = Things["PTC.WCAdapter"].GetRelatedItems({
item: input /* JSON */,
relation: {path:"Uses"} /* JSON */,
attributes: attributesConfigurationCCO /* JSON */
});

var convertedResults = me.ConvertUsageLinkToPart({
usageLinkCCO:tempResult /* JSON */
});
var result = convertedResults;
按一下「完成」。然後,按一下「儲存」以儲存此物範本。
現在,零件清單自訂實行及其組態已建立完畢,並且已準備好在 Mashup Builder 中使用。欲使用實行,請將「項目清單」基礎元件新增至混搭,然後按一下 。在「組態類型」下拉選單中,選取標記為 Parts Lists Demo - Parts List 的「零件清單」組態。
如果您要使用「零件清單」來建立下列視訊所示的混搭,以下是一些額外的程式碼:
用來配置 Attributes 元件的程式碼:
{
"layout": {
"selectedKey": "horizontal",
"additionalData": {
"dimensionDefinition": {
"maxHeight": {
"value": 200,
"version": "1.0.0"
},
"maxWidth": {
"value": 200,
"version": "1.0.0"
}
}
},
"version": "1.0.0"
},
"attributes": {
"sets": [{
"data": {
"selectedValues": {
"data": [{
"itemListsData": [{
"items": [{
"id": "ModifiedBy",
"ordinal": 0
}, {
"id": "GatheringPart",
"ordinal": 1
}, {
"id": "Latest",
"ordinal": 2
}, {
"id": "FolderName",
"ordinal": 3
}
],
"objectType": "PTC.ProdMgmt.Part"
}
],
"adapter": {
"instanceName": "windchill",
"thingName": "PTC.WCAdapter"
}
}
]
}
},
"name": ""
}
],
"inTailoring": true,
"filteredValues": {
"data": [{
"itemListsData": [{
"objectType": ""
}
],
"adapter": {
"instanceName": "",
"thingName": ""
}
}
]
},
"version": "1.0.0"
}
}
用於混搭 input 參數的程式碼。請務必新增有效的零件 ID:
{
"version": "1.0.0",
"data": [{
"adapter": {
"thingName": "PTC.WCAdapter",
"instanceName": "windchill"
},
"itemListsData": [{
"objectType": "PTC.ProdMgmt.Part",
"items": [{
"id": {
"value": ""
}
}
]
}
]
}
]
}
視訊教學專區:建立零件清單實行
下列兩個視訊教學專區顯示如何建立及使用自訂實行「零件清單」。第一個視訊顯示如何依照上述範例中的步驟建立「零件清單」。第二個視訊顯示如何在混搭中使用「零件清單」。您可以在上述範例中找到在視訊中使用的所有程式碼。
欲在較大的視窗中觀看視訊,請按一下播放器中的標題。隨即會在 YouTube 中以新標籤開啟此視訊。
這是否有幫助?