copyInstanceWithComposedDependency method

  1. @override
List<IInstance> copyInstanceWithComposedDependency(
  1. IModel fromModel,
  2. IInstance instance
)

Copies an instance along with its composed dependency hierarchy from another model.

This method performs a deep copy of the specified instance from the fromModel, including all its composed child instances and non-reference type dependencies. Unlike copyInstance, this method preserves the complete composition hierarchy by recursively copying dependent instances that are composed within the main instance.

The copy operation handles different attribute types differently:

  • Reference types: Attributes marked as reference types (via isReferenceType) are not copied and remain as null references in the copied instance
  • Non-reference types: Composed child instances and non-reference attributes are recursively copied to maintain the complete dependency hierarchy
  • Inverses: Inverses are cleared

Parameters:

  • fromModel: The source model containing the instance and its dependencies to copy. Must not be null for cross-model copying.
  • instance: The root instance to copy from the source model, along with its composed dependency hierarchy.

Returns a list of IInstance objects where:

  • The first element is the copy of the root instance

Returns an empty list if failed.

Example usage:

// Copy a wall with all its composed properties and geometry
final otherModel = getOtherModel();
final wallToCopy = otherModel.getInstancesByType(toTypeId('IfcWall')).first;

final copied = copyInstanceWithComposedDependency(otherModel, wallToCopy);
if (copied.isNotEmpty) {
  final copiedWall = copied[0];
  print('New wall ID: ${copiedWall.instanceId}');
  // Composition hierarchy is preserved, reference types are nullified
} else {
  print('Failed to copy wall with dependencies');
}

⚠️ Important Considerations:

  • Reference type attributes will be nullified in the copied hierarchy
  • All copied instances receive new consistent IDs in the target model

See also:

  • copyInstance: For copying single instances without dependencies
  • isReferenceType: To check which types are treated as references
  • resolveIndices: To ensure ID consistency after complex copy operations
  • For ifc model, IIfcModel.copyChildrenWithComposedDependency : For copying from one hierarchy to another.

Implementation

@override
List<IInstance> copyInstanceWithComposedDependency(
    IModel fromModel, IInstance instance) {
  if (instance is! PIInstance) {
    return [];
  }
  final vectPtr = fromModel is PIRocksFFIModel
      ? copyInstanceWithComposedDependancyFFI(
          projectPtr, ptr, fromModel.ptr, instance.ptr)
      : copyInstanceWithComposedDependancyFFI(
          projectPtr, ptr, nullptr, instance.ptr);
  final vect = InstanceVector(vectPtr);
  return vect.toList();
}