This example shows the use of tools for acquiring coordinates and objects on a graphic document.

In the example, when the user selects the command in the menu, the corresponding tool is activated. The result of the operation is displayed in the report bar.

Example

  Copy codeCopy code
function OnMenu()
{
  var menu = DlxApp.GetMenu(DlxApp.MENU_SCH);
  if (menu.IsValid())
  {
    menu.AddCommand("Pick Object", 100, "Activate the tool to acquire an object. ");
    menu.AddCommand("Pick Point", 200, "Activate the tool to acquire the coordinates of a point.");
    menu.AddCommand("Pick Segment", 300, "Activate the tool to acquire the coordinates of a line segment.");
    menu.AddCommand("Pick Rect", 400, "Activate the tool to acquire the coordinates of a rectangle.");
  }
}

function OnMenuUpdateCommand(cmd)
{
  switch (cmd.GetCommandId())
  {
    case 100:
    case 200:
    case 300:
    case 400:
      cmd.Enable(true);
      break;
  }
}

function OnMenuCommand(cmdId)
{
  switch (cmdId)
  {
  case 100:
    PickObject();
    break;
  case 200:
    PickPoint();
    break;
  case 300:
    PickSegment();
    break;
  case 400:
    PickRect();
    break;
  }
} 

function PickObject()
{
  var doc = DlxApp.GetActiveDocument();
  if (doc.IsValid())
  {
    if ((doc.GetDocumentType() == DlxApp.DOCTYPE_DRAWING) ||
        (doc.GetDocumentType() == DlxApp.DOCTYPE_SCHEMATIC))
    {
      var obj = doc.ToolPickObject("Click on the object.", false);
      DlxApp.Printf("Picked object: %s", obj.GetTypeName());
    }
  }
}

function PickPoint()
{
  var doc = DlxApp.GetActiveDocument();
  if (doc.IsValid())
  {
    if ((doc.GetDocumentType() == DlxApp.DOCTYPE_DRAWING) ||
        (doc.GetDocumentType() == DlxApp.DOCTYPE_SCHEMATIC))
    {
      var point = doc.ToolPickPoint(0, 1);
      DlxApp.Printf("Point: %.1f,%.1f", point.x, point.y);
    }
  }
}

function PickSegment()
{
  var doc = DlxApp.GetActiveDocument();
  if (doc.IsValid())
  {
    if ((doc.GetDocumentType() == DlxApp.DOCTYPE_DRAWING) ||
        (doc.GetDocumentType() == DlxApp.DOCTYPE_SCHEMATIC))
    {
      var seg = doc.ToolPickPoint(0, 2);
      DlxApp.Printf("Segment: p1(%.1f.%.1f) p2(%.1f.%.1f)", seg.p1.x, seg.p1.y, seg.p2.x, seg.p2.y);
    }
  }
}

function PickRect()
{
  var doc = DlxApp.GetActiveDocument();
  if (doc.IsValid())
  {
    if ((doc.GetDocumentType() == DlxApp.DOCTYPE_DRAWING) ||
        (doc.GetDocumentType() == DlxApp.DOCTYPE_SCHEMATIC))
    {
      var seg = doc.ToolPickPoint(0, 2, DlxApp.TOOLPICKPOINT_SHOWRECT);
      DlxApp.Printf("Segment: p1(%.1f.%.1f) p2(%.1f.%.1f)", seg.p1.x, seg.p1.y, seg.p2.x, seg.p2.y);
    }
  }
}

See also