创建自定义组件
组件架构主题中所述,每个 Windchill Navigate 组件都由用户界面和业务逻辑两个子组件构成。而其中的业务逻辑子组件又由基本业务逻辑和业务逻辑实现这两部分构成,在本主题中,这两部分分别被称为基本组件和实现。
每个基本组件都预设一个或多个实现。每个实现都用于在用户界面中显示特定信息。如需显示不同的信息,可通过创建自定义实现来自定义专属组件。例如,预设的“项列表”基本组件有六种实现:“活动”、“受影响的项”、“附件”、“设计文件”、“文档结构”、“绘图”、“部件结构”和“任务”。也可以创建“项列表”基本组件的自定义实现 (如“部件列表”)。
为组件创建自定义实现时,需要扩展现有基本组件或现有实现,然后改写必要的服务。以下各部分提供详细步骤和相应的示例。
* 
所有预设实体的版本控制方案是 nav.<version number>。例如,9.3 中所有预设组件的版本均为 nav.1。在创建自定义组件配置时,请不要使用此版本控制方案,此版本控制方案仅供 Windchill Navigate 内部使用。
通过扩展基本组件创建自定义实现 
请按照以下步骤,从现有基本组件创建自定义实现:
1. 选取一个现有基本组件,用作自定义实现的起始点。在下表中查找基本组件并记录其事物模板和项目。您稍后需要用到这些信息。
基本组件
事物模板
项目
属性
PTC.Nav.AttributesBusinessLogicThingTemplate
PTC.Nav.AttributesBusinessLogicProject
项标识
PTC.Nav.ItemIdentityBusinessLogicThingTemplate
PTC.Nav.ItemIdentityBusinessLogicProject
项列表
PTC.Nav.ItemListBusinessLogicThingTemplate
PTC.Nav.ItemListBusinessLogicProject
进度
PTC.Nav.ProgressBusinessLogicThingTemplate
PTC.Nav.ProgressBusinessLogicProject
磁贴
PTC.Nav.TilesBusinessLogicThingTemplate
PTC.Nav.TilesBusinessLogicProject
2. 为自定义实现创建项目。在“项目依存关系”下,输入步骤 1 中记录的项目。保存该项目。
3. 为自定义实现创建模板。在“项目”下,输入步骤 2 中创建的项目。在“基本事物模板”下,输入步骤 1 中记录的事物模板。
4. 为自定义实现创建事物。在“项目”下,输入步骤 2 中创建的项目。在“基本事物模板”下,输入步骤 3 中记录的事物模板。
5. 在创建的事物中,单击“服务”选项卡。然后单击 铅笔图标,改写 GetImplementationConfigurationVersion 服务并设置实现的版本。对于版本值,使用 user.version 作为方案,其中 version 可以有多个由点分隔的层级,并且 user 不能为 nav。保存该事物。
6. 标识自定义实现中需要改写的服务。有关预设组件中的服务的详情,请在可用组件下查找有关正在扩展的组件的主题,然后查看“自定义服务”表格。
7. 在创建的事物模板中,单击“服务”选项卡。然后单击 铅笔图标 改写必要的服务。建议在改写其他服务之前,先改写 GetConfigurations 服务。
GetConfigurations 服务的代码中设置 implementationConfigurationVersion 的版本,使其与步骤 5 中为 GetImplementationConfigurationVersion 设置的版本相匹配。跳过此步骤可能会导致在以后的 Windchill Navigate 版本中出现兼容性问题。
8. 改写剩余的必要服务。在改写以参数形式接收此配置的服务时,首先调用代码中的 Migrate 服务并传递基本和实现版本。
要确定在 Migrate 服务中使用的 targetBaseVersion 值,请对步骤 4 中创建的事物执行 GetBaseConfigurationVersion 服务。不要调用 GetBaseConfigurationVersion 服务代码并返回其值。
Migrate 服务中设置 targetImplementationVersion 时,请使用之前已在步骤 5 中设置的版本。
有关调用 Migrate 服务的详情,请参阅版本控制和迁移主题。
* 
改写服务时,请不要调用和改写类别为 PTC.Nav.Private 的服务或被标记为已弃用的服务。
9. 保存事物模板。现在,您的自定义实现已准备就绪,随时可供使用。要使用实现,请创建混搭。将基本组件的实现添加到混搭,然后单击 齿轮图标。在“配置类型”下拉菜单中,选择属于您的自定义实现的配置。
通过扩展实现创建自定义实现 
请按照以下步骤,从现有实现创建自定义实现:
1. 选取一个现有实现,用作自定义实现的起始点。
如果您使用的是预设实现,请根据 Windchill Navigate 版本在下表中查找该实现,并记录其事物模板和项目。您稍后需要用到这些信息。
如果使用自定义实现,请记录其事物模板和项目。您稍后需要用到这些信息。
实现
事物模板
项目
活动
PTC.Nav.ActivityBusinessLogicThingTemplate
PTC.Nav.ActivityBusinessLogicProject
受影响的项
PTC.Nav.AffectedItemsBusinessLogicThingTemplate
PTC.Nav.AffectedItemsBusinessLogicProject
附件
PTC.Nav.AttachmentsBusinessLogicThingTemplate
PTC.Nav.AttachmentsBusinessLogicProject
属性通用
PTC.Nav.AttributesGenericBusinessLogicThingTemplate
PTC.Nav.AttributesGenericBusinessLogicProject
设计文件
PTC.Nav.DesignFilesBusinessLogicThingTemplate
PTC.Nav.DesignFilesBusinessLogicProject
文档标识
PTC.Nav.DocumentIdentityBusinessLogicThingTemplate
PTC.Nav.DocumentIdentityBusinessLogicProject
文档结构
PTC.Nav.DocumentStructureBusinessLogicThingTemplate
PTC.Nav.DocumentStructureBusinessLogicProject
绘图
PTC.Nav.DrawingsListBusinessLogicThingTemplate
PTC.Nav.DrawingsListBusinessLogicProject
部件标识
PTC.Nav.PartIdentityBusinessLogicThingTemplate
PTC.Nav.PartIdentityBusinessLogicProject
部件结构
PTC.Nav.PartStructureBusinessLogicThingTemplate
PTC.Nav.PartStructureBusinessLogicProject
任务标识
PTC.Nav.TaskIdentityBusinessLogicThingTemplate
PTC.Nav.TaskIdentityBusinessLogicProject
任务进度
PTC.Nav.TaskProgressBusinessLogicThingTemplate
PTC.Nav.TaskProgressBusinessLogicProject
任务
PTC.Nav.TaskListBusinessLogicThingTemplate
PTC.Nav.TaskListBusinessLogicProject
磁贴通用
PTC.Nav.TilesGenericBusinessLogicThingTemplate
PTC.Nav.TilesGenericBusinessLogicProject
2. 为自定义实现创建项目。在“项目依存关系”下,输入步骤 1 中记录的项目。保存该项目。
3. 为自定义实现创建模板。在“项目”下,输入步骤 2 中创建的项目。在“基本事物模板”下,输入步骤 1 中记录的事物模板。
4. 为自定义实现创建事物。在“项目”下,输入步骤 2 中创建的项目。在“基本事物模板”下,输入步骤 3 中记录的事物模板。
5. 在创建的事物中,单击“服务”选项卡。
单击 铅笔图标,用您的配置版本控制方案改写 GetBaseMigrationVersion。不使用 'nav.<version number>' 方案。
单击 铅笔图标,用您的配置版本控制方案改写 GetImplementationConfigurationVersion。对于版本值,使用 user.version 作为方案,其中 version 可以有多个由点分隔的层级,并且 user 不能为 nav
6. 保存该事物。
7. 标识自定义实现中需要改写的服务。有关预设组件中的服务的详情,请在可用组件下查找有关正在扩展的组件的主题,然后查看“自定义服务”表格。
8. 在创建的事物模板中,单击“服务”选项卡。然后单击 铅笔图标 改写必要的服务。建议在改写其他服务之前,先改写 GetConfigurations 服务。
* 
改写 GetConfigurations 服务时,不支持通过预设的结构更改配置结构。只能更改单个 JSON 特性的值。有关详情,请参阅配置特性更改示例主题。
在配置中,请确保将 implementationConfigurationVersion 特性的值设置为 GetImplementationConfigurationVersion 服务所返回的值。
9. 改写剩余的必要服务。在改写以参数形式接收此配置的服务时,首先调用代码中的 Migrate 服务并传递基本和实现版本。
要确定将传递到 Migrate 服务的基本版本,请对步骤 4 中创建的事物执行 GetBaseConfigurationVersion 服务。不要调用 GetBaseConfigurationVersion 服务并返回其值。
要确定将传递到 Migrate 服务的实现版本,请对步骤 4 中创建的事物执行 GetImplementationConfigurationVersion 服务。不要调用 GetImplementationConfigurationVersion 服务并返回其值。
有关调用 Migrate 服务的详情,请参阅版本控制和迁移主题。
* 
改写服务时,请不要调用和改写类别为 PTC.Nav.Private 的服务或被标记为已弃用的服务。
10. 保存事物模板。现在,您的自定义实现已准备就绪,随时可供使用。要使用实现,请创建混搭。将基本组件的实现添加到混搭,然后单击 齿轮图标。在“配置类型”下拉菜单中,选择属于您的自定义实现的配置。
示例:创建“部件列表”实现 
以下示例将展示如何创建名为“部件列表”的自定义实现,它是项列表基本组件的实现。部件列表将部件作为输入接收,并返回其包含的第一级部件。
* 
此示例中的版本控制在 Windchill Navigate 中是正确的。如果使用的是早期版本的 Navigate,请确保根据前几节中的步骤,设置版本值。
1. ThingWorx Composer 中,创建名为 Demo.PartsListProject 的新项目。在“项目依存关系”下,输入 PTC.Nav.ItemListBusinessLogicProject
2. 创建名为 Demo.PartsListBusinessLogicThingTemplate 的新事物模板。将“项目”设置为 Demo.PartsListProject,并将“基本事物模板”设置为 PTC.Nav.ItemListBusinessLogicThingTemplate
3. 创建名为 Demo.PartsListBusinessLogicThing 的事物。将“项目”设置为 Demo.PartsListProject,并将“基本事物模板”设置为 Demo.PartsListBusinessLogicThingTemplate
4. 单击“服务”选项卡,然后查找 GetImplementationConfigurationVersion 服务。单击 ,改写服务并设置版本。例如,使用 user.version 方案将值设置为 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 和 Windchill Navigate 用户界面中标签为 Parts List 的“部件列表”配置。
result = {
"partsList": {
"label": "Parts List",
"configuration": {
"selectionType": {
"selectedKey": "single"
},
"waitForInput": {
"value": true
},
"actionBarConfiguration": {
"value": {
"actionBarConfigurationJSON": {
"value": ""
}
}
},
"baseConfigurationVersion": {
"value": "nav.1"
},
"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,而在 Windchill Navigate 用户界面中的标签为 Parts List
result = {
"partsList": {
"label": "Parts List",
"configuration": {
"selectionType": {
"selectedKey": "single"
},
"waitForInput": {
"value": true
},
"actionBarConfiguration": {
"value": {
"actionBarConfigurationJSON": {
"value": ""
}
}
},
"baseConfigurationVersion": {
"value": "nav.1"
},
"implementationConfigurationVersion": {
"value": "nav.1"
},
"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": "nav.1"
},
"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.WCAdapterGetRelatedItems 服务。此服务将数据作为 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: "nav.1" /* STRING */,
data: configuration /* JSON */,
migrationType: "Configuration" /* STRING */,
targetBaseVersion: "nav.1" /* 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": ""
}
}
]
}
]
}
]
}
视频教程:创建部件列表实现
下面两个视频教程演示如何创建和使用自定义实现“部件列表”。第一个视频演示如何创建“部件列表”,请遵循上面示例中所示的步骤。第二个视频演示如何在混搭中使用“部件列表”。您可以在上述示例中找到这些视频中使用的所有代码。
* 
请知悉,这些教程是在早期版本的 Windchill Navigate 中创建的,因此您可能需要使用与所示版本值不同的值。请遵循本主题中所示的版本控制,确保一切正常运行。
此外,在第二个教程中,“项列表”从左侧停靠栏的“混搭”选项卡添加到混搭中,这就是该组件在 Windchill Navigate 早期版本中的存在形式。从 9.4 版本开始,“项列表”组件成为小组件;因此,要添加“项列表”组件,请转至“小组件”选项卡进行添加。
要在较大的窗口中观看视频,请在播放器中单击视频标题。随即会在新的标签页中打开 YouTube 视频。
* 
由于这些视频是在早期版本的 Windchill Navigate 中创建的,系统会显示以前的产品名称 ThingWorx Navigate。
这对您有帮助吗?