'Labels remain when polygon is hidden

I am using the Autodesk Viewer with the Edit2D extension and working on labels. The strange thing is that labels seem to leave a 'residue' when the polygon is hidden.

I have several polygons on the layer and the ability for the user to hide them. When they do that, this code does the hiding:

export function setRegionVisibility(
  editor: Autodesk.Extensions.Edit2D,
  region: RegionData,
  geometry: SpaceGeometry,
  visible: boolean
): void {
  const shape = editor.defaultContext.layer.shapes.find(
    (s) => s.id === region.itemIds[0]
  );
  if (shape == null) {
    return;
  }
  // @ts-ignore
  shape.visible = visible;
  editor.defaultContext.layer.update();
  return;
}

However, when a polygon is hidden, the label that was for it remains on the view. When I re-show the shape it gets a NEW label which moves with it, but the old one remains on the view and just stays in a static location regardless of the zooming or panning of the view.

I tried to manually hide it or remove it but nothing seems to work. This is how I tried to do that:

export function setRegionVisibility(
  editor: Autodesk.Extensions.Edit2D,
  region: RegionData,
  geometry: SpaceGeometry,
  // @ts-ignore
  tagRule: Autodesk.Edit2D.ShapeLabelRule | undefined,
  visible: boolean
): void {
  const shape = editor.defaultContext.layer.shapes.find(
    (s) => s.id === region.itemIds[0]
  );
  if (shape == null) {
    return;
  }
  // @ts-ignore
  shape.visible = visible;
  let label: any;
  for (let labelsKey in tagRule.labels) {
    if (labelsKey === shape.id.toString()) {
      label = tagRule.labels[labelsKey];
    }
    console.log(tagRule.labels);
  }
  if (label != null) {
    label.visible = visible;
    console.log(label);
    label.dtor();
    label.update();
    label.layer.update();
  }
  editor.defaultContext.layer.update();
  return;
}

Any idea how to make that ghost go away?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source