The Merge structure extension (src/ForgeMergeStructure in forge-scripts) simplifies a product structure by merging sub-assemblies into single parts. It offers four strategies; this page details the Metadata one, and the code shows how to read the structure and replace it.
Job
{
"JobName": "Merge structure",
"ScriptRelativePath": "skrmergestructure/Skr.MergeStructure.exe",
"DefaultSettings": {
"Values": {
"mergeStrategy": { "Value": "PartLimit", "ValueType": "String" },
"metadataName": { "Value": "InstanceName", "ValueType": "String" },
"metadataValue": { "Value": "planes.1", "ValueType": "String" },
"mergeDepth": { "Value": "1", "ValueType": "Integer" },
"mergePartLimit": { "Value": "1000", "ValueType": "Integer" },
"mergeLevelsToRemove": { "Value": "1", "ValueType": "Integer" }
}
}
}
Strategies
|
mergeStrategy |
Settings |
What is merged |
|---|---|---|
|
|
|
Each assembly whose metadata (or property, such as |
|
|
|
Everything below max depth − mergeDepth. Example: max depth 7, |
|
|
|
Finds the level where merging can reach this number of parts, then merges first the components that fill their parent bounding box the most. |
|
|
|
The given number of levels above every leaf. |
How the Metadata strategy works
-
Read the structure and the parts with
Product3DComponentsContextHelper. -
Select the components to merge: the metadata
metadataNamemust equalmetadataValue; if the component has no such metadata, its property of that name is compared instead. Components that are parts themselves, or whose parent is already selected, are skipped. -
Merge the meshes of each selected assembly into a new part.
-
Write the new parts and meshes, then the new structure, and declare
ReplaceStructureandAddOrUpdatePart3Ds.
// Program.cs: read the settings and the structure
var mergeStrategy = "";
var metadataName = "";
var metadataValue = "";
ForgeAgentHelper.TryGetArgumentValue(args, nameof(mergeStrategy), ref mergeStrategy);
ForgeAgentHelper.TryGetArgumentValue(args, nameof(metadataName), ref metadataName);
ForgeAgentHelper.TryGetArgumentValue(args, nameof(metadataValue), ref metadataValue);
var p3DccHelper = new Product3DComponentsContextHelper(workingDirectory, Logger);
var product3dComponents = await p3DccHelper.GetProduct3DComponentsAsync();
var parts3Ds = await p3DccHelper.GetPart3DsAsync();
// MergeFromMetadata.cs: does a component match?
var metadata = component.Metadatas.FirstOrDefault(m => m.Name == metadataName);
if (metadata != ProductMetadata.Empty)
isMatching = metadata.Value == metadataValue;
else
isMatching = typeAccessor[component, metadataName]?.ToString() == metadataValue; // property fallback
Output
// OutputPersistence.cs
await p3DccHelper.WritePart3DsOutputFile(mergeResult.NewPart3Ds); // + each mesh in GetMesh3DOutputFilePath(...)
await p3DccHelper.WriteOutputDeclarationFile(new Product3DComponentsContextOutput
{
Product3DComponents = new Product3DComponentsOutput { ReplaceStructure = true },
Part3Ds = new Part3DOutput
{
AddOrUpdatePart3Ds = true,
AddVisualizationEngines = [VisualizationEngine.GenericLatest]
}
});
await p3DccHelper.WriteProduct3DComponentsStructureRequestOutputFile(newStructure.ToAsyncEnumerable());
When nothing matches, the program writes an empty declaration and exits successfully: the structure is left unchanged.
Example: to merge every assembly named planes.1, set mergeStrategy = Metadata, metadataName = InstanceName, metadataValue = planes.1.