Rasterex APINPM Package/docs/components/measurement/count-point-mode

Count Point Mode

Create Count groups, insert markers, and read or update marker metadata with the npm SDK.

Overview

Legacy is the default appearance. Canvas shows its native Count symbols and selection rings, which scale as you zoom.

Wait for Canvas to open the document before using Count. Check for countWorkflow to configure and finish a group, or countItemInfo to read and update markers.

Start and Finish Count

Configure the group and activate Count before placing markers in Canvas.

After the operator places the markers, finish the group and refresh the annotation list. Use count.onFinished(handler) to observe Finish from inside Canvas. Call the returned unsubscribe function when the view is disposed.

typescript
await viewer.annotations.count.configure({
  visibility: "always",
  content: "nameValue",
  name: "Fixtures",
  numbered: true
});
await viewer.tools.set({ group: "measurement", action: "COUNT", enabled: true });

// After the operator places the markers in Canvas.
await viewer.annotations.count.finish();
const { items } = await viewer.annotations.getData({ filter: "all" });
Start and Finish Count

Insert Count Markers

Use the GUID of a Count record in the current document. Switch to native selection mode and select an editable group owned by the current user. Imported groups retain their original ownership, so a visible group can be readable while unavailable for editing.

count.insertPoint() arms one click on the native selection. For repeated insertion, await count.setPointMode({ enabled: true, guid: countGuid }). Show the mode as active only after Canvas acknowledges arming it. Both insertion commands use existing Canvas support without a dedicated SDK capability flag.

countGuid is the GUID of the existing Count group. Read its markers before and after the user clicks in the viewer. Confirm that the count increased and the prior IDs remain. To exit repeated mode, await count.setPointMode({ enabled: false }) and clear the native tool before starting another workflow. Stop on target or document changes and clean up on disposal.

typescript
const count = viewer.annotations.count;

// Call this after selecting the intended editable Count in Canvas.
async function insertOneMarker() {
  await count.insertPoint();
}

// Use these in the separate Start and Stop point-mode handlers.
async function startPointMode() {
  await count.setPointMode({ enabled: true, guid: countGuid });
}

async function stopPointMode() {
  await count.setPointMode({ enabled: false });
  viewer.tools.clear();
}
Insert Count Markers

Read and Update Markers

count.getItems(countGuid) returns IDs, zero-based indices, points in nativeAnnotation coordinates, and per-marker info. Set itemId to the ID of the marker the operator chose.

setItemInfo() replaces the entire flat metadata object. Merge existing values when updating one field. Use strings, finite numbers, booleans, or null. Check the returned marker ID and metadata before updating the host UI.

After an error, refresh the list and confirm the group is still selected and editable before rearming insertion. After a timeout, an edit may already have happened. Read the markers before retrying.

typescript
const count = viewer.annotations.count;
const { items } = await count.getItems(countGuid);
const marker = items.find((item) => item.id === itemId);

if (marker) {
  await count.setItemInfo(countGuid, marker.id, {
    ...marker.info,
    reference: "A-01"
  });
}
Read and Update Markers

Fire-and-Forget Point Mode

Existing integrations can keep using viewer.annotations.setCountPointMode({ enabled: true, guid: countGuid }). This method returns void and exposes no acknowledgement to the caller.

Count point mode is available after the viewer is mounted, Canvas is ready, a document is open, and your app knows the target count annotation GUID.

typescript
import { createViewer } from "@rasterex/viewer";

const viewer = createViewer({
  container: "#viewer"
});

await viewer.mount();
await viewer.ready();

await viewer.documents.open({
  url: "https://files.example.com/floor-plan.pdf",
  displayName: "Floor Plan.pdf"
});
Fire-and-Forget Point Mode

Toggling Count Point Mode

Enable or disable insertion mode programmatically by passing the annotation GUID:

  • setCountPointMode() is fire-and-forget at the SDK boundary. Use the result-backed Count API when the host needs acknowledgement.
typescript
// Enable count point mode
viewer.annotations.setCountPointMode({
  guid: "count-annotation-guid",
  enabled: true
});

// Disable count point mode
viewer.annotations.setCountPointMode({
  guid: "count-annotation-guid",
  enabled: false
});
Toggling Count Point Mode

Track the Active Count Annotation

Use selection events to store the currently active count GUID:

typescript
let activeCountGuid: string | null = null;

const stopSelected = viewer.annotations.on("selected", (event) => {
  activeCountGuid = event.guid;
});
Track the Active Count Annotation

Toggle Button Pattern

Typical implementation for toolbar toggle button handlers:

typescript
let countPointModeEnabled = false;

function toggleCountPointMode() {
  if (!activeCountGuid) return;

  countPointModeEnabled = !countPointModeEnabled;

  viewer.annotations.setCountPointMode({
    guid: activeCountGuid,
    enabled: countPointModeEnabled
  });
}
Toggle Button Pattern

When to Disable

Always disable count point mode when targeting transitions or state clear:

  • The selected annotation changes.
  • The active document or file tab changes.
  • The component unmounts.
  • The user deactivates the toggle.
typescript
function disableCountPointMode() {
  if (!activeCountGuid || !countPointModeEnabled) return;

  viewer.annotations.setCountPointMode({
    guid: activeCountGuid,
    enabled: false
  });

  countPointModeEnabled = false;
}
When to Disable

Vanilla Example

Complete integration code setup for Vanilla HTML/JS environments:

<div id="viewer" style="height: 640px"></div>
<div>
  <button type="button" id="count-tool">Count Tool</button>
  <button type="button" id="toggle-count-points">Toggle Count Points</button>
</div>
<p id="status"></p>
Vanilla Example

React Integration

Complete React Component implementation details:

tsx
import { useEffect, useRef, useState } from "react";
import { createViewer, type RasterexViewer } from "@rasterex/viewer";

export function CountPointMode() {
  const hostRef = useRef<HTMLDivElement | null>(null);
  const viewerRef = useRef<RasterexViewer | null>(null);
  const activeCountGuidRef = useRef<string | null>(null);
  const countPointModeEnabledRef = useRef(false);
  const [activeCountGuid, setActiveCountGuid] = useState<string | null>(null);
  const [enabled, setEnabled] = useState(false);
  const [status, setStatus] = useState("Loading viewer");

  function disableCountPointMode() {
    const viewer = viewerRef.current;
    const guid = activeCountGuidRef.current;

    if (!viewer || !guid || !countPointModeEnabledRef.current) return;

    viewer.annotations.setCountPointMode({ guid, enabled: false });
    countPointModeEnabledRef.current = false;
    setEnabled(false);
  }

  useEffect(() => {
    let disposed = false;
    const viewer = createViewer({ container: hostRef.current! });
    viewerRef.current = viewer;

    const stopSelected = viewer.annotations.on("selected", (event) => {
      if (disposed) return;

      if (activeCountGuidRef.current && activeCountGuidRef.current !== event.guid) {
        disableCountPointMode();
      }

      activeCountGuidRef.current = event.guid;
      setActiveCountGuid(event.guid);
      setStatus(`Selected ${event.guid}`);
    });

    async function start() {
      await viewer.mount();
      await viewer.ready();
      await viewer.documents.open({
        url: "https://files.example.com/floor-plan.pdf",
        displayName: "Floor Plan.pdf"
      });
      if (!disposed) setStatus("Ready");
    }

    void start().catch((error) => {
      if (!disposed) setStatus(error instanceof Error ? error.message : "Viewer failed");
    });

    return () => {
      disposed = true;
      disableCountPointMode();
      stopSelected();
      viewer.destroy();
      viewerRef.current = null;
    };
  }, []);

  async function enableCountTool() {
    await viewerRef.current?.tools.set({ action: "COUNT", enabled: true });
  }

  function toggleCountPointMode() {
    const viewer = viewerRef.current;
    const guid = activeCountGuidRef.current;

    if (!viewer || !guid) {
      setStatus("Select a count annotation first");
      return;
    }

    const nextEnabled = !countPointModeEnabledRef.current;
    viewer.annotations.setCountPointMode({ guid, enabled: nextEnabled });
    countPointModeEnabledRef.current = nextEnabled;
    setEnabled(nextEnabled);
    setStatus(nextEnabled ? "Count point mode enabled" : "Count point mode disabled");
  }

  return (
    <section>
      <div ref={hostRef} style={{ height: 640 }} />
      <button type="button" onClick={() => void enableCountTool()}>
        Count Tool
      </button>
      <button type="button" aria-pressed={enabled} onClick={toggleCountPointMode}>
        Count Points
      </button>
      <p>{activeCountGuid ? `Target: ${activeCountGuid}` : "No count annotation selected"}</p>
      <p>{status}</p>
    </section>
  );
}
React Integration

Canvas Broker Mapping

Broker message mapping for setCountPointMode API:

  • viewer.annotations.setCountPointMode(...) maps to insertCountPointMode.