Primitive Pick

The primitive_pick() operation allows for scripts to request for the user to pick a primitive in the running application. This returns a primitive object which contains the path to the object the picked primitive belongs to and the index of the primitive. This allows for the picked primitive to be uniquely identified.

Point Primitive Pick

Point primitive picks allow the user to pick a point from an object. In the example below, we demonstrate how to print the index of the point in the points array, the path to the object which contains the point and the coordinate of the point you have picked.

from mapteksdk.pointstudio.operations import (primitive_pick, write_report, SelectablePrimitiveType)
from mapteksdk.project import Project
 
project = Project()
 
primitive = primitive_pick(SelectablePrimitiveType.POINT)
 
with project.read(primitive.path) as picked:
  point = picked.points[primitive.index]
  point_string = ", ".join([str(x) for x in point])
  message = (f"Picked point {primitive.index} of object {primitive.path} "
             f"which is located at ({point_string}).")
 
write_report("Point primitive pick", message)

Edge Primitive Pick

Edge primitive picks allow the script to prompt you to pick an edge in the currently running application. The following example reports the index of the picked edge in the edges array, the path to the object which contains the picked edge and the length of the picked edge in metres.

import numpy 
 
from mapteksdk.pointstudio.operations import (primitive_pick, write_report,
                                              SelectablePrimitiveType)
from mapteksdk.project import Project
 
project = Project()
 
primitive = primitive_pick(SelectablePrimitiveType.EDGE)
 
with project.read(primitive.path) as picked:
  edge = picked.edges[primitive.index]
  start = picked.points[edge[0]]
  end = picked.points[edge[1]]
  length = numpy.linalg.norm(start - end)
  message = (f"Picked edge {primitive.index} of object {primitive.path} "
             f"which is {length} metres long.")
 
write_report("Edge primitive pick", message)

Facet Primitive Pick

Facet primitive picks allow for scripts to prompt you to select a facet in the running application. The example below reports the index of the picked facet in the facets array, the path to the object which contains the picked facet and the area of the picked facet.

import numpy
from mapteksdk.pointstudio.operations import (primitive_pick, write_report,
                                              SelectablePrimitiveType)
from mapteksdk.project import Project
 
project = Project()
primitive = primitive_pick(SelectablePrimitiveType.FACET)
 
with project.read(primitive.path) as picked:
  facet = picked.facets[primitive.index]
  A = picked.points[facet[0]]
  B = picked.points[facet[1]]
  C = picked.points[facet[2]]
  AB = B - A
  AC = C - A
  area = 0.5 * numpy.linalg.norm(np.cross(AB, AC))
  message = (f"Picked facet {primitive.index} of object {primitive.path} "
             f"which has an area of {area} square metres.")
 
write_report("Facet primitive pick", message)

Cell Primitive Pick

Cell primitive picks allow for scripts to prompt you to select a cell in the running application. The example below reports the index of the picked cell in the cells array, the path to the cell which contains the picked cell and finally the area of the picked cell.

import numpy
from mapteksdk.pointstudio.operations import (primitive_pick, write_report,
                                              SelectablePrimitiveType)
from mapteksdk.project import Project
 
project = Project()
primitive = primitive_pick(SelectablePrimitiveType.CELL)
 
with project.read(primitive.path) as picked:
  cell = picked.cells[primitive.index]
  p0, p1, p2, p3 = picked.points[cell]
  area = 0
 
  # The squared length of the diagonals.
  diagonal_1 = numpy.sum(numpy.square(p0 - p2))
  diagonal_2 = numpy.sum(numpy.square(p1 - p3))
 
  # Calculate the area using the shorter diagonal.
  # This ensures correct results for concave cells.
  # This still gives incorrect values for degenerate cells.
  if diagonal_1 < diagonal_2:
    area += numpy.linalg.norm(numpy.cross(p1 - p0, p2 - p0))
    area += numpy.linalg.norm(numpy.cross(p2 - p0, p3 - p0))
  else:
    area += numpy.linalg.norm(numpy.cross(p0 - p1, p3 - p1))
    area += numpy.linalg.norm(numpy.cross(p2 - p1, p3 - p1))
  area /= 2
  message = (f"Picked cell {primitive.index} of object {primitive.path} "
              f"which has an area of {area} square metres.")
 
write_report("Cell primitive pick", message)