Creating copy of entity to edit without affecting the original?

Hello,

I’m trying to set up my Cesium application to download all entities present as a KMZ file via exportKml. It seems that when points are converted from Cesium entities to KML entities, the pixelSize of the point determines how big the icon is when viewed in Google Earth. As such, I currently have the code set up to create a copy of all entities in the Viewer, editing all points to be larger, and then using that to save.

It looks like this:

    const v = Cesium.Viewer;
    const entitiesToSave = Cesium.clone(v.entities);
    for (const ent of entitiesToSave.values) {
      ent.model = undefined; // Removing any models present
      if (ent.point) {
        ent.point.pixelSize = new Cesium.ConstantProperty(20.0); // To increase size of point icon in KMZ
      }
    }
    exportKml({
      entities: entitiesToSave,
      kmz: true,
    }).then((result): void => {
... saving

However, after making this edit, I find that the entities present in the cesium viewer are also edited (as in, their points are enlarged), even though I made a clone of the entities instead of editing them directly. I’m guessing that cloning the entities present in the simulation only makes a cloned copy of references to the entities rather than copies of the entities themselves.

Is there a way to make a copy of an entity or an entity collection (such as those present in the cesium viewer) such that they can be edited without editing the original entities they were copied from?

Figured out a way to do it: it’s possible to create a new Entity and assign it with the properties of another Entity by using merge().

The solution looks like this:

    const v = Cesium.Viewer;
    const entitiesToSave= new Cesium.EntityCollection();
    for (const ent of v.entities.values) {
      const newEnt = new Entity();
      newEnt.merge(ent); // AKA copy properties of 'ent' and put it into 'newEnt'

      ent.model = undefined; // Removing any models present
      if (ent.point) {
        ent.point.pixelSize = new Cesium.ConstantProperty(20.0); // To increase size of point icon in KMZ
      }
    }
    exportKml({
      entities: entitiesToSave,
      kmz: true,
    }).then((result): void => {
... saving

@jos

I am glad that you were able to resolve this issue! Thank you very much for following up on your original question with a solution. This thread will be a great resource for other community members who have a similar issue. Your solution looks great - merge() is the key here.

-Sam